47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import json
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
from .logging import Logging
|
|
from .email import Email
|
|
from .auth import Auth
|
|
from .database import Database
|
|
from .parse import assert_key_of_type, parse_nested_config
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
host: str | None
|
|
port: int | None
|
|
logging: Logging
|
|
email: Email
|
|
database: Database
|
|
auth: Auth
|
|
|
|
@classmethod
|
|
def from_dict(cls, config: dict[str, Any]) -> 'Config':
|
|
assert_key_of_type(config, 'host', str)
|
|
assert_key_of_type(config, 'port', int)
|
|
|
|
log_config = parse_nested_config(config, 'logging', Logging.from_dict)
|
|
email_config = parse_nested_config(config, 'email', Email.from_dict)
|
|
db_config = parse_nested_config(config, 'database', Database.from_dict)
|
|
auth_config = parse_nested_config(config, 'auth', Auth.from_dict)
|
|
|
|
return Config(
|
|
host=config['host'],
|
|
port=config['port'],
|
|
email=email_config,
|
|
logging=log_config,
|
|
database=db_config,
|
|
auth=auth_config,
|
|
)
|
|
|
|
|
|
def parse_config(filename: str) -> Config:
|
|
with open(filename, 'r') as infile:
|
|
config = json.loads(infile.read())
|
|
return Config.from_dict(config)
|
|
|
|
|
|
__all__ = ['Config', 'parse_config']
|