Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

refactor: web_logic 改名 catalog_rules,加分层门槛

longmeidao2d4c6c83c052
diff --git a/scripts/test_runner.py b/scripts/test_runner.py
index e10a50576..920ca88db 100644
--- a/scripts/test_runner.py
+++ b/scripts/test_runner.py
@@ -32,7 +32,7 @@ SCOPES: dict[str, tuple[str, ...]] = {
"tooling": ("test_scripts.py", "test_agent_worktree.py", "test_versioning.py",
"test_windows_update.py", "test_certs.py", "test_config.py",
"test_job_status.py", "test_jobs.py", "test_reference_updates.py",
- "test_subprocess_encoding.py"),
+ "test_subprocess_encoding.py", "test_module_layering.py"),
}
SCOPE_TEST_IDS: dict[str, tuple[str, ...]] = {
diff --git a/src/peach/web_logic.py b/src/peach/catalog_rules.py
similarity index 93%
rename from src/peach/web_logic.py
rename to src/peach/catalog_rules.py
index 9454c6e6a..b28e45e8a 100644
--- a/src/peach/web_logic.py
+++ b/src/peach/catalog_rules.py
@@ -1,4 +1,12 @@
-"""Pure, database-free rules shared by Peach's web contract domains."""
+"""编目规则:什么是番号、什么是分卷、标签属于哪一类、两条记录算不算重复。
+
+不碰数据库、不碰 HTTP、不依赖任何 Peach 模块,是最底下那层纯策略。
+
+这个文件曾经叫 `web_logic.py`,但里面没有一行是 web 的,而依赖它的四个模块里有
+三个不是 web 层:`repository`(数据层)取 `is_jav_code`,`taste_history` 取
+`LENGTH_TAGS`,`fc2_similarity` 取重复判据。数据层 import 一个叫 web 的模块,
+读代码的人会以为分层反了——反的其实是名字。2026-08-29 改名,内容一行未动。
+"""
from __future__ import annotations
import re
diff --git a/src/peach/fc2_similarity.py b/src/peach/fc2_similarity.py
index 4f601c46c..b424eefc9 100644
--- a/src/peach/fc2_similarity.py
+++ b/src/peach/fc2_similarity.py
@@ -6,7 +6,7 @@ from collections import defaultdict
from itertools import combinations
from typing import Iterable, Mapping
-from .web_logic import DUPLICATE_FLOOR_SECONDS, DUPLICATE_TOLERANCE, part_marker
+from .catalog_rules import DUPLICATE_FLOOR_SECONDS, DUPLICATE_TOLERANCE, part_marker
POLICY_VERSION = "fc2-similarity-policy-v1"
diff --git a/src/peach/repository.py b/src/peach/repository.py
index bbc36a7a9..9878fa24d 100644
--- a/src/peach/repository.py
+++ b/src/peach/repository.py
@@ -6,7 +6,7 @@ from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
-from .web_logic import is_jav_code
+from .catalog_rules import is_jav_code
class LedgerDatabase:
diff --git a/src/peach/taste_history.py b/src/peach/taste_history.py
index 3351f166c..cebde49af 100644
--- a/src/peach/taste_history.py
+++ b/src/peach/taste_history.py
@@ -22,7 +22,7 @@ from browserexport.browsers.chrome import Chrome
from browserexport.browsers.firefox import Firefox
from browserexport.browsers.safari import Safari
-from .web_logic import LENGTH_TAGS
+from .catalog_rules import LENGTH_TAGS
@dataclass(frozen=True)
diff --git a/src/peach/web_contract.py b/src/peach/web_contract.py
index 5e422ef2d..c3c58fb68 100644
--- a/src/peach/web_contract.py
+++ b/src/peach/web_contract.py
@@ -64,7 +64,7 @@ from .web_activity import (
w_quality_goal,
w_watch_later,
)
-from .web_logic import (
+from .catalog_rules import (
DUPLICATE_FLOOR_SECONDS,
DUPLICATE_TOLERANCE,
LENGTH_TAGS,
diff --git a/tests/test_module_layering.py b/tests/test_module_layering.py
new file mode 100644
index 000000000..41d507c6e
--- /dev/null
+++ b/tests/test_module_layering.py
@@ -0,0 +1,77 @@
+"""模块分层门槛。
+
+Peach 是模块化单体,分层靠约定而不是包结构,所以约定必须有人守。这里守一条:
+**web 层之外的模块不许 import web 层。**
+
+这条规则不是凭空立的。`catalog_rules.py` 原名 `web_logic.py`,而 `repository.py`
+(数据层)要用它的 `is_jav_code`,`taste_history.py` 要用 `LENGTH_TAGS`——数据层
+import 一个叫 web 的模块,读代码的人只会得出「分层反了」的结论。实际反的是名字,
+但名字骗人和结构错了一样难查。改名之后加这条断言,让下一次真反了的时候直接报错。
+"""
+import ast
+import pathlib
+import unittest
+
+import peach
+
+
+SOURCE_ROOT = pathlib.Path(peach.__file__).parent
+
+#: web 层自己。层内互相依赖是正常的。
+WEB_MODULES = {"web_contract", "web_follow", "web_activity", "web_playlists"}
+
+#: 应用层组装点:把 web 层挂上 FastAPI 是它的职责,方向是对的。
+COMPOSITION_ROOTS = {"api"}
+
+
+def _local_imports(path: pathlib.Path) -> set[str]:
+ """取出一个模块 import 的本包模块名(`from .x import y` 与 `from . import x`)。"""
+ tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
+ names: set[str] = set()
+ for node in ast.walk(tree):
+ if isinstance(node, ast.ImportFrom) and node.level:
+ if node.module:
+ names.add(node.module.split(".")[0])
+ else:
+ names.update(alias.name for alias in node.names)
+ return names
+
+
+class LayeringTests(unittest.TestCase):
+ def test_only_web_modules_and_the_composition_root_import_the_web_layer(self):
+ offenders = []
+ for path in sorted(SOURCE_ROOT.glob("*.py")):
+ module = path.stem
+ if module in WEB_MODULES or module in COMPOSITION_ROOTS:
+ continue
+ for imported in sorted(_local_imports(path)):
+ if imported in WEB_MODULES:
+ offenders.append(f"{module} → {imported}")
+ self.assertEqual(
+ offenders, [],
+ "非 web 模块不得依赖 web 层:把共享的纯规则下沉到 catalog_rules 一类的策略模块",
+ )
+
+ def test_catalog_rules_depends_on_nothing_inside_peach(self):
+ """最底下那层必须是纯的:只要它开始 import 别的 Peach 模块,就不再是策略了。"""
+ self.assertEqual(
+ _local_imports(SOURCE_ROOT / "catalog_rules.py"), set(),
+ "catalog_rules 是最底层纯规则,不能依赖任何 Peach 模块",
+ )
+
+ def test_the_old_web_logic_name_is_gone(self):
+ self.assertFalse(
+ (SOURCE_ROOT / "web_logic.py").exists(),
+ "web_logic.py 已改名为 catalog_rules.py;两个同时存在意味着改名没做干净",
+ )
+ # 只看 import,不看全文:`catalog_rules` 的 docstring 要讲清楚它改过名,
+ # 那句话里出现旧名字是应该的。
+ for path in sorted(SOURCE_ROOT.glob("*.py")):
+ self.assertNotIn(
+ "web_logic", _local_imports(path),
+ f"{path.name} 仍在 import 旧模块名 web_logic",
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()