# -*- coding: utf-8 -*- r'''把官网渲染换成新的独立模块 site.render_site()。 原来的 _site_page() 是内联在 relay.py 里的一大段 HTML。 现在拆到 relay/site.py,relay.py 里只留一个薄封装。 ''' from __future__ import annotations import ast import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") RELAY = ROOT / "relay" / "relay.py" # 找出 _site_page 函数的起止,整段替换 NEW_FUNC = '''def _site_page() -> str: """官网首页(公开,不用登录)。 实际渲染在 relay/site.py —— relay.py 已经两千多行, 再放几百行 HTML 就没法看了。网站属于展示层, 跟中转逻辑分开更清楚。 """ try: from site import render_site except ImportError: # 打包/部署时 relay 目录就是工作目录,用相对导入兜底 import importlib.util spec = importlib.util.spec_from_file_location( "ykxmz_site", Path(__file__).resolve().parent / "site.py") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) render_site = module.render_site return render_site(_list_downloads()) ''' def main() -> int: text = RELAY.read_text(encoding="utf-8") if "from site import render_site" in text: print(" 已经接过了") return 0 start = text.find("def _site_page() -> str:") if start < 0: print(" [失败] 找不到 _site_page") return 1 # 找到下一个顶层 def nxt = text.find("\ndef ", start + 10) if nxt < 0: print(" [失败] 找不到 _site_page 的结尾") return 1 text = text[:start] + NEW_FUNC + "\n" + text[nxt + 1:] # 确保 Path 和 _list_downloads 可用 if "\nfrom pathlib import Path" not in text: text = text.replace("import os\n", "import os\nfrom pathlib import Path\n", 1) print(" 补 from pathlib import Path") RELAY.write_text(text, encoding="utf-8", newline="\n") print(" _site_page 改成调用 site.render_site()") try: ast.parse(text) print(" relay.py 语法 OK") except SyntaxError as exc: print(" [失败] 语法错误: 行 %s: %s" % (exc.lineno, exc.msg)) lines = text.splitlines() for i in range(max(0, (exc.lineno or 1) - 3), min(len(lines), (exc.lineno or 1) + 2)): print(" %d: %s" % (i + 1, lines[i][:90])) return 1 # site.py 也检查一下 try: ast.parse((ROOT / "relay" / "site.py").read_text(encoding="utf-8")) print(" site.py 语法 OK") except SyntaxError as exc: print(" [失败] site.py 语法错误: 行 %s: %s" % (exc.lineno, exc.msg)) return 1 return 0 if __name__ == "__main__": sys.exit(main())