Skip to content

Commit f45bbf1

Browse files
committed
chore(refactor): use 'with open'; if ternarys; unnested if statements
1 parent 1caf7e1 commit f45bbf1

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

roundup/configuration.py

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,8 @@ def __init__(self, config, section, setting,
170170
self.aliases = []
171171
self.aliases.insert(0, self.name)
172172
# convert default to internal representation
173-
if default is NODEFAULT:
174-
_value = default
175-
else:
176-
_value = self.str2value(default)
173+
_value = default if default is NODEFAULT else self.str2value(default)
174+
177175
# value is private. use get() and set() to access
178176
self._value = self._default_value = _value
179177

@@ -248,10 +246,8 @@ def format(self):
248246
if _description:
249247
_desc_lines.extend(_description.split("\n"))
250248
# comment out the setting line if there is no value
251-
if self.isset():
252-
_is_set = ""
253-
else:
254-
_is_set = "#"
249+
_is_set = "" if self.isset() else "#"
250+
255251
_rv = "# %(description)s\n# Default: %(default)s\n" \
256252
"%(is_set)s%(name)s = %(value)s\n" % {
257253
"description": "\n# ".join(_desc_lines),
@@ -2132,25 +2128,25 @@ def save(self, ini_file=None):
21322128
_tmp_file = os.path.splitext(ini_file)[0]
21332129
_bak_file = _tmp_file + ".bak"
21342130
_tmp_file = _tmp_file + ".tmp"
2135-
_fp = open(_tmp_file, "wt")
2136-
_fp.write("# %s configuration file\n" % self._get_name())
2137-
_fp.write("# Autogenerated at %s\n" % time.asctime())
2138-
need_set = self._get_unset_options()
2139-
if need_set:
2140-
_fp.write("\n# WARNING! Following options need adjustments:\n")
2141-
for section, options in need_set.items():
2142-
_fp.write("# [%s]: %s\n" % (section, ", ".join(options)))
2143-
for section in self.sections:
2144-
comment = self.section_descriptions.get(section, None)
2145-
if comment:
2146-
_fp.write("\n# ".join([""] + comment.split("\n")) + "\n")
2147-
else:
2148-
# no section comment - just leave a blank line between sections
2149-
_fp.write("\n")
2150-
_fp.write("[%s]\n" % section)
2151-
for option in self._get_section_options(section):
2152-
_fp.write("\n" + self.options[(section, option)].format())
2153-
_fp.close()
2131+
with open(_tmp_file, "wt") as _fp:
2132+
_fp.write("# %s configuration file\n" % self._get_name())
2133+
_fp.write("# Autogenerated at %s\n" % time.asctime())
2134+
need_set = self._get_unset_options()
2135+
if need_set:
2136+
_fp.write("\n# WARNING! Following options need adjustments:\n")
2137+
for section, options in need_set.items():
2138+
_fp.write("# [%s]: %s\n" % (section, ", ".join(options)))
2139+
for section in self.sections:
2140+
comment = self.section_descriptions.get(section, None)
2141+
if comment:
2142+
_fp.write("\n# ".join([""] + comment.split("\n")) + "\n")
2143+
else:
2144+
# no section comment - just leave a blank line between sections
2145+
_fp.write("\n")
2146+
_fp.write("[%s]\n" % section)
2147+
for option in self._get_section_options(section):
2148+
_fp.write("\n" + self.options[(section, option)].format())
2149+
21542150
if os.access(ini_file, os.F_OK):
21552151
if os.access(_bak_file, os.F_OK):
21562152
os.remove(_bak_file)
@@ -2279,12 +2275,12 @@ def copy(self):
22792275
def _get_unset_options(self):
22802276
need_set = Config._get_unset_options(self)
22812277
# remove MAIL_PASSWORD if MAIL_USER is empty
2282-
if "password" in need_set.get("mail", []):
2283-
if not self["MAIL_USERNAME"]:
2284-
settings = need_set["mail"]
2285-
settings.remove("password")
2286-
if not settings:
2287-
del need_set["mail"]
2278+
if "password" in need_set.get("mail", []) and \
2279+
not self["MAIL_USERNAME"]:
2280+
settings = need_set["mail"]
2281+
settings.remove("password")
2282+
if not settings:
2283+
del need_set["mail"]
22882284
return need_set
22892285

22902286
def _get_name(self):
@@ -2309,10 +2305,9 @@ def init_logging(self):
23092305
_file = self["LOGGING_FILENAME"]
23102306
# set file & level on the roundup logger
23112307
logger = logging.getLogger('roundup')
2312-
if _file:
2313-
hdlr = logging.FileHandler(_file)
2314-
else:
2315-
hdlr = logging.StreamHandler(sys.stdout)
2308+
hdlr = logging.FileHandler(_file) if _file else \
2309+
logging.StreamHandler(sys.stdout)
2310+
23162311
formatter = logging.Formatter(
23172312
'%(asctime)s %(levelname)s %(message)s')
23182313
hdlr.setFormatter(formatter)

0 commit comments

Comments
 (0)