This repository has been archived on 2023-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
MirrorTea/mirrortea/config_dataclass.py

35 lines
779 B
Python

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()))