Add the bones of an Email Notification service
This commit is contained in:
parent
a7fdd6cbd0
commit
24bb9dae30
8 changed files with 138 additions and 8 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import json
|
||||
from logging import INFO
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from .logging import Logging
|
||||
from .email import Email
|
||||
from .parse import ConfigParseError, assert_key_of_type
|
||||
|
||||
|
||||
|
|
@ -12,21 +12,25 @@ class Config:
|
|||
host: str | None
|
||||
port: int | None
|
||||
logging: Logging
|
||||
email: Email
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, config: dict[str, Any]) -> 'Config':
|
||||
assert_key_of_type(config, 'logging', dict)
|
||||
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'])
|
||||
return Config(host=config['host'], port=config['port'], logging=log_config)
|
||||
|
||||
except ConfigParseError as e:
|
||||
raise ConfigParseError(['logging', *e.keypath], e.issue) from e
|
||||
|
||||
try:
|
||||
email_config = Email.from_dict(config['email'])
|
||||
except ConfigParseError as e:
|
||||
raise ConfigParseError(['email', *e.keypath], e.issue) from e
|
||||
|
||||
DEFAULT_CONFIG = Config(logging=Logging(level=INFO), port=None, host=None)
|
||||
return Config(host=config['host'], port=config['port'], email=email_config, logging=log_config)
|
||||
|
||||
|
||||
def parse_config(filename: str) -> Config:
|
||||
|
|
@ -35,4 +39,4 @@ def parse_config(filename: str) -> Config:
|
|||
return Config.from_dict(config)
|
||||
|
||||
|
||||
__all__ = ['Config', 'parse_config', 'DEFAULT_CONFIG']
|
||||
__all__ = ['Config', 'parse_config']
|
||||
|
|
|
|||
18
src/config/email.py
Normal file
18
src/config/email.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from src.config.parse import assert_key_of_type
|
||||
from typing import Any
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
EmailAddress = str
|
||||
|
||||
|
||||
@dataclass
|
||||
class Email:
|
||||
api_key: str
|
||||
sender: EmailAddress
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, config: dict[str, Any]) -> 'Email':
|
||||
assert_key_of_type(config, 'api_key', str)
|
||||
assert_key_of_type(config, 'sender', str)
|
||||
return Email(api_key=config['api_key'], sender=config['sender'])
|
||||
Loading…
Add table
Add a link
Reference in a new issue