Files
ltlnotifyer/config.py
2022-02-11 18:45:56 +03:00

57 lines
1.5 KiB
Python

from os import getenv
from dataclasses import dataclass
@dataclass
class Config:
"""
Notifier config dataclass.
* `base_url`: url of domain
* `port`: server port (default **3001**)
* `teletoken`: telegram api token.
* `bot_owner`: telegram id of bot owner.
"""
base_url: str
port: int
teletoken: str
bot_owner: int
class ConfigError(Exception):
pass
_teletoken = getenv('LTLNOTIFIER_TELETOKEN')
# Check api token
if not _teletoken:
raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN not set.')
if len(_teletoken) != 45 or _teletoken[9] != ':' or not _teletoken[:9].isdigit():
raise ConfigError('virtual environment LTLNOTIFIER_TELETOKEN incorrect.')
_bot_owner = getenv('LTLNOTIFIER_BOT_OWNER')
# Check bot owner
if not _bot_owner:
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER not set.')
try:
if len(_bot_owner) != 9:
raise ValueError
_bot_owner = int(_bot_owner)
except ValueError:
raise ConfigError('virtual environment LTLNOTIFIER_BOT_OWNER incorrect.')
_base_url = getenv('LTLNOTIFIER_BASE_URL')
# Check base url
if not _base_url:
raise ConfigError('virtual environment LTLNOTIFIER_BASE_URL not set')
_port = getenv('LTLNOTIFIER_PORT', default='')
_port = int(_port) if _port.isdigit() else 3001
config = Config(teletoken=_teletoken,
port=_port,
bot_owner=_bot_owner,
base_url=_base_url)
__all__ = ('Config', 'config')