From 803b65e08eeb74355897b2e970714da2256bd148 Mon Sep 17 00:00:00 2001 From: snowy Date: Sun, 18 Aug 2024 23:39:19 +0800 Subject: [PATCH] :sparkles: add color plugin name --- liteyuki/plugin/load.py | 31 +++++++++++++++++-- liteyuki/plugin/model.py | 4 +-- liteyuki/plugins/plugin_loader/__init__.py | 5 +-- src/liteyuki_plugins/lifespan_monitor.py | 3 +- .../process_manager/__init__.py | 9 +++--- src/liteyuki_plugins/reboot.py | 8 +++++ src/liteyuki_plugins/register_service.py | 3 +- .../resource_loader/__init__.py | 8 +++++ .../scheduled_tasks/__init__.py | 5 +-- 9 files changed, 60 insertions(+), 16 deletions(-) diff --git a/liteyuki/plugin/load.py b/liteyuki/plugin/load.py index 2c54822e..b60328cd 100644 --- a/liteyuki/plugin/load.py +++ b/liteyuki/plugin/load.py @@ -15,7 +15,7 @@ from pathlib import Path from typing import Optional from liteyuki.log import logger -from liteyuki.plugin.model import Plugin, PluginMetadata +from liteyuki.plugin.model import Plugin, PluginMetadata, PluginType from liteyuki.utils import path_to_module_name _plugins: dict[str, Plugin] = {} @@ -46,9 +46,10 @@ def load_plugin(module_path: str | Path) -> Optional[Plugin]: display_name = module.__name__.split(".")[-1] if module.__dict__.get("__plugin_meta__"): metadata: "PluginMetadata" = module.__dict__["__plugin_meta__"] - display_name = f"{metadata.name}({module.__name__.split('.')[-1]})" + display_name = format_display_name(f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type) + logger.opt(colors=True).success( - f'Succeeded to load liteyuki plugin "{display_name}"' + f'Succeeded to load liteyuki plugin "{display_name}"' ) return _plugins[module.__name__] @@ -100,3 +101,27 @@ def load_plugins(*plugin_dir: str, ignore_warning: bool = True) -> set[Plugin]: if _plugins.get(module_name): plugins.add(_plugins[module_name]) return plugins + + +def format_display_name(display_name: str, plugin_type: PluginType) -> str: + """ + 设置插件名称颜色,根据不同类型插件设置颜色 + Args: + display_name: 插件名称 + plugin_type: 插件类型 + + Returns: + str: 设置后的插件名称 name + """ + color = "y" + match plugin_type: + case PluginType.APPLICATION: + color = "m" + case PluginType.IMPLEMENTATION: + color = "g" + case PluginType.MODULE: + color = "e" + case PluginType.SERVICE: + color = "c" + + return f"<{color}>{display_name} [{plugin_type.name}]" diff --git a/liteyuki/plugin/model.py b/liteyuki/plugin/model.py index adabe144..4409ba2c 100644 --- a/liteyuki/plugin/model.py +++ b/liteyuki/plugin/model.py @@ -59,9 +59,9 @@ class PluginMetadata(BaseModel): name: str description: str = "" usage: str = "" - type: str = "" + type: PluginType = PluginType.UNCLASSIFIED author: str = "" - homepage: str = PluginType.UNCLASSIFIED + homepage: str = "" extra: dict[str, Any] = {} diff --git a/liteyuki/plugins/plugin_loader/__init__.py b/liteyuki/plugins/plugin_loader/__init__.py index 60a0508d..84ea5435 100644 --- a/liteyuki/plugins/plugin_loader/__init__.py +++ b/liteyuki/plugins/plugin_loader/__init__.py @@ -9,11 +9,12 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @Software: PyCharm """ from liteyuki import get_config, load_plugin -from liteyuki.plugin import PluginMetadata, load_plugins +from liteyuki.plugin import PluginMetadata, load_plugins, PluginType __plugin_meta__ = PluginMetadata( name="外部轻雪插件加载器", - description="插件加载器,用于加载轻雪原生插件" + description="插件加载器,用于加载轻雪原生插件", + type=PluginType.SERVICE ) diff --git a/src/liteyuki_plugins/lifespan_monitor.py b/src/liteyuki_plugins/lifespan_monitor.py index ff8bea5c..c2889448 100644 --- a/src/liteyuki_plugins/lifespan_monitor.py +++ b/src/liteyuki_plugins/lifespan_monitor.py @@ -10,12 +10,13 @@ import asyncio import multiprocessing -from liteyuki.plugin import PluginMetadata +from liteyuki.plugin import PluginMetadata, PluginType from liteyuki import get_bot, logger from liteyuki.comm.channel import get_channel __plugin_meta__ = PluginMetadata( name="生命周期日志", + type=PluginType.SERVICE, ) bot = get_bot() diff --git a/src/liteyuki_plugins/process_manager/__init__.py b/src/liteyuki_plugins/process_manager/__init__.py index dcdbd336..8c4be527 100644 --- a/src/liteyuki_plugins/process_manager/__init__.py +++ b/src/liteyuki_plugins/process_manager/__init__.py @@ -1,9 +1,8 @@ -from liteyuki.plugin import PluginMetadata - +from liteyuki.plugin import PluginMetadata, PluginType __plugin_meta__ = PluginMetadata( name="进程管理器", - version="0.1.0", author="snowykami", - description="进程管理器,用于管理子进程" -) \ No newline at end of file + description="进程管理器,用于管理子进程", + type=PluginType.SERVICE +) diff --git a/src/liteyuki_plugins/reboot.py b/src/liteyuki_plugins/reboot.py index ddb31053..3ba8eb81 100644 --- a/src/liteyuki_plugins/reboot.py +++ b/src/liteyuki_plugins/reboot.py @@ -8,3 +8,11 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @File : reloader.py.py @Software: PyCharm """ +from liteyuki.plugin import PluginMetadata, PluginType + +__plugin_meta__ = PluginMetadata( + name="重启", + author="snowykami", + description="进程管理器,用于管理子进程", + type=PluginType.MODULE +) diff --git a/src/liteyuki_plugins/register_service.py b/src/liteyuki_plugins/register_service.py index 1a319afc..9e441358 100644 --- a/src/liteyuki_plugins/register_service.py +++ b/src/liteyuki_plugins/register_service.py @@ -14,11 +14,12 @@ import platform from aiohttp import ClientSession from git import Repo -from liteyuki.plugin import PluginMetadata +from liteyuki.plugin import PluginMetadata, PluginType from liteyuki import get_bot, logger __plugin_meta__ = PluginMetadata( name="注册服务", + type=PluginType.SERVICE ) liteyuki = get_bot() diff --git a/src/liteyuki_plugins/resource_loader/__init__.py b/src/liteyuki_plugins/resource_loader/__init__.py index e69de29b..9eaecf7b 100644 --- a/src/liteyuki_plugins/resource_loader/__init__.py +++ b/src/liteyuki_plugins/resource_loader/__init__.py @@ -0,0 +1,8 @@ +from liteyuki.plugin import PluginMetadata, PluginType + +__plugin_meta__ = PluginMetadata( + name="资源加载器", + author="snowykami", + description="进程管理器,用于管理子进程", + type=PluginType.SERVICE +) diff --git a/src/liteyuki_plugins/scheduled_tasks/__init__.py b/src/liteyuki_plugins/scheduled_tasks/__init__.py index f8d0c15d..03cffe53 100644 --- a/src/liteyuki_plugins/scheduled_tasks/__init__.py +++ b/src/liteyuki_plugins/scheduled_tasks/__init__.py @@ -8,11 +8,12 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @File : __init__.py.py @Software: PyCharm """ -from liteyuki.plugin import PluginMetadata +from liteyuki.plugin import PluginMetadata, PluginType from .divided_by_lifespan import * -__plugin_mata__ = PluginMetadata( +__plugin_meta__ = PluginMetadata( name="计划任务", description="计划任务插件,一些杂项任务的计划执行。", + type=PluginType.SERVICE )