2020-06-04 19:16:51 +02:00
|
|
|
import logging
|
2020-10-22 00:00:48 +02:00
|
|
|
from typing import List, Dict, Any
|
2020-09-02 00:08:06 +02:00
|
|
|
from structured_config import (
|
2020-10-19 00:20:58 +02:00
|
|
|
Structure,
|
|
|
|
IntField,
|
|
|
|
StrField,
|
|
|
|
BoolField,
|
2020-09-02 00:08:06 +02:00
|
|
|
ConfigFile,
|
|
|
|
)
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2021-04-21 18:28:09 +02:00
|
|
|
__all__ = (
|
2020-10-19 22:17:19 +02:00
|
|
|
"Config",
|
|
|
|
"ConfigFile",
|
|
|
|
"search_for",
|
|
|
|
"set_for_key",
|
|
|
|
"set_for",
|
2021-04-20 15:43:20 +02:00
|
|
|
"set_if_none",
|
2021-04-21 18:28:09 +02:00
|
|
|
)
|
2020-06-06 02:00:16 +02:00
|
|
|
|
|
|
|
log = logging.getLogger("tuxbot.core.config")
|
2020-06-03 01:07:43 +02:00
|
|
|
|
|
|
|
|
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):
|
2021-01-19 14:30:25 +01:00
|
|
|
aliases: dict = {}
|
2020-10-19 15:04:10 +02:00
|
|
|
locale: str = StrField("")
|
|
|
|
blacklisted: bool = BoolField(False)
|
2020-10-19 00:20:58 +02:00
|
|
|
|
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):
|
2020-11-08 01:07:27 +01:00
|
|
|
class Database(Structure):
|
|
|
|
username: str = StrField("")
|
|
|
|
password: str = StrField("")
|
|
|
|
domain: str = StrField("")
|
|
|
|
port: str = IntField(5432)
|
|
|
|
db_name: str = StrField("")
|
|
|
|
|
2020-09-02 00:08:06 +02:00
|
|
|
owners_id: List[int] = []
|
|
|
|
prefixes: List[str] = []
|
|
|
|
token: str = StrField("")
|
2021-01-26 11:59:36 +01:00
|
|
|
ip: str = StrField("")
|
2021-03-01 14:11:18 +01:00
|
|
|
ip6: str = StrField("")
|
2020-09-02 00:08:06 +02:00
|
|
|
mentionable: bool = BoolField("")
|
|
|
|
locale: str = StrField("")
|
2020-10-19 00:20:58 +02:00
|
|
|
disabled_command: List[str] = []
|
2021-02-16 19:28:30 +01:00
|
|
|
instance_name: str = StrField("")
|
2020-10-19 00:53:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
# =============================================================================
|
2020-10-19 01:37:12 +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
|
|
|
|
|
|
|
|
2021-02-16 19:28:30 +01:00
|
|
|
def set_if_none(config, key, ctype) -> None:
|
|
|
|
if key not in config:
|
|
|
|
config[key] = ctype()
|
|
|
|
|
|
|
|
|
2020-10-22 00:00:48 +02:00
|
|
|
def set_for_key(config, key, ctype, **values) -> None:
|
2020-10-19 21:44:29 +02:00
|
|
|
# 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 :,( `*-* `*-* `*-*'
|
|
|
|
"""
|
2021-02-16 19:28:30 +01:00
|
|
|
set_if_none(config, key, ctype)
|
2020-10-19 15:04:10 +02:00
|
|
|
|
|
|
|
for k, v in values.items():
|
|
|
|
setattr(config[key], k, v)
|
2020-10-19 21:44:29 +02:00
|
|
|
|
|
|
|
|
2020-10-22 00:00:48 +02:00
|
|
|
def set_for(config, **values) -> None:
|
2020-10-19 21:44:29 +02:00
|
|
|
for k, v in values.items():
|
|
|
|
setattr(config, k, v)
|