Skip to content

Commit

Permalink
✨ add color plugin name
Browse files Browse the repository at this point in the history
  • Loading branch information
snowykami committed Aug 18, 2024
1 parent aa9abde commit 803b65e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 16 deletions.
31 changes: 28 additions & 3 deletions liteyuki/plugin/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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] = {}
Expand Down Expand Up @@ -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 "<y>{display_name}</y>"'
f'Succeeded to load liteyuki plugin "{display_name}"'
)
return _plugins[module.__name__]

Expand Down Expand Up @@ -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: 设置后的插件名称 <y>name</y>
"""
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}]</{color}>"
4 changes: 2 additions & 2 deletions liteyuki/plugin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}


Expand Down
5 changes: 3 additions & 2 deletions liteyuki/plugins/plugin_loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
@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
)


Expand Down
3 changes: 2 additions & 1 deletion src/liteyuki_plugins/lifespan_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions src/liteyuki_plugins/process_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -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="进程管理器,用于管理子进程"
)
description="进程管理器,用于管理子进程",
type=PluginType.SERVICE
)
8 changes: 8 additions & 0 deletions src/liteyuki_plugins/reboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@
@File : reloader.py.py
@Software: PyCharm
"""
from liteyuki.plugin import PluginMetadata, PluginType

__plugin_meta__ = PluginMetadata(
name="重启",
author="snowykami",
description="进程管理器,用于管理子进程",
type=PluginType.MODULE
)
3 changes: 2 additions & 1 deletion src/liteyuki_plugins/register_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
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()
Expand Down
8 changes: 8 additions & 0 deletions src/liteyuki_plugins/resource_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from liteyuki.plugin import PluginMetadata, PluginType

__plugin_meta__ = PluginMetadata(
name="资源加载器",
author="snowykami",
description="进程管理器,用于管理子进程",
type=PluginType.SERVICE
)
5 changes: 3 additions & 2 deletions src/liteyuki_plugins/scheduled_tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
@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
)

0 comments on commit 803b65e

Please sign in to comment.