fix typing, api change

This commit is contained in:
UnCLASPrommer
2025-07-15 00:57:43 +08:00
parent eae399fb95
commit af02f2ab57
5 changed files with 110 additions and 77 deletions

View File

@@ -26,7 +26,7 @@ def get_global_config(key: str, default: Any = None) -> Any:
插件应使用此方法读取全局配置,以保证只读和隔离性。
Args:
key: 配置键名,支持嵌套访问如 "section.subsection.key"
key: 命名空间式配置键名,支持嵌套访问"section.subsection.key",大小写敏感
default: 如果配置不存在时返回的默认值
Returns:
@@ -41,7 +41,7 @@ def get_global_config(key: str, default: Any = None) -> Any:
if hasattr(current, k):
current = getattr(current, k)
else:
return default
raise KeyError(f"配置中不存在子空间或键 '{k}'")
return current
except Exception as e:
logger.warning(f"[ConfigAPI] 获取全局配置 {key} 失败: {e}")
@@ -54,26 +54,28 @@ def get_plugin_config(plugin_config: dict, key: str, default: Any = None) -> Any
Args:
plugin_config: 插件配置字典
key: 配置键名,支持嵌套访问如 "section.subsection.key"
key: 配置键名,支持嵌套访问如 "section.subsection.key",大小写敏感
default: 如果配置不存在时返回的默认值
Returns:
Any: 配置值或默认值
"""
if not plugin_config:
return default
# 支持嵌套键访问
keys = key.split(".")
current = plugin_config
for k in keys:
if isinstance(current, dict) and k in current:
current = current[k]
else:
return default
return current
try:
for k in keys:
if isinstance(current, dict) and k in current:
current = current[k]
elif hasattr(current, k):
current = getattr(current, k)
else:
raise KeyError(f"配置中不存在子空间或键 '{k}'")
return current
except Exception as e:
logger.warning(f"[ConfigAPI] 获取插件配置 {key} 失败: {e}")
return default
# =============================================================================
@@ -82,7 +84,7 @@ def get_plugin_config(plugin_config: dict, key: str, default: Any = None) -> Any
async def get_user_id_by_person_name(person_name: str) -> tuple[str, str]:
"""根据用户名获取用户ID
"""根据内部用户名获取用户ID
Args:
person_name: 用户名
@@ -93,8 +95,8 @@ async def get_user_id_by_person_name(person_name: str) -> tuple[str, str]:
try:
person_info_manager = get_person_info_manager()
person_id = person_info_manager.get_person_id_by_person_name(person_name)
user_id = await person_info_manager.get_value(person_id, "user_id")
platform = await person_info_manager.get_value(person_id, "platform")
user_id: str = await person_info_manager.get_value(person_id, "user_id") # type: ignore
platform: str = await person_info_manager.get_value(person_id, "platform") # type: ignore
return platform, user_id
except Exception as e:
logger.error(f"[ConfigAPI] 根据用户名获取用户ID失败: {e}")
@@ -114,7 +116,10 @@ async def get_person_info(person_id: str, key: str, default: Any = None) -> Any:
"""
try:
person_info_manager = get_person_info_manager()
return await person_info_manager.get_value(person_id, key, default)
response = await person_info_manager.get_value(person_id, key)
if not response:
raise ValueError(f"[ConfigAPI] 获取用户 {person_id} 的信息 '{key}' 失败,返回默认值")
return response
except Exception as e:
logger.error(f"[ConfigAPI] 获取用户信息失败: {e}")
return default