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