flake8: bmconfigparser
This commit is contained in:
parent
de27c9082d
commit
31f107f048
|
@ -1,10 +1,14 @@
|
||||||
|
"""
|
||||||
|
BMConfigParser class definition and default configuration settings
|
||||||
|
"""
|
||||||
|
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import datetime
|
|
||||||
import shutil
|
import shutil
|
||||||
import os
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from singleton import Singleton
|
|
||||||
import state
|
import state
|
||||||
|
from singleton import Singleton
|
||||||
|
|
||||||
BMConfigDefaults = {
|
BMConfigDefaults = {
|
||||||
"bitmessagesettings": {
|
"bitmessagesettings": {
|
||||||
|
@ -36,23 +40,30 @@ BMConfigDefaults = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class BMConfigParser(ConfigParser.SafeConfigParser):
|
class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
|
"""Singleton class inherited from ConfigParser.SafeConfigParser
|
||||||
|
with additional methods specific to bitmessage config."""
|
||||||
|
|
||||||
def set(self, section, option, value=None):
|
def set(self, section, option, value=None):
|
||||||
if self._optcre is self.OPTCRE or value:
|
if self._optcre is self.OPTCRE or value:
|
||||||
if not isinstance(value, basestring):
|
if not isinstance(value, basestring):
|
||||||
raise TypeError("option values must be strings")
|
raise TypeError("option values must be strings")
|
||||||
if not self.validate(section, option, value):
|
if not self.validate(section, option, value):
|
||||||
raise ValueError("Invalid value %s" % str(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):
|
||||||
try:
|
try:
|
||||||
if section == "bitmessagesettings" and option == "timeformat":
|
if section == "bitmessagesettings" and option == "timeformat":
|
||||||
return ConfigParser.ConfigParser.get(self, section, option, raw, variables)
|
return ConfigParser.ConfigParser.get(
|
||||||
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
|
self, section, option, raw, variables)
|
||||||
|
return ConfigParser.ConfigParser.get(
|
||||||
|
self, section, option, True, variables)
|
||||||
except ConfigParser.InterpolationError:
|
except ConfigParser.InterpolationError:
|
||||||
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
|
return ConfigParser.ConfigParser.get(
|
||||||
|
self, section, option, True, variables)
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
|
||||||
try:
|
try:
|
||||||
return BMConfigDefaults[section][option]
|
return BMConfigDefaults[section][option]
|
||||||
|
@ -62,51 +73,62 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
def safeGetBoolean(self, section, field):
|
def safeGetBoolean(self, section, field):
|
||||||
try:
|
try:
|
||||||
return self.getboolean(section, field)
|
return self.getboolean(section, field)
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
|
||||||
|
ValueError, AttributeError):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def safeGetInt(self, section, field, default=0):
|
def safeGetInt(self, section, field, default=0):
|
||||||
try:
|
try:
|
||||||
return self.getint(section, field)
|
return self.getint(section, field)
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
|
||||||
|
ValueError, AttributeError):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def safeGet(self, section, option, default = None):
|
def safeGet(self, section, option, default=None):
|
||||||
try:
|
try:
|
||||||
return self.get(section, option)
|
return self.get(section, option)
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
|
||||||
|
ValueError, AttributeError):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def items(self, section, raw=False, variables=None):
|
def items(self, section, raw=False, variables=None):
|
||||||
return ConfigParser.ConfigParser.items(self, section, True, variables)
|
return ConfigParser.ConfigParser.items(self, section, True, variables)
|
||||||
|
|
||||||
def addresses(self):
|
def addresses(self):
|
||||||
return filter(lambda x: x.startswith('BM-'), BMConfigParser().sections())
|
return filter(
|
||||||
|
lambda x: x.startswith('BM-'), BMConfigParser().sections())
|
||||||
|
|
||||||
def read(self, filenames):
|
def read(self, filenames):
|
||||||
ConfigParser.ConfigParser.read(self, filenames)
|
ConfigParser.ConfigParser.read(self, filenames)
|
||||||
for section in self.sections():
|
for section in self.sections():
|
||||||
for option in self.options(section):
|
for option in self.options(section):
|
||||||
try:
|
try:
|
||||||
if not self.validate(section, option, ConfigParser.ConfigParser.get(self, section, option)):
|
if not self.validate(
|
||||||
|
section, option,
|
||||||
|
ConfigParser.ConfigParser.get(self, section, option)
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
newVal = BMConfigDefaults[section][option]
|
newVal = BMConfigDefaults[section][option]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
ConfigParser.ConfigParser.set(self, section, option, newVal)
|
ConfigParser.ConfigParser.set(
|
||||||
|
self, section, option, newVal)
|
||||||
except ConfigParser.InterpolationError:
|
except ConfigParser.InterpolationError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
fileName = os.path.join(state.appdata, 'keys.dat')
|
fileName = os.path.join(state.appdata, 'keys.dat')
|
||||||
fileNameBak = fileName + "." + datetime.datetime.now().strftime("%Y%j%H%M%S%f") + '.bak'
|
fileNameBak = '.'.join([
|
||||||
# create a backup copy to prevent the accidental loss due to the disk write failure
|
fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak'])
|
||||||
|
# create a backup copy to prevent the accidental loss due to
|
||||||
|
# the disk write failure
|
||||||
try:
|
try:
|
||||||
shutil.copyfile(fileName, fileNameBak)
|
shutil.copyfile(fileName, fileNameBak)
|
||||||
# The backup succeeded.
|
# The backup succeeded.
|
||||||
fileNameExisted = True
|
fileNameExisted = True
|
||||||
except (IOError, Exception):
|
except (IOError, Exception):
|
||||||
# The backup failed. This can happen if the file didn't exist before.
|
# The backup failed. This can happen if the file
|
||||||
|
# didn't exist before.
|
||||||
fileNameExisted = False
|
fileNameExisted = False
|
||||||
# write the file
|
# write the file
|
||||||
with open(fileName, 'wb') as configfile:
|
with open(fileName, 'wb') as configfile:
|
||||||
|
@ -117,7 +139,7 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
|
|
||||||
def validate(self, section, option, value):
|
def validate(self, section, option, value):
|
||||||
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
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user