File size: 8,187 Bytes
b4f2238 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | """
配置工具函数
提供便捷的配置访问和迁移辅助函数
"""
import logging
from typing import Any
from django.conf import settings
logger = logging.getLogger(__name__)
def get_config_value(key: str, default: Any | None = None, fallback_settings_key: str | None = None) -> Any:
"""
获取配置值的通用函数
优先从统一配置管理器获取,如果不可用则回退到 Django settings
Args:
key: 统一配置键(点号分隔)
default: 默认值
fallback_settings_key: 回退的 Django settings 键
Returns:
配置值
"""
# 尝试使用统一配置管理器
try:
if getattr(settings, "CONFIG_MANAGER_AVAILABLE", False):
get_unified_config = getattr(settings, "get_unified_config", None)
if get_unified_config:
value = get_unified_config(key, default)
if value is not None:
return value
except Exception as e:
logger.debug(f"从统一配置获取 {key} 失败: {e}")
# 回退到 Django settings
if fallback_settings_key:
return getattr(settings, fallback_settings_key, default)
return default
def get_nested_config_value(config_dict: dict[str, Any], key: str, default: Any = None) -> Any:
"""
从嵌套字典中获取配置值
Args:
config_dict: 配置字典
key: 配置键
default: 默认值
Returns:
配置值
"""
return config_dict.get(key, default)
def get_feishu_category_configs() -> dict[str, Any]:
"""
批量获取飞书分类配置
Returns:
飞书配置字典,key 为 DB 键名(如 FEISHU_APP_ID),value 为配置值
"""
try:
from apps.core.services.system_config_service import SystemConfigService
service = SystemConfigService()
result: dict[str, Any] = service.get_category_configs("feishu")
return result
except Exception as e:
logger.debug(f"从 SystemConfig 批量获取飞书配置失败: {e}")
return {}
def get_system_config_value(key: str, default: Any = None) -> Any:
"""
获取 SystemConfig 单个配置值
Args:
key: 配置键
default: 默认值
Returns:
配置值
"""
try:
from apps.core.services.system_config_service import SystemConfigService
service = SystemConfigService()
return service.get_value(key, default=default if default is not None else "")
except Exception as e:
logger.debug(f"从 SystemConfig 获取配置 {key} 失败: {e}")
return default
def get_feishu_config(key: str, default: Any = None) -> Any:
"""
获取飞书配置的便捷函数
Args:
key: 配置键(不包含前缀)
default: 默认值
Returns:
配置值
"""
unified_key = f"chat_platforms.feishu.{key}"
# 尝试统一配置
try:
if getattr(settings, "CONFIG_MANAGER_AVAILABLE", False):
get_unified_config = getattr(settings, "get_unified_config", None)
if get_unified_config:
value = get_unified_config(unified_key)
if value is not None:
return value
except Exception as e:
logger.debug(f"从统一配置获取飞书配置 {key} 失败: {e}")
# 回退到传统配置
feishu_config = getattr(settings, "FEISHU", {})
value = feishu_config.get(key.upper())
if value is not None:
return value
# 兼容旧配置
court_sms_config = getattr(settings, "COURT_SMS_PROCESSING", {})
old_key = f"FEISHU_{key.upper()}"
return court_sms_config.get(old_key, default)
def get_document_processing_config(key: str, default: Any = None) -> Any:
"""
获取文档处理配置的便捷函数
Args:
key: 配置键
default: 默认值
Returns:
配置值
"""
unified_key = f"features.document_processing.{key}"
# 尝试统一配置
try:
if getattr(settings, "CONFIG_MANAGER_AVAILABLE", False):
get_unified_config = getattr(settings, "get_unified_config", None)
if get_unified_config:
value = get_unified_config(unified_key)
if value is not None:
return value
except Exception as e:
logger.debug(f"从统一配置获取文档处理配置 {key} 失败: {e}")
# 回退到传统配置
doc_config = getattr(settings, "DOCUMENT_PROCESSING", {})
return doc_config.get(key.upper(), default)
def get_case_chat_config(key: str, default: Any = None) -> Any:
"""
获取案件群聊配置的便捷函数
Args:
key: 配置键
default: 默认值
Returns:
配置值
"""
unified_key = f"features.case_chat.{key}"
# 尝试统一配置
try:
if getattr(settings, "CONFIG_MANAGER_AVAILABLE", False):
get_unified_config = getattr(settings, "get_unified_config", None)
if get_unified_config:
value = get_unified_config(unified_key)
if value is not None:
return value
except Exception as e:
logger.debug(f"从统一配置获取案件群聊配置 {key} 失败: {e}")
# 回退到传统配置
case_chat_config = getattr(settings, "CASE_CHAT", {})
return case_chat_config.get(key.upper(), default)
def get_court_sms_config(key: str, default: Any = None) -> Any:
"""
获取法院短信配置的便捷函数
Args:
key: 配置键
default: 默认值
Returns:
配置值
"""
unified_key = f"features.court_sms.{key}"
# 尝试统一配置
try:
if getattr(settings, "CONFIG_MANAGER_AVAILABLE", False):
get_unified_config = getattr(settings, "get_unified_config", None)
if get_unified_config:
value = get_unified_config(unified_key)
if value is not None:
return value
except Exception as e:
logger.debug(f"从统一配置获取法院短信配置 {key} 失败: {e}")
# 回退到传统配置
court_sms_config = getattr(settings, "COURT_SMS_PROCESSING", {})
return court_sms_config.get(key.upper(), default)
def is_config_manager_available() -> bool:
"""
检查统一配置管理器是否可用
Returns:
bool: 是否可用
"""
return getattr(settings, "CONFIG_MANAGER_AVAILABLE", False)
def get_config_manager() -> Any:
"""
获取配置管理器实例
Returns:
ConfigManager 实例或 None
"""
if is_config_manager_available():
return getattr(settings, "UNIFIED_CONFIG_MANAGER", None)
return None
def register_config_change_listener(
listener: Any, key_filter: str | None = None, prefix_filter: str | None = None
) -> None:
"""
注册配置变更监听器
Args:
listener: 监听器实例
key_filter: 键过滤器
prefix_filter: 前缀过滤器
"""
config_manager = get_config_manager()
if config_manager:
config_manager.add_listener(listener, key_filter, prefix_filter)
else:
logger.warning("配置管理器不可用,无法注册监听器")
def migrate_legacy_config_access(legacy_settings_key: str, unified_config_key: str, default: Any = None) -> Any:
"""
迁移传统配置访问的辅助函数
Args:
legacy_settings_key: 传统 Django settings 键
unified_config_key: 统一配置键
default: 默认值
Returns:
配置值
"""
# 优先使用统一配置
if is_config_manager_available():
try:
get_unified_config = getattr(settings, "get_unified_config", None)
if get_unified_config:
value = get_unified_config(unified_config_key)
if value is not None:
return value
except Exception as e:
logger.debug(f"从统一配置获取 {unified_config_key} 失败: {e}")
# 回退到传统配置
return getattr(settings, legacy_settings_key, default)
|