Files
douyin_guaji-main/gui/web_gui_pkg/stats_helpers.py
T
CodexandClaude Opus 4.7 a6bbdcc400 拆分 web_gui (2251 → 5 子模块 + 2148 行原文件保留 ProcRunner/Handler/main)
把 gui/web_gui.py 中 11 个独立 helper 拆出到 gui/web_gui_pkg/ 子包,
ProcRunner 巨型类(1514 行 / 25 实例方法 / 390 处 self)+ Handler 类
(588 行)+ main 留在原文件。

新建子包:
- logger (29)         _wg_log + module logger
- ansi (17)           strip_ansi_escape_codes + _ANSI_ESCAPE_RE
- adb_compat (44)     5 个 thin wrapper(forward 到 gui.gui_core_pkg)
- nested (18)         get_nested_value(点号路径配置读取)
- stats_helpers (71)  _parse_countdown_seconds / _bucket / _inc_map(ProcRunner 3 个 @staticmethod)

策略:8 个顶层 helper 直接搬子包;ProcRunner 内 3 个 @staticmethod 也搬,
类内改成 1 行 @staticmethod forwarding 保持 ProcRunner._parse_countdown_seconds
等调用兼容。原文件 import gui.web_gui_pkg 暴露 11 个公共符号。

外部 import 路径全部不变:
- find_adb_executable / read_config / list_adb_devices 等共 70+ 处外部引用
  全部不需修改(CONFIG_PATH 17 处 / read_config 17 处 / find_adb_executable
  18 处 / list_adb_devices 21 处 / write_config 10 处 / 其他 1-7 处)

回归:
- 6 个新文件 + 改写后 web_gui.py compileall 通过
- pytest 353 passed / 1 skipped
- _wg_log / strip_ansi_escape_codes / get_nested_value 调用结果正确
- ProcRunner._parse_countdown_seconds('1:30') = 90 (薄壳 forwarding OK)
- ProcRunner._bucket(15, [0,10,50,100]) = '10-50'
- ProcRunner._inc_map(m, 'k', 5) -> {'k': 5}
- ProcRunner() 实例化 OK

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 18:57:52 +08:00

72 lines
1.9 KiB
Python

"""web_gui_pkg.stats_helpers - 统计辅助函数(从 ProcRunner @staticmethod 搬出)。
- _parse_countdown_seconds 解析倒计时文本到秒数(支持 hh:mm:ss / 中文 / 纯数字)
- _bucket 按 edges 数组将值分桶(用于直方图)
- _inc_map 字典内某 key 累加 n(缺失时初始为 0)
"""
from __future__ import annotations
import re
from typing import Optional
def _parse_countdown_seconds(text: str) -> Optional[int]:
t = str(text or "").strip()
if not t:
return None
# 优先解析 hh:mm:ss / mm:ss
if ":" in t:
parts = [p for p in t.split(":") if p != ""]
try:
nums = [int(p) for p in parts]
except Exception:
nums = []
if len(nums) == 2:
mm, ss = nums
return max(0, mm * 60 + ss)
if len(nums) == 3:
hh, mm, ss = nums
return max(0, hh * 3600 + mm * 60 + ss)
# 解析中文:xx小时xx分xx秒
total = 0
m = re.search(r"(\d+)\s*小时", t)
if m:
total += int(m.group(1)) * 3600
m = re.search(r"(\d+)\s*分", t)
if m:
total += int(m.group(1)) * 60
m = re.search(r"(\d+)\s*秒", t)
if m:
total += int(m.group(1))
if total > 0:
return total
# 兜底:纯数字当秒
if re.fullmatch(r"\d+", t):
try:
return int(t)
except Exception:
return None
return None
def _bucket(value: float, edges: list[float]) -> str:
# edges 例如 [0,10,50,100] => 0-10,10-50,50-100,100+
if not edges:
return "all"
for i in range(len(edges) - 1):
a = edges[i]
b = edges[i + 1]
if value >= a and value < b:
return f"{a:g}-{b:g}"
return f"{edges[-1]:g}+"
def _inc_map(m: dict, k: str, n: int = 1) -> None:
m[k] = int(m.get(k, 0)) + int(n)