Format bmconfigparser:
- remove meaningless comments, rename strange args, fix line lengths, - revert BMConfigParser.addresses() entanglement, - revert BMConfigParser read_file -> readfp alias.
This commit is contained in:
parent
161a0b2059
commit
7d2b13e4ac
|
@ -4,7 +4,6 @@ BMConfigParser class definition and default configuration settings
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys # FIXME: bad style! write more generally
|
|
||||||
from threading import Event
|
from threading import Event
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
@ -52,41 +51,36 @@ class BMConfigParser(SafeConfigParser):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self._temp[section] = {option: value}
|
self._temp[section] = {option: value}
|
||||||
|
|
||||||
def safeGetBoolean(self, section, field):
|
def safeGetBoolean(self, section, option):
|
||||||
"""Return value as boolean, False on exceptions"""
|
"""Return value as boolean, False on exceptions"""
|
||||||
try:
|
try:
|
||||||
# Used in the python2.7
|
return self.getboolean(section, option)
|
||||||
# return self.getboolean(section, field)
|
|
||||||
# Used in the python3.5.2
|
|
||||||
# print(config, section, field)
|
|
||||||
return self.getboolean(section, field)
|
|
||||||
except (configparser.NoSectionError, configparser.NoOptionError,
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
||||||
ValueError, AttributeError):
|
ValueError, AttributeError):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def safeGetInt(self, section, field, default=0):
|
def safeGetInt(self, section, option, default=0):
|
||||||
"""Return value as integer, default on exceptions,
|
"""Return value as integer, default on exceptions,
|
||||||
0 if default missing"""
|
0 if default missing"""
|
||||||
try:
|
try:
|
||||||
# Used in the python2.7
|
return int(self.get(section, option))
|
||||||
# return self.getint(section, field)
|
|
||||||
# Used in the python3.7.0
|
|
||||||
return int(self.get(section, field))
|
|
||||||
except (configparser.NoSectionError, configparser.NoOptionError,
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
||||||
ValueError, AttributeError):
|
ValueError, AttributeError):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def safeGetFloat(self, section, field, default=0.0):
|
def safeGetFloat(self, section, option, default=0.0):
|
||||||
"""Return value as float, default on exceptions,
|
"""Return value as float, default on exceptions,
|
||||||
0.0 if default missing"""
|
0.0 if default missing"""
|
||||||
try:
|
try:
|
||||||
return self.getfloat(section, field)
|
return self.getfloat(section, option)
|
||||||
except (configparser.NoSectionError, configparser.NoOptionError,
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
||||||
ValueError, AttributeError):
|
ValueError, AttributeError):
|
||||||
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"""
|
"""
|
||||||
|
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,
|
||||||
|
@ -111,26 +105,14 @@ class BMConfigParser(SafeConfigParser):
|
||||||
|
|
||||||
def read(self, filenames=None):
|
def read(self, filenames=None):
|
||||||
self._reset()
|
self._reset()
|
||||||
SafeConfigParser.read(self, os.path.join(os.path.dirname(__file__), 'default.ini'))
|
SafeConfigParser.read(
|
||||||
|
self, os.path.join(os.path.dirname(__file__), 'default.ini'))
|
||||||
if filenames:
|
if filenames:
|
||||||
SafeConfigParser.read(self, filenames)
|
SafeConfigParser.read(self, filenames)
|
||||||
|
|
||||||
if sys.version_info[0] == 3:
|
def addresses(self):
|
||||||
@staticmethod
|
|
||||||
def addresses(hidden=False):
|
|
||||||
"""Return a list of local bitmessage addresses (from section labels)"""
|
"""Return a list of local bitmessage addresses (from section labels)"""
|
||||||
return [x for x in config.sections() if x.startswith('BM-') and (
|
return [x for x in self.sections() if x.startswith('BM-')]
|
||||||
hidden or not config.safeGetBoolean(x, 'hidden'))]
|
|
||||||
|
|
||||||
def readfp(self, fp, filename=None):
|
|
||||||
# pylint: disable=no-member
|
|
||||||
SafeConfigParser.read_file(self, fp)
|
|
||||||
else:
|
|
||||||
@staticmethod
|
|
||||||
def addresses():
|
|
||||||
"""Return a list of local bitmessage addresses (from section labels)"""
|
|
||||||
return [
|
|
||||||
x for x in config.sections() if x.startswith('BM-')]
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Save the runtime config onto the filesystem"""
|
"""Save the runtime config onto the filesystem"""
|
||||||
|
@ -173,4 +155,7 @@ class BMConfigParser(SafeConfigParser):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
config = BMConfigParser()
|
if not getattr(BMConfigParser, 'read_file', False):
|
||||||
|
BMConfigParser.read_file = BMConfigParser.readfp
|
||||||
|
|
||||||
|
config = BMConfigParser() # TODO: remove this crutch
|
||||||
|
|
|
@ -81,7 +81,7 @@ class TestConfig(unittest.TestCase):
|
||||||
"""safeGetInt retuns provided default for bitmessagesettings option or 0"""
|
"""safeGetInt retuns provided default for bitmessagesettings option or 0"""
|
||||||
config = BMConfigParser()
|
config = BMConfigParser()
|
||||||
test_config_object = StringIO(test_config)
|
test_config_object = StringIO(test_config)
|
||||||
config.readfp(test_config_object)
|
config.read_file(test_config_object)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
config.safeGetInt('bitmessagesettings', 'maxaddrperstreamsend'), 100)
|
config.safeGetInt('bitmessagesettings', 'maxaddrperstreamsend'), 100)
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
|
|
Reference in New Issue
Block a user