rework config mechanism

This commit is contained in:
Yannik Enss
2021-12-07 16:18:58 +01:00
parent 21dbdfa7ab
commit 0bc6637047
4 changed files with 73 additions and 20 deletions

View File

@@ -25,6 +25,9 @@ WEEKDAYS = { 0: "Montag",
5: "Samstag",
6: "Sonntag" }
IMPORT_RE = re.compile(r"@import\((.*)\)")
FILE_RE = re.compile(r"@file\((.*)\)")
def decode_header(header):
decoded_header = email.header.decode_header(header)[0]
encoding = decoded_header[1] or "ascii"
@@ -152,6 +155,49 @@ def conf2top(top):
return Top(top["title"], sender, body, protostub)
import_cache = {}
def get_imported_conf_entry(f, key):
if f not in import_cache:
import_cache[f] = yaml.safe_load(open(f))
return import_cache[f][key]
def do_imports(entry):
if isinstance(entry, dict):
d = entry.items()
result = {}
for key,value in d:
try:
result[key] = do_imports(value)
except KeyError:
pass
return result
if isinstance(entry, list):
l = iter(entry)
result = []
for item in l:
result.append(do_imports(item))
return result
if isinstance(entry, str):
match = IMPORT_RE.match(entry)
if match:
f, key = match.group(1).split(":")
return get_imported_conf_entry(f, key)
match = FILE_RE.match(entry)
if match:
with open(match.group(1)) as f:
return f.read().strip()
return entry
def get_config(f):
raw_config = yaml.safe_load(open(f))
config = do_imports(raw_config)
return config
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", "-c", default=CONFIG_FILE)
@@ -161,24 +207,17 @@ if __name__ == "__main__":
mode.add_argument("--presentation", action="store_true")
mode.add_argument("--protocol", action="store_true")
parser.add_argument("--debug", action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--print-config", action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--write-mbox", action="store_true")
parser.add_argument("--send-mm", action="store_true")
parser.add_argument("--send-mail", action="store_true")
args = parser.parse_args()
config = yaml.safe_load(open(args.config))
config = get_config(args.config)
if config["redeleitung"]["name"].startswith("./"):
with open(config["redeleitung"]["name"]) as f:
config["redeleitung"]["name"] = f.read().strip()
if config["protokoll"]["name"].startswith("./"):
with open(config["protokoll"]["name"]) as f:
config["protokoll"]["name"] = f.read().strip()
if config["place"].startswith("./"):
with open(config["place"]) as f:
config["place"] = f.read().strip()
if args.print_config:
pprint(config)
sys.exit(0)
if args.invite:
template_file = config["invite_template_file"]