53
53
54
54
@Singleton
55
55
class BMConfigParser (SafeConfigParser ):
56
-
57
56
"""
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.
60
59
"""
61
60
# pylint: disable=too-many-ancestors
62
-
63
61
_temp = {}
64
62
65
63
def set (self , section , option , value = None ):
66
64
if self ._optcre is self .OPTCRE or value :
67
- if not isinstance (value , basestring ):
65
+ if not isinstance (value , str ):
68
66
raise TypeError ("option values must be strings" )
69
67
if not self .validate (section , option , value ):
70
68
raise ValueError ("Invalid value %s" % value )
@@ -73,20 +71,20 @@ def set(self, section, option, value=None):
73
71
def get (self , section , option , raw = False , vars = None ):
74
72
if sys .version_info [0 ] == 3 :
75
73
# pylint: disable=arguments-differ
76
- try :
74
+ try :
77
75
if section == "bitmessagesettings" and option == "timeformat" :
78
76
return ConfigParser .ConfigParser .get (
79
- self , section , option )
77
+ self , section , option , raw = True , vars = vars )
80
78
try :
81
79
return self ._temp [section ][option ]
82
80
except KeyError :
83
81
pass
84
82
return ConfigParser .ConfigParser .get (
85
- self , section , option )
86
- except ConfigParser .InterpolationError :
83
+ self , section , option , raw = True , vars = vars )
84
+ except ConfigParser .InterpolationError :
87
85
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 :
90
88
try :
91
89
return BMConfigDefaults [section ][option ]
92
90
except (KeyError , ValueError , AttributeError ):
@@ -122,6 +120,10 @@ def setTemp(self, section, option, value=None):
122
120
def safeGetBoolean (self , section , field ):
123
121
"""Return value as boolean, False on exceptions"""
124
122
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)
125
127
return self .getboolean (section , field )
126
128
except (ConfigParser .NoSectionError , ConfigParser .NoOptionError ,
127
129
ValueError , AttributeError ):
@@ -131,7 +133,10 @@ def safeGetInt(self, section, field, default=0):
131
133
"""Return value as integer, default on exceptions,
132
134
0 if default missing"""
133
135
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 ))
135
140
except (ConfigParser .NoSectionError , ConfigParser .NoOptionError ,
136
141
ValueError , AttributeError ):
137
142
return default
@@ -145,43 +150,71 @@ def safeGet(self, section, option, default=None):
145
150
return default
146
151
147
152
def items (self , section , raw = False , variables = None ):
153
+ # pylint: disable=signature-differs
148
154
"""Return section variables as parent,
149
155
but override the "raw" argument to always True"""
150
- # pylint: disable=arguments-differ
151
156
return ConfigParser .ConfigParser .items (self , section , True , variables )
152
157
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
185
218
186
219
def save (self ):
187
220
"""Save the runtime config onto the filesystem"""
@@ -198,8 +231,8 @@ def save(self):
198
231
# The backup failed. This can happen if the file
199
232
# didn't exist before.
200
233
fileNameExisted = False
201
- # write the file
202
- with open (fileName , 'wb ' ) as configfile :
234
+
235
+ with open (fileName , 'w ' ) as configfile :
203
236
self .write (configfile )
204
237
# delete the backup
205
238
if fileNameExisted :
@@ -208,7 +241,11 @@ def save(self):
208
241
def validate (self , section , option , value ):
209
242
"""Input validator interface (using factory pattern)"""
210
243
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 )
212
249
except AttributeError :
213
250
return True
214
251
@@ -222,4 +259,3 @@ def validate_bitmessagesettings_maxoutboundconnections(value):
222
259
if value < 0 or value > 8 :
223
260
return False
224
261
return True
225
-
0 commit comments