@kevlns/xlmerge 1.2.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Debussy XLSX/XLSM 冲突解析器(detect/prepare/launch/apply)。"""
@@ -0,0 +1,4 @@
1
+ from xlsx_resolver.resolve_xlsx_conflict import main
2
+
3
+ if __name__ == "__main__":
4
+ raise SystemExit(main())
@@ -0,0 +1,105 @@
1
+ """Local-only HTTP server for the sheet-aware XLSX resolver UI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import sys
7
+ import threading
8
+ import webbrowser
9
+ from pathlib import Path
10
+ from wsgiref.simple_server import make_server
11
+
12
+ from bottle import Bottle, request, response
13
+
14
+ SCRIPT_DIR = Path(__file__).resolve().parent
15
+ if str(SCRIPT_DIR) not in sys.path:
16
+ sys.path.insert(0, str(SCRIPT_DIR))
17
+
18
+ from xlsx_conflict import ConflictError, _build_commit_message, apply_manifest, load_manifest
19
+
20
+
21
+ def _json(data, status: int = 200):
22
+ response.status = status
23
+ response.content_type = "application/json; charset=utf-8"
24
+ return json.dumps(data, ensure_ascii=False)
25
+
26
+
27
+ def _public_manifest(manifest):
28
+ return {
29
+ "summary": manifest["summary"],
30
+ "files": [
31
+ {
32
+ "id": item["id"],
33
+ "path": item["path"],
34
+ "oursAuthor": item.get("oursAuthor", ""),
35
+ "theirsAuthor": item.get("theirsAuthor", ""),
36
+ "oursDate": item.get("oursDate", ""),
37
+ "theirsDate": item.get("theirsDate", ""),
38
+ "macro": item.get("macro"),
39
+ "diff": item["diff"],
40
+ }
41
+ for item in manifest["files"]
42
+ ],
43
+ }
44
+
45
+
46
+ def serve(manifest_path: Path | str, *, open_browser: bool = True) -> bool:
47
+ manifest_path = Path(manifest_path).resolve()
48
+ manifest = load_manifest(manifest_path)
49
+ html_path = Path(__file__).with_name("merge_ui.html")
50
+ app = Bottle()
51
+ state = {"resolved": False, "server": None}
52
+
53
+ @app.get("/")
54
+ def index():
55
+ response.content_type = "text/html; charset=utf-8"
56
+ return html_path.read_text(encoding="utf-8")
57
+
58
+ @app.get("/api/diff")
59
+ def diff():
60
+ return _json(_public_manifest(manifest))
61
+
62
+ @app.post("/api/commit-preview")
63
+ def commit_preview():
64
+ """只读:按当前 decisions 生成结构化 commit 信息,不执行任何写操作。"""
65
+ payload = request.json or {}
66
+ try:
67
+ message = _build_commit_message(manifest, payload.get("decisions") or {})
68
+ return _json({"message": message})
69
+ except (ConflictError, OSError, ValueError, KeyError) as exc:
70
+ return _json({"success": False, "error": str(exc)}, 400)
71
+
72
+ @app.post("/api/resolve")
73
+ def resolve():
74
+ payload = request.json or {}
75
+ try:
76
+ result = apply_manifest(
77
+ manifest_path,
78
+ payload.get("decisions") or {},
79
+ commit=bool(payload.get("commit", True)),
80
+ push=bool(payload.get("push", False)),
81
+ message=(payload.get("message") or "").strip() or None,
82
+ )
83
+ if result["success"]:
84
+ state["resolved"] = True
85
+ threading.Timer(2.0, state["server"].shutdown).start()
86
+ return _json(result, 200 if result["success"] else 500)
87
+ except (ConflictError, OSError, ValueError, KeyError) as exc:
88
+ return _json({"success": False, "error": str(exc)}, 400)
89
+
90
+ @app.post("/api/shutdown")
91
+ def shutdown():
92
+ threading.Thread(target=state["server"].shutdown, daemon=True).start()
93
+ return _json({"success": True})
94
+
95
+ server = make_server("127.0.0.1", 0, app)
96
+ state["server"] = server
97
+ url = f"http://127.0.0.1:{server.server_port}"
98
+ print(f"MERGE_SERVER_URL={url}", flush=True)
99
+ if open_browser:
100
+ webbrowser.open(url)
101
+ try:
102
+ server.serve_forever()
103
+ finally:
104
+ server.server_close()
105
+ return bool(state["resolved"])