From c937dae3b4613793e1eafeb5bac523303fece8c9 Mon Sep 17 00:00:00 2001 From: JoYo <> Date: Tue, 7 Dec 2021 17:00:56 -0500 Subject: [PATCH] initial config --- Dockerfile | 1 + banana/__main__.py | 3 +++ banana/config.py | 27 +++++++++++++++++++++++++++ banana/scripts.py | 39 +++++++++++++++++++++++++++++++++++++++ pyproject.toml | 5 +++-- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 banana/__main__.py create mode 100644 banana/config.py diff --git a/Dockerfile b/Dockerfile index b2138b6..f0faa7b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ ENV DEBIAN_FRONTEND noninteractive RUN apt-get update && apt-get install -y \ python3-pip \ + python3-yaml \ && apt-get clean COPY ./banana/ /app/banana/ diff --git a/banana/__main__.py b/banana/__main__.py new file mode 100644 index 0000000..bd099e3 --- /dev/null +++ b/banana/__main__.py @@ -0,0 +1,3 @@ +from . import scripts + +scripts.periodical_script() diff --git a/banana/config.py b/banana/config.py new file mode 100644 index 0000000..f906fa7 --- /dev/null +++ b/banana/config.py @@ -0,0 +1,27 @@ +import yaml +from pathlib import Path + + +def new(path: Path): + config = { + "addons": [None], + "eso": {"path": None}, + } + + with path.open("w") as file_open: + config = yaml.dump(config, file_open, default_flow_style=False) + + +def load(path: Path) -> dict: + with path.open("r") as file_open: + config = yaml.load(file_open) + + return config + + +def valid(config: dict) -> bool: + assert config.get("addons") + assert config.get("eso") + + if not "path" in config.get("eso"): + raise AssertionError() diff --git a/banana/scripts.py b/banana/scripts.py index e69de29..bcab438 100644 --- a/banana/scripts.py +++ b/banana/scripts.py @@ -0,0 +1,39 @@ +from argparse import ArgumentParser +import logging +import requests +from pathlib import Path + +from . import config + + +def periodical_script(): + parser = ArgumentParser(description="Secret sharing script.") + parser.add_argument("-v", "--verbose", action="count", help="verbose logging") + parser.add_argument( + "-c", "--config", default="banana.yaml", help="configuration file path" + ) + args = parser.parse_args() + + if args.verbose: + logging.basicConfig( + level=logging.DEBUG, + format="%(asctime)s %(filename)s:%(lineno)d %(message)s", + ) + else: + logging.basicConfig( + level=logging.INFO, + format="%(message)s", + ) + + logging.info(args) + + config_path = Path(args.config) + config_path.touch(exist_ok=True) + config_current = config.load(config_path) + + try: + config.valid(config_current) + except (AssertionError, AttributeError) as error: + logging.debug(error) + config.new(config_path) + config_current = config.load(config_path) diff --git a/pyproject.toml b/pyproject.toml index 6a2d177..d41070d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,14 +6,15 @@ build-backend = "poetry.masonry.api" authors = ["py "] description = "" license = "MIT" -name = "banana" +name = "eso-banana" version = "0.0.1" [[tool.poetry.packages]] include = "banana" [tool.poetry.scripts] -banana-script = "banana:scripts.periodical_script" +eso-banana-script = "banana:scripts.periodical_script" [tool.poetry.dependencies] requests = "" +PyYAML = ""