added conf for strategies and publisher

This commit is contained in:
Simone Robutti 2021-05-09 17:17:48 +02:00
parent d3f647c2fe
commit 765b78dfb9
7 changed files with 99 additions and 36 deletions

View File

@ -2,7 +2,7 @@ import logging.config
import sys import sys
from argparse import ArgumentParser from argparse import ArgumentParser
from config import settings from mobilizon_bots.config.config import settings
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -1,35 +0,0 @@
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()

View File

View File

@ -0,0 +1,35 @@
from dynaconf import Dynaconf, Validator
from mobilizon_bots.config import strategies, publishers
from mobilizon_bots.config.publishers import publisher_names
SETTINGS_FILE = [
"mobilizon_bots/settings.toml",
"mobilizon_bots/.secrets.toml",
"/etc/mobilizon_bots.toml",
"/etc/mobilizon_bots_secrets.toml",
]
ENVVAR_PREFIX = "MOBILIZON_BOTS"
base_validators = [Validator("selection.strategy", must_exist=True)] + [
Validator(f"publisher.{publisher_name}.active", must_exist=True, is_type_of=bool)
for publisher_name in publisher_names
]
raw_settings = Dynaconf(
environments=True,
envvar_prefix=ENVVAR_PREFIX,
settings_files=SETTINGS_FILE,
validators=base_validators,
)
strategy_validators = strategies.get_validators(raw_settings)
publisher_validators = publishers.get_validators(raw_settings)
settings = Dynaconf(
environments=True,
envvar_prefix=ENVVAR_PREFIX,
settings_files=SETTINGS_FILE,
validators=base_validators + strategy_validators + publisher_validators,
)
# TODO use validation control in dynaconf 3.2.0 once released
settings.validators.validate()

View File

@ -0,0 +1,31 @@
from dynaconf import Validator
telegram_validators = [Validator("publisher.telegram.chat_id", must_exist=True)]
zulip_validators = []
mastodon_validators = []
twitter_validators = []
facebook_validators = []
publisher_name_to_validators = {
"telegram": telegram_validators,
"facebook": facebook_validators,
"twitter": twitter_validators,
"mastodon": mastodon_validators,
"zulip": zulip_validators,
}
publisher_names = publisher_name_to_validators.keys()
def get_active_publishers(settings):
return filter(
lambda publisher_name: settings["publisher"][publisher_name]["active"],
publisher_names,
)
def get_validators(settings):
active_publishers = get_active_publishers(settings)
validators = []
for publisher in active_publishers:
validators += publisher_name_to_validators[publisher]
return validators

View File

@ -0,0 +1,14 @@
from dynaconf import Validator
next_event_validators = [
Validator(
"selection.strategy_options.break_between_events_in_minutes", must_exist=True
)
]
strategy_name_to_validators = {"next_event": next_event_validators}
def get_validators(settings):
return strategy_name_to_validators[settings["selection"]["strategy"]]

View File

@ -6,6 +6,24 @@ log_dir = "/var/log/mobots"
db_name = "events.db" db_name = "events.db"
db_path = "@format {this.local_state_dir}/{this.db_name}" db_path = "@format {this.local_state_dir}/{this.db_name}"
[default.selection]
strategy = "next_event"
[default.selection.strategy_options]
break_between_events_in_minutes =5
[default.publisher.telegram]
active=true
chat_id="pippo"
[default.publisher.facebook]
active=false
[default.publisher.zulip]
active=false
[default.publisher.twitter]
active=false
[default.publisher.mastodon]
active=false
[default.logging] [default.logging]
version = 1 version = 1
disable_existing_loggers = false disable_existing_loggers = false