11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
3- import importlib
43import inspect
54import os
65import warnings
109from fastapi import APIRouter
1110
1211from backend .core .path_conf import PLUGIN_DIR
12+ from backend .utils .import_parse import import_module_cached
1313
1414
1515def get_plugins () -> list [str ]:
@@ -32,19 +32,18 @@ def get_plugin_models() -> list:
3232 plugins = get_plugins ()
3333 for plugin in plugins :
3434 module_path = f'backend.plugin.{ plugin } .model'
35- module = importlib . import_module (module_path )
35+ module = import_module_cached (module_path )
3636 for name , obj in inspect .getmembers (module ):
3737 if inspect .isclass (obj ):
3838 classes .append (obj )
3939 return classes
4040
4141
42- def plugin_router_inject (router : APIRouter ) -> APIRouter :
42+ def plugin_router_inject () -> None :
4343 """
4444 插件路由注入
4545
46- :param router: 源总路由器
47- :return: 注入插件路由后的总路由器
46+ :return:
4847 """
4948 plugins = get_plugins ()
5049 for plugin in plugins :
@@ -66,17 +65,17 @@ def plugin_router_inject(router: APIRouter) -> APIRouter:
6665
6766 # 路由注入
6867 if app_name :
69- # 非独立应用:将插件中的路由注入到源程序对应模块的路由中
68+ # 非独立应用:将插件路由注入到对应模块的路由中
7069 for root , _ , api_files in os .walk (plugin_api_path ):
7170 for file in api_files :
7271 if file .endswith ('.py' ) and file != '__init__.py' :
73- api_files_path = os .path .join (root , file )
72+ file_path = os .path .join (root , file )
7473
7574 # 获取插件路由模块
76- path_to_module_str = os .path .relpath (api_files_path , PLUGIN_DIR ).replace (os .sep , '.' )[:- 3 ]
75+ path_to_module_str = os .path .relpath (file_path , PLUGIN_DIR ).replace (os .sep , '.' )[:- 3 ]
7776 module_path = f'backend.plugin.{ path_to_module_str } '
7877 try :
79- module = importlib . import_module (module_path )
78+ module = import_module_cached (module_path )
8079 except ImportError as e :
8180 raise ImportError (f'导入模块 { module_path } 失败:{ e } ' ) from e
8281 plugin_router = getattr (module , 'router' , None )
@@ -91,7 +90,7 @@ def plugin_router_inject(router: APIRouter) -> APIRouter:
9190 relative_path = os .path .relpath (root , plugin_api_path )
9291 target_module_path = f'backend.app.{ app_name } .api.{ relative_path .replace (os .sep , "." )} '
9392 try :
94- target_module = importlib . import_module (target_module_path )
93+ target_module = import_module_cached (target_module_path )
9594 except ImportError as e :
9695 raise ImportError (f'导入目标模块 { target_module_path } 失败:{ e } ' ) from e
9796 target_router = getattr (target_module , 'router' , None )
@@ -102,18 +101,21 @@ def plugin_router_inject(router: APIRouter) -> APIRouter:
102101 target_router .include_router (
103102 router = plugin_router ,
104103 prefix = prefix ,
105- tags = tags if type ( tags ) is list else [tags ],
104+ tags = tags if tags == [] else [tags ],
106105 )
107106 else :
108- # 独立应用:将插件中的路由直接注入到 app 中
107+ # 独立应用:将插件中的路由直接注入到总路由中
109108 module_path = f'backend.plugin.{ plugin } .api.router'
110109 try :
111- target_module = importlib . import_module (module_path )
110+ module = import_module_cached (module_path )
112111 except ImportError as e :
113112 raise ImportError (f'导入目标模块 { module_path } 失败:{ e } ' ) from e
114- target_router = getattr (target_module , 'router' , None )
115- if not target_router or not isinstance (target_router , APIRouter ):
113+ plugin_router = getattr (module , 'router' , None )
114+ if not plugin_router or not isinstance (plugin_router , APIRouter ):
116115 raise AttributeError (f'目标模块 { module_path } 中没有有效的 router,请检查插件文件是否完整' )
117- router .include_router (target_router )
116+ target_module_path = 'backend.app.router'
117+ target_module = import_module_cached (target_module_path )
118+ target_router = getattr (target_module , 'router' )
118119
119- return router
120+ # 将插件路由注入到目标 router 中
121+ target_router .include_router (plugin_router )
0 commit comments