-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_manager.py
More file actions
366 lines (309 loc) · 13.7 KB
/
data_manager.py
File metadata and controls
366 lines (309 loc) · 13.7 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
366
# Copyright © 2026 EddieChan1993. All rights reserved.
# Unauthorized commercial use is strictly prohibited.
import json
import os
import shutil
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional
LOCAL_DIR = Path.home() / ".cambridge_tool"
CACHE_FILE = LOCAL_DIR / "cache.json"
CACHE_EXPIRY_DAYS = 7
class DataManager:
def __init__(self, sync_dir: str = ""):
LOCAL_DIR.mkdir(exist_ok=True)
self._sync_dir: Optional[Path] = self._resolve_sync_dir(sync_dir)
if self._sync_dir:
self._sync_dir.mkdir(parents=True, exist_ok=True)
self.history = self._load(self._history_file, [])
self.favorites = self._load(self._favorites_file, {})
self._cache = None # lazy: loaded only on first word lookup
# word_key → [audio_urls]: lets us evict audio when its word expires/is removed
self._audio_url_map: dict = {}
# Track file mtimes to detect external changes (e.g. cloud sync from another device)
self._history_mtime = self._mtime(self._history_file)
self._favorites_mtime = self._mtime(self._favorites_file)
@property
def _history_file(self) -> Path:
return (self._sync_dir / "history.json") if self._sync_dir \
else (LOCAL_DIR / "history.json")
@property
def _favorites_file(self) -> Path:
return (self._sync_dir / "favorites.json") if self._sync_dir \
else (LOCAL_DIR / "favorites.json")
def has_local_data(self) -> bool:
return (LOCAL_DIR / "history.json").exists() \
or (LOCAL_DIR / "favorites.json").exists()
def sync_path_has_data(self, path: str) -> bool:
p = self._resolve_sync_dir(path)
return p is not None and (
(p / "history.json").exists() or (p / "favorites.json").exists()
)
def migrate_local_to_path(self, dest: str):
"""Copy local history/favorites to dest/HotDict/ (skip if already present)."""
dest_dir = self._resolve_sync_dir(dest)
dest_dir.mkdir(parents=True, exist_ok=True)
for fname in ("history.json", "favorites.json"):
src = LOCAL_DIR / fname
dst = dest_dir / fname
if src.exists() and not dst.exists():
shutil.copy2(src, dst)
@staticmethod
def _resolve_sync_dir(path: str) -> Optional[Path]:
"""Append HotDict subfolder so files don't clutter the cloud root."""
return (Path(path) / "HotDict") if path else None
def set_sync_dir(self, path: str):
"""Switch active data directory and reload history/favorites."""
self._sync_dir = self._resolve_sync_dir(path)
if self._sync_dir:
self._sync_dir.mkdir(parents=True, exist_ok=True)
self.history = self._load(self._history_file, [])
self.favorites = self._load(self._favorites_file, {})
self._history_mtime = self._mtime(self._history_file)
self._favorites_mtime = self._mtime(self._favorites_file)
@property
def cache(self):
if self._cache is None:
self._cache = self._load(CACHE_FILE, {})
return self._cache
@cache.setter
def cache(self, value):
self._cache = value
# In-memory audio cache: URL → MP3 bytes.
# Bounded to cached_word_count × 2 (UK + US per word), cleared with word cache.
audio_cache: dict = {}
@staticmethod
def _mtime(path: Path):
"""Return file mtime float, or None if the file doesn't exist."""
try:
return path.stat().st_mtime
except OSError:
return None
def _load(self, path, default):
try:
if path.exists():
with open(path, encoding="utf-8") as f:
return json.load(f)
except Exception:
pass
return default
def _save(self, path, data):
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
# Keep tracked mtime in sync so check_and_reload won't re-read our own write
if path == self._history_file:
self._history_mtime = self._mtime(path)
elif path == self._favorites_file:
self._favorites_mtime = self._mtime(path)
except Exception as e:
print(f"Save error: {e}")
def check_and_reload(self) -> tuple:
"""
Compare current file mtimes against stored values.
Reload from disk only if changed externally (e.g. cloud sync from another device).
Returns (history_changed, favorites_changed).
"""
hist_changed = fav_changed = False
new_h = self._mtime(self._history_file)
if new_h != self._history_mtime:
self.history = self._load(self._history_file, [])
self._history_mtime = new_h
hist_changed = True
new_f = self._mtime(self._favorites_file)
if new_f != self._favorites_mtime:
self.favorites = self._load(self._favorites_file, {})
self._favorites_mtime = new_f
fav_changed = True
return hist_changed, fav_changed
# ── Cache ────────────────────────────────────────────────────────────────
def _evict_audio(self, key: str):
"""Remove audio cache entries that belong to the given word key."""
for url in self._audio_url_map.pop(key, []):
self.audio_cache.pop(url, None)
def get_cached(self, word: str):
key = word.lower().strip()
entry = self.cache.get(key)
if entry:
try:
ts = datetime.fromisoformat(entry["cached_at"])
if datetime.now() - ts < timedelta(days=CACHE_EXPIRY_DAYS):
return entry["data"]
except Exception:
pass
del self.cache[key]
self._evict_audio(key) # word expired → evict its audio too
return None
def set_cached(self, word: str, data: dict):
key = word.lower().strip()
self.cache[key] = {"cached_at": datetime.now().isoformat(), "data": data}
# Record which audio URLs belong to this word so they can be evicted together
urls = [p["audio"] for p in data.get("pronunciations", []) if p.get("audio")]
if urls:
self._audio_url_map[key] = urls
self._save(CACHE_FILE, self.cache)
# ── History ──────────────────────────────────────────────────────────────
HISTORY_CAP = 1000
def add_history(self, word: str):
word = word.strip()
if not word:
return
now = datetime.now().isoformat()
existing = next(
(h for h in self.history if h["word"].lower() == word.lower()), None
)
if existing:
self.history.remove(existing)
existing["time"] = now
existing["count"] = existing.get("count", 1) + 1
self.history.insert(0, existing)
else:
self.history.insert(0, {"word": word, "time": now, "count": 1})
if len(self.history) > self.HISTORY_CAP:
self._lfu_evict()
self._save(self._history_file, self.history)
def _lfu_evict(self):
"""Drop entries down to HISTORY_CAP using LFU + LRU tie-break.
Score = count / (seconds_since_access + 1).
Lowest score is evicted first — infrequent AND stale entries go first.
"""
now = datetime.now()
def _score(h):
try:
age = (now - datetime.fromisoformat(h["time"])).total_seconds()
except Exception:
age = 0
return h.get("count", 1) / (age + 1)
self.history.sort(key=_score, reverse=True)
self.history = self.history[:self.HISTORY_CAP]
def get_history(self) -> list:
return [h["word"] for h in self.history]
def get_today_history_count(self) -> int:
today = datetime.now().date()
count = 0
for h in self.history:
try:
if datetime.fromisoformat(h["time"]).date() == today:
count += 1
except Exception:
pass
return count
def remove_cached(self, word: str):
key = word.lower().strip()
if key in self.cache:
del self.cache[key]
self._evict_audio(key) # remove its audio too
self._save(CACHE_FILE, self.cache)
def clear_cache(self):
self.cache = {}
self._audio_url_map.clear()
DataManager.audio_cache.clear()
self._save(CACHE_FILE, self.cache)
def put_audio_cache(self, url: str, data: bytes):
"""Insert into audio cache, evicting oldest entry when over limit.
Limit = current cached word count × 2 (UK + US), minimum 20.
"""
ac = DataManager.audio_cache
if url in ac:
return
limit = max(20, len(self.cache) * 2)
if len(ac) >= limit:
ac.pop(next(iter(ac))) # evict oldest (FIFO)
ac[url] = data
def remove_history(self, word: str):
self.history = [h for h in self.history if h["word"].lower() != word.lower()]
self._save(self._history_file, self.history)
def clear_history(self):
self.history = []
self._save(self._history_file, self.history)
self.clear_cache()
# ── Favorites ────────────────────────────────────────────────────────────
def toggle_favorite(self, word: str, data: dict = None) -> bool:
"""Returns True if now a favorite, False if removed."""
key = word.lower().strip()
if key in self.favorites:
del self.favorites[key]
self._save(self._favorites_file, self.favorites)
return False
self.favorites[key] = {
"word": word.strip(),
"data": data or {},
"time": datetime.now().isoformat(),
}
self._save(self._favorites_file, self.favorites)
return True
def is_favorite(self, word: str) -> bool:
return word.lower().strip() in self.favorites
def get_favorites(self) -> list:
"""Return favorites sorted by time descending (newest first)."""
return [
v["word"]
for v in sorted(
self.favorites.values(),
key=lambda x: x.get("time", ""),
reverse=True,
)
]
def remove_favorite(self, word: str):
key = word.lower().strip()
if key in self.favorites:
del self.favorites[key]
self._save(self._favorites_file, self.favorites)
def clear_favorites(self):
self.favorites = {}
self._save(self._favorites_file, self.favorites)
self.clear_cache()
def update_favorite_data(self, word: str, data: dict):
key = word.lower().strip()
if key in self.favorites:
self.favorites[key]["data"] = data
self._save(self._favorites_file, self.favorites)
# ── Export ───────────────────────────────────────────────────────────────
def export_favorites_xlsx(self, path: str):
import openpyxl
from openpyxl.styles import Font, PatternFill, Alignment
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "收藏单词"
headers = ["单词", "音标", "词性", "英文释义", "中文释义", "收藏时间"]
header_font = Font(bold=True, color="FFFFFF")
header_fill = PatternFill("solid", fgColor="4472C4")
for col, h in enumerate(headers, 1):
cell = ws.cell(row=1, column=col, value=h)
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
ws.column_dimensions["A"].width = 16
ws.column_dimensions["B"].width = 20
ws.column_dimensions["C"].width = 10
ws.column_dimensions["D"].width = 40
ws.column_dimensions["E"].width = 30
ws.column_dimensions["F"].width = 16
row = 2
for fav in self.favorites.values():
word = fav["word"]
data = fav.get("data") or {}
time_str = fav.get("time", "")[:10]
prons = data.get("pronunciations", [])
pron_str = " ".join(
f"{'uk' if 'uk' in p.get('label','').lower() else 'us' if 'us' in p.get('label','').lower() else p.get('label','')} /{p['ipa']}/"
for p in prons if p.get("ipa")
)
entries = data.get("entries", [])
if not entries:
ws.append([word, pron_str, "", "", "", time_str])
row += 1
continue
first = True
for entry in entries:
pos = entry.get("pos", "")
for defn in entry.get("definitions", []):
ws.cell(row=row, column=1, value=word if first else "")
ws.cell(row=row, column=2, value=pron_str if first else "")
ws.cell(row=row, column=3, value=pos)
ws.cell(row=row, column=4, value=defn.get("en", ""))
ws.cell(row=row, column=5, value=defn.get("zh", ""))
ws.cell(row=row, column=6, value=time_str if first else "")
row += 1
first = False
wb.save(path)