from pathlib import Path import tomllib import string def merge(a: dict, b: dict, path=[]): for key in b: if key in a: if isinstance(a[key], dict) and isinstance(b[key], dict): merge(a[key], b[key], path + [str(key)]) elif a[key] != b[key]: raise Exception('Conflict at ' + '.'.join(path + [str(key)])) else: a[key] = b[key] class Config: def __init__(self, config_path='config', parent_config={}): self.config = {} for file_path in Path(config_path).glob('**/*.toml'): with open(file_path, 'rb') as f: merge(self.config, tomllib.load(f)) merge(self.config, parent_config) def __getitem__(self, item): return self.config[item] class Templates: def __init__(self, template_path='templates'): self.templates = {} path = Path(template_path) for file_path in path.glob('**/*.html'): with open(file_path, "r", encoding='utf-8') as f: relative_path = file_path.relative_to(path).as_posix() self.templates[relative_path] = string.Template(f.read()) def __getitem__(self, item): return self.templates[item]