diff --git a/.gitignore b/.gitignore index b7c6bfa..018b26e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,9 @@ __pycache__/ *.py[cod] *$py.class +# Ignore dynaconf secret files +**/.secrets.* + # C extensions *.so diff --git a/mobilizon_bots/__init__.py b/mobilizon_bots/__init__.py index df552ad..e69de29 100644 --- a/mobilizon_bots/__init__.py +++ b/mobilizon_bots/__init__.py @@ -1,4 +0,0 @@ -__version__ = '0.1.0' - - -from . import publishers diff --git a/mobilizon_bots/cli.py b/mobilizon_bots/cli.py new file mode 100644 index 0000000..65dd7b4 --- /dev/null +++ b/mobilizon_bots/cli.py @@ -0,0 +1,25 @@ +import logging.config +import sys +from argparse import ArgumentParser + +from config import settings + +logger = logging.getLogger(__name__) + + +def command_line(): + parser = ArgumentParser() + + return parser.parse_args() + + +def run(): + logging.config.dictConfig(settings.logging) + logger.debug("Test message debug") + logger.info("Test message info") + logger.error("Test message error") + sys.exit(0) + + +if __name__ == "__main__": + run() diff --git a/mobilizon_bots/config.py b/mobilizon_bots/config.py new file mode 100644 index 0000000..44113ed --- /dev/null +++ b/mobilizon_bots/config.py @@ -0,0 +1,35 @@ +from dynaconf import Dynaconf, Validator + +settings = Dynaconf( + environments=True, + envvar_prefix="MOBILIZON_BOTS", + settings_files=[ + "mobilizon_bots/settings.toml", + "mobilizon_bots/.secrets.toml", + "/etc/mobilizon_bots.toml", + "/etc/mobilizon_bots_secrets.toml", + ], + validators=[ + # Ensure some parameters exists (are required) + Validator("local_state_dir", "log_dir", "db_path", must_exist=True), + # Ensure some parameter mets a condition + # conditions: (eq, ne, lt, gt, lte, gte, identity, is_type_of, is_in, is_not_in) + Validator("local_state_dir", "log_dir", "db_path", is_type_of=str), + # check file or directory + # validate a value is eq in specific env + # Validator('PROJECT', eq='hello_world', env='production'), + # + # # Ensure some parameter (string) meets a condition + # # conditions: (len_eq, len_ne, len_min, len_max, cont) + # # Determines the minimum and maximum length for the value + # Validator("NAME", len_min=3, len_max=125), + # + # # Signifies the presence of the value in a set, text or word + # Validator("DEV_SERVERS", cont='localhost'), + # + # # Checks whether the length is the same as defined. + # Validator("PORT", len_eq=4), + ], +) + +settings.validators.validate() diff --git a/mobilizon_bots/settings.toml b/mobilizon_bots/settings.toml new file mode 100644 index 0000000..5f7ead3 --- /dev/null +++ b/mobilizon_bots/settings.toml @@ -0,0 +1,39 @@ +[default] +debug = true +default = true +local_state_dir = "/var/mobots" +log_dir = "/var/log/mobots" +db_name = "events.db" +db_path = "@format {this.local_state_dir}/{this.db_name}" + +[default.logging] +version = 1 +disable_existing_loggers = false + +[default.logging.formatters.standard] +format = '[%(asctime)s] [%(levelno)s] [%(levelname)s] %(message)s' + +[default.logging.handlers.console] +level = "INFO" +class = "logging.StreamHandler" +formatter = "standard" +stream = "ext://sys.stderr" + +[default.logging.handlers.logfile] +level = "DEBUG" +class = "logging.handlers.RotatingFileHandler" +formatter = "standard" +filename = "@format {this.log_dir}/mobilizon_bots.log" +maxBytes = 52428800 +backupCount = 500 +encoding = "utf8" + +[default.logging.root] +level = "DEBUG" +handlers = ['console', 'logfile'] + +[testing] +DEBUG = true +LOCAL_STATE_DIR = "/tmp/mobots" +LOG_DIR = "/tmp/mobots" +DB_PATH = ":memory:" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index c243b95..123aa18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ aiosqlite = "^0.16" Jinja2 = "^2.11.3" [tool.poetry.dev-dependencies] -pytest = "^5.2" +pytest = "^5.3" [build-system] requires = ["poetry-core>=1.0.0"]