Reverting some strange logic and formatting. Insted use six

- to replace basestring with compatible type
  - for unified configparser import
This commit is contained in:
Dmitri Bogomolov 2020-11-06 11:58:36 +02:00
parent d5666f7ffc
commit bb144d78f6
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
3 changed files with 38 additions and 44 deletions

View File

@ -2,3 +2,4 @@ coverage
python_prctl python_prctl
psutil psutil
pycrypto pycrypto
six

View File

@ -59,7 +59,7 @@ if __name__ == "__main__":
libraries=['pthread', 'crypto'], libraries=['pthread', 'crypto'],
) )
installRequires = [] installRequires = ['six']
packages = [ packages = [
'pybitmessage', 'pybitmessage',
'pybitmessage.bitmessageqt', 'pybitmessage.bitmessageqt',

View File

@ -2,21 +2,18 @@
BMConfigParser class definition and default configuration settings BMConfigParser class definition and default configuration settings
""" """
import sys import os
if sys.version_info[0] == 3: import shutil
# python 3 import sys # FIXME: bad style! write more generally
import configparser as ConfigParser from datetime import datetime
SafeConfigParser = ConfigParser.ConfigParser
else: from six import string_types
# python 2 from six.moves import configparser
import ConfigParser
SafeConfigParser = ConfigParser.SafeConfigParser
import state import state
from singleton import Singleton from singleton import Singleton
import os
import shutil SafeConfigParser = configparser.SafeConfigParser
from datetime import datetime
BMConfigDefaults = { BMConfigDefaults = {
@ -54,37 +51,37 @@ BMConfigDefaults = {
@Singleton @Singleton
class BMConfigParser(SafeConfigParser): class BMConfigParser(SafeConfigParser):
""" """
Singleton class inherited from :class:`ConfigParser.SafeConfigParser` Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
with additional methods specific to bitmessage config. with additional methods specific to bitmessage config.
""" """
# pylint: disable=too-many-ancestors # pylint: disable=too-many-ancestors
_temp = {} _temp = {}
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, str): if not isinstance(value, string_types):
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" % value) raise ValueError("Invalid value %s" % value)
return ConfigParser.ConfigParser.set(self, section, option, value) return SafeConfigParser.set(self, section, option, value)
def get(self, section, option, raw=False, vars=None): def get(self, section, option, raw=False, vars=None):
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
# pylint: disable=arguments-differ # pylint: disable=arguments-differ
try: try:
if section == "bitmessagesettings" and option == "timeformat": if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get( return SafeConfigParser.get(
self, section, option, raw=True, vars=vars) self, section, option, raw=True, vars=vars)
try: try:
return self._temp[section][option] return self._temp[section][option]
except KeyError: except KeyError:
pass pass
return ConfigParser.ConfigParser.get( return SafeConfigParser.get(
self, section, option, raw=True, vars=vars) self, section, option, raw=True, vars=vars)
except ConfigParser.InterpolationError: except configparser.InterpolationError:
return ConfigParser.ConfigParser.get( return SafeConfigParser.get(
self, section, option, raw=True, vars=vars) self, section, option, raw=True, vars=vars)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: except (configparser.NoSectionError, configparser.NoOptionError) as e:
try: try:
return BMConfigDefaults[section][option] return BMConfigDefaults[section][option]
except (KeyError, ValueError, AttributeError): except (KeyError, ValueError, AttributeError):
@ -93,18 +90,18 @@ class BMConfigParser(SafeConfigParser):
# pylint: disable=arguments-differ # pylint: disable=arguments-differ
try: try:
if section == "bitmessagesettings" and option == "timeformat": if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get( return SafeConfigParser.get(
self, section, option, raw, vars) self, section, option, raw, vars)
try: try:
return self._temp[section][option] return self._temp[section][option]
except KeyError: except KeyError:
pass pass
return ConfigParser.ConfigParser.get( return SafeConfigParser.get(
self, section, option, True, vars) self, section, option, True, vars)
except ConfigParser.InterpolationError: except configparser.InterpolationError:
return ConfigParser.ConfigParser.get( return SafeConfigParser.get(
self, section, option, True, vars) self, section, option, True, vars)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: except (configparser.NoSectionError, configparser.NoOptionError) as e:
try: try:
return BMConfigDefaults[section][option] return BMConfigDefaults[section][option]
except (KeyError, ValueError, AttributeError): except (KeyError, ValueError, AttributeError):
@ -125,7 +122,7 @@ class BMConfigParser(SafeConfigParser):
# Used in the python3.5.2 # Used in the python3.5.2
# print(config, section, field) # print(config, section, field)
return self.getboolean(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
@ -137,7 +134,7 @@ class BMConfigParser(SafeConfigParser):
# return self.getint(section, field) # return self.getint(section, field)
# Used in the python3.7.0 # Used in the python3.7.0
return int(self.get(section, field)) return int(self.get(section, field))
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError): ValueError, AttributeError):
return default return default
@ -145,7 +142,7 @@ class BMConfigParser(SafeConfigParser):
"""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,
ValueError, AttributeError): ValueError, AttributeError):
return default return default
@ -153,7 +150,7 @@ class BMConfigParser(SafeConfigParser):
# pylint: disable=signature-differs # pylint: disable=signature-differs
"""Return section variables as parent, """Return section variables as parent,
but override the "raw" argument to always True""" but override the "raw" argument to always True"""
return ConfigParser.ConfigParser.items(self, section, True, variables) return SafeConfigParser.items(self, section, True, variables)
if sys.version_info[0] == 3: if sys.version_info[0] == 3:
@staticmethod @staticmethod
@ -163,7 +160,7 @@ class BMConfigParser(SafeConfigParser):
hidden or not BMConfigParser().safeGetBoolean(x, 'hidden'))] hidden or not BMConfigParser().safeGetBoolean(x, 'hidden'))]
def read(self, filenames): def read(self, filenames):
ConfigParser.ConfigParser.read(self, filenames) SafeConfigParser.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:
@ -173,13 +170,13 @@ class BMConfigParser(SafeConfigParser):
): ):
try: try:
newVal = BMConfigDefaults[section][option] newVal = BMConfigDefaults[section][option]
except ConfigParser.NoSectionError: except configparser.NoSectionError:
continue continue
except KeyError: except KeyError:
continue continue
ConfigParser.ConfigParser.set( SafeConfigParser.set(
self, section, option, newVal) self, section, option, newVal)
except ConfigParser.InterpolationError: except configparser.InterpolationError:
continue continue
else: else:
@ -199,21 +196,21 @@ class BMConfigParser(SafeConfigParser):
def read(self, filenames): def read(self, filenames):
"""Read config and populate defaults""" """Read config and populate defaults"""
self._reset() self._reset()
ConfigParser.ConfigParser.read(self, filenames) SafeConfigParser.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( if not self.validate(
section, option, section, option,
ConfigParser.ConfigParser.get(self, section, option) SafeConfigParser.get(self, section, option)
): ):
try: try:
newVal = BMConfigDefaults[section][option] newVal = BMConfigDefaults[section][option]
except KeyError: except KeyError:
continue continue
ConfigParser.ConfigParser.set( SafeConfigParser.set(
self, section, option, newVal) self, section, option, newVal)
except ConfigParser.InterpolationError: except configparser.InterpolationError:
continue continue
def save(self): def save(self):
@ -241,11 +238,7 @@ class BMConfigParser(SafeConfigParser):
def validate(self, section, option, value): def validate(self, section, option, value):
"""Input validator interface (using factory pattern)""" """Input validator interface (using factory pattern)"""
try: try:
if sys.version_info[0] == 3: return getattr(self, 'validate_%s_%s' % (section, option))(value)
return getattr(self, 'validate_{}_{}'.format(
section, option))(value)
else:
return getattr(self, 'validate_%s_%s' % (section, option))(value)
except AttributeError: except AttributeError:
return True return True