From 7d2b13e4ac52ebdfbfa6a7528a1e355ac4e6c941 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 16 Feb 2022 23:04:52 +0200 Subject: [PATCH] Format bmconfigparser: - remove meaningless comments, rename strange args, fix line lengths, - revert BMConfigParser.addresses() entanglement, - revert BMConfigParser read_file -> readfp alias. --- src/bmconfigparser.py | 51 ++++++++++++++-------------------------- src/tests/test_config.py | 2 +- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index 16d217e8..0a2ec558 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -4,7 +4,6 @@ BMConfigParser class definition and default configuration settings import os import shutil -import sys # FIXME: bad style! write more generally from threading import Event from datetime import datetime @@ -52,41 +51,36 @@ class BMConfigParser(SafeConfigParser): except KeyError: self._temp[section] = {option: value} - def safeGetBoolean(self, section, field): + def safeGetBoolean(self, section, option): """Return value as boolean, False on exceptions""" try: - # Used in the python2.7 - # return self.getboolean(section, field) - # Used in the python3.5.2 - # print(config, section, field) - return self.getboolean(section, field) + return self.getboolean(section, option) except (configparser.NoSectionError, configparser.NoOptionError, ValueError, AttributeError): return False - def safeGetInt(self, section, field, default=0): + def safeGetInt(self, section, option, default=0): """Return value as integer, default on exceptions, 0 if default missing""" try: - # Used in the python2.7 - # return self.getint(section, field) - # Used in the python3.7.0 - return int(self.get(section, field)) + return int(self.get(section, option)) except (configparser.NoSectionError, configparser.NoOptionError, ValueError, AttributeError): 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, 0.0 if default missing""" try: - return self.getfloat(section, field) + return self.getfloat(section, option) except (configparser.NoSectionError, configparser.NoOptionError, ValueError, AttributeError): return default 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: return self.get(section, option) except (configparser.NoSectionError, configparser.NoOptionError, @@ -111,26 +105,14 @@ class BMConfigParser(SafeConfigParser): def read(self, filenames=None): 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: SafeConfigParser.read(self, filenames) - if sys.version_info[0] == 3: - @staticmethod - def addresses(hidden=False): - """Return a list of local bitmessage addresses (from section labels)""" - return [x for x in config.sections() if x.startswith('BM-') and ( - 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 addresses(self): + """Return a list of local bitmessage addresses (from section labels)""" + return [x for x in self.sections() if x.startswith('BM-')] def save(self): """Save the runtime config onto the filesystem""" @@ -173,4 +155,7 @@ class BMConfigParser(SafeConfigParser): return True -config = BMConfigParser() +if not getattr(BMConfigParser, 'read_file', False): + BMConfigParser.read_file = BMConfigParser.readfp + +config = BMConfigParser() # TODO: remove this crutch diff --git a/src/tests/test_config.py b/src/tests/test_config.py index cb725369..f2574522 100644 --- a/src/tests/test_config.py +++ b/src/tests/test_config.py @@ -81,7 +81,7 @@ class TestConfig(unittest.TestCase): """safeGetInt retuns provided default for bitmessagesettings option or 0""" config = BMConfigParser() test_config_object = StringIO(test_config) - config.readfp(test_config_object) + config.read_file(test_config_object) self.assertEqual( config.safeGetInt('bitmessagesettings', 'maxaddrperstreamsend'), 100) # pylint: disable=protected-access