aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/configs/manager.py
diff options
context:
space:
mode:
Diffstat (limited to 'fg21sim/configs/manager.py')
-rw-r--r--fg21sim/configs/manager.py44
1 files changed, 39 insertions, 5 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py
index 933a63d..f5465a9 100644
--- a/fg21sim/configs/manager.py
+++ b/fg21sim/configs/manager.py
@@ -10,13 +10,19 @@ Configuration manager.
"""
import os
+import sys
from glob import glob
-from errors import ConfigError
+import logging
from configobj import ConfigObj, ConfigObjError, flatten_errors
from validate import Validator
+class ConfigError(Exception):
+ """Could not parse user configurations"""
+ pass
+
+
CONFIGS_PATH = os.path.dirname(__file__)
@@ -30,21 +36,23 @@ class ConfigManager:
spec = "\n".join([open(f).read() for f in configs_spec]).split("\n")
self._configspec = ConfigObj(spec, interpolation=False,
list_values=False, _inspec=True)
- self._validator = Validator()
- configs_default = ConfigObj(configspec=self._configspec)
+ configs_default = ConfigObj(interpolation=False,
+ configspec=self._configspec)
self._config = self.validate(configs_default)
if configs:
for config in configs:
self.read_config(config)
def read_config(self, config):
- newconfig = ConfigObj(config, configspec=self._configspec)
+ newconfig = ConfigObj(config, interpolation=False,
+ configspec=self._configspec)
newconfig = self.validate(newconfig)
self._config.merge(newconfig)
def validate(self, config):
+ validator = Validator()
try:
- results = config.validate(self._validator, preserve_errors=True)
+ results = config.validate(validator, preserve_errors=True)
except ConfigObjError as e:
raise ConfigError(e.message)
if not results:
@@ -72,3 +80,29 @@ class ConfigManager:
def set(self, key, value):
self._config[key] = value
+
+ @property
+ def logging(self):
+ """Get and prepare the logging configurations for
+ `logging.basicConfig()`
+ """
+ from logging import FileHandler, StreamHandler
+ conf = self.get("logging")
+ # logging handlers
+ handlers = []
+ stream = conf["stream"]
+ if stream:
+ handlers.append(StreamHandler(getattr(sys, stream)))
+ logfile = conf["filename"]
+ filemode = conf["filemode"]
+ if logfile:
+ handlers.append(FileHandler(logfile, mode=filemode))
+ #
+ logconf = {
+ "level": getattr(logging, conf["level"]),
+ "format": conf["format"],
+ "datefmt": conf["datefmt"],
+ "filemode": filemode,
+ "handlers": handlers,
+ }
+ return logconf