把 douyin/reservation/douyin_reservation_logic.py 按职责拆成 douyin/reservation/reservation_logic/ 子包: - _common (57) 共享错误处理工具占位 + ReservationItem 类型 - helpers (130) 内部工具:标题归一化 / 相似度 / 排序 / 时间戳 - add (447) add_reservation_core_logic 主入口(添加新预约) - cleanup_validate (136) validate_back_to_reservation / cleanup_expired_reservations - queue_full (305) handle_queue_full_wait_at_earliest 队列满时等待策略 - attend_window (1437) process_reservation_attend_window 进入预约窗口(最大头) - record (328) record_high_value_reservation 高价值预约登记入口 原文件改为 53 行 re-export shim,17 个公共符号全部保留。 - process_reservation_attend_window 7 处外部引用 - handle_queue_full_wait_at_earliest 5 处外部引用 - _now_ts 5 处 / cleanup_expired_reservations 4 处 / _sort_reservations_inplace 4 处 - 其他 5 处以下 全部不需修改。 回归: - 8 个新文件 + shim compileall 通过 - pytest 353 passed / 1 skipped - 旧 import 路径与新子包路径返回同一函数对象 - _now_ts / _normalize_room_title / _calculate_string_similarity 等纯函数 调用结果与拆分前一致 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
"""reservation_logic._common - 共享类型与错误处理工具。
|
|
|
|
被 add / cleanup_validate / queue_full / attend_window / record 各子模块共用。
|
|
- log_reservation_error / safe_write_state / format_reservation_context /
|
|
validate_reservation_item:从 douyin.reservation.core.reservation_error_utils 导入;
|
|
导入失败时降级为占位实现(参数宽松,调用 OK 但什么都不做)。
|
|
- ReservationItem:单条预约的字典类型别名。
|
|
- _RESERVATION_ERROR_UTILS_AVAILABLE:标记真实工具是否成功导入。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from douyin.reservation.core.reservation_error_utils import ( # pragma: no cover
|
|
log_reservation_error,
|
|
safe_write_state,
|
|
format_reservation_context,
|
|
validate_reservation_item,
|
|
)
|
|
|
|
_RESERVATION_ERROR_UTILS_AVAILABLE = True
|
|
else:
|
|
try:
|
|
from douyin.reservation.core.reservation_error_utils import ( # type: ignore[import]
|
|
log_reservation_error,
|
|
safe_write_state,
|
|
format_reservation_context,
|
|
validate_reservation_item,
|
|
)
|
|
|
|
_RESERVATION_ERROR_UTILS_AVAILABLE = True
|
|
except Exception:
|
|
# 运行时回退到通用占位实现(使用宽松签名以兼容各种调用)
|
|
def log_reservation_error(*args, **kwargs) -> None:
|
|
return None
|
|
|
|
def safe_write_state(*args, **kwargs) -> bool:
|
|
return True
|
|
|
|
def format_reservation_context(*args, **kwargs) -> Dict[str, Any]:
|
|
return {}
|
|
|
|
def validate_reservation_item(*args, **kwargs) -> bool:
|
|
return True
|
|
|
|
_RESERVATION_ERROR_UTILS_AVAILABLE = False
|
|
|
|
# 为兼容类型检查,将实际导入或占位函数转换为 Any(避免赋值/导入时的类型不匹配报错)
|
|
from typing import Any as _Any, cast
|
|
|
|
log_reservation_error = cast(_Any, log_reservation_error)
|
|
safe_write_state = cast(_Any, safe_write_state)
|
|
format_reservation_context = cast(_Any, format_reservation_context)
|
|
validate_reservation_item = cast(_Any, validate_reservation_item)
|
|
|
|
ReservationItem = Dict[str, Any]
|