diff options
Diffstat (limited to 'tools/readconfig.py')
-rw-r--r-- | tools/readconfig.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/tools/readconfig.py b/tools/readconfig.py index d883815..b9a0b07 100644 --- a/tools/readconfig.py +++ b/tools/readconfig.py @@ -12,28 +12,38 @@ def render_path(path): else: return "the top level" +import traceback + class ErrorHandlingDict(dict): - def __init__(self, filename, path): + def __init__(self, filename, path, optionals): self._filename = filename self._path = path + self._optionals = optionals dict.__init__({}) + def get(self, key, default=None): + path = "/".join(self._path + [key]) + if path in self._optionals: + return dict.get(self, key, default) + print >>sys.stderr, "error: read key", path, "optionally with default", default + traceback.print_stack() + sys.exit(1) def __missing__(self, key): path = render_path(self._path) print >>sys.stderr, "error: could not find configuration key '%s' at %s in %s" % (key, path, self._filename) sys.exit(1) -def errorhandlify(term, filename, path=[]): +def errorhandlify(term, filename, optionals, path=[]): if isinstance(term, basestring): return term elif isinstance(term, int): return term elif isinstance(term, dict): - result = ErrorHandlingDict(filename, path) + result = ErrorHandlingDict(filename, path, optionals) for k, v in term.items(): - result[k] = errorhandlify(v, filename, path + [k]) + result[k] = errorhandlify(v, filename, optionals, path + [k]) return result elif isinstance(term, list): - return [errorhandlify(e, filename, path + ["item %d" % i]) for i, e in enumerate(term, start=1)] + return [errorhandlify(e, filename, optionals, path + ["item %d" % i]) for i, e in enumerate(term, start=1)] else: print "unknown type", type(term) sys.exit(1) @@ -134,14 +144,16 @@ def insert_defaults(config, configdefaults): def common_read_config(f, filename, localconfig=True): if localconfig: schema = configschema.localconfigschema - configdefaults = [] + configdefaults = configschema.localconfigdefaults + optionals = configschema.localconfigoptionals else: schema = configschema.globalconfigschema configdefaults = configschema.globalconfigdefaults + optionals = configschema.globalconfigoptionals config = yaml.load(f, yaml.SafeLoader) insert_defaults(config, configdefaults) check_config_schema(config, schema) - return errorhandlify(config, filename) + return errorhandlify(config, filename, optionals) def read_config(filename, localconfig=True): return common_read_config(open(filename), filename, localconfig=localconfig) |