tuxbot-bot/tuxbot/core/config.py

111 lines
3 KiB
Python
Raw Permalink Normal View History

2020-06-04 19:16:51 +02:00
import logging
from typing import List, Dict, Any, NoReturn
2020-09-02 00:08:06 +02:00
from structured_config import (
Structure,
IntField,
StrField,
BoolField,
2020-09-02 00:08:06 +02:00
ConfigFile,
)
2020-10-19 22:17:19 +02:00
__all__ = [
"Config",
"ConfigFile",
"AppConfig",
"search_for",
"set_for_key",
"set_for",
]
2020-06-06 02:00:16 +02:00
log = logging.getLogger("tuxbot.core.config")
2020-10-19 15:04:10 +02:00
class Config(Structure):
class Server(Structure):
prefixes: List[str] = []
disabled_command: List[str] = []
locale: str = StrField("")
blacklisted: bool = BoolField(False)
2020-06-06 02:00:16 +02:00
2020-10-19 15:04:10 +02:00
class Channel(Structure):
disabled_command: List[str] = []
locale: str = StrField("")
blacklisted: bool = BoolField(False)
2020-09-02 00:08:06 +02:00
2020-10-19 15:04:10 +02:00
class User(Structure):
aliases: List[dict] = []
locale: str = StrField("")
blacklisted: bool = BoolField(False)
2020-10-19 15:04:10 +02:00
class Cog(Structure):
pass
2020-09-02 00:08:06 +02:00
2020-10-19 15:04:10 +02:00
Servers: Dict[int, Server] = {}
Channels: Dict[int, Channel] = {}
Users: Dict[int, User] = {}
Cogs: Dict[str, Cog] = {}
2020-09-02 00:08:06 +02:00
class Core(Structure):
owners_id: List[int] = []
prefixes: List[str] = []
token: str = StrField("")
mentionable: bool = BoolField("")
locale: str = StrField("")
disabled_command: List[str] = []
2020-09-02 00:08:06 +02:00
# =============================================================================
# Configuration of Tuxbot Application (not the bot)
# =============================================================================
class AppConfig(Structure):
class Instance(Structure):
path: str = StrField("")
active: bool = BoolField(False)
last_run: int = IntField(0)
2020-10-19 22:17:19 +02:00
Instances: Dict[str, Instance] = {}
2020-10-19 00:53:26 +02:00
# =============================================================================
# Useful functions to interact with configs
2020-10-19 00:53:26 +02:00
# =============================================================================
2020-10-19 15:04:10 +02:00
def search_for(config, key, value, default=False) -> Any:
if key in config:
return getattr(config[key], value)
2020-10-19 00:53:26 +02:00
return default
2020-10-19 15:04:10 +02:00
def set_for_key(config, key, ctype, **values) -> NoReturn:
# pylint: disable=anomalous-backslash-in-string
"""
La fonction suivante \`*-.
a été écrite le lundi ) _`-.
19 octobre 2020 a 13h40 . : `. .
soit 1h apres la découverte : _ ' \
du corps de mon chat ; *` _. `*-._
`-.-' `-.
; ` `.
:. . \
. \ . : .-' .
' `+.; ; ' :
: ' | ; ;-.
; ' : :`-: _.`* ;
rip roxy .*' / .*' ; .*`- +' `*'
201?-2020 :,( `*-* `*-* `*-*'
"""
2020-10-19 15:04:10 +02:00
if key not in config:
config[key] = ctype()
for k, v in values.items():
setattr(config[key], k, v)
def set_for(config, **values) -> NoReturn:
for k, v in values.items():
setattr(config, k, v)