From f50ba1050ad8dd342dcd719a867414ac6bc632a8 Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 25 Aug 2022 13:12:22 +0200 Subject: [PATCH] Add config tooling --- devloop/config.py | 6 ++++++ latecomers/config.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 4 ++++ 3 files changed, 60 insertions(+) create mode 100644 devloop/config.py create mode 100644 latecomers/config.py diff --git a/devloop/config.py b/devloop/config.py new file mode 100644 index 0000000..c44422d --- /dev/null +++ b/devloop/config.py @@ -0,0 +1,6 @@ +# -*- encoding: utf-8 -*- +from latecomers.config import Config + + +config = Config("./devloop/config.yaml") +print(config) diff --git a/latecomers/config.py b/latecomers/config.py new file mode 100644 index 0000000..5f05950 --- /dev/null +++ b/latecomers/config.py @@ -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" diff --git a/pyproject.toml b/pyproject.toml index 9fa5bb8..6d2e9e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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']