33 lines
768 B
Python
33 lines
768 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_bot_id: str
|
||
|
matrix_homeserver_url: str
|
||
|
matrix_password: str
|
||
|
matrix_owner_id: str
|
||
|
telegram_bot_token: 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()))
|