aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/configs
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-10-17 14:18:41 +0800
committerAaron LI <aaronly.me@outlook.com>2016-10-17 14:18:41 +0800
commit0220e396d3025d5ddc8226fc54d87f074c7e9143 (patch)
tree00c83b541077d5ea70d1f852e7e2a3c5fe7e7433 /fg21sim/configs
parent23fe3a3fcb7b8a74f51fcadb915b5b8fafea368e (diff)
downloadfg21sim-0220e396d3025d5ddc8226fc54d87f074c7e9143.tar.bz2
configs/manager.py: Change __init__() to load user config
Change the "__init__()" method to load the user configurations (i.e., by invoke the "read_userconfig()" method) instead of loading extra general configurations. Method "getn()": also check the validity for the parameter "sep".
Diffstat (limited to 'fg21sim/configs')
-rw-r--r--fg21sim/configs/manager.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py
index bb06394..64bace4 100644
--- a/fg21sim/configs/manager.py
+++ b/fg21sim/configs/manager.py
@@ -49,8 +49,12 @@ class ConfigManager:
Parameters
----------
- configs: list[str], optional
- List of user config files to be merged
+ userconfig: str, optional
+ Filename/path to the user configuration file.
+ If provided, the user configurations is also loaded, validated, and
+ merged into the configurations data.
+ The user configuration can also be later loaded by
+ ``self.read_userconfig()``.
Attributes
----------
@@ -67,9 +71,10 @@ class ConfigManager:
configs specifying the input templates or data files, therefore
allow the use of relative path for those configs.
"""
- def __init__(self, configs=None):
- """Initialize with the bundled default configurations and
- specifications.
+ def __init__(self, userconfig=None):
+ """Load the bundled default configurations and specifications.
+ If the ``userconfig`` provided, the user configurations is also
+ loaded, validated, and merged.
"""
configspec = _get_configspec()
self._configspec = ConfigObj(configspec, interpolation=False,
@@ -77,9 +82,8 @@ class ConfigManager:
configs_default = ConfigObj(interpolation=False,
configspec=self._configspec)
self._config = self._validate(configs_default)
- if configs:
- for config in configs:
- self.read_config(config)
+ if userconfig:
+ self.read_userconfig(userconfig)
def read_config(self, config):
"""Read, validate and merge the input config.
@@ -106,7 +110,7 @@ class ConfigManager:
Parameters
----------
- userconfig : filename
+ userconfig : str
Filename/path to the user configuration file.
NOTE
@@ -181,6 +185,8 @@ class ConfigManager:
- Stackoverflow: Checking a Dictionary using a dot notation string
https://stackoverflow.com/q/12414821/4856091
"""
+ if len(sep) != 1:
+ raise ValueError("Invalid parameter 'sep': %s" % sep)
if isinstance(key, str):
key = key.split(sep)
return reduce(dict.get, key, self._config)