Add unconnected database support
Also adds a helper for doing nested config parsing since we're getting more and more
This commit is contained in:
parent
cee708f4d8
commit
0b3b6f50fe
5 changed files with 53 additions and 12 deletions
|
|
@ -4,7 +4,8 @@ from typing import Any
|
|||
|
||||
from .logging import Logging
|
||||
from .email import Email
|
||||
from .parse import ConfigParseError, assert_key_of_type
|
||||
from .database import Database
|
||||
from .parse import assert_key_of_type, parse_nested_config
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -13,6 +14,7 @@ class Config:
|
|||
port: int | None
|
||||
logging: Logging
|
||||
email: Email
|
||||
database: Database
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, config: dict[str, Any]) -> 'Config':
|
||||
|
|
@ -20,17 +22,13 @@ class Config:
|
|||
assert_key_of_type(config, 'email', dict)
|
||||
assert_key_of_type(config, 'host', str)
|
||||
assert_key_of_type(config, 'port', int)
|
||||
try:
|
||||
log_config = Logging.from_dict(config['logging'])
|
||||
except ConfigParseError as e:
|
||||
raise ConfigParseError(['logging', *e.keypath], e.issue) from e
|
||||
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)
|
||||
|
||||
try:
|
||||
email_config = Email.from_dict(config['email'])
|
||||
except ConfigParseError as e:
|
||||
raise ConfigParseError(['email', *e.keypath], e.issue) from e
|
||||
|
||||
return Config(host=config['host'], port=config['port'], email=email_config, logging=log_config)
|
||||
return Config(
|
||||
host=config['host'], port=config['port'], email=email_config, logging=log_config, database=db_config
|
||||
)
|
||||
|
||||
|
||||
def parse_config(filename: str) -> Config:
|
||||
|
|
|
|||
16
src/config/database.py
Normal file
16
src/config/database.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from src.config.parse import assert_key_of_type
|
||||
from typing import Any
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Database:
|
||||
url: str
|
||||
echo: bool
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, config: dict[str, Any]) -> 'Database':
|
||||
assert_key_of_type(config, 'url', str)
|
||||
assert_key_of_type(config, 'echo', bool)
|
||||
|
||||
return Database(url=config['url'], echo=config['echo'])
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Any
|
||||
from typing import Any, TypeVar, Callable
|
||||
|
||||
|
||||
class ConfigParseError(Exception):
|
||||
|
|
@ -18,3 +18,14 @@ def assert_key_of_type(config: dict[str, Any], key: str, kind: Any):
|
|||
|
||||
if not isinstance(config[key], kind):
|
||||
raise ConfigParseError([key], f'type of "{key}" was {type(config[key])}, expected {kind}')
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
def parse_nested_config(config: dict[str, Any], key: str, parse: Callable[[dict[str, Any]], T]) -> T:
|
||||
assert_key_of_type(config, key, dict)
|
||||
try:
|
||||
return parse(config[key])
|
||||
except ConfigParseError as e:
|
||||
raise ConfigParseError([key, *e.keypath], e.issue)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue