mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-01 21:07:01 +08:00
refactor(types): govern filesystem worker request and response contracts
This commit is contained in:
@@ -23,10 +23,64 @@ import shutil
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict
|
||||
from typing import Callable, Required, TypedDict
|
||||
|
||||
|
||||
def _stat(payload, _emit):
|
||||
class _Request(TypedDict, total=False):
|
||||
"""父进程发送的操作名称与各操作参数,不改变坏请求的异常处理。"""
|
||||
|
||||
op: Required[str]
|
||||
path: str
|
||||
src: str
|
||||
dst: str
|
||||
chunk_size: int
|
||||
progress_interval: float
|
||||
max_check: int
|
||||
extensions: list[str]
|
||||
|
||||
|
||||
class _StatResult(TypedDict):
|
||||
"""路径属性及删除前核对的文件身份。"""
|
||||
|
||||
size: int
|
||||
mtime: float
|
||||
dev: int
|
||||
ino: int
|
||||
is_dir: bool
|
||||
is_file: bool
|
||||
|
||||
|
||||
class _CopyProgress(TypedDict):
|
||||
"""复制心跳和最终结果共享的字节进度。"""
|
||||
|
||||
copied: int
|
||||
total: int
|
||||
|
||||
|
||||
class _EntryCounts(TypedDict):
|
||||
"""递归扫描已经观察到的文件与目录数量。"""
|
||||
|
||||
file_count: int
|
||||
dir_count: int
|
||||
|
||||
|
||||
_Result = bool | str | list[str] | _StatResult | _CopyProgress | _EntryCounts
|
||||
|
||||
|
||||
class _Response(TypedDict, total=False):
|
||||
"""逐行输出的心跳、成功结果或失败信息。"""
|
||||
|
||||
ok: Required[bool]
|
||||
progress: _CopyProgress
|
||||
result: _Result
|
||||
errno: int
|
||||
error: str
|
||||
|
||||
|
||||
_Emit = Callable[[_Response], None]
|
||||
|
||||
|
||||
def _stat(payload: _Request, _emit: _Emit) -> _StatResult:
|
||||
"""
|
||||
读取路径属性及设备、inode 身份,供删除前确认仍是同一个文件。
|
||||
"""
|
||||
@@ -42,7 +96,7 @@ def _stat(payload, _emit):
|
||||
}
|
||||
|
||||
|
||||
def _exists(payload, _emit):
|
||||
def _exists(payload: _Request, _emit: _Emit) -> bool:
|
||||
"""
|
||||
判断路径是否存在。
|
||||
|
||||
@@ -53,19 +107,19 @@ def _exists(payload, _emit):
|
||||
return True
|
||||
|
||||
|
||||
def _realpath(payload: Dict[str, Any], _emit: Callable[[Dict[str, Any]], None]) -> str:
|
||||
def _realpath(payload: _Request, _emit: _Emit) -> str:
|
||||
"""解析已有父目录的符号链接,目标末段可尚不存在;异常交由代理保守处理。"""
|
||||
return str(Path(payload["path"]).resolve())
|
||||
|
||||
|
||||
def _listdir(payload, _emit):
|
||||
def _listdir(payload: _Request, _emit: _Emit) -> list[str]:
|
||||
"""
|
||||
列出目录下的条目名。
|
||||
"""
|
||||
return sorted(os.listdir(payload["path"]))
|
||||
|
||||
|
||||
def _copy(payload, emit):
|
||||
def _copy(payload: _Request, emit: _Emit) -> _CopyProgress:
|
||||
"""
|
||||
分块复制文件内容并周期上报进度。
|
||||
|
||||
@@ -103,7 +157,7 @@ def _copy(payload, emit):
|
||||
return {"copied": copied, "total": total}
|
||||
|
||||
|
||||
def _count_entries(payload, _emit):
|
||||
def _count_entries(payload: _Request, _emit: _Emit) -> _EntryCounts:
|
||||
"""
|
||||
统计目录下的文件与子目录数量,超过上限即提前结束。
|
||||
|
||||
@@ -123,7 +177,7 @@ def _count_entries(payload, _emit):
|
||||
return {"file_count": file_count, "dir_count": dir_count}
|
||||
|
||||
|
||||
def _has_file_suffix(payload: Dict[str, Any], _emit: Callable[..., None]) -> bool:
|
||||
def _has_file_suffix(payload: _Request, _emit: _Emit) -> bool:
|
||||
"""遍历目录树;读取失败必须抛出,避免将未完成的扫描视为安全。"""
|
||||
suffixes = {ext.casefold() for ext in payload["extensions"]}
|
||||
|
||||
@@ -137,7 +191,7 @@ def _has_file_suffix(payload: Dict[str, Any], _emit: Callable[..., None]) -> boo
|
||||
return False
|
||||
|
||||
|
||||
def _rename(payload, _emit):
|
||||
def _rename(payload: _Request, _emit: _Emit) -> bool:
|
||||
"""
|
||||
同一存储内重命名/移动。
|
||||
|
||||
@@ -152,7 +206,7 @@ def _rename(payload, _emit):
|
||||
return True
|
||||
|
||||
|
||||
def _unlink(payload, _emit):
|
||||
def _unlink(payload: _Request, _emit: _Emit) -> bool:
|
||||
"""
|
||||
删除单个文件。unlink 是原子操作,强杀后要么删掉了要么没删,没有中间状态。
|
||||
"""
|
||||
@@ -160,7 +214,7 @@ def _unlink(payload, _emit):
|
||||
return True
|
||||
|
||||
|
||||
def _rmtree(payload, _emit):
|
||||
def _rmtree(payload: _Request, _emit: _Emit) -> bool:
|
||||
"""
|
||||
递归删除目录。
|
||||
|
||||
@@ -172,7 +226,7 @@ def _rmtree(payload, _emit):
|
||||
return True
|
||||
|
||||
|
||||
_HANDLERS = {
|
||||
_HANDLERS: dict[str, Callable[[_Request, _Emit], _Result]] = {
|
||||
"stat": _stat,
|
||||
"realpath": _realpath,
|
||||
"exists": _exists,
|
||||
@@ -187,7 +241,7 @@ _HANDLERS = {
|
||||
}
|
||||
|
||||
|
||||
def _write(message):
|
||||
def _write(message: _Response) -> None:
|
||||
"""
|
||||
输出一行响应。
|
||||
"""
|
||||
@@ -195,20 +249,21 @@ def _write(message):
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
"""
|
||||
请求循环:每读一行处理一个请求,直到 stdin 关闭。
|
||||
|
||||
一个请求可能对应多行响应:长耗时操作先流式发若干 progress 行,最后发一行
|
||||
终态(result 或 error)。父进程据此区分「还在推进」和「已经挂死」。
|
||||
"""
|
||||
response: _Response
|
||||
for line in sys.stdin:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
payload = json.loads(line)
|
||||
handler = _HANDLERS.get(payload.get("op"))
|
||||
payload: _Request = json.loads(line)
|
||||
handler = _HANDLERS.get(payload.get("op", ""))
|
||||
if handler is None:
|
||||
response = {"ok": False, "errno": 0,
|
||||
"error": f"unknown op: {payload.get('op')}"}
|
||||
|
||||
@@ -75,7 +75,7 @@ uv run --locked --no-sync python scripts/architecture/complexity.py
|
||||
回调和返回值类型;全仓错误计数为零并不保证单独纳入 strict frontier 后也为零。
|
||||
|
||||
2026-10-16 开始逐文件清理类型债务,`app/domain/title.py` 通过局部可空类型声明保留
|
||||
既有搜索解析行为,并纳入严格清单;当前清单共 66 个文件(65 个生产文件和 1 个脚本)。
|
||||
既有搜索解析行为,并纳入严格清单;当前清单共 67 个文件(66 个生产文件和 1 个脚本)。
|
||||
每个治理提交聚焦一个生产文件及其必要测试、门禁配置、真实下降基线和指标文档。
|
||||
验收同时检查完整严格清单与全宿主 Linux 分析,避免 `follow_imports = skip` 隐藏依赖
|
||||
签名冲突;不通过忽略诊断或放宽类型合同接受回退。最新文件清单以 `mypy.ini` 为准,
|
||||
|
||||
@@ -106,9 +106,9 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁
|
||||
| Model/Oper 事务债务 | 0 | 自建 Session、自动事务装饰器、直接 commit/rollback 等基线均为零 |
|
||||
| Module Contract | 215 specs / 211 methods / 263 calls | 动态方法名为 0;分类事实富化通过显式合同进入插件调度,旧 YAML 分类方法已移除;download 新增 custom_words 可选参数,旧 provider 签名通过显式合同兼容 |
|
||||
| Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement |
|
||||
| Python 源码量 | 372,044 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
|
||||
| Python 源码量 | 372,099 行 | 本次重新扫描物理行数,排除 `app/plugins/**`;74 个文件超过 1,000 行,12 个超过 2,000 行;源码尺寸仅作观察 |
|
||||
| 长方法 | 309 个超过 80 行 | 本次 AST 扫描排除 `app/plugins/**`;61 个超过 150 行,17 个超过 250 行;新增函数仍受 C901/嵌套深度门禁约束 |
|
||||
| 全量 mypy 历史债务 | 9,030 / 486 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
|
||||
| 全量 mypy 历史债务 | 9,017 / 485 文件 | 通知排版补齐 Slack 消息块类型,历史债务减少 2 项;工作流资源过滤改用领域对象调用过滤链,恢复的节点输出在消费前归一化,并收紧可空资源列表的类型低水位;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降;微信 ClawBot 入站 item_list 媒体解析改为按子项键名分派后,类型错误减少 4 项;站点解析器分享率改用浮点初值,类型错误减少 20 项;本次下载与文件安全修复再减少 3 项;Transmission 进度回退值归一化再减少 2 项,逐文件逐错误码无增长 |
|
||||
| Ruff 历史诊断 | 100 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` |
|
||||
| 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 |
|
||||
|
||||
|
||||
1
mypy.ini
1
mypy.ini
@@ -23,6 +23,7 @@ files =
|
||||
app/adapters/network/ip.py,
|
||||
app/adapters/web/correlation.py,
|
||||
app/adapters/system/backup/database.py,
|
||||
app/adapters/system/fsworker.py,
|
||||
app/adapters/system/resource.py,
|
||||
app/agent/contracts.py,
|
||||
app/runtime/correlation.py,
|
||||
|
||||
@@ -70,10 +70,6 @@
|
||||
"type-arg": 1,
|
||||
"union-attr": 11
|
||||
},
|
||||
"app/adapters/system/fsworker.py": {
|
||||
"no-untyped-call": 3,
|
||||
"no-untyped-def": 10
|
||||
},
|
||||
"app/adapters/system/host.py": {
|
||||
"assignment": 4,
|
||||
"misc": 1,
|
||||
|
||||
@@ -287,3 +287,27 @@ def test_worker_runs_standalone_without_app_on_path(tmp_path):
|
||||
response = json.loads(result.stdout.strip())
|
||||
assert response["ok"] is True
|
||||
assert response["result"]["size"] == 3
|
||||
|
||||
|
||||
def test_worker_reports_unknown_operations_and_keeps_reading(tmp_path):
|
||||
"""操作名缺失、为空或未知时保留错误回执,随后仍能处理有效请求。"""
|
||||
worker_path = Path("app/adapters/system/fsworker.py").resolve()
|
||||
payloads = [{}, {"op": None}, {"op": ""}, {"op": "unknown"}, {"op": "ping"}]
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(worker_path)],
|
||||
input="".join(json.dumps(payload) + "\n" for payload in payloads),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
timeout=30,
|
||||
cwd=tmp_path,
|
||||
)
|
||||
responses = [json.loads(line) for line in result.stdout.splitlines()]
|
||||
|
||||
assert responses == [
|
||||
{"ok": False, "errno": 0, "error": "unknown op: None"},
|
||||
{"ok": False, "errno": 0, "error": "unknown op: None"},
|
||||
{"ok": False, "errno": 0, "error": "unknown op: "},
|
||||
{"ok": False, "errno": 0, "error": "unknown op: unknown"},
|
||||
{"ok": True, "result": True},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user