-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlabel_manager.py
More file actions
432 lines (329 loc) · 14.8 KB
/
label_manager.py
File metadata and controls
432 lines (329 loc) · 14.8 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import json
import os
from typing import List, Set, Dict, Any
from datetime import datetime
import logging
LABELS_FILE = "labels.json"
class LabelManager:
def __init__(self):
self.labels_file = LABELS_FILE
self._ensure_labels_file()
self._check_and_fix_format()
def _ensure_labels_file(self):
if not os.path.exists(self.labels_file):
default_data = {
"labels": [],
"created_at": datetime.now().isoformat(),
"last_updated": datetime.now().isoformat(),
"version": "1.0",
}
self._save_labels_data(default_data)
def _check_and_fix_format(self):
try:
data = self._load_labels_data()
except Exception as e:
logging.error(f"Error checking labels file format: {e}")
self._ensure_labels_file()
def _load_labels_data(self) -> Dict[str, Any]:
try:
with open(self.labels_file, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
logging.info("Detected old labels format, migrating to new format")
new_data = {
"labels": data,
"created_at": datetime.now().isoformat(),
"last_updated": datetime.now().isoformat(),
"version": "1.0",
}
self._save_labels_data(new_data)
return new_data
return data
except (FileNotFoundError, json.JSONDecodeError):
default_data = {
"labels": [],
"created_at": datetime.now().isoformat(),
"last_updated": datetime.now().isoformat(),
"version": "1.0",
}
self._save_labels_data(default_data)
return default_data
def _save_labels_data(self, data: Dict[str, Any]):
data["last_updated"] = datetime.now().isoformat()
with open(self.labels_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
def get_all_labels(self) -> List[str]:
data = self._load_labels_data()
return data.get("labels", [])
def get_all_labels_safe(self) -> List[str]:
try:
return self.get_all_labels()
except Exception as e:
logging.error(f"Error getting labels, trying fallback: {e}")
try:
with open(self.labels_file, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
return data
else:
return []
except:
return []
def add_label(self, label: str) -> Dict[str, Any]:
label = label.strip()
if not label:
return {"success": False, "message": "Label cannot be empty"}
data = self._load_labels_data()
labels = data.get("labels", [])
if label in labels:
return {"success": False, "message": f"Label '{label}' already exists"}
labels.append(label)
data["labels"] = labels
self._save_labels_data(data)
logging.info(f"Label '{label}' added successfully")
return {"success": True, "message": f"Label '{label}' added successfully"}
def remove_label(self, label: str) -> Dict[str, Any]:
label = label.strip()
if not label:
return {"success": False, "message": "Label cannot be empty"}
data = self._load_labels_data()
labels = data.get("labels", [])
if label not in labels:
return {"success": False, "message": f"Label '{label}' not found"}
labels.remove(label)
data["labels"] = labels
self._save_labels_data(data)
self._clean_label_from_data(label)
logging.info(f"Label '{label}' removed successfully")
return {"success": True, "message": f"Label '{label}' removed successfully"}
def _clean_label_from_data(self, label: str):
try:
with open("servers.json", "r", encoding="utf-8") as f:
servers = json.load(f)
updated = False
cleaned_count = 0
for server_name, server_info in servers.items():
if server_info.get("label") == label:
server_info["label"] = ""
updated = True
cleaned_count += 1
if updated:
with open("servers.json", "w", encoding="utf-8") as f:
json.dump(servers, f, indent=4, ensure_ascii=False)
logging.info(f"Cleaned label '{label}' from {cleaned_count} servers")
except Exception as e:
logging.error(f"Error cleaning label from servers: {e}")
try:
with open("domains.json", "r", encoding="utf-8") as f:
domains = json.load(f)
updated = False
cleaned_count = 0
for domain_name, domain_info in domains.items():
if domain_info.get("label") == label:
domain_info["label"] = ""
updated = True
cleaned_count += 1
if updated:
with open("domains.json", "w", encoding="utf-8") as f:
json.dump(domains, f, indent=4, ensure_ascii=False)
logging.info(f"Cleaned label '{label}' from {cleaned_count} domains")
except Exception as e:
logging.error(f"Error cleaning label from domains: {e}")
try:
from label_sync import force_sync
current_labels = self.get_all_labels()
force_sync()
logging.info(f"Force synced labels after cleaning '{label}'")
except Exception as e:
logging.error(f"Error force syncing after cleaning: {e}")
def sync_labels_from_data(self) -> Dict[str, Any]:
try:
existing_labels = set(self.get_all_labels())
data_labels = set()
try:
with open("servers.json", "r", encoding="utf-8") as f:
servers = json.load(f)
for server_info in servers.values():
label = server_info.get("label", "").strip()
if label:
data_labels.add(label)
except (FileNotFoundError, json.JSONDecodeError):
pass
try:
with open("domains.json", "r", encoding="utf-8") as f:
domains = json.load(f)
for domain_info in domains.values():
label = domain_info.get("label", "").strip()
if label:
data_labels.add(label)
except (FileNotFoundError, json.JSONDecodeError):
pass
try:
with open("labels.json", "r", encoding="utf-8") as f:
labels_data = json.load(f)
if isinstance(labels_data, list):
for label in labels_data:
if label and label.strip():
data_labels.add(label.strip())
except (FileNotFoundError, json.JSONDecodeError):
pass
new_labels = data_labels - existing_labels
if new_labels:
data = self._load_labels_data()
labels = data.get("labels", [])
for label in new_labels:
if label not in labels:
labels.append(label)
data["labels"] = labels
self._save_labels_data(data)
logging.info(f"Synced {len(new_labels)} new labels: {list(new_labels)}")
return {
"success": True,
"message": f"Synced {len(new_labels)} new labels from data",
"new_labels": list(new_labels),
}
else:
return {"success": True, "message": "No new labels found to sync"}
except Exception as e:
logging.error(f"Error syncing labels: {e}")
return {"success": False, "message": f"Error syncing labels: {str(e)}"}
def get_labels_usage(self) -> Dict[str, Any]:
try:
labels = self.get_all_labels()
usage = {label: {"servers": 0, "domains": 0} for label in labels}
try:
with open("servers.json", "r", encoding="utf-8") as f:
servers = json.load(f)
for server_info in servers.values():
label = server_info.get("label", "").strip()
if label and label in usage:
usage[label]["servers"] += 1
except (FileNotFoundError, json.JSONDecodeError):
pass
try:
with open("domains.json", "r", encoding="utf-8") as f:
domains = json.load(f)
for domain_info in domains.values():
label = domain_info.get("label", "").strip()
if label and label in usage:
usage[label]["domains"] += 1
except (FileNotFoundError, json.JSONDecodeError):
pass
return {"success": True, "usage": usage}
except Exception as e:
logging.error(f"Error getting label usage: {e}")
return {"success": False, "message": f"Error getting usage: {str(e)}"}
def validate_label(self, label: str) -> Dict[str, Any]:
label = label.strip()
if not label:
return {"valid": False, "message": "Label cannot be empty"}
if len(label) > 50:
return {
"valid": False,
"message": "Label cannot be longer than 50 characters",
}
invalid_chars = ["<", ">", '"', "'", "&", "`", "\\", "/", "|"]
for char in invalid_chars:
if char in label:
return {"valid": False, "message": f"Label cannot contain '{char}'"}
return {"valid": True, "message": "Label is valid"}
def export_labels(self) -> Dict[str, Any]:
try:
data = self._load_labels_data()
usage = self.get_labels_usage()
export_data = {
"labels": data.get("labels", []),
"metadata": {
"created_at": data.get("created_at"),
"last_updated": data.get("last_updated"),
"version": data.get("version", "1.0"),
"total_labels": len(data.get("labels", [])),
},
"usage": usage.get("usage", {}) if usage.get("success") else {},
}
return {"success": True, "data": export_data}
except Exception as e:
logging.error(f"Error exporting labels: {e}")
return {"success": False, "message": f"Error exporting: {str(e)}"}
def auto_sync_after_operation(self) -> Dict[str, Any]:
logging.info("Auto-sync disabled to prevent label conflicts")
return {"success": True, "message": "Auto-sync disabled to prevent conflicts"}
def force_sync_all(self) -> Dict[str, Any]:
try:
all_labels = set()
current_labels = self.get_all_labels()
all_labels.update(current_labels)
try:
with open("settings.json", "r", encoding="utf-8") as f:
settings = json.load(f)
settings_labels = settings.get("labels", [])
all_labels.update(settings_labels)
except Exception as e:
logging.error(f"Error reading settings.json: {e}")
try:
with open("servers.json", "r", encoding="utf-8") as f:
servers = json.load(f)
for server_info in servers.values():
label = server_info.get("label", "").strip()
if label:
all_labels.add(label)
except Exception as e:
logging.error(f"Error reading servers.json: {e}")
try:
with open("domains.json", "r", encoding="utf-8") as f:
domains = json.load(f)
for domain_info in domains.values():
label = domain_info.get("label", "").strip()
if label:
all_labels.add(label)
except Exception as e:
logging.error(f"Error reading domains.json: {e}")
final_labels = sorted(list(all_labels))
self._save_labels_data(
{
"labels": final_labels,
"created_at": datetime.now().isoformat(),
"last_updated": datetime.now().isoformat(),
"version": "1.0",
}
)
try:
with open("settings.json", "r", encoding="utf-8") as f:
settings = json.load(f)
settings["labels"] = final_labels
settings["default_labels"] = final_labels
with open("settings.json", "w", encoding="utf-8") as f:
json.dump(settings, f, indent=4, ensure_ascii=False)
except Exception as e:
logging.error(f"Error updating settings.json: {e}")
logging.info(f"✅ Force sync completed: {len(final_labels)} labels synced")
return {
"success": True,
"message": f"Force sync completed: {len(final_labels)} labels synced",
"labels": final_labels,
}
except Exception as e:
logging.error(f"Error in force sync: {e}")
return {"success": False, "message": f"Force sync failed: {str(e)}"}
label_manager = LabelManager()
def get_all_labels() -> List[str]:
return label_manager.get_all_labels_safe()
def add_label(label: str) -> Dict[str, Any]:
return label_manager.add_label(label)
def remove_label(label: str) -> Dict[str, Any]:
return label_manager.remove_label(label)
def sync_labels() -> Dict[str, Any]:
return label_manager.sync_labels_from_data()
def auto_sync_labels_on_startup() -> Dict[str, Any]:
try:
result = label_manager.auto_sync_after_operation()
if result.get("success"):
logging.info("Auto-sync labels on startup completed successfully")
return result
except Exception as e:
logging.error(f"Auto-sync labels on startup failed: {e}")
return {"success": False, "message": f"Auto-sync failed: {str(e)}"}
def force_sync_all_labels() -> Dict[str, Any]:
return label_manager.force_sync_all()
def auto_sync_after_operation() -> Dict[str, Any]:
return label_manager.auto_sync_after_operation()