2024-12-10 19:49:11 +08:00
|
|
|
|
from pathlib import Path
|
2024-08-20 21:29:42 +08:00
|
|
|
|
import shutil
|
2024-08-21 16:37:07 +08:00
|
|
|
|
import subprocess
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
2024-09-03 13:04:49 +08:00
|
|
|
|
from aiocache import cached
|
2024-12-10 19:49:11 +08:00
|
|
|
|
import ujson as json
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from zhenxun.builtin_plugins.auto_update.config import REQ_TXT_FILE_STRING
|
|
|
|
|
|
from zhenxun.builtin_plugins.plugin_store.models import StorePluginInfo
|
2024-08-29 22:01:34 +08:00
|
|
|
|
from zhenxun.models.plugin_info import PluginInfo
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from zhenxun.services.log import logger
|
|
|
|
|
|
from zhenxun.services.plugin_init import PluginInitManager
|
2024-09-16 20:08:42 +08:00
|
|
|
|
from zhenxun.utils.github_utils import GithubUtils
|
2024-09-08 09:38:55 +08:00
|
|
|
|
from zhenxun.utils.github_utils.models import RepoAPI
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from zhenxun.utils.http_utils import AsyncHttpx
|
|
|
|
|
|
from zhenxun.utils.image_utils import BuildImage, ImageTemplate, RowStyle
|
2025-01-09 10:29:49 +08:00
|
|
|
|
from zhenxun.utils.utils import is_number
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
2024-12-10 19:49:11 +08:00
|
|
|
|
from .config import BASE_PATH, DEFAULT_GITHUB_URL, EXTRA_GITHUB_URL
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
2025-06-16 09:11:41 +08:00
|
|
|
|
BAT_FILE = Path() / "win启动.bat"
|
|
|
|
|
|
|
|
|
|
|
|
WIN_COMMAND = ["./Python310/python.exe", "-m", "pip", "install", "-r"]
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_COMMAND = ["poetry", "run", "pip", "install", "-r"]
|
|
|
|
|
|
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
|
|
|
|
|
def row_style(column: str, text: str) -> RowStyle:
|
|
|
|
|
|
"""被动技能文本风格
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
column: 表头
|
|
|
|
|
|
text: 文本内容
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
RowStyle: RowStyle
|
|
|
|
|
|
"""
|
|
|
|
|
|
style = RowStyle()
|
2024-08-29 22:01:34 +08:00
|
|
|
|
if column == "-" and text == "已安装":
|
|
|
|
|
|
style.font_color = "#67C23A"
|
2024-08-20 21:29:42 +08:00
|
|
|
|
return style
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-21 16:37:07 +08:00
|
|
|
|
def install_requirement(plugin_path: Path):
|
2024-08-26 10:39:33 +08:00
|
|
|
|
requirement_files = ["requirement.txt", "requirements.txt"]
|
|
|
|
|
|
requirement_paths = [plugin_path / file for file in requirement_files]
|
2024-08-24 14:49:55 +08:00
|
|
|
|
|
2024-08-26 19:43:12 +08:00
|
|
|
|
existing_requirements = next(
|
|
|
|
|
|
(path for path in requirement_paths if path.exists()), None
|
|
|
|
|
|
)
|
2024-08-26 10:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
if not existing_requirements:
|
2024-08-26 19:43:12 +08:00
|
|
|
|
logger.debug(
|
|
|
|
|
|
f"No requirement.txt found for plugin: {plugin_path.name}", "插件管理"
|
|
|
|
|
|
)
|
2024-08-21 16:37:07 +08:00
|
|
|
|
return
|
2024-08-24 14:49:55 +08:00
|
|
|
|
|
2024-08-21 16:37:07 +08:00
|
|
|
|
try:
|
2025-06-16 09:11:41 +08:00
|
|
|
|
command = WIN_COMMAND if BAT_FILE.exists() else DEFAULT_COMMAND
|
|
|
|
|
|
command.append(str(existing_requirements))
|
2024-08-24 19:32:52 +08:00
|
|
|
|
result = subprocess.run(
|
2025-06-16 09:11:41 +08:00
|
|
|
|
command,
|
2024-08-24 19:32:52 +08:00
|
|
|
|
check=True,
|
2024-08-29 22:01:34 +08:00
|
|
|
|
capture_output=True,
|
2024-08-24 19:32:52 +08:00
|
|
|
|
text=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
logger.debug(
|
2024-08-29 22:01:34 +08:00
|
|
|
|
"Successfully installed dependencies for"
|
|
|
|
|
|
f" plugin: {plugin_path.name}. Output:\n{result.stdout}",
|
2024-08-24 19:32:52 +08:00
|
|
|
|
"插件管理",
|
|
|
|
|
|
)
|
2024-08-29 22:01:34 +08:00
|
|
|
|
except subprocess.CalledProcessError:
|
2024-08-24 19:32:52 +08:00
|
|
|
|
logger.error(
|
2024-08-29 22:01:34 +08:00
|
|
|
|
f"Failed to install dependencies for plugin: {plugin_path.name}. "
|
|
|
|
|
|
" Error:\n{e.stderr}"
|
2024-08-24 19:32:52 +08:00
|
|
|
|
)
|
2024-08-21 16:37:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-08-20 21:29:42 +08:00
|
|
|
|
class ShopManage:
|
|
|
|
|
|
@classmethod
|
2024-09-03 13:04:49 +08:00
|
|
|
|
@cached(60)
|
2024-09-29 20:47:58 +08:00
|
|
|
|
async def get_data(cls) -> dict[str, StorePluginInfo]:
|
2024-08-20 21:29:42 +08:00
|
|
|
|
"""获取插件信息数据
|
|
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
|
ValueError: 访问请求失败
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
dict: 插件信息数据
|
|
|
|
|
|
"""
|
2025-02-18 15:32:26 +08:00
|
|
|
|
default_github_repo = GithubUtils.parse_github_url(DEFAULT_GITHUB_URL)
|
|
|
|
|
|
extra_github_repo = GithubUtils.parse_github_url(EXTRA_GITHUB_URL)
|
|
|
|
|
|
for repo_info in [default_github_repo, extra_github_repo]:
|
|
|
|
|
|
if await repo_info.update_repo_commit():
|
|
|
|
|
|
logger.info(f"获取最新提交: {repo_info.branch}", "插件管理")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.warning(f"获取最新提交失败: {repo_info}", "插件管理")
|
|
|
|
|
|
default_github_url = await default_github_repo.get_raw_download_urls(
|
|
|
|
|
|
"plugins.json"
|
|
|
|
|
|
)
|
|
|
|
|
|
extra_github_url = await extra_github_repo.get_raw_download_urls("plugins.json")
|
2025-06-09 14:41:19 +08:00
|
|
|
|
res = await AsyncHttpx.get(default_github_url, check_status_code=200)
|
|
|
|
|
|
res2 = await AsyncHttpx.get(extra_github_url, check_status_code=200)
|
2024-08-26 10:39:33 +08:00
|
|
|
|
|
|
|
|
|
|
# 检查请求结果
|
|
|
|
|
|
if res.status_code != 200 or res2.status_code != 200:
|
|
|
|
|
|
raise ValueError(f"下载错误, code: {res.status_code}, {res2.status_code}")
|
|
|
|
|
|
|
|
|
|
|
|
# 解析并合并返回的 JSON 数据
|
|
|
|
|
|
data1 = json.loads(res.text)
|
|
|
|
|
|
data2 = json.loads(res2.text)
|
2024-09-02 01:50:39 +08:00
|
|
|
|
return {
|
|
|
|
|
|
name: StorePluginInfo(**detail)
|
|
|
|
|
|
for name, detail in {**data1, **data2}.items()
|
|
|
|
|
|
}
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
2024-08-24 20:24:05 +08:00
|
|
|
|
@classmethod
|
2024-09-02 01:50:39 +08:00
|
|
|
|
def version_check(cls, plugin_info: StorePluginInfo, suc_plugin: dict[str, str]):
|
|
|
|
|
|
"""版本检查
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
plugin_info: StorePluginInfo
|
|
|
|
|
|
suc_plugin: dict[str, str]
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 版本号
|
|
|
|
|
|
"""
|
|
|
|
|
|
module = plugin_info.module
|
2024-09-02 15:00:37 +08:00
|
|
|
|
if suc_plugin.get(module) and not cls.check_version_is_new(
|
|
|
|
|
|
plugin_info, suc_plugin
|
|
|
|
|
|
):
|
2024-09-02 01:50:39 +08:00
|
|
|
|
return f"{suc_plugin[module]} (有更新->{plugin_info.version})"
|
|
|
|
|
|
return plugin_info.version
|
2024-08-24 20:24:05 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2024-09-02 01:50:39 +08:00
|
|
|
|
def check_version_is_new(
|
|
|
|
|
|
cls, plugin_info: StorePluginInfo, suc_plugin: dict[str, str]
|
|
|
|
|
|
):
|
|
|
|
|
|
"""检查版本是否有更新
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
plugin_info: StorePluginInfo
|
|
|
|
|
|
suc_plugin: dict[str, str]
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
bool: 是否有更新
|
|
|
|
|
|
"""
|
|
|
|
|
|
module = plugin_info.module
|
2024-09-02 12:00:27 +08:00
|
|
|
|
return suc_plugin.get(module) and plugin_info.version == suc_plugin[module]
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def get_loaded_plugins(cls, *args) -> list[tuple[str, str]]:
|
|
|
|
|
|
"""获取已加载的插件
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
list[str]: 已加载的插件
|
|
|
|
|
|
"""
|
|
|
|
|
|
return await PluginInfo.filter(load_status=True).values_list(*args)
|
2024-08-24 20:24:05 +08:00
|
|
|
|
|
2024-08-20 21:29:42 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
|
async def get_plugins_info(cls) -> BuildImage | str:
|
|
|
|
|
|
"""插件列表
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
BuildImage | str: 返回消息
|
|
|
|
|
|
"""
|
2024-09-29 20:47:58 +08:00
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
2024-08-20 21:29:42 +08:00
|
|
|
|
column_name = ["-", "ID", "名称", "简介", "作者", "版本", "类型"]
|
2024-09-02 12:00:27 +08:00
|
|
|
|
plugin_list = await cls.get_loaded_plugins("module", "version")
|
2024-08-30 23:50:45 +08:00
|
|
|
|
suc_plugin = {p[0]: (p[1] or "0.1") for p in plugin_list}
|
2024-08-20 21:29:42 +08:00
|
|
|
|
data_list = [
|
|
|
|
|
|
[
|
2024-09-02 01:50:39 +08:00
|
|
|
|
"已安装" if plugin_info[1].module in suc_plugin else "",
|
2024-08-24 14:49:55 +08:00
|
|
|
|
id,
|
|
|
|
|
|
plugin_info[0],
|
2024-09-02 01:50:39 +08:00
|
|
|
|
plugin_info[1].description,
|
|
|
|
|
|
plugin_info[1].author,
|
2024-08-24 20:24:05 +08:00
|
|
|
|
cls.version_check(plugin_info[1], suc_plugin),
|
2024-09-02 01:50:39 +08:00
|
|
|
|
plugin_info[1].plugin_type_name,
|
2024-08-20 21:29:42 +08:00
|
|
|
|
]
|
2024-08-24 14:49:55 +08:00
|
|
|
|
for id, plugin_info in enumerate(data.items())
|
2024-08-20 21:29:42 +08:00
|
|
|
|
]
|
|
|
|
|
|
return await ImageTemplate.table_page(
|
|
|
|
|
|
"插件列表",
|
2024-08-30 23:50:45 +08:00
|
|
|
|
"通过添加/移除插件 ID 来管理插件",
|
2024-08-20 21:29:42 +08:00
|
|
|
|
column_name,
|
|
|
|
|
|
data_list,
|
|
|
|
|
|
text_style=row_style,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-01-09 10:29:49 +08:00
|
|
|
|
async def add_plugin(cls, plugin_id: str) -> str:
|
2024-08-24 20:24:05 +08:00
|
|
|
|
"""添加插件
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
2024-10-02 18:32:21 +08:00
|
|
|
|
plugin_id: 插件id或模块名
|
2024-08-24 20:24:05 +08:00
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 返回消息
|
|
|
|
|
|
"""
|
2024-09-29 20:47:58 +08:00
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
2024-10-02 18:32:21 +08:00
|
|
|
|
try:
|
|
|
|
|
|
plugin_key = await cls._resolve_plugin_key(plugin_id)
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
return str(e)
|
2024-09-02 15:00:37 +08:00
|
|
|
|
plugin_list = await cls.get_loaded_plugins("module")
|
2024-08-20 21:29:42 +08:00
|
|
|
|
plugin_info = data[plugin_key]
|
2024-09-02 15:00:37 +08:00
|
|
|
|
if plugin_info.module in [p[0] for p in plugin_list]:
|
|
|
|
|
|
return f"插件 {plugin_key} 已安装,无需重复安装"
|
2024-09-02 01:50:39 +08:00
|
|
|
|
is_external = True
|
|
|
|
|
|
if plugin_info.github_url is None:
|
|
|
|
|
|
plugin_info.github_url = DEFAULT_GITHUB_URL
|
|
|
|
|
|
is_external = False
|
2024-09-21 17:38:37 +08:00
|
|
|
|
version_split = plugin_info.version.split("-")
|
|
|
|
|
|
if len(version_split) > 1:
|
|
|
|
|
|
github_url_split = plugin_info.github_url.split("/tree/")
|
|
|
|
|
|
plugin_info.github_url = f"{github_url_split[0]}/tree/{version_split[1]}"
|
2024-09-02 01:50:39 +08:00
|
|
|
|
logger.info(f"正在安装插件 {plugin_key}...")
|
|
|
|
|
|
await cls.install_plugin_with_repo(
|
|
|
|
|
|
plugin_info.github_url,
|
|
|
|
|
|
plugin_info.module_path,
|
|
|
|
|
|
plugin_info.is_dir,
|
|
|
|
|
|
is_external,
|
|
|
|
|
|
)
|
|
|
|
|
|
return f"插件 {plugin_key} 安装成功! 重启后生效"
|
2024-08-26 10:39:33 +08:00
|
|
|
|
|
2024-09-02 01:50:39 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
|
async def install_plugin_with_repo(
|
|
|
|
|
|
cls, github_url: str, module_path: str, is_dir: bool, is_external: bool = False
|
|
|
|
|
|
):
|
2024-09-03 15:43:09 +08:00
|
|
|
|
files: list[str]
|
2024-09-08 09:38:55 +08:00
|
|
|
|
repo_api: RepoAPI
|
2024-09-16 20:08:42 +08:00
|
|
|
|
repo_info = GithubUtils.parse_github_url(github_url)
|
2025-02-18 15:32:26 +08:00
|
|
|
|
if await repo_info.update_repo_commit():
|
|
|
|
|
|
logger.info(f"获取最新提交: {repo_info.branch}", "插件管理")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.warning(f"获取最新提交失败: {repo_info}", "插件管理")
|
2024-09-02 01:50:39 +08:00
|
|
|
|
logger.debug(f"成功获取仓库信息: {repo_info}", "插件管理")
|
2024-09-16 20:08:42 +08:00
|
|
|
|
for repo_api in GithubUtils.iter_api_strategies():
|
2024-09-03 15:43:09 +08:00
|
|
|
|
try:
|
2024-09-08 09:38:55 +08:00
|
|
|
|
await repo_api.parse_repo_info(repo_info)
|
2024-09-03 15:43:09 +08:00
|
|
|
|
break
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.warning(
|
2024-09-08 09:38:55 +08:00
|
|
|
|
f"获取插件文件失败: {e} | API类型: {repo_api.strategy}", "插件管理"
|
2024-09-03 15:43:09 +08:00
|
|
|
|
)
|
|
|
|
|
|
continue
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError("所有API获取插件文件失败,请检查网络连接")
|
2024-12-23 10:09:38 +08:00
|
|
|
|
if module_path == ".":
|
|
|
|
|
|
module_path = ""
|
2025-02-03 21:23:14 +08:00
|
|
|
|
replace_module_path = module_path.replace(".", "/")
|
2024-09-08 09:38:55 +08:00
|
|
|
|
files = repo_api.get_files(
|
2025-02-03 21:23:14 +08:00
|
|
|
|
module_path=replace_module_path + ("" if is_dir else ".py"),
|
2024-09-03 15:43:09 +08:00
|
|
|
|
is_dir=is_dir,
|
2024-09-02 01:50:39 +08:00
|
|
|
|
)
|
2024-09-16 20:08:42 +08:00
|
|
|
|
download_urls = [await repo_info.get_raw_download_urls(file) for file in files]
|
2024-09-02 01:50:39 +08:00
|
|
|
|
base_path = BASE_PATH / "plugins" if is_external else BASE_PATH
|
2024-12-23 10:09:38 +08:00
|
|
|
|
base_path = base_path if module_path else base_path / repo_info.repo
|
2024-09-02 01:50:39 +08:00
|
|
|
|
download_paths: list[Path | str] = [base_path / file for file in files]
|
|
|
|
|
|
logger.debug(f"插件下载路径: {download_paths}", "插件管理")
|
|
|
|
|
|
result = await AsyncHttpx.gather_download_file(download_urls, download_paths)
|
|
|
|
|
|
for _id, success in enumerate(result):
|
|
|
|
|
|
if not success:
|
|
|
|
|
|
break
|
|
|
|
|
|
else:
|
|
|
|
|
|
# 安装依赖
|
|
|
|
|
|
plugin_path = base_path / "/".join(module_path.split("."))
|
2025-02-03 21:23:14 +08:00
|
|
|
|
try:
|
|
|
|
|
|
req_files = repo_api.get_files(
|
|
|
|
|
|
f"{replace_module_path}/{REQ_TXT_FILE_STRING}", False
|
|
|
|
|
|
)
|
|
|
|
|
|
req_files.extend(
|
|
|
|
|
|
repo_api.get_files(f"{replace_module_path}/requirement.txt", False)
|
2024-09-02 01:50:39 +08:00
|
|
|
|
)
|
2025-02-03 21:23:14 +08:00
|
|
|
|
logger.debug(f"获取插件依赖文件列表: {req_files}", "插件管理")
|
|
|
|
|
|
req_download_urls = [
|
|
|
|
|
|
await repo_info.get_raw_download_urls(file) for file in req_files
|
|
|
|
|
|
]
|
|
|
|
|
|
req_paths: list[Path | str] = [plugin_path / file for file in req_files]
|
|
|
|
|
|
logger.debug(f"插件依赖文件下载路径: {req_paths}", "插件管理")
|
|
|
|
|
|
if req_files:
|
|
|
|
|
|
result = await AsyncHttpx.gather_download_file(
|
|
|
|
|
|
req_download_urls, req_paths
|
|
|
|
|
|
)
|
|
|
|
|
|
for success in result:
|
|
|
|
|
|
if not success:
|
|
|
|
|
|
raise Exception("插件依赖文件下载失败")
|
|
|
|
|
|
logger.debug(f"插件依赖文件列表: {req_paths}", "插件管理")
|
|
|
|
|
|
install_requirement(plugin_path)
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
logger.warning("未获取到依赖文件路径...", e=e)
|
2024-09-02 01:50:39 +08:00
|
|
|
|
return True
|
2025-02-03 21:23:14 +08:00
|
|
|
|
raise Exception("插件下载失败...")
|
2024-08-20 21:29:42 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-01-09 10:29:49 +08:00
|
|
|
|
async def remove_plugin(cls, plugin_id: str) -> str:
|
2024-08-24 20:24:05 +08:00
|
|
|
|
"""移除插件
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
2024-10-02 18:32:21 +08:00
|
|
|
|
plugin_id: 插件id或模块名
|
2024-08-24 20:24:05 +08:00
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 返回消息
|
|
|
|
|
|
"""
|
2024-09-29 20:47:58 +08:00
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
2024-10-02 18:32:21 +08:00
|
|
|
|
try:
|
|
|
|
|
|
plugin_key = await cls._resolve_plugin_key(plugin_id)
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
return str(e)
|
|
|
|
|
|
plugin_info = data[plugin_key]
|
2024-08-20 21:29:42 +08:00
|
|
|
|
path = BASE_PATH
|
2024-09-02 01:50:39 +08:00
|
|
|
|
if plugin_info.github_url:
|
2024-08-26 19:43:12 +08:00
|
|
|
|
path = BASE_PATH / "plugins"
|
2024-09-02 01:50:39 +08:00
|
|
|
|
for p in plugin_info.module_path.split("."):
|
2024-08-20 21:29:42 +08:00
|
|
|
|
path = path / p
|
2024-09-02 01:50:39 +08:00
|
|
|
|
if not plugin_info.is_dir:
|
2024-08-20 21:29:42 +08:00
|
|
|
|
path = Path(f"{path}.py")
|
|
|
|
|
|
if not path.exists():
|
|
|
|
|
|
return f"插件 {plugin_key} 不存在..."
|
|
|
|
|
|
logger.debug(f"尝试移除插件 {plugin_key} 文件: {path}", "插件管理")
|
2024-09-02 01:50:39 +08:00
|
|
|
|
if plugin_info.is_dir:
|
2024-08-20 21:29:42 +08:00
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
|
else:
|
|
|
|
|
|
path.unlink()
|
2024-09-27 10:29:59 +08:00
|
|
|
|
await PluginInitManager.remove(f"zhenxun.{plugin_info.module_path}")
|
2024-08-26 10:39:33 +08:00
|
|
|
|
return f"插件 {plugin_key} 移除成功! 重启后生效"
|
2024-08-24 20:24:05 +08:00
|
|
|
|
|
2024-08-24 14:49:55 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
|
async def search_plugin(cls, plugin_name_or_author: str) -> BuildImage | str:
|
2024-08-24 20:24:05 +08:00
|
|
|
|
"""搜索插件
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
plugin_name_or_author: 插件名称或作者
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
BuildImage | str: 返回消息
|
|
|
|
|
|
"""
|
2024-09-29 20:47:58 +08:00
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
2024-09-02 12:00:27 +08:00
|
|
|
|
plugin_list = await cls.get_loaded_plugins("module", "version")
|
2024-09-02 15:00:37 +08:00
|
|
|
|
suc_plugin = {p[0]: (p[1] or "Unknown") for p in plugin_list}
|
2024-08-24 14:49:55 +08:00
|
|
|
|
filtered_data = [
|
|
|
|
|
|
(id, plugin_info)
|
|
|
|
|
|
for id, plugin_info in enumerate(data.items())
|
2024-08-24 20:24:05 +08:00
|
|
|
|
if plugin_name_or_author.lower() in plugin_info[0].lower()
|
2024-09-02 01:50:39 +08:00
|
|
|
|
or plugin_name_or_author.lower() in plugin_info[1].author.lower()
|
2024-08-24 14:49:55 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
data_list = [
|
|
|
|
|
|
[
|
2024-09-02 01:50:39 +08:00
|
|
|
|
"已安装" if plugin_info[1].module in suc_plugin else "",
|
2024-08-24 14:49:55 +08:00
|
|
|
|
id,
|
|
|
|
|
|
plugin_info[0],
|
2024-09-02 01:50:39 +08:00
|
|
|
|
plugin_info[1].description,
|
|
|
|
|
|
plugin_info[1].author,
|
2024-08-24 20:24:05 +08:00
|
|
|
|
cls.version_check(plugin_info[1], suc_plugin),
|
2024-09-02 01:50:39 +08:00
|
|
|
|
plugin_info[1].plugin_type_name,
|
2024-08-24 14:49:55 +08:00
|
|
|
|
]
|
|
|
|
|
|
for id, plugin_info in filtered_data
|
|
|
|
|
|
]
|
2024-08-24 20:00:29 +08:00
|
|
|
|
if not data_list:
|
2024-08-24 19:07:06 +08:00
|
|
|
|
return "未找到相关插件..."
|
2024-08-29 22:01:34 +08:00
|
|
|
|
column_name = ["-", "ID", "名称", "简介", "作者", "版本", "类型"]
|
2024-08-24 14:49:55 +08:00
|
|
|
|
return await ImageTemplate.table_page(
|
|
|
|
|
|
"插件列表",
|
2024-08-29 22:01:34 +08:00
|
|
|
|
"通过添加/移除插件 ID 来管理插件",
|
2024-08-24 14:49:55 +08:00
|
|
|
|
column_name,
|
|
|
|
|
|
data_list,
|
|
|
|
|
|
text_style=row_style,
|
2024-08-24 19:07:06 +08:00
|
|
|
|
)
|
2024-08-24 20:24:05 +08:00
|
|
|
|
|
2024-08-24 19:07:06 +08:00
|
|
|
|
@classmethod
|
2025-01-09 10:29:49 +08:00
|
|
|
|
async def update_plugin(cls, plugin_id: str) -> str:
|
2024-08-24 20:24:05 +08:00
|
|
|
|
"""更新插件
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
plugin_id: 插件id
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 返回消息
|
|
|
|
|
|
"""
|
2024-09-29 20:47:58 +08:00
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
2024-10-02 18:32:21 +08:00
|
|
|
|
try:
|
|
|
|
|
|
plugin_key = await cls._resolve_plugin_key(plugin_id)
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
return str(e)
|
2024-09-02 01:50:39 +08:00
|
|
|
|
logger.info(f"尝试更新插件 {plugin_key}", "插件管理")
|
2024-08-24 19:07:06 +08:00
|
|
|
|
plugin_info = data[plugin_key]
|
2024-09-02 12:00:27 +08:00
|
|
|
|
plugin_list = await cls.get_loaded_plugins("module", "version")
|
2024-09-02 15:00:37 +08:00
|
|
|
|
suc_plugin = {p[0]: (p[1] or "Unknown") for p in plugin_list}
|
|
|
|
|
|
if plugin_info.module not in [p[0] for p in plugin_list]:
|
|
|
|
|
|
return f"插件 {plugin_key} 未安装,无法更新"
|
2024-09-02 01:50:39 +08:00
|
|
|
|
logger.debug(f"当前插件列表: {suc_plugin}", "插件管理")
|
|
|
|
|
|
if cls.check_version_is_new(plugin_info, suc_plugin):
|
|
|
|
|
|
return f"插件 {plugin_key} 已是最新版本"
|
|
|
|
|
|
is_external = True
|
|
|
|
|
|
if plugin_info.github_url is None:
|
|
|
|
|
|
plugin_info.github_url = DEFAULT_GITHUB_URL
|
|
|
|
|
|
is_external = False
|
|
|
|
|
|
await cls.install_plugin_with_repo(
|
|
|
|
|
|
plugin_info.github_url,
|
|
|
|
|
|
plugin_info.module_path,
|
|
|
|
|
|
plugin_info.is_dir,
|
|
|
|
|
|
is_external,
|
|
|
|
|
|
)
|
2024-08-26 10:39:33 +08:00
|
|
|
|
return f"插件 {plugin_key} 更新成功! 重启后生效"
|
2024-10-02 18:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def update_all_plugin(cls) -> str:
|
|
|
|
|
|
"""更新插件
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
plugin_id: 插件id
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 返回消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
|
|
|
|
|
plugin_list = list(data.keys())
|
2024-12-19 09:15:39 +08:00
|
|
|
|
update_failed_list = []
|
|
|
|
|
|
update_success_list = []
|
|
|
|
|
|
result = "--已更新{}个插件 {}个失败 {}个成功--"
|
2024-10-02 18:32:21 +08:00
|
|
|
|
logger.info(f"尝试更新全部插件 {plugin_list}", "插件管理")
|
|
|
|
|
|
for plugin_key in plugin_list:
|
2024-12-19 09:15:39 +08:00
|
|
|
|
try:
|
|
|
|
|
|
plugin_info = data[plugin_key]
|
|
|
|
|
|
plugin_list = await cls.get_loaded_plugins("module", "version")
|
|
|
|
|
|
suc_plugin = {p[0]: (p[1] or "Unknown") for p in plugin_list}
|
|
|
|
|
|
if plugin_info.module not in [p[0] for p in plugin_list]:
|
|
|
|
|
|
logger.debug(f"插件 {plugin_key} 未安装,跳过", "插件管理")
|
|
|
|
|
|
continue
|
|
|
|
|
|
if cls.check_version_is_new(plugin_info, suc_plugin):
|
|
|
|
|
|
logger.debug(f"插件 {plugin_key} 已是最新版本,跳过", "插件管理")
|
|
|
|
|
|
continue
|
|
|
|
|
|
logger.info(f"正在更新插件 {plugin_key}", "插件管理")
|
|
|
|
|
|
is_external = True
|
|
|
|
|
|
if plugin_info.github_url is None:
|
|
|
|
|
|
plugin_info.github_url = DEFAULT_GITHUB_URL
|
|
|
|
|
|
is_external = False
|
|
|
|
|
|
await cls.install_plugin_with_repo(
|
|
|
|
|
|
plugin_info.github_url,
|
|
|
|
|
|
plugin_info.module_path,
|
|
|
|
|
|
plugin_info.is_dir,
|
|
|
|
|
|
is_external,
|
|
|
|
|
|
)
|
|
|
|
|
|
update_success_list.append(plugin_key)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"更新插件 {plugin_key} 失败: {e}", "插件管理")
|
|
|
|
|
|
update_failed_list.append(plugin_key)
|
|
|
|
|
|
if not update_success_list and not update_failed_list:
|
2024-10-02 18:32:21 +08:00
|
|
|
|
return "全部插件已是最新版本"
|
2024-12-19 09:15:39 +08:00
|
|
|
|
if update_success_list:
|
|
|
|
|
|
result += "\n* 以下插件更新成功:\n\t- {}".format(
|
|
|
|
|
|
"\n\t- ".join(update_success_list)
|
|
|
|
|
|
)
|
|
|
|
|
|
if update_failed_list:
|
|
|
|
|
|
result += "\n* 以下插件更新失败:\n\t- {}".format(
|
|
|
|
|
|
"\n\t- ".join(update_failed_list)
|
|
|
|
|
|
)
|
|
|
|
|
|
return (
|
|
|
|
|
|
result.format(
|
|
|
|
|
|
len(update_success_list) + len(update_failed_list),
|
|
|
|
|
|
len(update_failed_list),
|
|
|
|
|
|
len(update_success_list),
|
|
|
|
|
|
)
|
|
|
|
|
|
+ "\n重启后生效"
|
2024-10-02 18:32:21 +08:00
|
|
|
|
)
|
2024-12-10 19:49:11 +08:00
|
|
|
|
|
2024-10-02 18:32:21 +08:00
|
|
|
|
@classmethod
|
2025-01-09 10:29:49 +08:00
|
|
|
|
async def _resolve_plugin_key(cls, plugin_id: str) -> str:
|
2024-10-02 18:32:21 +08:00
|
|
|
|
data: dict[str, StorePluginInfo] = await cls.get_data()
|
2025-01-09 10:29:49 +08:00
|
|
|
|
if is_number(plugin_id):
|
|
|
|
|
|
idx = int(plugin_id)
|
|
|
|
|
|
if idx < 0 or idx >= len(data):
|
2024-10-02 18:32:21 +08:00
|
|
|
|
raise ValueError("插件ID不存在...")
|
2025-01-09 10:29:49 +08:00
|
|
|
|
return list(data.keys())[idx]
|
2024-10-02 18:32:21 +08:00
|
|
|
|
elif isinstance(plugin_id, str):
|
|
|
|
|
|
if plugin_id not in [v.module for k, v in data.items()]:
|
|
|
|
|
|
raise ValueError("插件Module不存在...")
|
|
|
|
|
|
return {v.module: k for k, v in data.items()}[plugin_id]
|