Add config tooling
This commit is contained in:
parent
705432c4a5
commit
f50ba1050a
6
devloop/config.py
Normal file
6
devloop/config.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
from latecomers.config import Config
|
||||
|
||||
|
||||
config = Config("./devloop/config.yaml")
|
||||
print(config)
|
50
latecomers/config.py
Normal file
50
latecomers/config.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
import logging
|
||||
import typing as T
|
||||
|
||||
from latecomers.helpers import logit
|
||||
|
||||
import yaml
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_section(obj: T.Dict[T.Text, T.Any], key: T.Text) -> T.Any:
|
||||
value = obj.get(key)
|
||||
if value is None:
|
||||
raise ValueError(f"Missing '{key}' value")
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class Config(object):
|
||||
smtp: T.Dict[T.Text, T.Any] = {}
|
||||
to: T.List[T.Text] = []
|
||||
|
||||
def __init__(self, path: T.Text) -> None:
|
||||
self.from_file(path)
|
||||
|
||||
@logit(logger)
|
||||
def from_file(self, path: T.Text) -> None:
|
||||
"""
|
||||
Parses the file and validates it.
|
||||
"""
|
||||
with open(path) as f:
|
||||
content = yaml.safe_load(f)
|
||||
|
||||
self.to = get_section(content, "to")
|
||||
|
||||
smtp = get_section(content, "smtp")
|
||||
for key in [
|
||||
"smtp_addr",
|
||||
"smtp_port",
|
||||
"smtp_starttls",
|
||||
"smtp_user",
|
||||
"smtp_pass",
|
||||
]:
|
||||
self.smtp[key] = get_section(smtp, key)
|
||||
|
||||
self.smtp["smtp_from"] = smtp.get("smtp_from")
|
||||
|
||||
def __str__(self) -> T.Text:
|
||||
return f"Config<smtp={self.smtp},to={self.to}>"
|
|
@ -11,6 +11,7 @@ dependencies = [
|
|||
"lxml",
|
||||
"pandas",
|
||||
"openpyxl",
|
||||
"PyYAML",
|
||||
]
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
|
@ -30,6 +31,9 @@ requires = [
|
|||
]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project.scripts]
|
||||
latecomers = "latecomers.main:cli"
|
||||
|
||||
[tool.black]
|
||||
line-length = 88
|
||||
target_version = ['py36']
|
||||
|
|
Loading…
Reference in New Issue
Block a user