Skip to content

armgit / peach

Public Imported from GitHub
0

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

refactor(checks): 复杂度门槛收判过就该长的函数,退回只为压分支数的拆分

longmeidao674ef04cac57
diff --git a/src/peach/api.py b/src/peach/api.py
index d489987c8..68a84c10f 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -110,14 +110,6 @@ def _writer(sync: LedgerSync | None) -> bool:
return sync is None or not sync.read_only
-def _resolve_tunnel_manager(
- settings: PeachSettings, manager: tunnel.TunnelManager | None,
-) -> tunnel.TunnelManager:
- return manager if manager is not None else tunnel.TunnelManager(
- settings.tunnel_state_root, settings.tunnel_log_root,
- )
-
-
def _start_tunnel(settings: PeachSettings, manager: tunnel.TunnelManager) -> None:
"""公网入口只按本次启动注入的设置开;不回头读设置文件里的当前值。"""
if not (settings.tunnel_enabled and settings.configured):
@@ -154,7 +146,8 @@ def create_app(
) -> FastAPI:
"""`sync` 由 CLI 注入。测试直接建 app 时不传,复制与只读闸门整体不参与。"""
settings = settings or PeachSettings()
- tunnel_manager = _resolve_tunnel_manager(settings, tunnel_manager)
+ if tunnel_manager is None:
+ tunnel_manager = tunnel.TunnelManager(settings.tunnel_state_root, settings.tunnel_log_root)
database = LedgerDatabase(settings.db_path)
contract = web_contract.WebContract(
settings.db_path, settings.snapshot_root,
diff --git a/src/peach/jav_cover_fetch.py b/src/peach/jav_cover_fetch.py
index 13f7fe144..13df7677b 100644
--- a/src/peach/jav_cover_fetch.py
+++ b/src/peach/jav_cover_fetch.py
@@ -139,16 +139,6 @@ def candidate_improves(candidate: Candidate, pixels: int,
return candidate_quality(candidate) > baseline_quality or pixels > baseline_pixels
-def _previous_quality(previous) -> int:
- return candidate_quality(previous[0] if previous is not None else None)
-
-
-def _keep_upgrade_existing(upgrade: bool, candidate: Candidate, pixels: int,
- baseline_quality: int, baseline_pixels: int) -> bool:
- return upgrade and not candidate_improves(
- candidate, pixels, baseline_quality, baseline_pixels)
-
-
@dataclass(frozen=True)
class MetadataEvidence:
candidates: tuple[Candidate, ...] = ()
@@ -1087,10 +1077,10 @@ def run(args: argparse.Namespace, handle: TaskRunHandle | None = None) -> int:
except (UnidentifiedImageError, OSError):
pass
previous = logged_success_evidence(previous_rows, code)
- baseline_quality = _previous_quality(previous)
- prior = tuple(candidate for candidate in (
- fc2_candidates.get(code), previous[0] if previous is not None else None,
- ) if candidate is not None)
+ last = previous[0] if previous is not None else None
+ baseline_quality = candidate_quality(last)
+ prior = tuple(candidate for candidate in (fc2_candidates.get(code), last)
+ if candidate is not None)
known_sizes = ({previous[0].url: previous[1]}
if previous is not None else {})
winner, (width, height), data = best_cover(
@@ -1119,8 +1109,8 @@ def run(args: argparse.Namespace, handle: TaskRunHandle | None = None) -> int:
print(f"[{index}/{len(todo)}] 未取得 {code}:"
f"{type(exc).__name__} {exc}", flush=True)
else:
- if _keep_upgrade_existing(
- args.upgrade_existing, winner, width * height,
+ if args.upgrade_existing and not candidate_improves(
+ winner, width * height,
baseline_quality, current_size[0] * current_size[1]):
stats["kept"] += 1
print(f"[{index}/{len(todo)}] 保留 {code} "
diff --git a/src/peach/web_review.py b/src/peach/web_review.py
index a6f57d087..0a4d527dd 100644
--- a/src/peach/web_review.py
+++ b/src/peach/web_review.py
@@ -321,8 +321,8 @@ def _code_creator_rows(connection) -> list[dict]:
return rows
-#: 现算类别取行的函数。写成表而不是分支:`_review_rows` 已经顶着复杂度基线,
-#: 每加一类都往里塞一个 if 的话,最先撑不住的是那个函数而不是这张表。
+#: 现算类别各自的取行函数,键与 `LIVE_CATEGORIES` 一一对应。加一个现算类别就是在
+#: 这里登一行,读队列的 `_queue_rows` 不用跟着改。
LIVE_CATEGORY_ROWS = {"code_creators": _code_creator_rows}
diff --git a/src/peach/web_scraping.py b/src/peach/web_scraping.py
index a4f6de7b3..2a871883d 100644
--- a/src/peach/web_scraping.py
+++ b/src/peach/web_scraping.py
@@ -11,16 +11,19 @@ from .http import HttpRequest
from .scraping_access import SOURCES, SourcePaused, SourceTransport, describe, save
-def _installed_candidate(installed, previous, candidate_for):
- if isinstance(installed, dict) and installed.get("source_url"):
- return candidate_for(str(installed["source_url"]))
- return previous[0] if previous is not None else None
-
+def _kept_cover_size(target, incoming, incoming_size, installed, previous):
+ """本机封面不比来源这张差时回它的尺寸;该换、或本机还没有封面时回 `None`。
-def _kept_cover_size(target, incoming, incoming_size, current,
- candidate_improves, candidate_quality):
+ 本机那张的图片角色按装它时的来源认:边车记着来源地址就按它,否则按采集日志里
+ 上一次成功的那条。比较口径与批处理脚本同一个 `candidate_improves`。
+ """
+ from .jav_cover_fetch import candidate_for, candidate_improves, candidate_quality
if not target.is_file():
return None
+ if isinstance(installed, dict) and installed.get("source_url"):
+ current = candidate_for(str(installed["source_url"]))
+ else:
+ current = previous[0] if previous is not None else None
with Image.open(target) as image:
current_size = image.size
if candidate_improves(
@@ -29,6 +32,7 @@ def _kept_cover_size(target, incoming, incoming_size, current,
return None
return current_size
+
def w_scraping_cover(contract, body):
"""仅处理用户指定且馆藏命中的番号;完整解码后才允许升级封面。"""
from .catalog_rules import is_korean_mib_code
@@ -45,8 +49,7 @@ def w_scraping_cover(contract, body):
def _fetch_cover(contract, code):
from .cover_artwork import install_cover
- from .jav_cover_fetch import (best_cover, candidate_for, candidate_improves,
- candidate_quality,
+ from .jav_cover_fetch import (best_cover,
HostLimitedTransport, NO_USABLE_OFFICIAL,
OFFICIAL_PLACEHOLDER_ONLY, Unavailable,
fc2_cover_candidates, logged_success_evidence)
@@ -86,10 +89,7 @@ def _fetch_cover(contract, code):
candidate, size, data = best_cover(observed, code, 0, diagnostics=diagnostics,
prior_candidates=prior,
metadata_root=contract.follow_sources_root / "metadata" / "javinizer-go")
- current_candidate = _installed_candidate(installed, previous, candidate_for)
- kept_size = _kept_cover_size(
- target, candidate, size, current_candidate,
- candidate_improves, candidate_quality)
+ kept_size = _kept_cover_size(target, candidate, size, installed, previous)
if kept_size is not None:
suffix = "部分来源连接失败,未能完成全部来源比较。" if network_failed or any(s >= 400 and s != 404 for s in statuses) else "没有找到更大或更合适的封面。"
return {"ok": True, "code": code, "reason": "kept_existing",
diff --git a/tests/test_complexity_ratchet.py b/tests/test_complexity_ratchet.py
index ef7bacc91..cb1ce96e0 100644
--- a/tests/test_complexity_ratchet.py
+++ b/tests/test_complexity_ratchet.py
@@ -1,4 +1,4 @@
-"""函数复杂度棘轮:已经很复杂的函数不许再长,新函数不许长到那个程度。
+"""函数复杂度棘轮:函数变复杂必须有人判断过,不许「顺手加一个 if」悄悄堆上去。
2026-09-13 全仓盘点,`src/peach/` 与 `scripts/` 里分支数达到 30 的函数有 42 个,
最大的两个(`library_processing.process_library`、`web_batch.q_ads`)分别是 91 与 85。
@@ -8,8 +8,18 @@
每个推导式子句、每个 `and`/`or`、每个 `case` 与条件表达式各计 1,嵌套函数算进
外层。数值只和它自己比,不和别的工具比。
-基线只能降:函数变简单了就把基线改小,降到限值以下就把它从表里删掉;想调大要么
-拆函数,要么在同一次改动里写明为什么这一处非长不可。
+两张表,数都必须与代码一致:
+
+- `BASELINE` 是盘点留下的存量,只能降。函数变简单了就把数改小,降到限值以下就删掉。
+- `ACCEPTED` 是判过「就该这么长」的函数,每条带一句理由:顺序流水线、按类型分派、
+ 逐字段解析这类,拆开只会把一路传下去的状态散进几个只调用一次的函数里。它长了就在
+ 同一次改动里改数,理由跟着再看一遍还成不成立。
+
+函数超过自己的数时先判断,再二选一。一是拆:拆出去的那块要能独立命名,有自己的前提
+和结果,比如 `jav_cover_fetch._download_best`「按档次完整下载,挑第一张静态图」。二是
+挪进 `ACCEPTED` 写理由。下面几种不算拆,只是把分支数藏起来,评审一律退回:只转发一个
+表达式的包装;把模块函数当参数传给 helper;为了少一个 if 建只有一项的分派表;在注释里
+拿这道门槛当设计理由。
"""
import ast
import pathlib
@@ -23,9 +33,8 @@ SOURCE_ROOTS = (REPO / "src" / "peach", REPO / "scripts")
#: 达到这个分支数的函数进入基线;基线之外的函数不许长到这里。
LIMIT = 30
-#: 键是 `<仓库相对路径>:<限定名>`,值是盘点当天的分支数。
+#: 键是 `<仓库相对路径>:<限定名>`,值是当前的分支数,只能往下改。
BASELINE: dict[str, int] = {
- "src/peach/library_processing.py:process_library": 65,
"src/peach/web_batch.py:q_ads": 85,
"scripts/scrape_codes.py:_scrape": 77,
"scripts/localize_performer_names.py:collect": 64,
@@ -34,7 +43,6 @@ BASELINE: dict[str, int] = {
"src/peach/fanbox.py:normalize_fanbox_post": 59,
"src/peach/web_follow.py:q_follow": 52,
"scripts/merge_duplicate_identities.py:collect": 50,
- "src/peach/jav_cover_fetch.py:run": 49,
"src/peach/metadata_auto_apply.py:_apply_metadata_candidate": 38,
"src/peach/web_review.py:_attach_review_asset_context": 46,
"src/peach/web_review.py:w_review_decision": 46,
@@ -47,7 +55,6 @@ BASELINE: dict[str, int] = {
"src/peach/taste_history.py:_taste_analysis": 40,
"src/peach/web_catalog.py:catalog_filter": 40,
"scripts/audit_domain_codes.py:collect": 39,
- "src/peach/api.py:create_app": 39,
"src/peach/web_catalog.py:q_items": 38,
"scripts/harvest_social_avatars.py:harvest_entity": 37,
"scripts/probe.py:run": 36,
@@ -62,6 +69,23 @@ BASELINE: dict[str, int] = {
"src/peach/resource_identification.py:ingest_results": 30,
}
+#: 判过就该这么长的函数:`(分支数, 为什么拆开更差)`。键与 `BASELINE` 同形、不重叠。
+ACCEPTED: dict[str, tuple[int, str]] = {
+ "src/peach/api.py:create_app": (
+ 40, "应用工厂:按启动顺序构造账本、各项后台服务与路由并接成一个 app,每个可注入的"
+ "依赖各带一个缺省;拆开只是把同一条构造顺序分散到几处"),
+ "src/peach/library_processing.py:process_library": (
+ 65, "顺序流水线:扫描、探时长、读本地资料、联网采集、写候选、收尾共用一份进度状态"
+ "与问题记录;能独立命名的段已经拆出(`_RemoteSession`、`_merge_candidates`、"
+ "`_record_issue`),剩下的是编排本身"),
+ "src/peach/jav_cover_fetch.py:run": (
+ 50, "批处理的主循环:每个番号依次量本机尺寸、择优、比较、落盘、记日志,统计、日志行"
+ "和出错后换新的连接池在整轮里共用,逐段拆开只是把这几样来回传"),
+}
+
+#: 一句理由至少要说清「是哪种形状、拆开差在哪」,短于这个长度的多半只是占位。
+MIN_REASON = 20
+
BRANCHES = (ast.If, ast.For, ast.AsyncFor, ast.While, ast.ExceptHandler,
ast.With, ast.AsyncWith, ast.Assert, ast.IfExp, ast.match_case)
@@ -104,6 +128,11 @@ def survey() -> dict[str, int]:
return found
+def recorded() -> dict[str, int]:
+ """两张表里记下的数合在一起。"""
+ return {**BASELINE, **{key: value for key, (value, _reason) in ACCEPTED.items()}}
+
+
class ComplexityRatchetTests(unittest.TestCase):
def test_the_counter_counts_the_shapes_it_names(self):
source = (
@@ -120,29 +149,36 @@ class ComplexityRatchetTests(unittest.TestCase):
# 1 + for + if + and + or + except + 推导式 + 推导式的 if
self.assertEqual(complexity(node), 8)
- def test_no_function_grew_past_its_baseline(self):
- current = survey()
+ def test_no_function_grew_past_its_recorded_count(self):
+ current, limits = survey(), recorded()
grown = sorted(
- f"{key}: {value}(基线 {BASELINE.get(key, LIMIT - 1)})"
+ f"{key}: {value}(记录 {limits.get(key, LIMIT - 1)})"
for key, value in current.items()
- if value > BASELINE.get(key, LIMIT - 1))
+ if value > limits.get(key, LIMIT - 1))
self.assertEqual(grown, [],
- "这些函数的分支数超过了基线,拆出去或在同一次改动里说明理由:\n "
- + "\n ".join(grown))
+ "这些函数的分支数超过了记录。拆出能独立命名的一块,或判过它就该"
+ "这么长后挪进 ACCEPTED 写理由;转发包装、把函数当参数传、单项分派表"
+ "都不算拆:\n " + "\n ".join(grown))
- def test_the_baseline_only_ratchets_down(self):
+ def test_the_recorded_counts_match_the_code(self):
current = survey()
stale = sorted(
- f"{key}: 现在 {current.get(key, '不存在')},基线 {value}"
- for key, value in BASELINE.items()
+ f"{key}: 现在 {current.get(key, '不存在')},记录 {value}"
+ for key, value in recorded().items()
if key not in current or current[key] < value)
self.assertEqual(stale, [],
- "基线落后于代码:函数变简单或删掉了就把这里改小或删掉,"
- "基线不能替将来的增长留余量:\n " + "\n ".join(stale))
-
- def test_the_baseline_holds_only_functions_at_or_over_the_limit(self):
- below = sorted(key for key, value in BASELINE.items() if value < LIMIT)
- self.assertEqual(below, [], f"低于 {LIMIT} 的函数不该进基线:{below}")
+ "记录落后于代码:函数变简单或删掉了就把这里改小或删掉,"
+ "记录不能替将来的增长留余量:\n " + "\n ".join(stale))
+
+ def test_the_tables_hold_only_functions_at_or_over_the_limit(self):
+ below = sorted(key for key, value in recorded().items() if value < LIMIT)
+ self.assertEqual(below, [], f"低于 {LIMIT} 的函数不该进表:{below}")
+
+ def test_an_accepted_function_says_why_splitting_is_worse(self):
+ self.assertEqual(sorted(set(BASELINE) & set(ACCEPTED)), [], "一个函数只能在一张表里")
+ thin = sorted(key for key, (_value, reason) in ACCEPTED.items()
+ if len(reason.strip()) < MIN_REASON)
+ self.assertEqual(thin, [], f"这些条目的理由不足 {MIN_REASON} 字:{thin}")
if __name__ == "__main__":