@kevlns/xlmerge 2.0.3 → 2.0.5

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.
@@ -3,6 +3,8 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import json
6
+ import os
7
+ import uuid
6
8
  import sys
7
9
  import threading
8
10
  import webbrowser
@@ -16,6 +18,7 @@ if str(SCRIPT_DIR) not in sys.path:
16
18
  sys.path.insert(0, str(SCRIPT_DIR))
17
19
 
18
20
  from xlsx_conflict import ConflictError, _build_commit_message, apply_manifest, load_manifest
21
+ from session import save_session
19
22
  from prepare_progress import preparation_context, PreparationCancelled, checkpoint
20
23
 
21
24
 
@@ -73,6 +76,9 @@ def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0
73
76
  status = {'state': 'preparing' if prepare_repo else 'ready', 'phase': '开始解析',
74
77
  'file': '', 'fileDone': 0, 'fileTotal': 0, 'sheet': '', 'sheetDone': 0, 'sheetTotal': 0, 'side': ''}
75
78
 
79
+ receipt = {"sessionId": uuid.uuid4().hex, "pid": os.getpid(), "manifest": str(manifest_path), "state": "running"}
80
+ receipt_file = manifest_path.parent / "session.json"
81
+
76
82
  def report(progress):
77
83
  with lock:
78
84
  status.update(progress)
@@ -109,6 +115,11 @@ def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0
109
115
  with lock:
110
116
  return _json(dict(status))
111
117
 
118
+ @app.get("/api/session")
119
+ def session_status():
120
+ with lock:
121
+ return _json({**receipt, "progress": dict(status)})
122
+
112
123
  @app.get("/api/diff")
113
124
  def diff():
114
125
  if manifest is None:
@@ -141,6 +152,7 @@ def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0
141
152
  message=(payload.get("message") or "").strip() or None,
142
153
  )
143
154
  if result["success"]:
155
+ receipt.update(state="completed", result=result)
144
156
  state["resolved"] = True
145
157
  threading.Timer(2.0, state["server"].shutdown).start()
146
158
  return _json(result, 200 if result["success"] else 500)
@@ -153,6 +165,10 @@ def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0
153
165
 
154
166
  @app.post("/api/shutdown")
155
167
  def shutdown():
168
+ _payload()
169
+ identity = request.headers.get("X-Xlmerge-Session")
170
+ if identity is not None and identity != receipt["sessionId"]:
171
+ return _json({"success": False, "error": "审阅身份不一致"}, 409)
156
172
  with lock:
157
173
  if status['state'] == 'preparing':
158
174
  cancelled.set()
@@ -169,11 +185,19 @@ def serve(manifest_path: Path | str, *, open_browser: bool = True, port: int = 0
169
185
  if worker:
170
186
  worker.start()
171
187
  url = f"http://127.0.0.1:{server.server_port}"
188
+ receipt["url"] = url
189
+ save_session(receipt_file, receipt)
172
190
  print(f"MERGE_SERVER_URL={url}", flush=True)
173
191
  if open_browser:
174
192
  webbrowser.open(url)
175
193
  try:
176
194
  server.serve_forever()
177
195
  finally:
196
+ cancelled.set()
197
+ if worker:
198
+ worker.join()
178
199
  server.server_close()
200
+ if receipt["state"] != "completed":
201
+ receipt["state"] = "failed" if status["state"] == "failed" else "cancelled"
202
+ save_session(receipt_file, receipt)
179
203
  return bool(state["resolved"])