bmconfigparser quality fixes

This commit is contained in:
lakshyacis 2019-11-04 20:15:02 +05:30
parent af52d95503
commit 77b8b5aa42
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB

View File

@ -58,7 +58,7 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
raise ValueError("Invalid value %s" % value) raise ValueError("Invalid value %s" % value)
return ConfigParser.ConfigParser.set(self, section, option, value) return ConfigParser.ConfigParser.set(self, section, option, value)
def get(self, section, option, raw=False, variables=None): def get(self, section, option, raw=False, variables=None): # pylint: disable=arguments-differ
try: try:
if section == "bitmessagesettings" and option == "timeformat": if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get( return ConfigParser.ConfigParser.get(
@ -86,6 +86,7 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
self._temp[section] = {option: value} self._temp[section] = {option: value}
def safeGetBoolean(self, section, field): def safeGetBoolean(self, section, field):
"""Return value as boolean, False on exceptions"""
try: try:
return self.getboolean(section, field) return self.getboolean(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
@ -93,6 +94,7 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
return False return False
def safeGetInt(self, section, field, default=0): def safeGetInt(self, section, field, default=0):
"""Return value as integer, default on exceptions, 0 if default missing"""
try: try:
return self.getint(section, field) return self.getint(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
@ -100,18 +102,22 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
return default return default
def safeGet(self, section, option, default=None): def safeGet(self, section, option, default=None):
"""Return value as is, default on exceptions, None if default missing"""
try: try:
return self.get(section, option) return self.get(section, option)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
ValueError, AttributeError): ValueError, AttributeError):
return default return default
def items(self, section, raw=False, variables=None): def items(self, section, raw=False, variables=None): # pylint: disable=arguments-differ
"""Return section variables as parent, but override the "raw" argument to always True"""
return ConfigParser.ConfigParser.items(self, section, True, variables) return ConfigParser.ConfigParser.items(self, section, True, variables)
def addresses(self): @staticmethod
return filter( def addresses():
lambda x: x.startswith('BM-'), BMConfigParser().sections()) """Return a list of local bitmessage addresses (from section labels)"""
return [
x for x in BMConfigParser().sections() if x.startswith('BM-')]
def read(self, filenames): def read(self, filenames):
ConfigParser.ConfigParser.read(self, filenames) ConfigParser.ConfigParser.read(self, filenames)
@ -132,6 +138,7 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
continue continue
def save(self): def save(self):
"""Save the runtime config onto the filesystem"""
fileName = os.path.join(state.appdata, 'keys.dat') fileName = os.path.join(state.appdata, 'keys.dat')
fileNameBak = '.'.join([ fileNameBak = '.'.join([
fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak']) fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak'])
@ -153,12 +160,15 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
os.remove(fileNameBak) os.remove(fileNameBak)
def validate(self, section, option, value): def validate(self, section, option, value):
"""Input validator interface (using factory pattern)"""
try: try:
return getattr(self, 'validate_%s_%s' % (section, option))(value) return getattr(self, 'validate_%s_%s' % (section, option))(value)
except AttributeError: except AttributeError:
return True return True
def validate_bitmessagesettings_maxoutboundconnections(self, value): @staticmethod
def validate_bitmessagesettings_maxoutboundconnections(value):
"""Reject maxoutboundconnections that are too high or too low"""
try: try:
value = int(value) value = int(value)
except ValueError: except ValueError: