From 06cab993d9a7ec78d0fc0ea116314cc0ae2d3653 Mon Sep 17 00:00:00 2001 From: Muzahid Date: Tue, 16 Mar 2021 15:18:38 +0530 Subject: [PATCH] Fix configparser import error --- src/bmconfigparser.py | 76 ++++++++++++++++++++--------- src/tests/test_config.py | 33 ------------- src/tests/test_config_functional.py | 39 +++++++++++++++ 3 files changed, 93 insertions(+), 55 deletions(-) create mode 100644 src/tests/test_config_functional.py diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index ff43fd7c..be7b3e87 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -2,13 +2,22 @@ BMConfigParser class definition and default configuration settings """ -import ConfigParser +import sys +if sys.version_info[0] == 3: + # python 3 + import configparser as ConfigParser + SafeConfigParser = ConfigParser.ConfigParser +else: + # python 2 + import ConfigParser + SafeConfigParser = ConfigParser.SafeConfigParser + +import state +from singleton import Singleton import os import shutil from datetime import datetime -import state -from singleton import Singleton BMConfigDefaults = { "bitmessagesettings": { @@ -43,7 +52,8 @@ BMConfigDefaults = { @Singleton -class BMConfigParser(ConfigParser.SafeConfigParser): +class BMConfigParser(SafeConfigParser): + """ Singleton class inherited from :class:`ConfigParser.SafeConfigParser` with additional methods specific to bitmessage config. @@ -60,26 +70,47 @@ class BMConfigParser(ConfigParser.SafeConfigParser): raise ValueError("Invalid value %s" % value) return ConfigParser.ConfigParser.set(self, section, option, value) - def get(self, section, option, raw=False, variables=None): - # pylint: disable=arguments-differ - try: - if section == "bitmessagesettings" and option == "timeformat": + def get(self, section, option, raw=False, vars=None): + if sys.version_info[0] == 3: + # pylint: disable=arguments-differ + try: + if section == "bitmessagesettings" and option == "timeformat": + return ConfigParser.ConfigParser.get( + self, section, option) + try: + return self._temp[section][option] + except KeyError: + pass return ConfigParser.ConfigParser.get( - self, section, option, raw, variables) + self, section, option) + except ConfigParser.InterpolationError: + return ConfigParser.ConfigParser.get( + self, section, option) + except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: + try: + return BMConfigDefaults[section][option] + except (KeyError, ValueError, AttributeError): + raise e + else: + # pylint: disable=arguments-differ try: - return self._temp[section][option] - except KeyError: - pass - return ConfigParser.ConfigParser.get( - self, section, option, True, variables) - except ConfigParser.InterpolationError: - return ConfigParser.ConfigParser.get( - self, section, option, True, variables) - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: - try: - return BMConfigDefaults[section][option] - except (KeyError, ValueError, AttributeError): - raise e + if section == "bitmessagesettings" and option == "timeformat": + return ConfigParser.ConfigParser.get( + self, section, option, raw, vars) + try: + return self._temp[section][option] + except KeyError: + pass + return ConfigParser.ConfigParser.get( + self, section, option, True, vars) + except ConfigParser.InterpolationError: + return ConfigParser.ConfigParser.get( + self, section, option, True, vars) + except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: + try: + return BMConfigDefaults[section][option] + except (KeyError, ValueError, AttributeError): + raise e def setTemp(self, section, option, value=None): """Temporary set option to value, not saving.""" @@ -191,3 +222,4 @@ class BMConfigParser(ConfigParser.SafeConfigParser): if value < 0 or value > 8: return False return True + diff --git a/src/tests/test_config.py b/src/tests/test_config.py index a3b90a4c..d44ec738 100644 --- a/src/tests/test_config.py +++ b/src/tests/test_config.py @@ -2,11 +2,7 @@ Various tests for config """ -import os import unittest -import tempfile - -from .test_process import TestProcessProto from pybitmessage.bmconfigparser import BMConfigParser @@ -38,32 +34,3 @@ class TestConfig(unittest.TestCase): BMConfigParser().safeGetInt('nonexistent', 'nonexistent'), 0) self.assertEqual( BMConfigParser().safeGetInt('nonexistent', 'nonexistent', 42), 42) - - -class TestProcessConfig(TestProcessProto): - """A test case for keys.dat""" - home = tempfile.mkdtemp() - - def test_config_defaults(self): - """Test settings in the generated config""" - self._stop_process() - self._kill_process() - config = BMConfigParser() - config.read(os.path.join(self.home, 'keys.dat')) - - self.assertEqual(config.safeGetInt( - 'bitmessagesettings', 'settingsversion'), 10) - self.assertEqual(config.safeGetInt( - 'bitmessagesettings', 'port'), 8444) - # don't connect - self.assertTrue(config.safeGetBoolean( - 'bitmessagesettings', 'dontconnect')) - # API disabled - self.assertFalse(config.safeGetBoolean( - 'bitmessagesettings', 'apienabled')) - - # extralowdifficulty is false - self.assertEqual(config.safeGetInt( - 'bitmessagesettings', 'defaultnoncetrialsperbyte'), 1000) - self.assertEqual(config.safeGetInt( - 'bitmessagesettings', 'defaultpayloadlengthextrabytes'), 1000) diff --git a/src/tests/test_config_functional.py b/src/tests/test_config_functional.py new file mode 100644 index 00000000..3b973911 --- /dev/null +++ b/src/tests/test_config_functional.py @@ -0,0 +1,39 @@ +""" +Various tests for config +""" + +import os +import unittest +import tempfile + +from .test_process import TestProcessProto +from pybitmessage.bmconfigparser import BMConfigParser + + +class TestProcessConfig(TestProcessProto): + """A test case for keys.dat""" + home = tempfile.mkdtemp() + + def test_config_defaults(self): + """Test settings in the generated config""" + self._stop_process() + self._kill_process() + config = BMConfigParser() + config.read(os.path.join(self.home, 'keys.dat')) + + self.assertEqual(config.safeGetInt( + 'bitmessagesettings', 'settingsversion'), 10) + self.assertEqual(config.safeGetInt( + 'bitmessagesettings', 'port'), 8444) + # don't connect + self.assertTrue(config.safeGetBoolean( + 'bitmessagesettings', 'dontconnect')) + # API disabled + self.assertFalse(config.safeGetBoolean( + 'bitmessagesettings', 'apienabled')) + + # extralowdifficulty is false + self.assertEqual(config.safeGetInt( + 'bitmessagesettings', 'defaultnoncetrialsperbyte'), 1000) + self.assertEqual(config.safeGetInt( + 'bitmessagesettings', 'defaultpayloadlengthextrabytes'), 1000)