diff options
author | Magnus Ahltorp <map@kth.se> | 2017-07-18 09:23:54 +0200 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2017-07-18 09:23:54 +0200 |
commit | b7074968b4f02a5201ce1092351ee36277fa3953 (patch) | |
tree | 9f4c2182d995284144fcceff2fd063b7bd678e14 /tools/readconfig.py | |
parent | bb67c23918ba22be498537a29c01b696732d5b3b (diff) |
Added formalized config defaults
Refactor reading of config
Move config schemas to separate file
Diffstat (limited to 'tools/readconfig.py')
-rw-r--r-- | tools/readconfig.py | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/tools/readconfig.py b/tools/readconfig.py index 15b9c61..9c023b1 100644 --- a/tools/readconfig.py +++ b/tools/readconfig.py @@ -4,6 +4,7 @@ import hashlib import yaml import base64 import sys +import configschema def render_path(path): if path: @@ -47,18 +48,12 @@ def verify_config(rawconfig, signature, publickey_base64, filename): except ecdsa.keys.BadSignatureError: print >>sys.stderr, "error: configuration file %s did not have a correct signature" % (filename,) sys.exit(1) + return common_read_config(io.BytesIO(rawconfig), filename, localconfig=False) - return errorhandlify(yaml.load(io.BytesIO(rawconfig), yaml.SafeLoader), filename) - -def verify_and_read_config(filename, publickey_base64, schema=None): +def verify_and_read_config(filename, publickey_base64): rawconfig = open(filename).read() signature = open(filename + ".sig").read() - - verify_config(rawconfig, signature, publickey_base64, filename) - config = yaml.load(io.BytesIO(rawconfig), yaml.SafeLoader) - if schema: - check_config_schema(config, schema) - return errorhandlify(config, filename) + return verify_config(rawconfig, signature, publickey_base64, filename) def insert_schema_path(schema, path, datatype, highleveldatatype): if len(path) == 1: @@ -122,9 +117,28 @@ def check_config_schema_part(term, schema, path=[]): print >>sys.stderr, "unknown type", type(term) sys.exit(1) +def insert_defaults(config, configdefaults): + for (rawpath, value) in configdefaults: + path = rawpath.split("/") + node = config + for e in path[:-1]: + assert(e != "[]") + node = node[e] + lastelem = path[-1] + if lastelem not in node: + node[lastelem] = value -def read_config(filename, schema=None): - config = yaml.load(open(filename), yaml.SafeLoader) - if schema: - check_config_schema(config, schema) +def common_read_config(f, filename, localconfig=True): + if localconfig: + schema = configschema.localconfigschema + configdefaults = [] + else: + schema = configschema.globalconfigschema + configdefaults = configschema.globalconfigdefaults + config = yaml.load(f, yaml.SafeLoader) + insert_defaults(config, configdefaults) + check_config_schema(config, schema) return errorhandlify(config, filename) + +def read_config(filename, localconfig=True): + return common_read_config(open(filename), filename, localconfig=localconfig) |