Split ConfigManager.read from constructor.

This commit is contained in:
Florian Bruhin 2015-12-11 07:09:09 +01:00
parent b3b2fb51fc
commit 10dd1b50b9
2 changed files with 22 additions and 15 deletions

View File

@ -140,9 +140,10 @@ def _init_main_config(parent=None):
parent: The parent to pass to ConfigManager.
"""
args = objreg.get('args')
config_obj = ConfigManager(parent=parent)
try:
config_obj = ConfigManager(standarddir.config(), 'qutebrowser.conf',
args.relaxed_config, parent=parent)
config_obj.read(standarddir.config(), 'qutebrowser.conf',
relaxed=args.relaxed_config)
except (configexc.Error, configparser.Error, UnicodeDecodeError) as e:
log.init.exception(e)
errstr = "Error while reading config:"
@ -362,24 +363,16 @@ class ConfigManager(QObject):
changed = pyqtSignal(str, str)
style_changed = pyqtSignal(str, str)
def __init__(self, configdir, fname, relaxed=False, parent=None):
def __init__(self, parent=None):
super().__init__(parent)
self._initialized = False
self._configdir = None
self._fname = None
self.sections = configdata.data()
self._interpolation = configparser.ExtendedInterpolation()
self._proxies = {}
for sectname in self.sections:
self._proxies[sectname] = SectionProxy(self, sectname)
self._fname = fname
if configdir is None:
self._configdir = None
self._initialized = True
else:
self._configdir = configdir
parser = ini.ReadConfigParser(configdir, fname)
self._from_cp(parser, relaxed)
self._initialized = True
self._validate_all()
def __getitem__(self, key):
"""Get a section from the config."""
@ -571,6 +564,19 @@ class ConfigManager(QObject):
option.value()):
self._changed(sectname, optname)
def read(self, configdir, fname, relaxed=False):
"""Read the config from the given directory/file."""
self._fname = fname
if configdir is None:
self._configdir = None
self._initialized = True
else:
self._configdir = configdir
parser = ini.ReadConfigParser(configdir, fname)
self._from_cp(parser, relaxed)
self._initialized = True
self._validate_all()
def items(self, sectname, raw=True):
"""Get a list of (optname, value) tuples for a section.

View File

@ -47,7 +47,8 @@ class TestConfigParser:
cp = configparser.ConfigParser(interpolation=None,
comment_prefixes='#')
cp.optionxform = lambda opt: opt # be case-insensitive
cfg = config.ConfigManager(None, None)
cfg = config.ConfigManager()
cfg.read(None, None)
return self.Objects(cp=cp, cfg=cfg)
def test_simple(self, objects):
@ -272,7 +273,7 @@ class TestDefaultConfig:
@pytest.mark.usefixtures('qapp')
def test_default_config(self):
"""Test validating of the default config."""
conf = config.ConfigManager(None, None)
conf = config.ConfigManager()
conf._validate_all()
def test_default_key_config(self):