From 0d02f2dd524824f9d7c59c0e539ed1b92f4284c6 Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Sun, 2 May 2021 16:44:03 +0200 Subject: [PATCH 1/4] Init configuration --- .gitignore | 3 +++ mobilizon_bots/__init__.py | 5 +---- mobilizon_bots/config.py | 34 ++++++++++++++++++++++++++++++++++ mobilizon_bots/settings.toml | 21 +++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 mobilizon_bots/config.py create mode 100644 mobilizon_bots/settings.toml 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..3dc1f76 100644 --- a/mobilizon_bots/__init__.py +++ b/mobilizon_bots/__init__.py @@ -1,4 +1 @@ -__version__ = '0.1.0' - - -from . import publishers +__version__ = "0.1.0" diff --git a/mobilizon_bots/config.py b/mobilizon_bots/config.py new file mode 100644 index 0000000..f07b2ee --- /dev/null +++ b/mobilizon_bots/config.py @@ -0,0 +1,34 @@ +from dynaconf import Dynaconf, Validator + +settings = Dynaconf( + envvar_prefix="MOBOTS", + settings_files=[ + "settings.toml", + ".secrets.toml", + "/etc/mobots.toml", + "/etc/mobots_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..de2535d --- /dev/null +++ b/mobilizon_bots/settings.toml @@ -0,0 +1,21 @@ +[default] +DEBUG = false +DEVELOPMENT = false +LOCAL_STATE_DIR = "/var/mobots" +LOG_DIR = "/var/log/mobots" +DB_NAME = "events.db" +DB_PATH ="@jinja /{{ this.LOCAL_STATE_DIR }}/{{ this.DB_NAME | abspath }}" + +[development] +DEVELOPMENT = true +DEBUG = true +LOCAL_STATE_DIR = "/tmp/mobots" +LOG_DIR = "/tmp/mobots" +DB_NAME = "development.db" +DB_PATH="@jinja /{{ this.LOCAL_STATE_DIR }}/{{ this.DB_NAME | abspath }}" + +[testing] +DEBUG = true +LOCAL_STATE_DIR = "/tmp/mobots" +LOG_DIR = "/tmp/mobots" +DB_PATH=":memory:" \ No newline at end of file From 6a51dc86ee9d1047204f319ddc7a63c13194dc68 Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Mon, 3 May 2021 19:48:04 +0200 Subject: [PATCH 2/4] mobilizon_bots: Configure logger with dynaconf. * mobilizon_bots/cli.py: New file. * mobilizon_bots/config.py: Minor tweaks. * mobilizon_bots/settings.toml: Downcase variables, drop jinja2 templating in favor of format, add logging configuration. --- mobilizon_bots/cli.py | 25 ++++++++++++++++++++ mobilizon_bots/config.py | 15 ++++++------ mobilizon_bots/settings.toml | 46 +++++++++++++++++++++++++----------- 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 mobilizon_bots/cli.py 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 index f07b2ee..44113ed 100644 --- a/mobilizon_bots/config.py +++ b/mobilizon_bots/config.py @@ -1,19 +1,20 @@ from dynaconf import Dynaconf, Validator settings = Dynaconf( - envvar_prefix="MOBOTS", + environments=True, + envvar_prefix="MOBILIZON_BOTS", settings_files=[ - "settings.toml", - ".secrets.toml", - "/etc/mobots.toml", - "/etc/mobots_secrets.toml", + "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), + 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), + 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'), diff --git a/mobilizon_bots/settings.toml b/mobilizon_bots/settings.toml index de2535d..5f7ead3 100644 --- a/mobilizon_bots/settings.toml +++ b/mobilizon_bots/settings.toml @@ -1,21 +1,39 @@ [default] -DEBUG = false -DEVELOPMENT = false -LOCAL_STATE_DIR = "/var/mobots" -LOG_DIR = "/var/log/mobots" -DB_NAME = "events.db" -DB_PATH ="@jinja /{{ this.LOCAL_STATE_DIR }}/{{ this.DB_NAME | abspath }}" +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}" -[development] -DEVELOPMENT = true -DEBUG = true -LOCAL_STATE_DIR = "/tmp/mobots" -LOG_DIR = "/tmp/mobots" -DB_NAME = "development.db" -DB_PATH="@jinja /{{ this.LOCAL_STATE_DIR }}/{{ this.DB_NAME | abspath }}" +[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 +DB_PATH = ":memory:" \ No newline at end of file From 218e228e6ab6fa65484f7bee421b86a06e176a3c Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Mon, 3 May 2021 19:51:19 +0200 Subject: [PATCH 3/4] pyproject.toml: Update pytest to 5.3. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index ea9f83c..43860e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ tortoise-orm = "^0.16.21" aiosqlite = "^0.17.0" [tool.poetry.dev-dependencies] -pytest = "^5.2" +pytest = "^5.3" [build-system] requires = ["poetry-core>=1.0.0"] From f5ba924e9294855b78acb3defcb15e44686ff8ed Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Sun, 9 May 2021 15:57:04 +0200 Subject: [PATCH 4/4] Versioning will be handled elsewhere. --- mobilizon_bots/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mobilizon_bots/__init__.py b/mobilizon_bots/__init__.py index 3dc1f76..e69de29 100644 --- a/mobilizon_bots/__init__.py +++ b/mobilizon_bots/__init__.py @@ -1 +0,0 @@ -__version__ = "0.1.0"