# -*- coding: utf-8 -*- r'''修上一步弄坏的登录表单。 上一步的正则插账号时把字典插坏了: urlencode({"username": "xmz7891", {"password": "120830"}) ^ 多了个左括号 应该是: urlencode({"username": "xmz7891", "password": "120830"}) ''' from __future__ import annotations import ast import re import sys from pathlib import Path TESTS = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\_tests") ADMIN_FILES = [ "test_admin_js.py", "test_admin_batch_delete.py", "test_admin_delete.py", "test_admin_button.py", "test_admin_netcheck.py", "test_relay_admin_login.py", ] # 把插坏的 {"username": "...", {"password": ... 还原成正常的 BROKEN = re.compile(r'\{"username":\s*"xmz7891",\s*\{\s*"password"') FIXED = '{"username": "xmz7891", "password"' def main() -> int: for name in ADMIN_FILES: path = TESTS / name if not path.is_file(): continue text = path.read_text(encoding="utf-8") # 1) 先修字典 text = BROKEN.sub(FIXED, text) # 2) 保险:如果还有 "password" 前面紧跟 { 的,把多余的 { 去掉 text = re.sub(r'\{\s*,\s*"password"', ', "password"', text) text = text.replace('{"username": "xmz7891", {', '{"username": "xmz7891", {') path.write_text(text, encoding="utf-8", newline="\n") print(" 语法检查:") ok = True for name in ADMIN_FILES: path = TESTS / name if not path.is_file(): continue try: ast.parse(path.read_text(encoding="utf-8")) print(f" {name}: OK") except SyntaxError as exc: print(f" [失败] {name}: 行 {exc.lineno}: {exc.msg}") lines = path.read_text(encoding="utf-8").splitlines() if exc.lineno and exc.lineno <= len(lines): print(f" {lines[exc.lineno - 1].strip()}") ok = False print() print(" 登录表单现在的样子:") for name in ("test_admin_js.py",): path = TESTS / name for line in path.read_text(encoding="utf-8").splitlines(): if "urlencode" in line: print(f" {line.strip()}") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())