-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaudit.py
More file actions
389 lines (323 loc) · 13.3 KB
/
Copy pathaudit.py
File metadata and controls
389 lines (323 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python3
"""Audit plrom README content count and subscription cost.
Counts every bullet item per category, splits active vs deprecated by
<details> blocks, and totals recurring subscription cost normalized to ¥/month.
Usage:
python3 .github/scripts/audit.py [path/to/README.md] # print full report
python3 .github/scripts/audit.py --update README.md # rewrite AUDIT block in place
"""
from __future__ import annotations
import argparse
import json
import re
import sys
import tomllib
from dataclasses import dataclass, field
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any, Callable
SENTINEL_START = "<!-- AUDIT:START -->"
SENTINEL_END = "<!-- AUDIT:END -->"
LIMITS_PATH = Path("audit/limits.toml")
FOLO_SNAPSHOT_PATH = Path("audit/folo-snapshot.json")
FOLO_SNAPSHOT_MAX_AGE = timedelta(days=35)
USD_TO_CNY = 7.2 # rough April 2026 rate
# Subscription patterns: each is (regex, factor_fn) where factor_fn(match) -> ¥/month
PRICE_PATTERNS: list[tuple[re.Pattern[str], Callable[[re.Match[str]], float]]] = [
(re.compile(r"\$(\d+(?:\.\d+)?)/m\b"), lambda _: USD_TO_CNY),
(re.compile(r"\$(\d+(?:\.\d+)?)/y\b"), lambda _: USD_TO_CNY / 12),
(re.compile(r"\$(\d+(?:\.\d+)?)/年"), lambda _: USD_TO_CNY / 12),
(re.compile(r"\$(\d+(?:\.\d+)?)/(\d+)d\b"), lambda m: USD_TO_CNY * 30 / int(m.group(2))),
(re.compile(r"¥(\d+(?:\.\d+)?)/m\b"), lambda _: 1.0),
(re.compile(r"¥(\d+(?:\.\d+)?)/y\b"), lambda _: 1.0 / 12),
(re.compile(r"¥(\d+(?:\.\d+)?)/年"), lambda _: 1.0 / 12),
(re.compile(r"¥(\d+(?:\.\d+)?)/(\d+)d\b"), lambda m: 30 / int(m.group(2))),
]
LINK_LABEL = re.compile(r"\[([^\]]+)\]")
BOLD_LABEL = re.compile(r"\*\*([^*]+)\*\*")
# H2 sections that are meta/maintenance, not content
SKIP_H2 = {"维护说明"}
@dataclass
class Subscription:
label: str
raw: str
yuan_per_month: float
@dataclass
class Category:
h2: str
h3: str
active: int = 0
deprecated: int = 0
subs: list[Subscription] = field(default_factory=list)
def extract_label(line: str) -> str:
line = line.lstrip("- ").strip()
line = line.lstrip("~") # drop leading ~~strikethrough markers
if (m := LINK_LABEL.match(line)) is not None:
return m.group(1)
if (m := BOLD_LABEL.match(line)) is not None:
return m.group(1)
return (line[:40] + "…") if len(line) > 40 else line
SUBSCRIPTION_SKIP_KEYWORDS = ("公司付费",)
def parse_subscriptions(line: str) -> list[Subscription]:
"""Extract every recurring-price annotation on the metadata portion of a bullet line.
Convention: ``- [Name](url)(price annotation) - free-text description...``
Only the part before the first `` - `` separator is treated as authoritative pricing
metadata, so prices mentioned descriptively (e.g. iCloud's ¥68/m full price next to the
user's ¥23/m share, or 1Password ¥498/y next to ¥166/y share) are NOT double-counted.
A line can still carry 2+ recurring prices in metadata (e.g. HSR 大月卡 + 小月卡).
"""
if any(kw in line for kw in SUBSCRIPTION_SKIP_KEYWORDS):
return []
sep = line.find(" - ")
metadata = line[:sep] if sep != -1 else line
label = extract_label(line)
found: list[Subscription] = []
for pattern, factor_fn in PRICE_PATTERNS:
for m in pattern.finditer(metadata):
amount = float(m.group(1))
found.append(
Subscription(
label=label,
raw=m.group(0),
yuan_per_month=amount * factor_fn(m),
)
)
return found
def audit(readme: Path) -> list[Category]:
cats: dict[tuple[str, str], Category] = {}
h2 = h3 = ""
in_details = 0 # depth, in case of nested <details>
for raw in readme.read_text(encoding="utf-8").splitlines():
line = raw.rstrip()
stripped = line.lstrip()
if "<details>" in line:
in_details += 1
continue
if "</details>" in line:
in_details = max(in_details - 1, 0)
continue
if line.startswith("<!--") or line.startswith("-->"):
continue
if stripped.startswith(">"):
continue
if line.startswith("## ") and not line.startswith("### "):
h2 = line[3:].strip()
h3 = ""
continue
if line.startswith("### "):
h3 = line[4:].strip()
if h2 not in SKIP_H2:
cats.setdefault((h2, h3), Category(h2, h3))
continue
if not stripped.startswith("-"):
continue
if not h3 or h2 in SKIP_H2:
continue
cat = cats.setdefault((h2, h3), Category(h2, h3))
if in_details:
cat.deprecated += 1
continue # skip subscription parsing for deprecated items
cat.active += 1
for sub in parse_subscriptions(stripped):
sub.label = f"{sub.label} ({h3})"
cat.subs.append(sub)
return list(cats.values())
def render_full(cats: list[Category]) -> str:
"""Full standalone report (for stdout / one-off check)."""
total_active = sum(c.active for c in cats)
total_deprecated = sum(c.deprecated for c in cats)
all_subs = [s for c in cats for s in c.subs]
monthly = sum(s.yuan_per_month for s in all_subs)
out: list[str] = []
out.append("# plrom 体量盘点\n")
out.append("## 总览\n")
out.append(f"- 活跃条目:**{total_active}** 个")
out.append(f"- 过期/不活跃条目:**{total_deprecated}** 个")
out.append(f"- 月订阅烧钱:**¥{monthly:.0f} / 月**(年度 ¥{monthly * 12:.0f})")
out.append(f"- 在订订阅条目数:**{len(all_subs)}** 个\n")
out.append("## 按分类(活跃数降序)\n")
out.append("| H2 | H3 | 活跃 | 不活跃 | 月订阅 ¥ |")
out.append("|----|-----|------|--------|----------|")
for c in sorted(cats, key=lambda x: (-x.active, -x.deprecated)):
sub_total = sum(s.yuan_per_month for s in c.subs)
sub_cell = f"{sub_total:.0f}" if sub_total > 0 else ""
out.append(f"| {c.h2} | {c.h3} | {c.active} | {c.deprecated} | {sub_cell} |")
if all_subs:
out.append("\n## 月订阅明细(¥/月 降序)\n")
out.append("| 条目 | 原价 | ¥/月 |")
out.append("|------|------|------|")
for s in sorted(all_subs, key=lambda x: -x.yuan_per_month):
out.append(f"| {s.label} | `{s.raw}` | {s.yuan_per_month:.1f} |")
out.append("\n## TOP 10 最胖分类\n")
for i, c in enumerate(sorted(cats, key=lambda x: -x.active)[:10], 1):
out.append(f"{i}. **{c.h3}** ({c.h2}) — 活跃 {c.active},不活跃 {c.deprecated}")
return "\n".join(out) + "\n"
METRICS: dict[str, Callable[..., float]] = {
"subscription_total": lambda cats: sum(s.yuan_per_month for c in cats for s in c.subs),
"subscription_count": lambda cats: sum(len(c.subs) for c in cats),
"total_active": lambda cats: sum(c.active for c in cats),
"total_deprecated": lambda cats: sum(c.deprecated for c in cats),
"h2_active": lambda cats, h2: sum(c.active for c in cats if c.h2 == h2),
"h3_active": lambda cats, h3: sum(c.active for c in cats if c.h3 == h3),
}
FOLO_METRICS: dict[str, tuple[str, ...]] = {
"folo_attention_minutes": ("attention", "budgeted_minutes_per_week"),
"folo_core_count": ("lanes", "core", "source_count"),
"folo_changelog_count": ("lanes", "changelog", "source_count"),
"folo_uncategorized_count": ("totals", "uncategorized_sources"),
"folo_abnormal_count": ("totals", "abnormal_sources"),
"folo_core_max_source_share": ("lanes", "core", "max_source_share_percent"),
}
def load_limits(path: Path) -> list[dict[str, Any]]:
if not path.exists():
return []
with path.open("rb") as f:
data = tomllib.load(f)
return data.get("items", [])
def load_folo_snapshot(
path: Path = FOLO_SNAPSHOT_PATH,
*,
now: datetime | None = None,
) -> dict[str, Any] | None:
"""Load a fresh Folo snapshot; missing, invalid, or stale means unavailable."""
if not path.exists():
return None
try:
data = json.loads(path.read_text(encoding="utf-8"))
generated = datetime.fromisoformat(
str(data["generated_at"]).replace("Z", "+00:00")
).astimezone(UTC)
except (OSError, ValueError, KeyError, TypeError, json.JSONDecodeError):
return None
current = (now or datetime.now(UTC)).astimezone(UTC)
if generated > current + timedelta(minutes=5):
return None
if current - generated > FOLO_SNAPSHOT_MAX_AGE:
return None
return data
def _lookup_number(data: dict[str, Any], path: tuple[str, ...]) -> float | None:
value: Any = data
for key in path:
if not isinstance(value, dict) or key not in value:
return None
value = value[key]
if isinstance(value, bool) or not isinstance(value, (int, float)):
return None
return float(value)
def compute_metric(
item: dict[str, Any],
cats: list[Category],
folo_snapshot: dict[str, Any] | None = None,
) -> float | None:
name = item["metric"]
if name in FOLO_METRICS:
if folo_snapshot is None:
return None
return _lookup_number(folo_snapshot, FOLO_METRICS[name])
fn = METRICS.get(name)
if fn is None:
raise ValueError(f"unknown metric: {name!r} in item {item.get('name', '?')}")
if "arg" in item:
return fn(cats, item["arg"])
return fn(cats)
def format_value(value: float | None, unit: str | None = None) -> str:
if value is None:
return "N/A"
rounded = str(int(value)) if value.is_integer() else f"{value:.1f}"
if unit == "minutes":
return f"{rounded} 分钟"
if unit == "percent":
return f"{rounded}%"
return rounded
def status_cell(
current: float | None,
limit: float | None,
unit: str | None = None,
) -> str:
if current is None:
return "⚪ N/A"
if limit is None:
return "—"
diff = current - limit
if diff > 0:
return f"🚨 超 {format_value(diff, unit)}"
if diff == 0:
return "🟡 持平"
return f"✅ 留白 {format_value(-diff, unit)}"
def render_inline(
cats: list[Category],
limits: list[dict[str, Any]],
folo_snapshot: dict[str, Any] | None = None,
) -> str:
"""Compact limits dashboard for embedding in README.md between sentinels."""
out: list[str] = []
out.append("### 📊 体量盘点")
out.append("")
out.append(
"> 由 [.github/scripts/audit.py](.github/scripts/audit.py) "
"依据 [audit/limits.toml](audit/limits.toml) 自动生成,pre-commit hook 刷新。"
)
if folo_snapshot is None and any(
item.get("metric") in FOLO_METRICS for item in limits
):
out.append("> Folo 快照缺失或已超过 35 天;相关指标显示 `N/A`,不会按 0 处理。")
out.append("")
if not limits:
out.append("_未配置 [audit/limits.toml](audit/limits.toml),无可对照的上限_。")
return "\n".join(out)
out.append("| # | 维度 | 当前 | 上限 | 状态 | 备注 |")
out.append("|---|------|------|------|------|------|")
for i, item in enumerate(limits, 1):
current = compute_metric(item, cats, folo_snapshot)
limit = item.get("limit")
unit = item.get("unit")
limit_cell = format_value(float(limit), unit) if limit is not None else "—"
status = status_cell(current, float(limit) if limit is not None else None, unit)
note = item.get("note", "")
out.append(
f"| {i} | {item['name']} | {format_value(current, unit)} | "
f"{limit_cell} | {status} | {note} |"
)
return "\n".join(out)
def update_in_place(readme: Path, limits_path: Path) -> bool:
"""Rewrite the AUDIT block between sentinels. Returns True if file changed."""
content = readme.read_text(encoding="utf-8")
if SENTINEL_START not in content or SENTINEL_END not in content:
sys.stderr.write(
f"[audit] sentinel markers not found in {readme}; "
f"add {SENTINEL_START} ... {SENTINEL_END} block first\n"
)
sys.exit(2)
cats = audit(readme)
limits = load_limits(limits_path)
inline = render_inline(cats, limits, load_folo_snapshot())
new_block = f"{SENTINEL_START}\n\n{inline}\n\n{SENTINEL_END}"
pattern = re.compile(
re.escape(SENTINEL_START) + r".*?" + re.escape(SENTINEL_END),
re.DOTALL,
)
new_content = pattern.sub(new_block, content)
if new_content == content:
return False
readme.write_text(new_content, encoding="utf-8")
return True
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("readme", nargs="?", default="README.md", type=Path)
parser.add_argument(
"--update",
action="store_true",
help="rewrite the AUDIT:START/END block in README in place",
)
args = parser.parse_args()
if args.update:
changed = update_in_place(args.readme, LIMITS_PATH)
if changed:
print(f"[audit] updated AUDIT block in {args.readme}")
else:
print(f"[audit] AUDIT block already current in {args.readme}")
else:
cats = audit(args.readme)
print(render_full(cats))
if __name__ == "__main__":
main()