@kevlns/xlmerge 2.0.0 → 2.0.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.
@@ -1,126 +1,126 @@
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 _payload() -> dict:
22
- """Read raw JSON without Bottle's 100 KiB ``request.json`` form-memory limit."""
23
- if not request.content_length:
24
- return {}
25
- data = request.body.read()
26
- if not data:
27
- return {}
28
- parsed = json.loads(data.decode("utf-8"))
29
- return parsed if isinstance(parsed, dict) else {}
30
-
31
-
32
- def _json(data, status: int = 200):
33
- response.status = status
34
- response.content_type = "application/json; charset=utf-8"
35
- return json.dumps(data, ensure_ascii=False)
36
-
37
-
38
- def _public_manifest(manifest):
39
- return {
40
- "summary": manifest["summary"],
41
- "files": [
42
- {
43
- "id": item["id"],
44
- "path": item["path"],
45
- "oursAuthor": item.get("oursAuthor", ""),
46
- "theirsAuthor": item.get("theirsAuthor", ""),
47
- "oursDate": item.get("oursDate", ""),
48
- "theirsDate": item.get("theirsDate", ""),
49
- "macro": item.get("macro"),
50
- "diff": item["diff"],
51
- }
52
- for item in manifest["files"]
53
- ],
54
- }
55
-
56
-
57
- def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0) -> bool:
58
- manifest_path = Path(manifest_path).resolve()
59
- manifest = load_manifest(manifest_path)
60
- if not manifest["files"]:
61
- print(
62
- "Manifest has no conflicts requiring review; use resolve/apply for auto-theirs entries.",
63
- file=sys.stderr,
64
- )
65
- return False
66
- html_path = Path(__file__).with_name("merge_ui.html")
67
- app = Bottle()
68
- state = {"resolved": False, "server": None}
69
-
70
- @app.get("/")
71
- def index():
72
- response.content_type = "text/html; charset=utf-8"
73
- return html_path.read_text(encoding="utf-8")
74
-
75
- @app.get("/api/diff")
76
- def diff():
77
- return _json(_public_manifest(manifest))
78
-
79
- @app.post("/api/commit-preview")
80
- def commit_preview():
81
- """只读:按当前 decisions 生成结构化 commit 信息,不执行任何写操作。"""
82
- payload = _payload()
83
- try:
84
- message = _build_commit_message(manifest, payload.get("decisions") or {})
85
- return _json({"message": message})
86
- except (ConflictError, OSError, ValueError, KeyError) as exc:
87
- return _json({"success": False, "error": str(exc)}, 400)
88
-
89
- @app.post("/api/resolve")
90
- def resolve():
91
- payload = _payload()
92
- try:
93
- result = apply_manifest(
94
- manifest_path,
95
- payload.get("decisions") or {},
96
- commit=bool(payload.get("commit", True)),
97
- push=bool(payload.get("push", False)),
98
- message=(payload.get("message") or "").strip() or None,
99
- )
100
- if result["success"]:
101
- state["resolved"] = True
102
- threading.Timer(2.0, state["server"].shutdown).start()
103
- return _json(result, 200 if result["success"] else 500)
104
- except (ConflictError, OSError, ValueError, KeyError) as exc:
105
- return _json({"success": False, "error": str(exc)}, 400)
106
-
107
- @app.error(413)
108
- def too_large(error):
109
- return _json({"success": False, "error": "request body too large"}, 413)
110
-
111
- @app.post("/api/shutdown")
112
- def shutdown():
113
- threading.Thread(target=state["server"].shutdown, daemon=True).start()
114
- return _json({"success": True})
115
-
116
- server = make_server("127.0.0.1", port, app)
117
- state["server"] = server
118
- url = f"http://127.0.0.1:{server.server_port}"
119
- print(f"MERGE_SERVER_URL={url}", flush=True)
120
- if open_browser:
121
- webbrowser.open(url)
122
- try:
123
- server.serve_forever()
124
- finally:
125
- server.server_close()
126
- return bool(state["resolved"])
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 _payload() -> dict:
22
+ """Read raw JSON without Bottle's 100 KiB ``request.json`` form-memory limit."""
23
+ if not request.content_length:
24
+ return {}
25
+ data = request.body.read()
26
+ if not data:
27
+ return {}
28
+ parsed = json.loads(data.decode("utf-8"))
29
+ return parsed if isinstance(parsed, dict) else {}
30
+
31
+
32
+ def _json(data, status: int = 200):
33
+ response.status = status
34
+ response.content_type = "application/json; charset=utf-8"
35
+ return json.dumps(data, ensure_ascii=False)
36
+
37
+
38
+ def _public_manifest(manifest):
39
+ return {
40
+ "summary": manifest["summary"],
41
+ "files": [
42
+ {
43
+ "id": item["id"],
44
+ "path": item["path"],
45
+ "oursAuthor": item.get("oursAuthor", ""),
46
+ "theirsAuthor": item.get("theirsAuthor", ""),
47
+ "oursDate": item.get("oursDate", ""),
48
+ "theirsDate": item.get("theirsDate", ""),
49
+ "macro": item.get("macro"),
50
+ "diff": item["diff"],
51
+ }
52
+ for item in manifest["files"]
53
+ ],
54
+ }
55
+
56
+
57
+ def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0) -> bool:
58
+ manifest_path = Path(manifest_path).resolve()
59
+ manifest = load_manifest(manifest_path)
60
+ if not manifest["files"]:
61
+ print(
62
+ "Manifest has no conflicts requiring review; use resolve/apply for auto-theirs entries.",
63
+ file=sys.stderr,
64
+ )
65
+ return False
66
+ html_path = Path(__file__).with_name("merge_ui.html")
67
+ app = Bottle()
68
+ state = {"resolved": False, "server": None}
69
+
70
+ @app.get("/")
71
+ def index():
72
+ response.content_type = "text/html; charset=utf-8"
73
+ return html_path.read_text(encoding="utf-8")
74
+
75
+ @app.get("/api/diff")
76
+ def diff():
77
+ return _json(_public_manifest(manifest))
78
+
79
+ @app.post("/api/commit-preview")
80
+ def commit_preview():
81
+ """只读:按当前 decisions 生成结构化 commit 信息,不执行任何写操作。"""
82
+ payload = _payload()
83
+ try:
84
+ message = _build_commit_message(manifest, payload.get("decisions") or {})
85
+ return _json({"message": message})
86
+ except (ConflictError, OSError, ValueError, KeyError) as exc:
87
+ return _json({"success": False, "error": str(exc)}, 400)
88
+
89
+ @app.post("/api/resolve")
90
+ def resolve():
91
+ payload = _payload()
92
+ try:
93
+ result = apply_manifest(
94
+ manifest_path,
95
+ payload.get("decisions") or {},
96
+ commit=bool(payload.get("commit", True)),
97
+ push=bool(payload.get("push", False)),
98
+ message=(payload.get("message") or "").strip() or None,
99
+ )
100
+ if result["success"]:
101
+ state["resolved"] = True
102
+ threading.Timer(2.0, state["server"].shutdown).start()
103
+ return _json(result, 200 if result["success"] else 500)
104
+ except (ConflictError, OSError, ValueError, KeyError) as exc:
105
+ return _json({"success": False, "error": str(exc)}, 400)
106
+
107
+ @app.error(413)
108
+ def too_large(error):
109
+ return _json({"success": False, "error": "request body too large"}, 413)
110
+
111
+ @app.post("/api/shutdown")
112
+ def shutdown():
113
+ threading.Thread(target=state["server"].shutdown, daemon=True).start()
114
+ return _json({"success": True})
115
+
116
+ server = make_server("127.0.0.1", port, app)
117
+ state["server"] = server
118
+ url = f"http://127.0.0.1:{server.server_port}"
119
+ print(f"MERGE_SERVER_URL={url}", flush=True)
120
+ if open_browser:
121
+ webbrowser.open(url)
122
+ try:
123
+ server.serve_forever()
124
+ finally:
125
+ server.server_close()
126
+ return bool(state["resolved"])