fix 插件重载找不到__init__.py的场景及部分细节调整

This commit is contained in:
InfinityPacer 2024-06-10 09:45:56 +08:00
parent 3cdbdc2f78
commit fcbdef5e66

View File

@ -1,11 +1,11 @@
import concurrent import concurrent
import concurrent.futures import concurrent.futures
import inspect import inspect
import os
import threading import threading
import time import time
import traceback import traceback
from typing import List, Any, Dict, Tuple, Optional, Callable from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer from watchdog.observers import Observer
@ -13,8 +13,8 @@ from watchdog.observers import Observer
from app import schemas from app import schemas
from app.core.config import settings from app.core.config import settings
from app.core.event import eventmanager from app.core.event import eventmanager
from app.db.systemconfig_oper import SystemConfigOper
from app.db.plugindata_oper import PluginDataOper from app.db.plugindata_oper import PluginDataOper
from app.db.systemconfig_oper import SystemConfigOper
from app.helper.module import ModuleHelper from app.helper.module import ModuleHelper
from app.helper.plugin import PluginHelper from app.helper.plugin import PluginHelper
from app.helper.sites import SitesHelper from app.helper.sites import SitesHelper
@ -42,21 +42,35 @@ class PluginMonitorHandler(FileSystemEventHandler):
""" """
if event.is_directory: if event.is_directory:
return return
# 使用 pathlib 处理文件路径,跳过非 .py 文件以及 pycache 目录中的文件
event_path = Path(event.src_path)
if not event_path.name.endswith(".py") or "pycache" in event_path.parts:
return
current_time = time.time() current_time = time.time()
if current_time - self.__last_modified < self.__timeout: if current_time - self.__last_modified < self.__timeout:
return return
self.__last_modified = current_time self.__last_modified = current_time
# 读取插件根目录下的__init__.py文件读取class XXXX(_PluginBase)的类名 # 读取插件根目录下的__init__.py文件读取class XXXX(_PluginBase)的类名
try: try:
# 使用os.path和pathlib处理跨平台的路径问题 plugins_root = settings.ROOT_PATH / "app" / "plugins"
plugin_dir = event.src_path.split("plugins" + os.sep)[1].split(os.sep)[0] # 确保修改的文件在 plugins 目录下
init_file = settings.ROOT_PATH / "app" / "plugins" / plugin_dir / "__init__.py" if plugins_root not in event_path.parents:
return
# 获取插件目录路径没有找到__init__.py时说明不是有效包跳过插件重载
# 插件重载目前没有支持app/plugins/plugin/package/__init__.py的场景这里也不做支持
plugin_dir = event_path.parent
init_file = plugin_dir / "__init__.py"
if not init_file.exists():
logger.debug(f"{plugin_dir} 下没有找到 __init__.py跳过插件重载")
return
with open(init_file, "r", encoding="utf-8") as f: with open(init_file, "r", encoding="utf-8") as f:
lines = f.readlines() lines = f.readlines()
pid = None pid = None
for line in lines: for line in lines:
if line.startswith("class") and "(_PluginBase)" in line: if line.startswith("class") and "(_PluginBase)" in line:
pid = line.split("class ")[1].split("(_PluginBase)")[0] pid = line.split("class ")[1].split("(_PluginBase)")[0].strip()
if pid: if pid:
# 防抖处理,通过计时器延迟加载 # 防抖处理,通过计时器延迟加载
if self.__reload_timer: if self.__reload_timer: