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
Raw Normal View History

2023-01-10 00:48:55 +00:00
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
2023-01-10 13:16:50 +00:00
matrix_full_bot_id: str
matrix_bot_password: str
2023-01-10 00:48:55 +00:00
telegram_bot_token: str
2023-01-10 13:16:50 +00:00
matrix_owner_id: str
2023-01-10 00:48:55 +00:00
@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()))