简道云V2.0
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
"""
|
||||
模块注册和路由管理
|
||||
提供统一的模块注册机制,方便添加新功能模块
|
||||
模块注册表模块
|
||||
|
||||
本模块提供统一的模块注册机制,用于管理所有业务模块和操作。
|
||||
使用注册表模式可以:
|
||||
- 统一管理所有操作
|
||||
- 方便添加新功能模块
|
||||
- 支持动态路由和操作查找
|
||||
- 提供操作元数据管理
|
||||
"""
|
||||
from typing import Dict, Callable, Optional, Any
|
||||
from dataclasses import dataclass
|
||||
@@ -8,7 +14,18 @@ from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class ActionConfig:
|
||||
"""操作配置"""
|
||||
"""
|
||||
操作配置数据类
|
||||
|
||||
存储操作的配置信息,包括处理函数、所属模块、描述等。
|
||||
|
||||
属性:
|
||||
handler: 处理函数,执行具体业务逻辑
|
||||
module_name: 所属模块名称
|
||||
description: 操作描述,用于文档和日志
|
||||
requires_auth: 是否需要认证,默认 True
|
||||
header_module: 使用的请求头模块名称,可选
|
||||
"""
|
||||
handler: Callable # 处理函数
|
||||
module_name: str # 所属模块名称
|
||||
description: Optional[str] = None # 描述
|
||||
@@ -17,9 +34,19 @@ class ActionConfig:
|
||||
|
||||
|
||||
class ModuleRegistry:
|
||||
"""模块注册表"""
|
||||
"""
|
||||
模块注册表类
|
||||
|
||||
统一管理所有业务模块和操作的注册表。
|
||||
支持注册模块实例和操作,并提供查询功能。
|
||||
|
||||
属性:
|
||||
_actions: 操作字典,格式为 {操作名: ActionConfig}
|
||||
_modules: 模块字典,格式为 {模块名: 模块信息}
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化模块注册表"""
|
||||
self._actions: Dict[str, ActionConfig] = {}
|
||||
self._modules: Dict[str, Dict[str, Any]] = {}
|
||||
|
||||
@@ -65,7 +92,12 @@ class ModuleRegistry:
|
||||
return self._actions.get(action_name)
|
||||
|
||||
def get_all_actions(self) -> Dict[str, ActionConfig]:
|
||||
"""获取所有注册的操作"""
|
||||
"""
|
||||
获取所有注册的操作
|
||||
|
||||
Returns:
|
||||
Dict[str, ActionConfig]: 所有注册的操作字典的副本
|
||||
"""
|
||||
return self._actions.copy()
|
||||
|
||||
def register_module(self, module_name: str, module_instance: Any, **metadata):
|
||||
@@ -113,4 +145,5 @@ class ModuleRegistry:
|
||||
|
||||
|
||||
# 全局模块注册表实例
|
||||
# 在应用启动时使用此实例来注册所有模块和操作
|
||||
registry = ModuleRegistry()
|
||||
|
||||
Reference in New Issue
Block a user