plugins sys 修改,复用plugin_base(原base_plugin)

This commit is contained in:
UnCLAS-Prommer
2025-07-16 18:02:42 +08:00
parent e533fec8f6
commit 30b35357d4
7 changed files with 729 additions and 700 deletions

View File

@@ -6,14 +6,14 @@
from .manifest_utils import (
ManifestValidator,
ManifestGenerator,
validate_plugin_manifest,
generate_plugin_manifest,
# ManifestGenerator,
# validate_plugin_manifest,
# generate_plugin_manifest,
)
__all__ = [
"ManifestValidator",
"ManifestGenerator",
"validate_plugin_manifest",
"generate_plugin_manifest",
# "ManifestGenerator",
# "validate_plugin_manifest",
# "generate_plugin_manifest",
]

View File

@@ -7,10 +7,13 @@
import json
import os
import re
from typing import Dict, Any, Optional, Tuple
from typing import Dict, Any, Optional, Tuple, TYPE_CHECKING
from src.common.logger import get_logger
from src.config.config import MMC_VERSION
# if TYPE_CHECKING:
# from src.plugin_system.base.base_plugin import BasePlugin
logger = get_logger("manifest_utils")
@@ -371,149 +374,149 @@ class ManifestValidator:
return "\n".join(report)
class ManifestGenerator:
"""Manifest文件生成器"""
# class ManifestGenerator:
# """Manifest文件生成器"""
def __init__(self):
self.template = {
"manifest_version": 1,
"name": "",
"version": "1.0.0",
"description": "",
"author": {"name": "", "url": ""},
"license": "MIT",
"host_application": {"min_version": "1.0.0", "max_version": "4.0.0"},
"homepage_url": "",
"repository_url": "",
"keywords": [],
"categories": [],
"default_locale": "zh-CN",
"locales_path": "_locales",
}
# def __init__(self):
# self.template = {
# "manifest_version": 1,
# "name": "",
# "version": "1.0.0",
# "description": "",
# "author": {"name": "", "url": ""},
# "license": "MIT",
# "host_application": {"min_version": "1.0.0", "max_version": "4.0.0"},
# "homepage_url": "",
# "repository_url": "",
# "keywords": [],
# "categories": [],
# "default_locale": "zh-CN",
# "locales_path": "_locales",
# }
def generate_from_plugin(self, plugin_instance) -> Dict[str, Any]:
"""从插件实例生成manifest
# def generate_from_plugin(self, plugin_instance: BasePlugin) -> Dict[str, Any]:
# """从插件实例生成manifest
Args:
plugin_instance: BasePlugin实例
# Args:
# plugin_instance: BasePlugin实例
Returns:
Dict[str, Any]: 生成的manifest数据
"""
manifest = self.template.copy()
# Returns:
# Dict[str, Any]: 生成的manifest数据
# """
# manifest = self.template.copy()
# 基本信息
manifest["name"] = plugin_instance.plugin_name
manifest["version"] = plugin_instance.plugin_version
manifest["description"] = plugin_instance.plugin_description
# # 基本信息
# manifest["name"] = plugin_instance.plugin_name
# manifest["version"] = plugin_instance.plugin_version
# manifest["description"] = plugin_instance.plugin_description
# 作者信息
if plugin_instance.plugin_author:
manifest["author"]["name"] = plugin_instance.plugin_author
# # 作者信息
# if plugin_instance.plugin_author:
# manifest["author"]["name"] = plugin_instance.plugin_author
# 组件信息
components = []
plugin_components = plugin_instance.get_plugin_components()
# # 组件信息
# components = []
# plugin_components = plugin_instance.get_plugin_components()
for component_info, component_class in plugin_components:
component_data = {
"type": component_info.component_type.value,
"name": component_info.name,
"description": component_info.description,
}
# for component_info, component_class in plugin_components:
# component_data: Dict[str, Any] = {
# "type": component_info.component_type.value,
# "name": component_info.name,
# "description": component_info.description,
# }
# 添加激活模式信息对于Action组件
if hasattr(component_class, "focus_activation_type"):
activation_modes = []
if hasattr(component_class, "focus_activation_type"):
activation_modes.append(component_class.focus_activation_type.value)
if hasattr(component_class, "normal_activation_type"):
activation_modes.append(component_class.normal_activation_type.value)
component_data["activation_modes"] = list(set(activation_modes))
# # 添加激活模式信息对于Action组件
# if hasattr(component_class, "focus_activation_type"):
# activation_modes = []
# if hasattr(component_class, "focus_activation_type"):
# activation_modes.append(component_class.focus_activation_type.value)
# if hasattr(component_class, "normal_activation_type"):
# activation_modes.append(component_class.normal_activation_type.value)
# component_data["activation_modes"] = list(set(activation_modes))
# 添加关键词信息
if hasattr(component_class, "activation_keywords"):
keywords = getattr(component_class, "activation_keywords", [])
if keywords:
component_data["keywords"] = keywords
# # 添加关键词信息
# if hasattr(component_class, "activation_keywords"):
# keywords = getattr(component_class, "activation_keywords", [])
# if keywords:
# component_data["keywords"] = keywords
components.append(component_data)
# components.append(component_data)
manifest["plugin_info"] = {"is_built_in": True, "plugin_type": "general", "components": components}
# manifest["plugin_info"] = {"is_built_in": True, "plugin_type": "general", "components": components}
return manifest
# return manifest
def save_manifest(self, manifest_data: Dict[str, Any], plugin_dir: str) -> bool:
"""保存manifest文件
# def save_manifest(self, manifest_data: Dict[str, Any], plugin_dir: str) -> bool:
# """保存manifest文件
Args:
manifest_data: manifest数据
plugin_dir: 插件目录
# Args:
# manifest_data: manifest数据
# plugin_dir: 插件目录
Returns:
bool: 是否保存成功
"""
try:
manifest_path = os.path.join(plugin_dir, "_manifest.json")
with open(manifest_path, "w", encoding="utf-8") as f:
json.dump(manifest_data, f, ensure_ascii=False, indent=2)
logger.info(f"Manifest文件已保存: {manifest_path}")
return True
except Exception as e:
logger.error(f"保存manifest文件失败: {e}")
return False
# Returns:
# bool: 是否保存成功
# """
# try:
# manifest_path = os.path.join(plugin_dir, "_manifest.json")
# with open(manifest_path, "w", encoding="utf-8") as f:
# json.dump(manifest_data, f, ensure_ascii=False, indent=2)
# logger.info(f"Manifest文件已保存: {manifest_path}")
# return True
# except Exception as e:
# logger.error(f"保存manifest文件失败: {e}")
# return False
def validate_plugin_manifest(plugin_dir: str) -> bool:
"""验证插件目录中的manifest文件
# def validate_plugin_manifest(plugin_dir: str) -> bool:
# """验证插件目录中的manifest文件
Args:
plugin_dir: 插件目录路径
# Args:
# plugin_dir: 插件目录路径
Returns:
bool: 是否验证通过
"""
manifest_path = os.path.join(plugin_dir, "_manifest.json")
# Returns:
# bool: 是否验证通过
# """
# manifest_path = os.path.join(plugin_dir, "_manifest.json")
if not os.path.exists(manifest_path):
logger.warning(f"未找到manifest文件: {manifest_path}")
return False
# if not os.path.exists(manifest_path):
# logger.warning(f"未找到manifest文件: {manifest_path}")
# return False
try:
with open(manifest_path, "r", encoding="utf-8") as f:
manifest_data = json.load(f)
# try:
# with open(manifest_path, "r", encoding="utf-8") as f:
# manifest_data = json.load(f)
validator = ManifestValidator()
is_valid = validator.validate_manifest(manifest_data)
# validator = ManifestValidator()
# is_valid = validator.validate_manifest(manifest_data)
logger.info(f"Manifest验证结果:\n{validator.get_validation_report()}")
# logger.info(f"Manifest验证结果:\n{validator.get_validation_report()}")
return is_valid
# return is_valid
except Exception as e:
logger.error(f"读取或验证manifest文件失败: {e}")
return False
# except Exception as e:
# logger.error(f"读取或验证manifest文件失败: {e}")
# return False
def generate_plugin_manifest(plugin_instance, save_to_file: bool = True) -> Optional[Dict[str, Any]]:
"""为插件生成manifest文件
# def generate_plugin_manifest(plugin_instance: BasePlugin, save_to_file: bool = True) -> Optional[Dict[str, Any]]:
# """为插件生成manifest文件
Args:
plugin_instance: BasePlugin实例
save_to_file: 是否保存到文件
# Args:
# plugin_instance: BasePlugin实例
# save_to_file: 是否保存到文件
Returns:
Optional[Dict[str, Any]]: 生成的manifest数据
"""
try:
generator = ManifestGenerator()
manifest_data = generator.generate_from_plugin(plugin_instance)
# Returns:
# Optional[Dict[str, Any]]: 生成的manifest数据
# """
# try:
# generator = ManifestGenerator()
# manifest_data = generator.generate_from_plugin(plugin_instance)
if save_to_file and plugin_instance.plugin_dir:
generator.save_manifest(manifest_data, plugin_instance.plugin_dir)
# if save_to_file and plugin_instance.plugin_dir:
# generator.save_manifest(manifest_data, plugin_instance.plugin_dir)
return manifest_data
# return manifest_data
except Exception as e:
logger.error(f"生成manifest文件失败: {e}")
return None
# except Exception as e:
# logger.error(f"生成manifest文件失败: {e}")
# return None