-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththeme_io.py
More file actions
365 lines (302 loc) · 11.2 KB
/
Copy paththeme_io.py
File metadata and controls
365 lines (302 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from __future__ import annotations
import json
import hashlib
from datetime import datetime
from pathlib import Path
from typing import Callable, Optional
from PySide6.QtCore import QSettings
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget
from crash_logging import log_event, log_exception
THEME_FORMAT_VERSION = 1
class ThemeIOError(RuntimeError):
pass
def export_themes_ui(parent: QWidget, settings: QSettings) -> None:
"""
UI flow:
- pick .json path
- export themes from QSettings
- atomic write
- success/error messagebox
"""
try:
suggested = f"themes_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
out_path, _ = QFileDialog.getSaveFileName(
parent,
"Export themes",
suggested,
"JSON (*.json);;All files (*.*)",
)
if not out_path:
return
log_event(
"Theme export started",
context="theme.export",
details={"target_path": out_path},
)
payload = export_themes_payload(settings)
write_json_atomic(Path(out_path), payload)
log_event(
"Theme export completed",
context="theme.export",
details={"target_path": out_path, "theme_count": len(payload.get("themes") or {})},
)
QMessageBox.information(
parent,
"Themes exported",
f"Themes exported successfully.\n\nFile:\n{out_path}",
)
except Exception as e:
log_exception(e, context="theme.export")
QMessageBox.critical(parent, "Theme export failed", _fmt_exc("Export failed", e))
def import_themes_ui(
parent: QWidget,
settings: QSettings,
apply_callback: Optional[Callable[[], None]] = None,
) -> None:
"""
UI flow:
- pick themes backup .json
- validate checksum + structure
- handle conflicts (overwrite / keep both / skip)
- import into QSettings
- optionally set imported current theme active
- apply_callback() can re-apply theme immediately
"""
try:
in_path, _ = QFileDialog.getOpenFileName(
parent,
"Import themes",
"",
"JSON (*.json);;All files (*.*)",
)
if not in_path:
return
log_event(
"Theme import started",
context="theme.import",
details={"source_path": in_path},
)
payload = read_themes_file(Path(in_path), parent=parent)
report = import_themes_payload(parent, settings, payload)
if report["applied_current"] and apply_callback:
try:
apply_callback()
except Exception:
pass
QMessageBox.information(
parent,
"Themes imported",
_fmt_import_report(report),
)
log_event(
"Theme import completed",
context="theme.import",
details={
"source_path": in_path,
"mode": str(report.get("mode") or ""),
"created": int(report.get("created") or 0),
"overwritten": int(report.get("overwritten") or 0),
"renamed": int(report.get("renamed") or 0),
"skipped": int(report.get("skipped") or 0),
"applied_current": bool(report.get("applied_current")),
},
)
except Exception as e:
log_exception(e, context="theme.import")
QMessageBox.critical(parent, "Theme import failed", _fmt_exc("Import failed", e))
def export_themes_payload(settings: QSettings) -> dict:
names = _get_list(settings, "themes/list")
current = str(settings.value("themes/current") or "")
themes: dict[str, dict] = {}
for name in names:
raw = settings.value(f"themes/data/{name}")
if not raw:
continue
try:
themes[name] = json.loads(str(raw))
except Exception:
themes[name] = {"name": name}
payload = {
"format_version": THEME_FORMAT_VERSION,
"exported_at": _now_iso(),
"current_theme": current,
"themes": themes,
}
payload["checksum_sha256"] = _sha256_canonical_json(payload)
return payload
def import_themes_payload(parent: QWidget, settings: QSettings, payload: dict) -> dict:
_validate_payload(payload)
incoming: dict[str, dict] = payload["themes"]
incoming_current: str = str(payload.get("current_theme") or "")
existing_names = set(_get_list(settings, "themes/list"))
incoming_names = list(incoming.keys())
conflicts = [n for n in incoming_names if n in existing_names]
mode = "merge"
if conflicts:
mode = _prompt_conflict_mode(parent, conflicts)
if mode == "cancel":
raise ThemeIOError("Import cancelled by user.")
final_name_map: dict[str, str] = {}
created = 0
overwritten = 0
skipped = 0
renamed = 0
names_list = _get_list(settings, "themes/list")
names_set = set(names_list)
for name, theme in incoming.items():
if not isinstance(theme, dict):
skipped += 1
continue
final_name = name
if name in names_set:
if mode == "skip":
skipped += 1
continue
if mode == "overwrite":
overwritten += 1
if mode == "keep_both":
final_name = _unique_name(name, names_set)
renamed += 1
theme = dict(theme)
theme["name"] = final_name
settings.setValue(f"themes/data/{final_name}", json.dumps(theme, ensure_ascii=False))
if final_name not in names_set:
names_list.append(final_name)
names_set.add(final_name)
created += 1
final_name_map[name] = final_name
settings.setValue("themes/list", names_list)
applied_current = False
if incoming_current:
mapped = final_name_map.get(incoming_current)
if mapped and mapped in names_set:
res = QMessageBox.question(
parent,
"Set active theme?",
f"Set imported theme '{mapped}' as the active theme now?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.Yes,
)
if res == QMessageBox.StandardButton.Yes:
settings.setValue("themes/current", mapped)
applied_current = True
return {
"mode": mode,
"created": created,
"overwritten": overwritten,
"renamed": renamed,
"skipped": skipped,
"applied_current": applied_current,
}
def write_json_atomic(path: Path, payload: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
tmp.replace(path)
def read_themes_file(path: Path, parent: Optional[QWidget] = None) -> dict:
raw = path.read_text(encoding="utf-8")
payload = json.loads(raw)
_validate_payload(payload)
claimed = str(payload.get("checksum_sha256") or "")
actual = _sha256_canonical_json(payload)
if claimed and claimed != actual:
if parent is not None:
res = QMessageBox.warning(
parent,
"Themes integrity warning",
"Checksum mismatch.\n\n"
"The file may be corrupted or edited.\n\n"
"Continue anyway?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if res != QMessageBox.StandardButton.Yes:
raise ThemeIOError("Import cancelled due to checksum mismatch.")
else:
raise ThemeIOError("Checksum mismatch.")
return payload
def _validate_payload(payload: dict) -> None:
if not isinstance(payload, dict):
raise ThemeIOError("Theme backup is not a JSON object.")
if int(payload.get("format_version", -1)) != THEME_FORMAT_VERSION:
raise ThemeIOError(f"Unsupported theme backup format_version: {payload.get('format_version')}")
if "themes" not in payload or not isinstance(payload["themes"], dict):
raise ThemeIOError("Theme backup missing 'themes' object.")
for k, v in payload["themes"].items():
if not isinstance(k, str) or not k.strip():
raise ThemeIOError("Theme backup contains an invalid theme name.")
if not isinstance(v, dict):
raise ThemeIOError(f"Theme '{k}' data is not an object.")
def _sha256_canonical_json(payload: dict) -> str:
p = dict(payload)
p["checksum_sha256"] = ""
s = json.dumps(p, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(s.encode("utf-8")).hexdigest()
def _get_list(settings: QSettings, key: str) -> list[str]:
v = settings.value(key, [])
if v is None:
return []
if isinstance(v, str):
return [v]
if isinstance(v, (list, tuple)):
return [str(x) for x in v]
return []
def _unique_name(base: str, taken: set[str]) -> str:
cand = f"{base} (imported)"
if cand not in taken:
return cand
i = 2
while True:
cand = f"{base} (imported {i})"
if cand not in taken:
return cand
i += 1
def _prompt_conflict_mode(parent: QWidget, conflicts: list[str]) -> str:
preview = "\n".join([f"• {n}" for n in conflicts[:12]])
if len(conflicts) > 12:
preview += f"\n… and {len(conflicts) - 12} more"
mb = QMessageBox(parent)
mb.setIcon(QMessageBox.Icon.Question)
mb.setWindowTitle("Theme name conflicts")
mb.setText(
"Some imported themes already exist in your app:\n\n"
f"{preview}\n\n"
"Choose how to handle conflicts:"
)
btn_overwrite = mb.addButton("Overwrite existing", QMessageBox.ButtonRole.AcceptRole)
btn_keep = mb.addButton("Keep both (rename imported)", QMessageBox.ButtonRole.ActionRole)
btn_skip = mb.addButton("Skip conflicting", QMessageBox.ButtonRole.ActionRole)
btn_cancel = mb.addButton("Cancel", QMessageBox.ButtonRole.RejectRole)
mb.setDefaultButton(btn_keep)
mb.exec()
clicked = mb.clickedButton()
if clicked == btn_overwrite:
return "overwrite"
if clicked == btn_keep:
return "keep_both"
if clicked == btn_skip:
return "skip"
return "cancel"
def _now_iso() -> str:
return datetime.now().replace(microsecond=0).isoformat(sep=" ")
def _fmt_exc(prefix: str, e: Exception) -> str:
return (
f"{prefix}.\n\n"
f"Type: {type(e).__name__}\n"
f"Details: {e}\n\n"
"Tip: run from a terminal to see full tracebacks if needed."
)
def _fmt_import_report(r: dict) -> str:
lines = [
"Theme import completed.",
"",
f"Conflict mode: {r['mode']}",
f"New themes added: {r['created']}",
f"Themes overwritten: {r['overwritten']}",
f"Themes renamed: {r['renamed']}",
f"Themes skipped: {r['skipped']}",
]
if r.get("applied_current"):
lines.append("")
lines.append("Active theme was updated.")
return "\n".join(lines)