# -*- coding: utf-8 -*-
"""把每个
的值包进 (按行精确替换)。
上一版锚点因为行尾一个多余空格没匹配上。
这次直接按行号/逐行替换,稳一点。
"""
from __future__ import annotations
import ast
import sys
from pathlib import Path
RELAY = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\relay" r"\relay.py")
# 逐行替换:原行 -> 新行
REPLACEMENTS = [
(
""" f' | {_esc(device["code"])} | '""",
""" f''
f'{_esc(device["code"])} | '""",
),
(
""" f'{badge} | '""",
""" f'{badge} | '""",
),
(
""" f'{device["idleSeconds"]}s | '""",
""" f''
f'{device["idleSeconds"]}s | '""",
),
(
""" f'{device["queuedToClient"]} / '""",
""" f' | '
f'{device["queuedToClient"]} / '""",
),
(
""" f'{device["queuedToAgent"]} | '""",
""" f'{device["queuedToAgent"]}'""",
),
(
""" f''""",
""" f' | '""",
),
(
""" f'{_kb(device["bytesIn"])} / {_kb(device["bytesOut"])} | '""",
""" f'{_kb(device["bytesIn"])} / {_kb(device["bytesOut"])}'
f''""",
),
]
def main() -> int:
text = RELAY.read_text(encoding="utf-8")
ok = True
for old, new in REPLACEMENTS:
if new.strip().splitlines()[0] in text and old not in text:
print(f" 已改过: {old.strip()[:44]}")
continue
if old not in text:
print(f" [失败] 没找到: {old.strip()[:56]!r}")
ok = False
continue
text = text.replace(old, new, 1)
print(f" ✓ {old.strip()[:48]}")
RELAY.write_text(text, encoding="utf-8", newline="\n")
ast.parse(text)
print(" relay.py 语法 OK")
# 复核
print(" 复核:")
print(f" span.val 出现 {text.count('class=val')} 次")
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())