-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_wizard.py
More file actions
214 lines (180 loc) · 7.31 KB
/
Copy pathsetup_wizard.py
File metadata and controls
214 lines (180 loc) · 7.31 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
"""First-run setup wizard for UnityScraper."""
from __future__ import annotations
import json
import tkinter as tk
from pathlib import Path
from tkinter import filedialog, messagebox, ttk
from app_paths import (
CONFIG_PATH,
DOWNLOADS_DIR,
FIRST_RUN_PATH,
TITLEIDS_PATH,
ensure_app_dirs,
ensure_user_titleids_file,
)
from platform_support import desktop_font_family
from knowledge_scheduler import KnowledgeScheduler
UI_FONT = desktop_font_family()
class SetupWizard(tk.Toplevel):
"""Small first-run wizard that records safe defaults."""
def __init__(self, parent: tk.Misc) -> None:
super().__init__(parent)
self.parent = parent
self.completed = False
self.title("Welcome to UnityScraper")
self.geometry("650x610")
self.resizable(False, False)
self.transient(parent)
self.grab_set()
ensure_app_dirs()
ensure_user_titleids_file()
self.output_var = tk.StringVar(value=str(DOWNLOADS_DIR))
self.titleids_var = tk.StringVar()
self.collection_var = tk.StringVar()
self.catalog_var = tk.BooleanVar(value=True)
self.knowledge_var = tk.BooleanVar(value=False)
self.refresh_days_var = tk.IntVar(value=7)
self._build()
def _build(self) -> None:
container = ttk.Frame(self, padding=24)
container.pack(fill=tk.BOTH, expand=True)
ttk.Label(
container,
text="Set up your Xbox 360 archive",
font=(UI_FONT, 18, "bold"),
).pack(anchor=tk.W)
ttk.Label(
container,
text=(
"UnityScraper scans XboxUnity metadata first, then lets you "
"choose which covers and compatible title updates to download."
),
wraplength=560,
).pack(anchor=tk.W, pady=(8, 22))
ttk.Label(container, text="Archive folder").pack(anchor=tk.W)
folder_row = ttk.Frame(container)
folder_row.pack(fill=tk.X, pady=(5, 16))
ttk.Entry(folder_row, textvariable=self.output_var).pack(
side=tk.LEFT, fill=tk.X, expand=True
)
ttk.Button(folder_row, text="Browse...", command=self._browse).pack(
side=tk.LEFT, padx=(8, 0)
)
ttk.Label(container, text="Optional TitleIDs").pack(anchor=tk.W)
ttk.Entry(container, textvariable=self.titleids_var).pack(
fill=tk.X, pady=(5, 4)
)
ttk.Label(
container,
text="Comma-separated, for example: 4D53082D, 584109A8",
).pack(anchor=tk.W)
ttk.Label(container, text="Optional collection folder").pack(anchor=tk.W, pady=(16, 0))
collection_row = ttk.Frame(container)
collection_row.pack(fill=tk.X, pady=(5, 4))
ttk.Entry(collection_row, textvariable=self.collection_var).pack(
side=tk.LEFT, fill=tk.X, expand=True
)
ttk.Button(collection_row, text="Browse", command=self._browse_collection).pack(
side=tk.LEFT, padx=(8, 0)
)
sources = ttk.LabelFrame(container, text="Local metadata", padding=10)
sources.pack(fill=tk.X, pady=(12, 0))
ttk.Checkbutton(
sources,
text="Pre-cache the XboxUnity title catalog for offline autocomplete",
variable=self.catalog_var,
).grid(row=0, column=0, columnspan=3, sticky="w")
ttk.Checkbutton(
sources,
text="Refresh ConsoleMods, XenonLibrary, and Free60 knowledge automatically",
variable=self.knowledge_var,
).grid(row=1, column=0, columnspan=3, sticky="w", pady=(6, 0))
ttk.Label(sources, text="Every").grid(row=2, column=0, sticky="w", pady=(6, 0))
ttk.Spinbox(
sources, from_=1, to=365, width=6, textvariable=self.refresh_days_var
).grid(row=2, column=1, sticky="w", padx=5, pady=(6, 0))
ttk.Label(sources, text="days").grid(row=2, column=2, sticky="w", pady=(6, 0))
ttk.Separator(container).pack(fill=tk.X, pady=22)
ttk.Label(
container,
text=(
"Recommended defaults will be used for request rate, retries, "
"worker count, and timeouts. These can be changed later under Settings."
),
wraplength=560,
).pack(anchor=tk.W)
buttons = ttk.Frame(container)
buttons.pack(side=tk.BOTTOM, fill=tk.X)
ttk.Button(buttons, text="Cancel", command=self.destroy).pack(side=tk.RIGHT)
ttk.Button(
buttons,
text="Finish Setup",
command=self._finish,
).pack(side=tk.RIGHT, padx=(0, 8))
def _browse(self) -> None:
selected = filedialog.askdirectory(
parent=self,
initialdir=self.output_var.get(),
title="Choose archive folder",
)
if selected:
self.output_var.set(selected)
def _browse_collection(self) -> None:
selected = filedialog.askdirectory(parent=self, title="Choose Xbox 360 collection")
if selected:
self.collection_var.set(selected)
def _finish(self) -> None:
output = Path(self.output_var.get()).expanduser()
try:
output.mkdir(parents=True, exist_ok=True)
except OSError as exc:
messagebox.showerror(
"Unable to use folder",
f"UnityScraper could not create or use that folder:\n\n{exc}",
parent=self,
)
return
config: dict[str, object] = {}
if CONFIG_PATH.exists():
try:
config = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
config = {}
config.update(
{
"output_dir": str(output),
"workers": int(config.get("workers", 4)),
"rate_limit": float(config.get("rate_limit", 0.35)),
"timeout": int(config.get("timeout", 30)),
"max_retries": int(config.get("max_retries", 3)),
"collection_roots": (
[self.collection_var.get().strip()]
if self.collection_var.get().strip()
else config.get("collection_roots", [])
),
"ui_scale": float(config.get("ui_scale", 1.0)),
"sync_title_catalog_on_start": self.catalog_var.get(),
"language": str(config.get("language", "en")),
}
)
CONFIG_PATH.write_text(json.dumps(config, indent=2), encoding="utf-8")
titleids = [
value.strip().upper()
for value in self.titleids_var.get().replace("\n", ",").split(",")
if value.strip()
]
if titleids:
TITLEIDS_PATH.write_text(",".join(dict.fromkeys(titleids)), encoding="utf-8")
KnowledgeScheduler().configure(
self.knowledge_var.get(), max(1, self.refresh_days_var.get()) * 24
)
FIRST_RUN_PATH.write_text("complete\n", encoding="utf-8")
self.completed = True
self.destroy()
def run_first_run_wizard(parent: tk.Misc) -> bool:
"""Run the wizard when setup has not yet been completed."""
if FIRST_RUN_PATH.exists():
return True
wizard = SetupWizard(parent)
parent.wait_window(wizard)
return wizard.completed