5353
5454@Singleton
5555class BMConfigParser (SafeConfigParser ):
56-
5756 """
58- Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
59- with additional methods specific to bitmessage config.
57+ Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
58+ with additional methods specific to bitmessage config.
6059 """
6160 # pylint: disable=too-many-ancestors
62-
6361 _temp = {}
6462
6563 def set (self , section , option , value = None ):
6664 if self ._optcre is self .OPTCRE or value :
67- if not isinstance (value , basestring ):
65+ if not isinstance (value , str ):
6866 raise TypeError ("option values must be strings" )
6967 if not self .validate (section , option , value ):
7068 raise ValueError ("Invalid value %s" % value )
@@ -73,20 +71,20 @@ def set(self, section, option, value=None):
7371 def get (self , section , option , raw = False , vars = None ):
7472 if sys .version_info [0 ] == 3 :
7573 # pylint: disable=arguments-differ
76- try :
74+ try :
7775 if section == "bitmessagesettings" and option == "timeformat" :
7876 return ConfigParser .ConfigParser .get (
79- self , section , option )
77+ self , section , option , raw = True , vars = vars )
8078 try :
8179 return self ._temp [section ][option ]
8280 except KeyError :
8381 pass
8482 return ConfigParser .ConfigParser .get (
85- self , section , option )
86- except ConfigParser .InterpolationError :
83+ self , section , option , raw = True , vars = vars )
84+ except ConfigParser .InterpolationError :
8785 return ConfigParser .ConfigParser .get (
88- self , section , option )
89- except (ConfigParser .NoSectionError , ConfigParser .NoOptionError ) as e :
86+ self , section , option , raw = True , vars = vars )
87+ except (ConfigParser .NoSectionError , ConfigParser .NoOptionError ) as e :
9088 try :
9189 return BMConfigDefaults [section ][option ]
9290 except (KeyError , ValueError , AttributeError ):
@@ -122,6 +120,10 @@ def setTemp(self, section, option, value=None):
122120 def safeGetBoolean (self , section , field ):
123121 """Return value as boolean, False on exceptions"""
124122 try :
123+ # Used in the python2.7
124+ # return self.getboolean(section, field)
125+ # Used in the python3.5.2
126+ # print(config, section, field)
125127 return self .getboolean (section , field )
126128 except (ConfigParser .NoSectionError , ConfigParser .NoOptionError ,
127129 ValueError , AttributeError ):
@@ -131,7 +133,10 @@ def safeGetInt(self, section, field, default=0):
131133 """Return value as integer, default on exceptions,
132134 0 if default missing"""
133135 try :
134- return self .getint (section , field )
136+ # Used in the python2.7
137+ # return self.getint(section, field)
138+ # Used in the python3.7.0
139+ return int (self .get (section , field ))
135140 except (ConfigParser .NoSectionError , ConfigParser .NoOptionError ,
136141 ValueError , AttributeError ):
137142 return default
@@ -145,43 +150,71 @@ def safeGet(self, section, option, default=None):
145150 return default
146151
147152 def items (self , section , raw = False , variables = None ):
153+ # pylint: disable=signature-differs
148154 """Return section variables as parent,
149155 but override the "raw" argument to always True"""
150- # pylint: disable=arguments-differ
151156 return ConfigParser .ConfigParser .items (self , section , True , variables )
152157
153- @staticmethod
154- def addresses ():
155- """Return a list of local bitmessage addresses (from section labels)"""
156- return [
157- x for x in BMConfigParser ().sections () if x .startswith ('BM-' )]
158-
159- def _reset (self ):
160- """Reset current config. There doesn't appear to be a built in
161- method for this"""
162- sections = self .sections ()
163- for x in sections :
164- self .remove_section (x )
165-
166- def read (self , filenames ):
167- """Read config and populate defaults"""
168- self ._reset ()
169- ConfigParser .ConfigParser .read (self , filenames )
170- for section in self .sections ():
171- for option in self .options (section ):
172- try :
173- if not self .validate (
174- section , option ,
175- ConfigParser .ConfigParser .get (self , section , option )
176- ):
177- try :
178- newVal = BMConfigDefaults [section ][option ]
179- except KeyError :
180- continue
181- ConfigParser .ConfigParser .set (
182- self , section , option , newVal )
183- except ConfigParser .InterpolationError :
184- continue
158+ if sys .version_info [0 ] == 3 :
159+ @staticmethod
160+ def addresses (hidden = False ):
161+ """Return a list of local bitmessage addresses (from section labels)"""
162+ return [x for x in BMConfigParser ().sections () if x .startswith ('BM-' ) and (
163+ hidden or not BMConfigParser ().safeGetBoolean (x , 'hidden' ))]
164+
165+ def read (self , filenames ):
166+ ConfigParser .ConfigParser .read (self , filenames )
167+ for section in self .sections ():
168+ for option in self .options (section ):
169+ try :
170+ if not self .validate (
171+ section , option ,
172+ self [section ][option ]
173+ ):
174+ try :
175+ newVal = BMConfigDefaults [section ][option ]
176+ except ConfigParser .NoSectionError :
177+ continue
178+ except KeyError :
179+ continue
180+ ConfigParser .ConfigParser .set (
181+ self , section , option , newVal )
182+ except ConfigParser .InterpolationError :
183+ continue
184+
185+ else :
186+ @staticmethod
187+ def addresses ():
188+ """Return a list of local bitmessage addresses (from section labels)"""
189+ return [
190+ x for x in BMConfigParser ().sections () if x .startswith ('BM-' )]
191+
192+ def _reset (self ):
193+ """Reset current config. There doesn't appear to be a built in
194+ method for this"""
195+ sections = self .sections ()
196+ for x in sections :
197+ self .remove_section (x )
198+
199+ def read (self , filenames ):
200+ """Read config and populate defaults"""
201+ self ._reset ()
202+ ConfigParser .ConfigParser .read (self , filenames )
203+ for section in self .sections ():
204+ for option in self .options (section ):
205+ try :
206+ if not self .validate (
207+ section , option ,
208+ ConfigParser .ConfigParser .get (self , section , option )
209+ ):
210+ try :
211+ newVal = BMConfigDefaults [section ][option ]
212+ except KeyError :
213+ continue
214+ ConfigParser .ConfigParser .set (
215+ self , section , option , newVal )
216+ except ConfigParser .InterpolationError :
217+ continue
185218
186219 def save (self ):
187220 """Save the runtime config onto the filesystem"""
@@ -198,8 +231,8 @@ def save(self):
198231 # The backup failed. This can happen if the file
199232 # didn't exist before.
200233 fileNameExisted = False
201- # write the file
202- with open (fileName , 'wb ' ) as configfile :
234+
235+ with open (fileName , 'w ' ) as configfile :
203236 self .write (configfile )
204237 # delete the backup
205238 if fileNameExisted :
@@ -208,7 +241,11 @@ def save(self):
208241 def validate (self , section , option , value ):
209242 """Input validator interface (using factory pattern)"""
210243 try :
211- return getattr (self , 'validate_%s_%s' % (section , option ))(value )
244+ if sys .version_info [0 ] == 3 :
245+ return getattr (self , 'validate_{}_{}' .format (
246+ section , option ))(value )
247+ else :
248+ return getattr (self , 'validate_%s_%s' % (section , option ))(value )
212249 except AttributeError :
213250 return True
214251
@@ -222,4 +259,3 @@ def validate_bitmessagesettings_maxoutboundconnections(value):
222259 if value < 0 or value > 8 :
223260 return False
224261 return True
225-
0 commit comments