changeset 7811:a4b71f16c264

chore(refactor): use 'with open'; if ternarys; unnested if statements
author John Rouillard <rouilj@ieee.org>
date Sat, 16 Mar 2024 20:03:16 -0400
parents 890097bc4cd0
children ecc34b7917e2
files roundup/configuration.py
diffstat 1 files changed, 32 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/configuration.py	Sat Mar 16 19:41:36 2024 -0400
+++ b/roundup/configuration.py	Sat Mar 16 20:03:16 2024 -0400
@@ -170,10 +170,8 @@
             self.aliases = []
         self.aliases.insert(0, self.name)
         # convert default to internal representation
-        if default is NODEFAULT:
-            _value = default
-        else:
-            _value = self.str2value(default)
+        _value = default if default is NODEFAULT else self.str2value(default)
+
         # value is private.  use get() and set() to access
         self._value = self._default_value = _value
 
@@ -248,10 +246,8 @@
             if _description:
                 _desc_lines.extend(_description.split("\n"))
         # comment out the setting line if there is no value
-        if self.isset():
-            _is_set = ""
-        else:
-            _is_set = "#"
+        _is_set = "" if self.isset() else "#"
+
         _rv = "# %(description)s\n# Default: %(default)s\n" \
             "%(is_set)s%(name)s = %(value)s\n" % {
                 "description": "\n# ".join(_desc_lines),
@@ -2132,25 +2128,25 @@
         _tmp_file = os.path.splitext(ini_file)[0]
         _bak_file = _tmp_file + ".bak"
         _tmp_file = _tmp_file + ".tmp"
-        _fp = open(_tmp_file, "wt")
-        _fp.write("# %s configuration file\n" % self._get_name())
-        _fp.write("# Autogenerated at %s\n" % time.asctime())
-        need_set = self._get_unset_options()
-        if need_set:
-            _fp.write("\n# WARNING! Following options need adjustments:\n")
-            for section, options in need_set.items():
-                _fp.write("#  [%s]: %s\n" % (section, ", ".join(options)))
-        for section in self.sections:
-            comment = self.section_descriptions.get(section, None)
-            if comment:
-                _fp.write("\n# ".join([""] + comment.split("\n")) + "\n")
-            else:
-                # no section comment - just leave a blank line between sections
-                _fp.write("\n")
-            _fp.write("[%s]\n" % section)
-            for option in self._get_section_options(section):
-                _fp.write("\n" + self.options[(section, option)].format())
-        _fp.close()
+        with open(_tmp_file, "wt") as _fp:
+            _fp.write("# %s configuration file\n" % self._get_name())
+            _fp.write("# Autogenerated at %s\n" % time.asctime())
+            need_set = self._get_unset_options()
+            if need_set:
+                _fp.write("\n# WARNING! Following options need adjustments:\n")
+                for section, options in need_set.items():
+                    _fp.write("#  [%s]: %s\n" % (section, ", ".join(options)))
+            for section in self.sections:
+                comment = self.section_descriptions.get(section, None)
+                if comment:
+                    _fp.write("\n# ".join([""] + comment.split("\n")) + "\n")
+                else:
+                    # no section comment - just leave a blank line between sections
+                    _fp.write("\n")
+                _fp.write("[%s]\n" % section)
+                for option in self._get_section_options(section):
+                    _fp.write("\n" + self.options[(section, option)].format())
+
         if os.access(ini_file, os.F_OK):
             if os.access(_bak_file, os.F_OK):
                 os.remove(_bak_file)
@@ -2279,12 +2275,12 @@
     def _get_unset_options(self):
         need_set = Config._get_unset_options(self)
         # remove MAIL_PASSWORD if MAIL_USER is empty
-        if "password" in need_set.get("mail", []):
-            if not self["MAIL_USERNAME"]:
-                settings = need_set["mail"]
-                settings.remove("password")
-                if not settings:
-                    del need_set["mail"]
+        if "password" in need_set.get("mail", []) and \
+           not self["MAIL_USERNAME"]:
+            settings = need_set["mail"]
+            settings.remove("password")
+            if not settings:
+                del need_set["mail"]
         return need_set
 
     def _get_name(self):
@@ -2309,10 +2305,9 @@
         _file = self["LOGGING_FILENAME"]
         # set file & level on the roundup logger
         logger = logging.getLogger('roundup')
-        if _file:
-            hdlr = logging.FileHandler(_file)
-        else:
-            hdlr = logging.StreamHandler(sys.stdout)
+        hdlr = logging.FileHandler(_file) if _file else \
+            logging.StreamHandler(sys.stdout)
+
         formatter = logging.Formatter(
             '%(asctime)s %(levelname)s %(message)s')
         hdlr.setFormatter(formatter)

Roundup Issue Tracker: http://roundup-tracker.org/