Update config check to use default values dynamically

This commit is contained in:
Tim Van Baak 2019-04-22 14:56:47 -07:00
parent 95d2cddf17
commit 1f702b5af4
1 changed files with 35 additions and 26 deletions

View File

@ -1,5 +1,6 @@
import os import os
import re import re
import io
from urllib import parse from urllib import parse
import pkg_resources import pkg_resources
@ -43,12 +44,9 @@ def load_resource(filename, cache={}):
cache[filename] = unistr cache[filename] = unistr
return cache[filename] return cache[filename]
def load_config(name): def parse_config_file(f):
""" """Parses a Lexipython config file."""
Loads values from a Lexicon's config file.
"""
config = {} config = {}
with open(os.path.join("lexicon", name, "lexicon.cfg"), "r", encoding="utf8") as f:
line = f.readline() line = f.readline()
while line: while line:
# Skim lines until a value definition begins # Skim lines until a value definition begins
@ -66,12 +64,23 @@ def load_config(name):
line = f.readline() line = f.readline()
conf_match = re.match(r"<<<{0}<<<\s+".format(conf), line) conf_match = re.match(r"<<<{0}<<<\s+".format(conf), line)
if not line: if not line:
# TODO Not this raise EOFError("Reached EOF while reading config value {}".format(conf))
raise SystemExit("Reached EOF while reading config value {}".format(conf))
config[conf] = conf_value.strip() config[conf] = conf_value.strip()
# Check that all necessary values were configured return config
for config_value in ['LEXICON_TITLE', 'PROMPT', 'SESSION_PAGE', "INDEX_LIST"]:
if config_value not in config: def load_config(name):
# TODO Not this either """
raise SystemExit("Error: {} not set in lexipython.cfg".format(config_value)) Loads values from a Lexicon's config file.
"""
with open(os.path.join("lexicon", name, "lexicon.cfg"), "r", encoding="utf8") as f:
config = parse_config_file(f)
# Check that no values are missing that are present in the default config
with io.StringIO(load_resource("lexicon.cfg")) as f:
default_config = parse_config_file(f)
missing_keys = []
for key in default_config.keys():
if key not in config:
missing_keys.append(key)
if missing_keys:
raise KeyError("{} missing config values for: {}".format(name, " ".join(missing_keys)))
return config return config