import os import yaml from pathlib import Path from dataclasses import dataclass from jinja2 import BaseLoader, Environment def render_env_template(raw_config: str) -> dict: template = Environment(loader=BaseLoader).from_string(raw_config) conf = template.render(**os.environ) return yaml.safe_load(conf) @dataclass class Config: db_path: Path matrix_homeserver_url: str matrix_full_bot_id: str matrix_bot_password: str telegram_bot_token: str matrix_owner_id: str @classmethod def from_dict(config_class, dict): return config_class(**dict) @classmethod def from_yaml_config(config_class, path: Path): with open(path) as raw: return config_class.from_dict(render_env_template(raw.read()))