2024-07-24 02:36:46 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
|
|
|
|
|
|
|
|
|
@Time : 2024/7/23 下午11:59
|
|
|
|
|
@Author : snowykami
|
|
|
|
|
@Email : snowykami@outlook.com
|
|
|
|
|
@File : load.py
|
|
|
|
|
@Software: PyCharm
|
|
|
|
|
"""
|
|
|
|
|
import os
|
|
|
|
|
import traceback
|
2024-08-18 04:40:02 +08:00
|
|
|
|
from importlib import import_module
|
2024-07-24 02:36:46 +08:00
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
2024-08-18 01:25:11 +08:00
|
|
|
|
from liteyuki.log import logger
|
2024-08-18 23:39:19 +08:00
|
|
|
|
from liteyuki.plugin.model import Plugin, PluginMetadata, PluginType
|
2024-07-24 02:36:46 +08:00
|
|
|
|
from liteyuki.utils import path_to_module_name
|
|
|
|
|
|
|
|
|
|
_plugins: dict[str, Plugin] = {}
|
|
|
|
|
|
2024-07-26 14:35:47 +08:00
|
|
|
|
__all__ = [
|
2024-08-24 01:09:54 +08:00
|
|
|
|
"load_plugin",
|
|
|
|
|
"load_plugins",
|
|
|
|
|
"_plugins",
|
2024-07-26 14:35:47 +08:00
|
|
|
|
]
|
|
|
|
|
|
2024-07-24 02:36:46 +08:00
|
|
|
|
|
|
|
|
|
def load_plugin(module_path: str | Path) -> Optional[Plugin]:
|
|
|
|
|
"""加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
module_path: 插件名称 `path.to.your.plugin`
|
|
|
|
|
或插件路径 `pathlib.Path(path/to/your/plugin)`
|
|
|
|
|
"""
|
2024-08-24 01:09:54 +08:00
|
|
|
|
module_path = (
|
|
|
|
|
path_to_module_name(Path(module_path))
|
|
|
|
|
if isinstance(module_path, Path)
|
|
|
|
|
else module_path
|
|
|
|
|
)
|
2024-07-24 02:36:46 +08:00
|
|
|
|
try:
|
|
|
|
|
module = import_module(module_path)
|
|
|
|
|
_plugins[module.__name__] = Plugin(
|
|
|
|
|
name=module.__name__,
|
|
|
|
|
module=module,
|
|
|
|
|
module_name=module_path,
|
|
|
|
|
)
|
2024-08-24 01:43:35 +08:00
|
|
|
|
if module.__dict__.get("__plugin_metadata__", None):
|
|
|
|
|
metadata: "PluginMetadata" = module.__dict__["__plugin_metadata__"]
|
|
|
|
|
display_name = module.__name__.split(".")[-1]
|
|
|
|
|
elif module.__dict__.get("__liteyuki_plugin_meta__", None):
|
2024-08-24 01:09:54 +08:00
|
|
|
|
metadata: "PluginMetadata" = module.__dict__["__liteyuki_plugin_meta__"]
|
|
|
|
|
display_name = format_display_name(
|
|
|
|
|
f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type
|
|
|
|
|
)
|
2024-08-24 01:43:35 +08:00
|
|
|
|
elif module.__dict__.get("__plugin_meta__", None):
|
2024-08-12 02:40:51 +08:00
|
|
|
|
metadata: "PluginMetadata" = module.__dict__["__plugin_meta__"]
|
2024-08-24 01:09:54 +08:00
|
|
|
|
display_name = format_display_name(
|
|
|
|
|
f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type
|
|
|
|
|
)
|
2024-08-24 01:43:35 +08:00
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
logger.opt(colors=True).warning(
|
|
|
|
|
f'The metadata of Liteyuki plugin "{module.__name__}" is not specified, use empty.'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
metadata = PluginMetadata(
|
|
|
|
|
name=module.__name__,
|
|
|
|
|
)
|
|
|
|
|
display_name = module.__name__.split(".")[-1]
|
|
|
|
|
|
|
|
|
|
_plugins[module.__name__].metadata = metadata
|
2024-08-18 23:39:19 +08:00
|
|
|
|
|
2024-07-24 02:36:46 +08:00
|
|
|
|
logger.opt(colors=True).success(
|
2024-08-18 23:39:19 +08:00
|
|
|
|
f'Succeeded to load liteyuki plugin "{display_name}"'
|
2024-07-24 02:36:46 +08:00
|
|
|
|
)
|
|
|
|
|
return _plugins[module.__name__]
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True).success(
|
|
|
|
|
f'Failed to load liteyuki plugin "<r>{module_path}</r>"'
|
|
|
|
|
)
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2024-08-18 04:40:02 +08:00
|
|
|
|
def load_plugins(*plugin_dir: str, ignore_warning: bool = True) -> set[Plugin]:
|
2024-07-24 02:36:46 +08:00
|
|
|
|
"""导入文件夹下多个插件
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
plugin_dir: 文件夹路径
|
2024-08-18 04:40:02 +08:00
|
|
|
|
ignore_warning: 是否忽略警告,通常是目录不存在或目录为空
|
2024-07-24 02:36:46 +08:00
|
|
|
|
"""
|
|
|
|
|
plugins = set()
|
|
|
|
|
for dir_path in plugin_dir:
|
|
|
|
|
# 遍历每一个文件夹下的py文件和包含__init__.py的文件夹,不递归
|
2024-08-18 04:34:54 +08:00
|
|
|
|
if not os.path.exists(dir_path):
|
2024-08-18 04:40:02 +08:00
|
|
|
|
if not ignore_warning:
|
|
|
|
|
logger.warning(f"Plugins dir '{dir_path}' does not exist.")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not os.listdir(dir_path):
|
|
|
|
|
if not ignore_warning:
|
|
|
|
|
logger.warning(f"Plugins dir '{dir_path}' is empty.")
|
2024-08-18 04:34:54 +08:00
|
|
|
|
continue
|
2024-08-18 04:40:02 +08:00
|
|
|
|
|
|
|
|
|
if not os.path.isdir(dir_path):
|
|
|
|
|
if not ignore_warning:
|
|
|
|
|
logger.warning(f"Plugins dir '{dir_path}' is not a directory.")
|
|
|
|
|
continue
|
|
|
|
|
|
2024-07-24 02:36:46 +08:00
|
|
|
|
for f in os.listdir(dir_path):
|
|
|
|
|
path = Path(os.path.join(dir_path, f))
|
|
|
|
|
|
|
|
|
|
module_name = None
|
2024-08-24 01:09:54 +08:00
|
|
|
|
if os.path.isfile(path) and f.endswith(".py") and f != "__init__.py":
|
2024-07-24 02:36:46 +08:00
|
|
|
|
module_name = f"{path_to_module_name(Path(dir_path))}.{f[:-3]}"
|
|
|
|
|
|
2024-08-24 01:09:54 +08:00
|
|
|
|
elif os.path.isdir(path) and os.path.exists(
|
|
|
|
|
os.path.join(path, "__init__.py")
|
|
|
|
|
):
|
2024-07-24 02:36:46 +08:00
|
|
|
|
module_name = path_to_module_name(path)
|
|
|
|
|
|
|
|
|
|
if module_name:
|
|
|
|
|
load_plugin(module_name)
|
|
|
|
|
if _plugins.get(module_name):
|
|
|
|
|
plugins.add(_plugins[module_name])
|
|
|
|
|
return plugins
|
2024-08-18 23:39:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
2024-08-20 06:20:41 +08:00
|
|
|
|
case PluginType.TEST:
|
2024-08-18 23:39:19 +08:00
|
|
|
|
color = "g"
|
|
|
|
|
case PluginType.MODULE:
|
|
|
|
|
color = "e"
|
|
|
|
|
case PluginType.SERVICE:
|
|
|
|
|
color = "c"
|
|
|
|
|
|
|
|
|
|
return f"<{color}>{display_name} [{plugin_type.name}]</{color}>"
|