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
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ __pycache__/*
|
||||||
config.json
|
config.json
|
||||||
result
|
result
|
||||||
todo.md
|
todo.md
|
||||||
|
*.db
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ from typing import Any
|
||||||
|
|
||||||
from .logging import Logging
|
from .logging import Logging
|
||||||
from .email import Email
|
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
|
@dataclass
|
||||||
|
|
@ -13,6 +14,7 @@ class Config:
|
||||||
port: int | None
|
port: int | None
|
||||||
logging: Logging
|
logging: Logging
|
||||||
email: Email
|
email: Email
|
||||||
|
database: Database
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(cls, config: dict[str, Any]) -> 'Config':
|
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, 'email', dict)
|
||||||
assert_key_of_type(config, 'host', str)
|
assert_key_of_type(config, 'host', str)
|
||||||
assert_key_of_type(config, 'port', int)
|
assert_key_of_type(config, 'port', int)
|
||||||
try:
|
log_config = parse_nested_config(config, 'logging', Logging.from_dict)
|
||||||
log_config = Logging.from_dict(config['logging'])
|
email_config = parse_nested_config(config, 'email', Email.from_dict)
|
||||||
except ConfigParseError as e:
|
db_config = parse_nested_config(config, 'database', Database.from_dict)
|
||||||
raise ConfigParseError(['logging', *e.keypath], e.issue) from e
|
|
||||||
|
|
||||||
try:
|
return Config(
|
||||||
email_config = Email.from_dict(config['email'])
|
host=config['host'], port=config['port'], email=email_config, logging=log_config, database=db_config
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_config(filename: str) -> 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):
|
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):
|
if not isinstance(config[key], kind):
|
||||||
raise ConfigParseError([key], f'type of "{key}" was {type(config[key])}, expected {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)
|
||||||
|
|
|
||||||
15
src/infra/db.py
Normal file
15
src/infra/db.py
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
from src.config.database import Database as DatabaseConfig
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_database(config: DatabaseConfig):
|
||||||
|
engine = create_engine(config.url, echo=config.echo)
|
||||||
|
# Ensure that all tables exist in the database
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
return engine
|
||||||
Loading…
Add table
Add a link
Reference in a new issue