Merged with feat/add-standard-help

This commit is contained in:
Maxim Slipenko 2024-08-16 16:25:42 +03:00
parent 79298e5441
commit df1fed10c2
10 changed files with 125 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import asyncio
from importlib.metadata import version
from ocab_core import OCAB
from ocab_modules import module_loader
@ -14,7 +15,9 @@ async def main():
module_loader("standard", "filters", safe=False),
module_loader("standard", "report"),
module_loader("standard", "welcome", safe=False),
]
module_loader("standard", "help"),
],
metainfo={"app_version": version("altlinux")},
)
await ocab.start()

View File

@ -17,9 +17,11 @@ class OCAB:
def __init__(self) -> None:
pass
async def init_app(self, bot_modules):
async def init_app(self, bot_modules, metainfo=None):
setup_logger()
singleton = Singleton()
if isinstance(metainfo, dict):
singleton.storage["metainfo"] = metainfo
try:
singleton.modules_manager = ModulesManager()

View File

@ -3,6 +3,7 @@ from ocab_core.logger import log
from .public_api import (
Storage,
get_fsm_context,
get_metainfo,
get_module,
register_outer_message_middleware,
register_router,

View File

@ -26,6 +26,11 @@ def register_outer_message_middleware(middleware: BaseMiddleware):
app.storage["_outer_message_middlewares"].append(middleware)
def get_metainfo():
app = Singleton()
return app.storage["metainfo"].copy()
async def set_my_commands(commands):
app = Singleton()
await app.bot.set_my_commands(commands)

View File

@ -12,6 +12,7 @@ from RestrictedPython import (
from RestrictedPython.Eval import default_guarded_getitem, default_guarded_getiter
from RestrictedPython.Guards import ( # guarded_setattr,; full_write_guard,
_write_wrapper,
guarded_iter_unpack_sequence,
guarded_unpack_sequence,
safer_getattr,
)
@ -138,6 +139,7 @@ BUILTINS["_getitem_"] = default_guarded_getitem
BUILTINS["_getattr_"] = safes_getattr
BUILTINS["_getiter_"] = default_guarded_getiter
BUILTINS["_write_"] = write_guard()
BUILTINS["_iter_unpack_sequence_"] = guarded_iter_unpack_sequence
BUILTINS["_unpack_sequence_"] = guarded_unpack_sequence
BUILTINS["staticmethod"] = staticmethod
BUILTINS["tuple"] = tuple

View File

@ -1 +1 @@
from .main import module_late_init, register_command
from .main import get_user_commands, module_late_init, register_command

View File

@ -32,5 +32,11 @@ async def set_user_commands():
)
def get_user_commands():
if "USER" in commands:
return commands["USER"].copy()
return {}
async def module_late_init():
await set_user_commands()

View File

@ -0,0 +1 @@
from .main import module_init

View File

@ -0,0 +1,21 @@
{
"id": "standard.help",
"name": "Help",
"description": "Модуль для вывода /help сообщения",
"author": "OCAB Team",
"version": "1.0.0",
"privileged": false,
"dependencies": {
"required": {
"standard.config": "^1.0.0"
},
"optional": {
"standard.command_helper": "^1.0.0"
}
},
"pythonDependencies": {
"required": {
"string": "*"
}
}
}

View File

@ -0,0 +1,81 @@
import string
from typing import TYPE_CHECKING
from aiogram import Router
from aiogram.filters import Command
from aiogram.types import Message
from ocab_core.modules_system.public_api import (
get_metainfo,
get_module,
register_router,
)
if TYPE_CHECKING:
from ocab_modules.standard.config import IConfig
config: "IConfig" = get_module("standard.config", "config")
try:
(register_command, get_user_commands) = get_module(
"standard.command_helper", ["register_command", "get_user_commands"]
)
COMMAND_HELPER_MODULE_LOADED = True
except Exception:
COMMAND_HELPER_MODULE_LOADED = False
pass
FOOTER = """===
Разработано командой ALT Gnome Infrastructure в рамках проекта Каркас.
Исходный код: https://gitflic.ru/project/alt-gnome/karkas
Оставить репорт: https://gitflic.ru/project/alt-gnome/karkas/issue/create
Руководитель проекта: Семен Фомченков
Ведущий разработчик: Максим Слипенко
Версия: $version
"""
def format_commands(commands_dict):
formatted_commands = []
for command, details in commands_dict.items():
formatted_commands.append(f"/{command} - {details['description']}")
return "\n".join(formatted_commands)
async def help(message: Message):
commands = ""
version = ""
if COMMAND_HELPER_MODULE_LOADED:
commands = format_commands(get_user_commands())
metainfo = get_metainfo()
if "app_version" in metainfo:
version = metainfo["app_version"]
await message.reply(
string.Template(config.get("help::message") + "\n\n" + FOOTER).substitute(
commands=commands, version=version or "не указана"
)
)
async def module_init():
config.register(
"help::message",
"string",
default_value="$commands",
)
router = Router()
router.message.register(help, Command("help"))
register_router(router)
if COMMAND_HELPER_MODULE_LOADED:
register_command("help", "Cправка")