2021-07-12 22:17:49 +02:00
|
|
|
import os
|
2021-05-30 21:47:36 +02:00
|
|
|
from typing import List
|
|
|
|
|
2021-05-09 17:17:48 +02:00
|
|
|
from dynaconf import Dynaconf, Validator
|
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.config import strategies, publishers, notifiers
|
|
|
|
from mobilizon_reshare.config.notifiers import notifier_names
|
|
|
|
|
|
|
|
from mobilizon_reshare.config.publishers import publisher_names
|
2021-05-09 17:17:48 +02:00
|
|
|
|
2021-05-30 21:47:36 +02:00
|
|
|
|
|
|
|
def build_settings(
|
|
|
|
settings_files: List[str] = None, validators: List[Validator] = None
|
|
|
|
):
|
2021-07-12 22:17:49 +02:00
|
|
|
SETTINGS_FILE = (
|
|
|
|
settings_files
|
2021-08-16 10:49:52 +02:00
|
|
|
or os.environ.get("MOBILIZION_RESHARE_SETTINGS_FILE")
|
2021-07-12 22:17:49 +02:00
|
|
|
or [
|
2021-08-16 10:49:52 +02:00
|
|
|
"mobilizon_reshare/settings.toml",
|
|
|
|
"mobilizon_reshare/.secrets.toml",
|
|
|
|
"/etc/mobilizon_reshare.toml",
|
|
|
|
"/etc/mobilizon_reshare_secrets.toml",
|
2021-07-12 22:17:49 +02:00
|
|
|
]
|
|
|
|
)
|
2021-08-16 10:49:52 +02:00
|
|
|
ENVVAR_PREFIX = "MOBILIZON_RESHARE"
|
2021-05-30 21:47:36 +02:00
|
|
|
|
|
|
|
return Dynaconf(
|
|
|
|
environments=True,
|
|
|
|
envvar_prefix=ENVVAR_PREFIX,
|
|
|
|
settings_files=SETTINGS_FILE,
|
|
|
|
validators=validators or [],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def build_and_validate_settings(settings_files: List[str] = None):
|
|
|
|
"""
|
|
|
|
Creates a settings object to be used in the application. It collects and apply generic validators and validators
|
|
|
|
specific for each publisher, notifier and publication strategy.
|
|
|
|
"""
|
|
|
|
|
2021-07-18 18:23:30 +02:00
|
|
|
preliminary_validators = (
|
|
|
|
[Validator("selection.strategy", must_exist=True, is_type_of=str)]
|
2021-05-30 21:47:36 +02:00
|
|
|
+ [
|
|
|
|
Validator(
|
|
|
|
f"publisher.{publisher_name}.active", must_exist=True, is_type_of=bool
|
|
|
|
)
|
|
|
|
for publisher_name in publisher_names
|
|
|
|
]
|
|
|
|
+ [
|
|
|
|
Validator(
|
|
|
|
f"notifier.{notifier_name}.active", must_exist=True, is_type_of=bool
|
|
|
|
)
|
|
|
|
for notifier_name in notifier_names
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-07-18 18:23:30 +02:00
|
|
|
# we first do a preliminary load of the settings without validation. We will later use them to determine which
|
|
|
|
# publishers, notifiers and strategy have been selected
|
|
|
|
raw_settings = build_settings(
|
|
|
|
settings_files=settings_files, validators=preliminary_validators
|
|
|
|
)
|
|
|
|
|
|
|
|
# These validators are always applied
|
|
|
|
base_validators = [
|
|
|
|
# strategy to decide events to publish
|
|
|
|
Validator(
|
2021-08-28 13:17:39 +02:00
|
|
|
"publishing.window.begin",
|
|
|
|
must_exist=True,
|
|
|
|
is_type_of=int,
|
|
|
|
gte=0,
|
|
|
|
lte=24,
|
2021-07-18 18:23:30 +02:00
|
|
|
),
|
|
|
|
Validator(
|
|
|
|
"publishing.window.end", must_exist=True, is_type_of=int, gte=0, lte=24
|
|
|
|
),
|
|
|
|
# url of the main Mobilizon instance to download events from
|
|
|
|
Validator("source.mobilizon.url", must_exist=True, is_type_of=str),
|
|
|
|
Validator("source.mobilizon.group", must_exist=True, is_type_of=str),
|
|
|
|
] + preliminary_validators
|
|
|
|
|
2021-05-30 21:47:36 +02:00
|
|
|
# we retrieve validators that are conditional. Each module will analyze the settings and decide which validators
|
|
|
|
# need to be applied.
|
|
|
|
strategy_validators = strategies.get_validators(raw_settings)
|
|
|
|
publisher_validators = publishers.get_validators(raw_settings)
|
|
|
|
notifier_validators = notifiers.get_validators(raw_settings)
|
|
|
|
|
|
|
|
# we rebuild the settings, providing all the selected validators.
|
|
|
|
settings = build_settings(
|
|
|
|
settings_files,
|
|
|
|
base_validators
|
|
|
|
+ strategy_validators
|
|
|
|
+ publisher_validators
|
|
|
|
+ notifier_validators,
|
|
|
|
)
|
|
|
|
# TODO use validation control in dynaconf 3.2.0 once released
|
|
|
|
settings.validators.validate()
|
|
|
|
return settings
|
|
|
|
|
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
# this singleton and functions are necessary to put together
|
|
|
|
# the necessities of the testing suite, the CLI and still having a single entrypoint to the config.
|
|
|
|
# The CLI needs to provide the settings file at run time so we cannot work at import time.
|
|
|
|
# The normal Dynaconf options to specify the settings files are also not a valid option because of the two steps
|
|
|
|
# validation that prevents us to employ their mechanism to specify settings files. This could probably be reworked
|
|
|
|
# better in the future.
|
|
|
|
class CustomConfig:
|
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def __new__(cls, settings_files: List[str] = None):
|
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = super(CustomConfig, cls).__new__(cls)
|
2021-07-18 18:23:30 +02:00
|
|
|
cls.settings = build_and_validate_settings(settings_files)
|
2021-07-12 22:17:49 +02:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
def update(self, settings_files: List[str] = None):
|
2021-07-18 18:23:30 +02:00
|
|
|
self.settings = build_and_validate_settings(settings_files)
|
2021-07-12 22:17:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_settings():
|
|
|
|
config = CustomConfig()
|
|
|
|
return config.settings
|
|
|
|
|
|
|
|
|
|
|
|
def update_settings_files(settings_files: List[str] = None):
|
|
|
|
CustomConfig().update(settings_files)
|
|
|
|
return CustomConfig().settings
|