@@ -170,10 +170,8 @@ def __init__(self, config, section, setting,
170
170
self .aliases = []
171
171
self .aliases .insert (0 , self .name )
172
172
# 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
+
177
175
# value is private. use get() and set() to access
178
176
self ._value = self ._default_value = _value
179
177
@@ -248,10 +246,8 @@ def format(self):
248
246
if _description :
249
247
_desc_lines .extend (_description .split ("\n " ))
250
248
# 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
+
255
251
_rv = "# %(description)s\n # Default: %(default)s\n " \
256
252
"%(is_set)s%(name)s = %(value)s\n " % {
257
253
"description" : "\n # " .join (_desc_lines ),
@@ -2132,25 +2128,25 @@ def save(self, ini_file=None):
2132
2128
_tmp_file = os .path .splitext (ini_file )[0 ]
2133
2129
_bak_file = _tmp_file + ".bak"
2134
2130
_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
+
2154
2150
if os .access (ini_file , os .F_OK ):
2155
2151
if os .access (_bak_file , os .F_OK ):
2156
2152
os .remove (_bak_file )
@@ -2279,12 +2275,12 @@ def copy(self):
2279
2275
def _get_unset_options (self ):
2280
2276
need_set = Config ._get_unset_options (self )
2281
2277
# 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" ]
2288
2284
return need_set
2289
2285
2290
2286
def _get_name (self ):
@@ -2309,10 +2305,9 @@ def init_logging(self):
2309
2305
_file = self ["LOGGING_FILENAME" ]
2310
2306
# set file & level on the roundup logger
2311
2307
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
+
2316
2311
formatter = logging .Formatter (
2317
2312
'%(asctime)s %(levelname)s %(message)s' )
2318
2313
hdlr .setFormatter (formatter )
0 commit comments