Fix configparser import error
This commit is contained in:
parent
6f9b66ddff
commit
06cab993d9
|
@ -2,13 +2,22 @@
|
||||||
BMConfigParser class definition and default configuration settings
|
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 os
|
||||||
import shutil
|
import shutil
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import state
|
|
||||||
from singleton import Singleton
|
|
||||||
|
|
||||||
BMConfigDefaults = {
|
BMConfigDefaults = {
|
||||||
"bitmessagesettings": {
|
"bitmessagesettings": {
|
||||||
|
@ -43,7 +52,8 @@ BMConfigDefaults = {
|
||||||
|
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class BMConfigParser(ConfigParser.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.
|
||||||
|
@ -60,26 +70,47 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
raise ValueError("Invalid value %s" % 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, vars=None):
|
||||||
# pylint: disable=arguments-differ
|
if sys.version_info[0] == 3:
|
||||||
try:
|
# pylint: disable=arguments-differ
|
||||||
if section == "bitmessagesettings" and option == "timeformat":
|
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(
|
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:
|
try:
|
||||||
return self._temp[section][option]
|
if section == "bitmessagesettings" and option == "timeformat":
|
||||||
except KeyError:
|
return ConfigParser.ConfigParser.get(
|
||||||
pass
|
self, section, option, raw, vars)
|
||||||
return ConfigParser.ConfigParser.get(
|
try:
|
||||||
self, section, option, True, variables)
|
return self._temp[section][option]
|
||||||
except ConfigParser.InterpolationError:
|
except KeyError:
|
||||||
return ConfigParser.ConfigParser.get(
|
pass
|
||||||
self, section, option, True, variables)
|
return ConfigParser.ConfigParser.get(
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
|
self, section, option, True, vars)
|
||||||
try:
|
except ConfigParser.InterpolationError:
|
||||||
return BMConfigDefaults[section][option]
|
return ConfigParser.ConfigParser.get(
|
||||||
except (KeyError, ValueError, AttributeError):
|
self, section, option, True, vars)
|
||||||
raise e
|
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):
|
def setTemp(self, section, option, value=None):
|
||||||
"""Temporary set option to value, not saving."""
|
"""Temporary set option to value, not saving."""
|
||||||
|
@ -191,3 +222,4 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
if value < 0 or value > 8:
|
if value < 0 or value > 8:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,7 @@
|
||||||
Various tests for config
|
Various tests for config
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from .test_process import TestProcessProto
|
|
||||||
from pybitmessage.bmconfigparser import BMConfigParser
|
from pybitmessage.bmconfigparser import BMConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,32 +34,3 @@ class TestConfig(unittest.TestCase):
|
||||||
BMConfigParser().safeGetInt('nonexistent', 'nonexistent'), 0)
|
BMConfigParser().safeGetInt('nonexistent', 'nonexistent'), 0)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
BMConfigParser().safeGetInt('nonexistent', 'nonexistent', 42), 42)
|
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)
|
|
||||||
|
|
39
src/tests/test_config_functional.py
Normal file
39
src/tests/test_config_functional.py
Normal file
|
@ -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)
|
Loading…
Reference in New Issue
Block a user