[Concept,03/11] buildman: Fix pylint warnings in bsettings.py

Message ID 20260104200844.481633-4-sjg@u-boot.org
State New
Headers
Series buildman: Pylint cleanups |

Commit Message

Simon Glass Jan. 4, 2026, 8:08 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

- Add module docstring and define settings at module level
- Fix docstrings: correct param names, add types, remove redundant returns
- Add docstrings for add_file() and add_section()
- Remove pointless except:raise and unused variable in get_items()
- Remove unnecessary global statements in set_item()
- Add encoding='utf-8' to open() calls
- Rename param to avoid shadowing global config_fname
- Use with statement for file handling
- Convert % formatting to f-strings
- Remove unnecessary semicolon
- Add pylint disable comments for intentional global usage

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 tools/buildman/bsettings.py | 60 ++++++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 28 deletions(-)
  

Patch

diff --git a/tools/buildman/bsettings.py b/tools/buildman/bsettings.py
index a7358cfc08a..1af2bc66101 100644
--- a/tools/buildman/bsettings.py
+++ b/tools/buildman/bsettings.py
@@ -1,26 +1,30 @@ 
 # SPDX-License-Identifier: GPL-2.0+
 # Copyright (c) 2012 The Chromium OS Authors.
 
+"""Handles settings for buildman, reading from a config file."""
+
 import configparser
-import os
 import io
+import os
 
+# pylint: disable=C0103
+settings = None
 config_fname = None
 
 def setup(fname=''):
     """Set up the buildman settings module by reading config files
 
     Args:
-        config_fname:   Config filename to read ('' for default)
+        fname (str): Config filename to read ('' for default)
     """
-    global settings
-    global config_fname
+    global settings  # pylint: disable=W0603
+    global config_fname  # pylint: disable=W0603
 
     settings = configparser.ConfigParser()
     if fname is not None:
         config_fname = fname
         if config_fname == '':
-            config_fname = '%s/.buildman' % os.getenv('HOME')
+            config_fname = f"{os.getenv('HOME')}/.buildman"
         if not os.path.exists(config_fname):
             print('No config file found ~/.buildman\nCreating one...\n')
             create_buildman_config_file(config_fname)
@@ -29,32 +33,40 @@  def setup(fname=''):
             settings.read(config_fname)
 
 def add_file(data):
+    """Add settings from a string
+
+    Args:
+        data (str): Config data in INI format
+    """
     settings.read_file(io.StringIO(data))
 
 def add_section(name):
+    """Add a new section to the settings
+
+    Args:
+        name (str): Name of section to add
+    """
     settings.add_section(name)
 
 def get_items(section):
     """Get the items from a section of the config.
 
     Args:
-        section: name of section to retrieve
+        section (str): name of section to retrieve
 
     Returns:
-        List of (name, value) tuples for the section
+        list of tuple: List of (name, value) tuples for the section
     """
     try:
         return settings.items(section)
-    except configparser.NoSectionError as e:
+    except configparser.NoSectionError:
         return []
-    except:
-        raise
 
 def get_global_item_value(name):
     """Get an item from the 'global' section of the config.
 
     Args:
-        name: name of item to retrieve
+        name (str): name of item to retrieve
 
     Returns:
         str: Value of item, or None if not present
@@ -63,30 +75,20 @@  def get_global_item_value(name):
 
 def set_item(section, tag, value):
     """Set an item and write it back to the settings file"""
-    global settings
-    global config_fname
-
     settings.set(section, tag, value)
     if config_fname is not None:
-        with open(config_fname, 'w') as fd:
+        with open(config_fname, 'w', encoding='utf-8') as fd:
             settings.write(fd)
 
-def create_buildman_config_file(config_fname):
+def create_buildman_config_file(cfgname):
     """Creates a new config file with no tool chain information.
 
     Args:
-        config_fname: Config filename to create
-
-    Returns:
-        None
+        cfgname (str): Config filename to create
     """
     try:
-        f = open(config_fname, 'w')
-    except IOError:
-        print("Couldn't create buildman config file '%s'\n" % config_fname)
-        raise
-
-    print('''[toolchain]
+        with open(cfgname, 'w', encoding='utf-8') as out:
+            print('''[toolchain]
 # name = path
 # e.g. x86 = /opt/gcc-4.6.3-nolibc/x86_64-linux
 other = /
@@ -108,5 +110,7 @@  x86 = i386
 # snapper-boards=ENABLE_AT91_TEST=1
 # snapper9260=${snapper-boards} BUILD_TAG=442
 # snapper9g45=${snapper-boards} BUILD_TAG=443
-''', file=f)
-    f.close();
+''', file=out)
+    except IOError:
+        print(f"Couldn't create buildman config file '{cfgname}'\n")
+        raise