Source code for pinger_bot.config

"""File for the Config dataclass."""
import dataclasses
import gettext as gettext_orig
import os
import pathlib
import typing as t

import omegaconf
from omegaconf import dictconfig

[docs]BASE_DIR = pathlib.Path(__file__).parent.parent
"""Base directory of the project.""" @dataclasses.dataclass
[docs]class Config: """Main dataclass for config. .. note:: If you will add more variables with value ``???``, do not forget them mock in tests! """
[docs] discord_token: str = "???"
"""Your Discord bot token."""
[docs] locale: str = "ru"
"""Bot's language, on which it speak. At now we only support ``en`` (English), ``uk`` (Ukrainian) and ``ru`` (Russian). """
[docs] debug: bool = False
"""Debug mode. Produce a lot of spam."""
[docs] verbose: bool = debug
"""Not so much info, that in debug."""
[docs] db_uri: str = "sqlite+aiosqlite:///pinger_bot.db"
"""DB_URI to connect."""
[docs] ping_interval: int = 5
"""Interval between pings, for collecting statistic of the server. In minutes."""
[docs] honeybadger_token: t.Optional[str] = None
"""Token for Honeybadger.io. If you don't have it, just leave it as ``None``.""" @classmethod
[docs] def setup(cls) -> "Config": """Set up the config. It is just load config from file, also it is rewrite config with merged data. Returns: :py:class:`.Config` instance. """ config_path = BASE_DIR / "config.yml" cfg = omegaconf.OmegaConf.structured(Config) if config_path.exists(): loaded_config = omegaconf.OmegaConf.load(config_path) cfg = omegaconf.OmegaConf.merge(cfg, loaded_config) with open(config_path, "w") as config_file: omegaconf.OmegaConf.save(cfg, config_file) cls._handle_env_variables(cfg) return cfg # type: ignore[no-any-return] # actually return :py:class:`.Config`
@staticmethod
[docs] def _handle_env_variables(cfg: dictconfig.DictConfig) -> None: """Process all values, and redef them with values from env variables. Args: cfg: :py:class:`.Config` instance. """ for key in cfg: if str(key).upper() in os.environ: cfg[str(key)] = os.environ[str(key).upper()]
[docs]config = Config.setup()
"""Initialized :py:class:`Config`."""
[docs]translation_obj = gettext_orig.translation( "messages", str(pathlib.Path(__file__).parent.parent / "locales"), languages=[config.locale] )
"""This is setuped :py:obj:`gettext.translation` object.""" translation_obj.install()
[docs]gettext = translation_obj.gettext
"""Function for getting translated messages. Examples: .. code-block:: python from pinger_bot.config import gettext as _ print(_("Hello World!")) Will print ``Hello World!``, ``Привет Мир!`` or ``Привіт Світ!``, depending on :py:attr:`.Config.locale` option. """