From 554ec46413e7eda9538814c40ca764306fc8aec7 Mon Sep 17 00:00:00 2001 From: Romain J Date: Mon, 19 Oct 2020 15:04:10 +0200 Subject: [PATCH] feat(lang): done lang switcher --- .idea/dictionaries/romain.xml | 13 +++++++ .pylintrc | 2 + README.rst | 4 +- tuxbot/cogs/admin/admin.py | 6 ++- tuxbot/core/config.py | 69 ++++++++++++++++++----------------- 5 files changed, 58 insertions(+), 36 deletions(-) diff --git a/.idea/dictionaries/romain.xml b/.idea/dictionaries/romain.xml index a01d992..d6f5b03 100644 --- a/.idea/dictionaries/romain.xml +++ b/.idea/dictionaries/romain.xml @@ -3,30 +3,43 @@ anglais appdirs + apres asctime commandstats + ctype + debian + découverte + fonction francais français gnous ipinfo iplocalise jishaku + langue levelname + liste localiseip + octobre + pacman postgresql pred pylint releaselevel rprint socketstats + soit splt + suivante systemd tldr tutux tuxbot tuxbot's tuxvenv + venv webhooks + écrite \ No newline at end of file diff --git a/.pylintrc b/.pylintrc index 9d24328..bb75e4a 100644 --- a/.pylintrc +++ b/.pylintrc @@ -2,6 +2,8 @@ good-names= e, # (exception) as e f, # (file) as f + k, # for k, v in + v, # for k, v in [MASTER] disable= diff --git a/README.rst b/README.rst index 814b7dd..347f000 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,7 @@ Arch Linux $ sudo pacman -Syu python python-pip python-virtualenv git -Continue to `create the venv <#creating-the-virtual-environnement>`__. +Continue to `create the venv <#creating-the-virtual-environment>`__. -------------- @@ -40,7 +40,7 @@ Debian $ sudo apt update $ sudo apt -y install python3 python3-dev python3-pip python3-venv git -Continue to `create the venv <#creating-the-virtual-environnement>`__. +Continue to `create the venv <#creating-the-virtual-environment>`__. -------------- diff --git a/tuxbot/cogs/admin/admin.py b/tuxbot/cogs/admin/admin.py index 3ab95db..abfcaab 100644 --- a/tuxbot/cogs/admin/admin.py +++ b/tuxbot/cogs/admin/admin.py @@ -5,6 +5,8 @@ from discord.ext import commands from tuxbot.core import checks from tuxbot.core.bot import Tux +from tuxbot.core.config import set_for +from tuxbot.core.config import Config from tuxbot.core.i18n import ( Translator, find_locale, @@ -26,7 +28,9 @@ class Admin(commands.Cog, name="Admin"): self.bot = bot async def _save_lang(self, ctx: ContextPlus, lang: str): - self.bot.config.Servers.all[ctx.guild.id].locale = lang + set_for( + self.bot.config.Servers, ctx.guild.id, Config.Server, locale=lang + ) @group_extra(name="lang", aliases=["locale", "langue"], deletable=True) @commands.guild_only() diff --git a/tuxbot/core/config.py b/tuxbot/core/config.py index 5b14d5a..584f527 100644 --- a/tuxbot/core/config.py +++ b/tuxbot/core/config.py @@ -1,5 +1,5 @@ import logging -from typing import List, Dict +from typing import List, Dict, Any from structured_config import ( Structure, IntField, @@ -9,39 +9,35 @@ from structured_config import ( ) -__all__ = ["Config", "ConfigFile", "search_for"] +__all__ = ["Config", "ConfigFile", "search_for", "set_for"] log = logging.getLogger("tuxbot.core.config") -class Server(Structure): - prefixes: List[str] = [] - disabled_command: List[str] = [] - locale: str = StrField("") - blacklisted: bool = BoolField(False) - - -class Channel(Structure): - disabled_command: List[str] = [] - locale: str = StrField("") - blacklisted: bool = BoolField(False) - - -class User(Structure): - aliases: List[dict] = [] - locale: str = StrField("") - blacklisted: bool = BoolField(False) - - class Config(Structure): - class Servers(Structure): - all: Dict[int, Server] = {} + class Server(Structure): + prefixes: List[str] = [] + disabled_command: List[str] = [] + locale: str = StrField("") + blacklisted: bool = BoolField(False) - class Channels(Structure): - all: Dict[int, Channel] = {} + class Channel(Structure): + disabled_command: List[str] = [] + locale: str = StrField("") + blacklisted: bool = BoolField(False) - class Users(Structure): - all: Dict[int, User] = {} + class User(Structure): + aliases: List[dict] = [] + locale: str = StrField("") + blacklisted: bool = BoolField(False) + + class Cog(Structure): + pass + + Servers: Dict[int, Server] = {} + Channels: Dict[int, Channel] = {} + Users: Dict[int, User] = {} + Cogs: Dict[str, Cog] = {} class Core(Structure): owners_id: List[int] = [] @@ -51,9 +47,6 @@ class Config(Structure): locale: str = StrField("") disabled_command: List[str] = [] - class Cogs(Structure): - pass - # ============================================================================= # Configuration of Tuxbot Application (not the bot) @@ -74,7 +67,17 @@ class AppConfig(Structure): # ============================================================================= -def search_for(config, key, value, default=False): - if key in config.all: - return getattr(config.all[key], value) +def search_for(config, key, value, default=False) -> Any: + if key in config: + return getattr(config[key], value) return default + + +# la fonction suivante a été écrite le lundi 19 octobre 2020 à 13h49 soit 1h +# apres la découverte de mon chat, rip roxy, 201?-2020 :'( +def set_for(config, key, ctype, **values) -> Any: + if key not in config: + config[key] = ctype() + + for k, v in values.items(): + setattr(config[key], k, v)