aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/configs
diff options
context:
space:
mode:
Diffstat (limited to 'fg21sim/configs')
-rw-r--r--fg21sim/configs/00-general.conf.spec34
-rw-r--r--fg21sim/configs/manager.py44
2 files changed, 73 insertions, 5 deletions
diff --git a/fg21sim/configs/00-general.conf.spec b/fg21sim/configs/00-general.conf.spec
new file mode 100644
index 0000000..c1a0eb3
--- /dev/null
+++ b/fg21sim/configs/00-general.conf.spec
@@ -0,0 +1,34 @@
+# Configurations for "fg21sim"
+# -*- mode: conf -*-
+#
+# Syntax: `ConfigObj`, https://github.com/DiffSK/configobj
+#
+# This file contains the general configurations, which control the general
+# behaviors, or will be used in other configuration sections.
+
+[logging]
+# DEBUG: Detailed information, typically of interest only when diagnosing
+# problems.
+# INFO: Confirmation that things are working as expected.
+# WARNING: An dinciation that something unexpected happended, or indicative
+# of some problem in the near future (e.g., "disk space low").
+# The software is still working as expected.
+# ERROR: Due to a more serious problem, the software has not been able to
+# perform some function.
+# CRITICAL: A serious error, indicating that the program itself may be unable
+# to continue running.
+level = option("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", default="INFO")
+
+# Set the format of displayed messages
+format = string(default="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
+
+# Set the date/time format in messages (default: ISO8601)
+datefmt = string(default="%Y-%m-%dT%H:%M:%S")
+
+# Set the logging filename (will create a `FileHandler`)
+filename = string(default="")
+# Set the mode to open the above logging file
+filemode = option("w", "a", default="a")
+
+# Set the stream used to initialize the `StreamHandler`
+stream = option("stderr", "stdout", "", default="stderr")
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