@@ -56,68 +56,80 @@ def plugin_router_inject() -> None:
5656 if not os .path .exists (toml_path ):
5757 raise FileNotFoundError (f'插件 { plugin } 缺少 plugin.toml 配置文件,请检查插件是否合法' )
5858
59- # 解析 plugin.toml
59+ # 获取 plugin.toml 配置
6060 with open (toml_path , 'r' , encoding = 'utf-8' ) as f :
6161 data = rtoml .load (f )
62- app_name = data .get ('app' , '' )
63- prefix = data .get ('api' , {}).get ('prefix' , '' )
64- tags = data .get ('api' , {}).get ('tags' , [])
65-
66- # 插件中 API 路由文件的路径
67- plugin_api_path = os .path .join (PLUGIN_DIR , plugin , 'api' )
68- if not os .path .exists (plugin_api_path ):
69- raise FileNotFoundError (f'插件 { plugin } 缺少 api 目录,请检查插件文件是否完整' )
70-
71- # 路由注入
72- if app_name :
73- # 非独立应用:将插件路由注入到对应模块的路由中
62+ api = data .get ('api' , {})
63+
64+ # 非独立 app
65+ if api :
66+ app_include = data .get ('app' , {}).get ('include' , '' )
67+ if not app_include :
68+ raise ValueError (f'插件 { plugin } 配置文件存在错误,请检查' )
69+
70+ # 插件中 API 路由文件的路径
71+ plugin_api_path = os .path .join (PLUGIN_DIR , plugin , 'api' )
72+ if not os .path .exists (plugin_api_path ):
73+ raise FileNotFoundError (f'插件 { plugin } 缺少 api 目录,请检查插件文件是否完整' )
74+
75+ # 将插件路由注入到对应模块的路由中
7476 for root , _ , api_files in os .walk (plugin_api_path ):
7577 for file in api_files :
7678 if file .endswith ('.py' ) and file != '__init__.py' :
77- file_path = os .path .join (root , file )
79+ # 解析插件路由配置
80+ prefix = data .get ('api' , {}).get (f'{ file [:- 3 ]} ' , {}).get ('prefix' , '' )
81+ tags = data .get ('api' , {}).get (f'{ file [:- 3 ]} ' , {}).get ('tags' , [])
7882
7983 # 获取插件路由模块
84+ file_path = os .path .join (root , file )
8085 path_to_module_str = os .path .relpath (file_path , PLUGIN_DIR ).replace (os .sep , '.' )[:- 3 ]
8186 module_path = f'backend.plugin.{ path_to_module_str } '
8287 try :
8388 module = import_module_cached (module_path )
8489 except ImportError as e :
85- raise ImportError (f'导入模块 { module_path } 失败:{ e } ' ) from e
90+ raise ImportError (f'导入非独立 app 插件 { plugin } 模块 { module_path } 失败:{ e } ' ) from e
8691 plugin_router = getattr (module , 'router' , None )
8792 if not plugin_router :
8893 warnings .warn (
89- f'目标模块 { module_path } 中没有有效的 router,请检查插件文件是否完整' ,
94+ f'非独立 app 插件 { plugin } 模块 { module_path } 中没有有效的 router,'
95+ '请检查插件文件是否完整' ,
9096 FutureWarning ,
9197 )
9298 continue
9399
94100 # 获取源程序路由模块
95101 relative_path = os .path .relpath (root , plugin_api_path )
96- target_module_path = f'backend.app.{ app_name } .api.{ relative_path .replace (os .sep , "." )} '
102+ target_module_path = f'backend.app.{ app_include } .api.{ relative_path .replace (os .sep , "." )} '
97103 try :
98104 target_module = import_module_cached (target_module_path )
99105 except ImportError as e :
100- raise ImportError (f'导入目标模块 { target_module_path } 失败:{ e } ' ) from e
106+ raise ImportError (f'导入源程序模块 { target_module_path } 失败:{ e } ' ) from e
101107 target_router = getattr (target_module , 'router' , None )
102108 if not target_router or not isinstance (target_router , APIRouter ):
103- raise AttributeError (f'目标模块 { module_path } 中没有有效的 router,请检查插件文件是否完整' )
109+ raise AttributeError (
110+ f'非独立 app 插件 { plugin } 模块 { module_path } 中没有有效的 router,'
111+ '请检查插件文件是否完整'
112+ )
104113
105114 # 将插件路由注入到目标 router 中
106115 target_router .include_router (
107116 router = plugin_router ,
108117 prefix = prefix ,
109- tags = tags if tags == [] else [tags ],
118+ tags = [ tags ] if tags else [],
110119 )
120+ # 独立 app
111121 else :
112- # 独立应用: 将插件中的路由直接注入到总路由中
122+ # 将插件中的路由直接注入到总路由中
113123 module_path = f'backend.plugin.{ plugin } .api.router'
114124 try :
115125 module = import_module_cached (module_path )
116126 except ImportError as e :
117- raise ImportError (f'导入目标模块 { module_path } 失败:{ e } ' ) from e
127+ raise ImportError (f'导入独立 app 插件 { plugin } 模块 { module_path } 失败:{ e } ' ) from e
118128 plugin_router = getattr (module , 'router' , None )
119129 if not plugin_router or not isinstance (plugin_router , APIRouter ):
120- raise AttributeError (f'目标模块 { module_path } 中没有有效的 router,请检查插件文件是否完整' )
130+ raise AttributeError (
131+ f'独立 app 插件 { plugin } 模块 { module_path } 中没有有效的 router,请检查插件文件是否完整'
132+ )
121133 target_module_path = 'backend.app.router'
122134 target_module = import_module_cached (target_module_path )
123135 target_router = getattr (target_module , 'router' )
0 commit comments