fix(config): change set_for to set_for_key

This commit is contained in:
Romain J 2020-10-19 21:44:29 +02:00
parent 554ec46413
commit 421ecbf6cb
5 changed files with 162 additions and 31 deletions

View file

@ -5,7 +5,7 @@ from discord.ext import commands
from tuxbot.core import checks from tuxbot.core import checks
from tuxbot.core.bot import Tux from tuxbot.core.bot import Tux
from tuxbot.core.config import set_for from tuxbot.core.config import set_for_key
from tuxbot.core.config import Config from tuxbot.core.config import Config
from tuxbot.core.i18n import ( from tuxbot.core.i18n import (
Translator, Translator,
@ -28,7 +28,7 @@ class Admin(commands.Cog, name="Admin"):
self.bot = bot self.bot = bot
async def _save_lang(self, ctx: ContextPlus, lang: str): async def _save_lang(self, ctx: ContextPlus, lang: str):
set_for( set_for_key(
self.bot.config.Servers, ctx.guild.id, Config.Server, locale=lang self.bot.config.Servers, ctx.guild.id, Config.Server, locale=lang
) )

View file

@ -10,9 +10,27 @@ class AdminConfig(Structure):
extra = { extra = {
"dm": str, "dm": {
"mentions": str, "type": str,
"guilds": str, "description": "URL of the webhook used for send DMs "
"errors": str, "received and sent by the bot",
"gateway": str, },
"mentions": {
"type": str,
"description": "URL of the webhook used for send Mentions "
"received by the bot",
},
"guilds": {
"type": str,
"description": "URL of the webhook used for send guilds where the "
"bot is added or removed",
},
"errors": {
"type": str,
"description": "URL of the webhook used for send errors in the bot",
},
"gateway": {
"type": str,
"description": "URL of the webhook used for send gateway information",
},
} }

View file

@ -1,5 +1,5 @@
import logging import logging
from typing import List, Dict, Any from typing import List, Dict, Any, NoReturn
from structured_config import ( from structured_config import (
Structure, Structure,
IntField, IntField,
@ -9,7 +9,7 @@ from structured_config import (
) )
__all__ = ["Config", "ConfigFile", "search_for", "set_for"] __all__ = ["Config", "ConfigFile", "search_for", "set_for_key", "set_for_key"]
log = logging.getLogger("tuxbot.core.config") log = logging.getLogger("tuxbot.core.config")
@ -73,11 +73,31 @@ def search_for(config, key, value, default=False) -> Any:
return default return default
# la fonction suivante a été écrite le lundi 19 octobre 2020 à 13h49 soit 1h def set_for_key(config, key, ctype, **values) -> NoReturn:
# apres la découverte de mon chat, rip roxy, 201?-2020 :'( # pylint: disable=anomalous-backslash-in-string
def set_for(config, key, ctype, **values) -> Any: """
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 :,( `*-* `*-* `*-*'
"""
if key not in config: if key not in config:
config[key] = ctype() config[key] = ctype()
for k, v in values.items(): for k, v in values.items():
setattr(config[key], k, v) setattr(config[key], k, v)
def set_for(config, **values) -> NoReturn:
for k, v in values.items():
setattr(config, k, v)

View file

@ -38,3 +38,19 @@ def logs_data_path(instance_name: str) -> Path:
Generated path for logs files. Generated path for logs files.
""" """
return data_path(instance_name) / "logs" return data_path(instance_name) / "logs"
def cogs_data_path(instance_name: str, cog_name: str = "") -> Path:
"""Return Path for cogs.
Parameters
----------
instance_name:str
cog_name:str
Returns
-------
Path
Generated path for cogs configs.
"""
return data_path(instance_name) / "cogs" / cog_name

View file

@ -1,21 +1,24 @@
import json import argparse
import importlib
import logging import logging
import re import re
import sys import sys
from argparse import Namespace
from pathlib import Path from pathlib import Path
from typing import NoReturn, Union, List from typing import NoReturn, Union, List
from rich.prompt import Prompt, IntPrompt from rich.prompt import Prompt, IntPrompt
from rich.console import Console from rich.console import Console
from rich.rule import Rule from rich.rule import Rule
from rich.style import Style
from rich.traceback import install from rich.traceback import install
from tuxbot.core.config import set_for
from tuxbot.logging import formatter from tuxbot.logging import formatter
from tuxbot.core.data_manager import config_dir, app_dir from tuxbot.core.data_manager import config_dir, app_dir, cogs_data_path
from tuxbot.core import config from tuxbot.core import config
console = Console() console = Console()
console.clear()
install(console=console) install(console=console)
try: try:
@ -207,7 +210,12 @@ def get_multiple(
return values return values
def additional_config() -> dict: def get_extra(question: str, value_type: type) -> Union[str, int]:
prompt = IntPrompt if value_type is int else Prompt
return prompt.ask(question, console=console)
def additional_config(instance: str, cogs: str = "**"):
"""Asking for additional configs in cogs. """Asking for additional configs in cogs.
Returns Returns
@ -215,24 +223,45 @@ def additional_config() -> dict:
dict: dict:
Dict with cog name as key and configs as value. Dict with cog name as key and configs as value.
""" """
if cogs is None or "all" in sum(cogs, []):
cogs = []
else:
cogs = sum(cogs, [])
if len(cogs) == 0:
paths = Path("tuxbot/cogs").glob("**/config.py") paths = Path("tuxbot/cogs").glob("**/config.py")
data = {} else:
paths = [Path(f"tuxbot/cogs/{cog}/config.py") for cog in cogs]
for file in paths:
console.print("\n" * 4)
cog_name = str(file.parent).split("/")[-1]
data[cog_name] = {}
with file.open("r") as f:
data = json.load(f)
for path in paths:
cog_name = str(path.parent).split("/")[-1]
if path.exists():
console.print(Rule(f"\nConfiguration for `{cog_name}` module")) console.print(Rule(f"\nConfiguration for `{cog_name}` module"))
mod = importlib.import_module(str(path).replace("/", ".")[:-3])
mod_config_type = getattr(mod, cog_name.capitalize() + "Config")
mod_extra = getattr(mod, "extra")
for key, value in data.items(): mod_config = config.ConfigFile(
console.print() str(cogs_data_path(instance, cog_name) / "config.yaml"),
data[cog_name][key] = Prompt.ask(value["description"]) mod_config_type,
).config
return data extras = {}
for key, value in mod_extra.items():
extras[key] = get_extra(value["description"], value["type"])
console.log(mod_config)
console.log(dir(mod_config))
console.log(mod_config_type)
set_for(mod_config, **extras)
else:
console.print(
Rule(
f"\nFailed to fetch information for `{cog_name}` module",
style=Style(color="red"),
)
)
def finish_setup(data_dir: Path) -> NoReturn: def finish_setup(data_dir: Path) -> NoReturn:
@ -326,11 +355,48 @@ def basic_setup() -> NoReturn:
console.print() console.print()
console.print( console.print(
f"Instance successfully created! " f"Instance successfully created! "
f"You can now run `tuxbot {name}` to launch this instance" f"You can now run `tuxbot {name}` to launch this instance now or "
f"setup the additional configs by running "
f"`tuxbot-setup {name} --additional-config=all`"
) )
def parse_cli_flags(args: list) -> Namespace:
"""Parser for cli values.
Parameters
----------
args:list
Is a list of all passed values.
Returns
-------
Namespace
"""
parser = argparse.ArgumentParser(
description="Tuxbot Setup - OpenSource bot",
usage="tuxbot-setup [instance] [arguments]",
)
parser.add_argument(
"instance_name",
nargs="?",
help="Name of the bot instance to edit.",
)
parser.add_argument(
"-a",
"--additional-config",
action="append",
nargs="+",
help="Execute setup to additional configs",
)
args = parser.parse_args(args)
return args
def setup() -> NoReturn: def setup() -> NoReturn:
cli_flags = parse_cli_flags(sys.argv[1:])
try: try:
# Create a new instance. # Create a new instance.
level = logging.DEBUG level = logging.DEBUG
@ -340,6 +406,17 @@ def setup() -> NoReturn:
stdout_handler.setFormatter(formatter) stdout_handler.setFormatter(formatter)
base_logger.addHandler(stdout_handler) base_logger.addHandler(stdout_handler)
if cli_flags.additional_config and not cli_flags.instance_name:
console.print(
"[red]No instance to modify provided ! "
"You can use 'tuxbot -L' to list all available instances"
)
elif cli_flags.instance_name:
additional_config(
cli_flags.instance_name, cli_flags.additional_config
)
else:
console.clear()
basic_setup() basic_setup()
except KeyboardInterrupt: except KeyboardInterrupt:
console.print("Exiting...") console.print("Exiting...")