Skip to content

Commit

Permalink
✨ 父级插件加载
Browse files Browse the repository at this point in the history
  • Loading branch information
HibiKier committed Aug 30, 2024
1 parent 0074483 commit a368121
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 153 deletions.
115 changes: 59 additions & 56 deletions zhenxun/builtin_plugins/init/init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import aiofiles
import ujson as json
from ruamel.yaml import YAML
from nonebot.plugin import Plugin
from nonebot.drivers import Driver
from nonebot import get_loaded_plugins
from nonebot.plugin import Plugin, PluginMetadata

from zhenxun.services.log import logger
from zhenxun.models.task_info import TaskInfo
Expand Down Expand Up @@ -41,64 +41,68 @@ async def _handle_setting(
plugin_list: 插件列表
limit_list: 插件限制列表
"""
if metadata := plugin.metadata:
extra = metadata.extra
extra_data = PluginExtraData(**extra)
logger.debug(f"{metadata.name}:{plugin.name} -> {extra}", "初始化插件数据")
setting = extra_data.setting or PluginSetting()
if metadata.type == "library":
extra_data.plugin_type = PluginType.HIDDEN
if (
extra_data.plugin_type
== PluginType.HIDDEN
# and extra_data.plugin_type != "功能"
):
extra_data.menu_type = ""
plugin_list.append(
PluginInfo(
metadata = plugin.metadata
if not metadata:
if not plugin.sub_plugins:
return
"""父插件"""
metadata = PluginMetadata(name=plugin.name, description="", usage="")
extra = metadata.extra
extra_data = PluginExtraData(**extra)
logger.debug(f"{metadata.name}:{plugin.name} -> {extra}", "初始化插件数据")
setting = extra_data.setting or PluginSetting()
if metadata.type == "library":
extra_data.plugin_type = PluginType.HIDDEN
if extra_data.plugin_type == PluginType.HIDDEN:
extra_data.menu_type = ""
if plugin.sub_plugins:
extra_data.plugin_type = PluginType.PARENT
plugin_list.append(
PluginInfo(
module=plugin.name,
module_path=plugin.module_name,
name=metadata.name,
author=extra_data.author,
version=extra_data.version,
level=setting.level,
default_status=setting.default_status,
limit_superuser=setting.limit_superuser,
menu_type=extra_data.menu_type,
cost_gold=setting.cost_gold,
plugin_type=extra_data.plugin_type,
admin_level=extra_data.admin_level,
parent=(plugin.parent_plugin.module_name if plugin.parent_plugin else None),
)
)
if extra_data.limits:
limit_list.extend(
PluginLimit(
module=plugin.name,
module_path=plugin.module_name,
name=metadata.name,
author=extra_data.author,
version=extra_data.version,
level=setting.level,
default_status=setting.default_status,
limit_superuser=setting.limit_superuser,
menu_type=extra_data.menu_type,
cost_gold=setting.cost_gold,
plugin_type=extra_data.plugin_type,
admin_level=extra_data.admin_level,
limit_type=limit._type,
watch_type=limit.watch_type,
status=limit.status,
check_type=limit.check_type,
result=limit.result,
cd=getattr(limit, "cd", None),
max_count=getattr(limit, "max_count", None),
)
for limit in extra_data.limits
)
if extra_data.limits:
limit_list.extend(
PluginLimit(
module=plugin.name,
module_path=plugin.module_name,
limit_type=limit._type,
watch_type=limit.watch_type,
status=limit.status,
check_type=limit.check_type,
result=limit.result,
cd=getattr(limit, "cd", None),
max_count=getattr(limit, "max_count", None),
)
for limit in extra_data.limits
)
if extra_data.tasks:
task_list.extend(
(
task.create_status,
TaskInfo(
module=task.module,
name=task.name,
status=task.status,
run_time=task.run_time,
default_status=task.default_status,
),
)
for task in extra_data.tasks
if extra_data.tasks:
task_list.extend(
(
task.create_status,
TaskInfo(
module=task.module,
name=task.name,
status=task.status,
run_time=task.run_time,
default_status=task.default_status,
),
)
for task in extra_data.tasks
)


@driver.on_startup
Expand All @@ -115,8 +119,7 @@ async def _():
module2id = {m["module_path"]: m["id"] for m in module_list}
for plugin in get_loaded_plugins():
load_plugin.append(plugin.module_name)
if plugin.metadata:
await _handle_setting(plugin, plugin_list, limit_list, task_list)
await _handle_setting(plugin, plugin_list, limit_list, task_list)
create_list = []
update_list = []
for plugin in plugin_list:
Expand Down
22 changes: 13 additions & 9 deletions zhenxun/builtin_plugins/plugin_store/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot_plugin_alconna import Alconna, Args, Subcommand, on_alconna
from nonebot_plugin_session import EventSession
from nonebot_plugin_alconna import Args, Alconna, Subcommand, on_alconna

from zhenxun.configs.utils import PluginExtraData
from zhenxun.services.log import logger
from zhenxun.utils.enum import PluginType
from zhenxun.utils.message import MessageUtils
from zhenxun.configs.utils import PluginExtraData

from .data_source import ShopManage

Expand Down Expand Up @@ -68,6 +68,7 @@
prefix=True,
)


@_matcher.assign("$main")
async def _(session: EventSession):
try:
Expand All @@ -82,9 +83,7 @@ async def _(session: EventSession):
@_matcher.assign("add")
async def _(session: EventSession, plugin_id: int):
try:
await MessageUtils.build_message(
f"正在添加插件 Id: {plugin_id}"
).send()
await MessageUtils.build_message(f"正在添加插件 Id: {plugin_id}").send()
result = await ShopManage.add_plugin(plugin_id)
except Exception as e:
logger.error(f"添加插件 Id: {plugin_id}失败", "插件商店", session=session, e=e)
Expand All @@ -107,24 +106,29 @@ async def _(session: EventSession, plugin_id: int):
logger.info(f"移除插件 Id: {plugin_id}", "插件商店", session=session)
await MessageUtils.build_message(result).send()


@_matcher.assign("search")
async def _(session: EventSession, plugin_name_or_author: str):
try:
result = await ShopManage.search_plugin(plugin_name_or_author)
except Exception as e:
logger.error(f"搜索插件 name: {plugin_name_or_author}失败", "插件商店", session=session, e=e)
logger.error(
f"搜索插件 name: {plugin_name_or_author}失败",
"插件商店",
session=session,
e=e,
)
await MessageUtils.build_message(
f"搜索插件 name: {plugin_name_or_author} 失败 e: {e}"
).finish()
logger.info(f"搜索插件 name: {plugin_name_or_author}", "插件商店", session=session)
await MessageUtils.build_message(result).send()


@_matcher.assign("update")
async def _(session: EventSession, plugin_id: int):
try:
await MessageUtils.build_message(
f"正在更新插件 Id: {plugin_id}"
).send()
await MessageUtils.build_message(f"正在更新插件 Id: {plugin_id}").send()
result = await ShopManage.update_plugin(plugin_id)
except Exception as e:
logger.error(f"更新插件 Id: {plugin_id}失败", "插件商店", session=session, e=e)
Expand Down
4 changes: 2 additions & 2 deletions zhenxun/builtin_plugins/plugin_store/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ async def get_plugins_info(cls) -> BuildImage | str:
plugin_list = await PluginInfo.filter(load_status=True).values_list(
"module", "version"
)
suc_plugin = {p[0]: p[1] for p in plugin_list if p[1]}
suc_plugin = {p[0]: (p[1] or "0.1") for p in plugin_list}
data_list = [
[
"已安装" if plugin_info[1]["module"] in suc_plugin else "",
Expand All @@ -226,7 +226,7 @@ async def get_plugins_info(cls) -> BuildImage | str:
]
return await ImageTemplate.table_page(
"插件列表",
"通过安装/卸载插件 ID 来管理插件",
"通过添加/移除插件 ID 来管理插件",
column_name,
data_list,
text_style=row_style,
Expand Down
2 changes: 1 addition & 1 deletion zhenxun/configs/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def get_config(self, module: str, key: str, default: Any = None) -> Any:
value = default
logger.debug(
f"获取配置 MODULE: [<u><y>{module}</y></u>] | "
" KEY: [<u><y>{key}</y></u>] -> [<u><c>{value}</c></u>]"
f" KEY: [<u><y>{key}</y></u>] -> [<u><c>{value}</c></u>]"
)
return value

Expand Down
13 changes: 10 additions & 3 deletions zhenxun/models/plugin_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from zhenxun.services.db_context import Model
from zhenxun.utils.enum import BlockType, PluginType

from .plugin_limit import PluginLimit
from zhenxun.models.plugin_limit import PluginLimit # noqa: F401


class PluginInfo(Model):
Expand Down Expand Up @@ -45,7 +44,15 @@ class PluginInfo(Model):
"""调用所需权限等级"""
is_delete = fields.BooleanField(default=False, description="是否删除")
"""是否删除"""
parent = fields.CharField(max_length=255, null=True, description="父插件")
"""父插件"""

class Meta:
class Meta: # type: ignore
table = "plugin_info"
table_description = "插件基本信息"

@classmethod
async def _run_script(cls):
return [
"ALTER TABLE plugin_info ADD COLUMN parent character varying(255);",
]
Loading

0 comments on commit a368121

Please sign in to comment.