diff options
| author | Igor Tolmachov <me@igorek.dev> | 2022-12-03 17:35:55 +0900 |
|---|---|---|
| committer | Igor Tolmachov <me@igorek.dev> | 2022-12-03 17:35:55 +0900 |
| commit | d717c4e08245196b0d528716f9d269c71749365a (patch) | |
| tree | 72effa0c24f35f428558137f855f52b42e69f988 /handlers/config.py | |
| parent | 9432f267ee0b98272fb830d2bff56f23fe9766f4 (diff) | |
| download | karpov_ai_bot-d717c4e08245196b0d528716f9d269c71749365a.tar.gz karpov_ai_bot-d717c4e08245196b0d528716f9d269c71749365a.zip | |
2.0
Diffstat (limited to 'handlers/config.py')
| -rw-r--r-- | handlers/config.py | 111 |
1 files changed, 65 insertions, 46 deletions
diff --git a/handlers/config.py b/handlers/config.py index 7eaf9eb..21d556b 100644 --- a/handlers/config.py +++ b/handlers/config.py | |||
| @@ -1,7 +1,4 @@ | |||
| 1 | from ast import literal_eval | 1 | from json import JSONDecodeError, dumps, loads |
| 2 | from copy import deepcopy | ||
| 3 | from logging import info | ||
| 4 | from typing import Any, get_args | ||
| 5 | 2 | ||
| 6 | from aiogram import types as t | 3 | from aiogram import types as t |
| 7 | from pydantic import BaseModel, ValidationError | 4 | from pydantic import BaseModel, ValidationError |
| @@ -29,49 +26,71 @@ async def void_command(msg: t.Message) -> None: | |||
| 29 | async def settings_command(msg: t.Message) -> None: | 26 | async def settings_command(msg: t.Message) -> None: |
| 30 | def get_fields(config: BaseModel, level: int = 1) -> str: | 27 | def get_fields(config: BaseModel, level: int = 1) -> str: |
| 31 | text = "" | 28 | text = "" |
| 32 | for name in config.__fields__: | 29 | for field_name in config.__fields__: |
| 33 | value = getattr(config, name) | 30 | field = getattr(config, field_name) |
| 34 | if isinstance(value, BaseModel): | 31 | field_info = config.__fields__[field_name].field_info |
| 35 | text += f"\n{' '*level*4}<code>{name}.</code>" | 32 | |
| 36 | text += get_fields(value, level + 1) | 33 | text += "\n" + " " * level |
| 34 | if isinstance(field, BaseModel): | ||
| 35 | text += f"<code>{field_name}.</code> {field_info.description}" | ||
| 36 | text += get_fields(field, level + 1) | ||
| 37 | else: | 37 | else: |
| 38 | text += f"\n{' '*level*4}<code>{name}</code> = {value!r}" | 38 | text += f"<code>{field_name}</code> = {dumps(field)}" |
| 39 | return text | 39 | return text |
| 40 | 40 | ||
| 41 | args = msg.get_args().split() | 41 | def get_field(config: BaseModel, path: list[str]) -> str: |
| 42 | chat_config = deepcopy(config.get_config(msg.chat.id)) | 42 | text = "" |
| 43 | try: | 43 | for field_name in path: |
| 44 | if len(args) == 0: | 44 | assert ( |
| 45 | text = f"<code>/config</code>{get_fields(chat_config)}\n\n" | 45 | isinstance(config, BaseModel) and f in config.__fields__ |
| 46 | await msg.reply(text, parse_mode=t.ParseMode.HTML) | 46 | ), "Параметр не найден" |
| 47 | field_info = config.__fields__[field_name].field_info | ||
| 48 | config = getattr(config, field_name) | ||
| 49 | |||
| 50 | text += field_info.description | ||
| 51 | text += "\n\n" | ||
| 52 | if isinstance(config, BaseModel): | ||
| 53 | text += f"<code>/config {'.'.join(path)}.</code>" | ||
| 54 | text += get_fields(config, 1) | ||
| 47 | else: | 55 | else: |
| 48 | conf = chat_config | 56 | text += f"<code>/config {'.'.join(path)}</code> {dumps(config)}" |
| 49 | *path, field = args[0].split(".") | 57 | return text |
| 50 | for f in path: | 58 | |
| 51 | conf = getattr(conf, f) | 59 | def set_filed(config: BaseModel, path: list[str], value: str) -> str: |
| 52 | if not isinstance(conf, BaseModel): | 60 | text = "" |
| 53 | raise KeyError() | 61 | field_name = path[-1] |
| 54 | 62 | ||
| 55 | if len(args) == 2: | 63 | for f in path[:-1]: |
| 56 | if isinstance(getattr(conf, field), BaseModel): | 64 | assert ( |
| 57 | raise KeyError() | 65 | isinstance(config, BaseModel) and f in config.__fields__ |
| 58 | value = args[1] | 66 | ), "Параметр не найден" |
| 59 | setattr(conf, field, literal_eval(value)) | 67 | config = getattr(config, f) |
| 60 | 68 | ||
| 61 | config.set_config(msg.chat.id, Config.parse_obj(chat_config.dict())) | 69 | assert not isinstance( |
| 62 | config.save("data/config.json") | 70 | getattr(config, field_name), BaseModel |
| 63 | 71 | ), "Нельзя установить значение для группы параметров" | |
| 64 | await msg.reply( | 72 | |
| 65 | f"<code>/config {args[0]}</code> = {getattr(conf, field)!r}", | 73 | setattr(config, field_name, loads(value)) |
| 66 | parse_mode=t.ParseMode.HTML, | 74 | |
| 67 | ) | 75 | text += ( |
| 68 | else: | 76 | f"Значение <code>{'.'.join(path)}</code> установлено на <code>{value}</code>" |
| 69 | field_info = conf.__fields__[field].field_info | 77 | ) |
| 70 | await msg.reply( | 78 | |
| 71 | f"<code>/config {args[0]}</code> = {getattr(conf, field)!r}\n{field_info.description}", | 79 | return text |
| 72 | parse_mode=t.ParseMode.HTML, | 80 | |
| 73 | ) | 81 | chat_config = config.get_config(msg.chat.id) |
| 74 | except (ValidationError, ValueError, SyntaxError): | 82 | args = msg.get_args().split() |
| 75 | await msg.reply("Неверное значение") | 83 | if len(args) == 0: |
| 76 | except (KeyError, AttributeError): | 84 | text = f"<code>/config</code>{get_fields(chat_config)}" |
| 77 | await msg.reply("Параметр не найден") | 85 | elif len(args) == 1: |
| 86 | text = get_field(chat_config, args[0].split(".")) | ||
| 87 | elif len(args) == 2: | ||
| 88 | try: | ||
| 89 | text = set_filed(chat_config, args[0].split("."), args[1]) | ||
| 90 | config.set_config(msg.chat.id, Config.parse_obj(chat_config.dict())) | ||
| 91 | config.save("data/config.json") | ||
| 92 | except (JSONDecodeError, ValidationError): | ||
| 93 | text = "Неверное значение" | ||
| 94 | else: | ||
| 95 | text = "Слишком много аргументов" | ||
| 96 | await msg.answer(text, parse_mode=t.ParseMode.HTML) | ||
