1717from backend .utils .import_parse import import_module_cached
1818
1919
20+ class PluginInjectError (Exception ):
21+ pass
22+
23+
2024def get_plugins () -> list [str ]:
2125 """获取插件"""
2226 plugin_packages = []
@@ -54,7 +58,7 @@ def plugin_router_inject() -> None:
5458 for plugin in plugins :
5559 toml_path = os .path .join (PLUGIN_DIR , plugin , 'plugin.toml' )
5660 if not os .path .exists (toml_path ):
57- raise FileNotFoundError (f'插件 { plugin } 缺少 plugin.toml 配置文件,请检查插件是否合法' )
61+ raise PluginInjectError (f'插件 { plugin } 缺少 plugin.toml 配置文件,请检查插件是否合法' )
5862
5963 # 获取 plugin.toml 配置
6064 with open (toml_path , 'r' , encoding = 'utf-8' ) as f :
@@ -65,12 +69,12 @@ def plugin_router_inject() -> None:
6569 if api :
6670 app_include = data .get ('app' , {}).get ('include' , '' )
6771 if not app_include :
68- raise ValueError (f'插件 { plugin } 配置文件存在错误,请检查' )
72+ raise PluginInjectError (f'非独立 app 插件 { plugin } 配置文件存在错误,请检查' )
6973
7074 # 插件中 API 路由文件的路径
7175 plugin_api_path = os .path .join (PLUGIN_DIR , plugin , 'api' )
7276 if not os .path .exists (plugin_api_path ):
73- raise FileNotFoundError (f'插件 { plugin } 缺少 api 目录,请检查插件文件是否完整' )
77+ raise PluginInjectError (f'插件 { plugin } 缺少 api 目录,请检查插件文件是否完整' )
7478
7579 # 将插件路由注入到对应模块的路由中
7680 for root , _ , api_files in os .walk (plugin_api_path ):
@@ -86,8 +90,8 @@ def plugin_router_inject() -> None:
8690 module_path = f'backend.plugin.{ path_to_module_str } '
8791 try :
8892 module = import_module_cached (module_path )
89- except ImportError as e :
90- raise ImportError (f'导入非独立 app 插件 { plugin } 模块 { module_path } 失败:{ e } ' ) from e
93+ except PluginInjectError as e :
94+ raise PluginInjectError (f'导入非独立 app 插件 { plugin } 模块 { module_path } 失败:{ e } ' ) from e
9195 plugin_router = getattr (module , 'router' , None )
9296 if not plugin_router :
9397 warnings .warn (
@@ -102,11 +106,11 @@ def plugin_router_inject() -> None:
102106 target_module_path = f'backend.app.{ app_include } .api.{ relative_path .replace (os .sep , "." )} '
103107 try :
104108 target_module = import_module_cached (target_module_path )
105- except ImportError as e :
106- raise ImportError (f'导入源程序模块 { target_module_path } 失败:{ e } ' ) from e
109+ except PluginInjectError as e :
110+ raise PluginInjectError (f'导入源程序模块 { target_module_path } 失败:{ e } ' ) from e
107111 target_router = getattr (target_module , 'router' , None )
108112 if not target_router or not isinstance (target_router , APIRouter ):
109- raise AttributeError (
113+ raise PluginInjectError (
110114 f'非独立 app 插件 { plugin } 模块 { module_path } 中没有有效的 router,'
111115 '请检查插件文件是否完整'
112116 )
@@ -123,19 +127,23 @@ def plugin_router_inject() -> None:
123127 module_path = f'backend.plugin.{ plugin } .api.router'
124128 try :
125129 module = import_module_cached (module_path )
126- except ImportError as e :
127- raise ImportError (f'导入独立 app 插件 { plugin } 模块 { module_path } 失败:{ e } ' ) from e
128- plugin_router = getattr (module , 'router' , None )
129- if not plugin_router or not isinstance (plugin_router , APIRouter ):
130- raise AttributeError (
131- f'独立 app 插件 { plugin } 模块 { module_path } 中没有有效的 router,请检查插件文件是否完整'
132- )
133- target_module_path = 'backend.app.router'
134- target_module = import_module_cached (target_module_path )
135- target_router = getattr (target_module , 'router' )
136-
137- # 将插件路由注入到目标 router 中
138- target_router .include_router (plugin_router )
130+ except PluginInjectError as e :
131+ raise PluginInjectError (f'导入独立 app 插件 { plugin } 模块 { module_path } 失败:{ e } ' ) from e
132+ routers = data .get ('app' , {}).get ('router' , [])
133+ if not routers or not isinstance (routers , list ):
134+ raise PluginInjectError (f'独立 app 插件 { plugin } 配置文件存在错误,请检查' )
135+ for router in routers :
136+ plugin_router = getattr (module , router , None )
137+ if not plugin_router or not isinstance (plugin_router , APIRouter ):
138+ raise PluginInjectError (
139+ f'独立 app 插件 { plugin } 模块 { module_path } 中没有有效的 router,请检查插件文件是否完整'
140+ )
141+ target_module_path = 'backend.app.router'
142+ target_module = import_module_cached (target_module_path )
143+ target_router = getattr (target_module , 'router' )
144+
145+ # 将插件路由注入到目标 router 中
146+ target_router .include_router (plugin_router )
139147
140148
141149def install_requirements () -> None :
@@ -150,7 +158,7 @@ def install_requirements() -> None:
150158 subprocess .run ([sys .executable , '-m' , 'ensurepip' , '--upgrade' ])
151159 subprocess .check_call ([sys .executable , '-m' , 'pip' , 'install' , '-r' , requirements_file ])
152160 except subprocess .CalledProcessError as e :
153- raise EnvironmentError (f'插件 { plugin } 依赖安装失败:{ e } ' ) from e
161+ raise PluginInjectError (f'插件 { plugin } 依赖安装失败:{ e } ' ) from e
154162
155163
156164async def install_requirements_async () -> None :
@@ -174,4 +182,4 @@ async def install_requirements_async() -> None:
174182 )
175183 _ , stderr = await res .communicate ()
176184 if res .returncode != 0 :
177- raise EnvironmentError (f'插件 { plugin } 依赖包安装失败:{ stderr } ' )
185+ raise PluginInjectError (f'插件 { plugin } 依赖包安装失败:{ stderr } ' )
0 commit comments