forked from mohitkumhar/450-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_import.py
More file actions
252 lines (206 loc) · 7.29 KB
/
Copy pathprogress_import.py
File metadata and controls
252 lines (206 loc) · 7.29 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
import csv
import json
from io import StringIO
def normalize_url(url):
if not url:
return ""
url = url.strip().lower()
if url.startswith("https://"):
url = url[8:]
elif url.startswith("http://"):
url = url[7:]
if url.startswith("www."):
url = url[4:]
if url.endswith("/"):
url = url[:-1]
return url
def parse_csv_backup(content_str):
f = StringIO(content_str)
reader = csv.reader(f)
try:
headers = next(reader)
except StopIteration:
return [], "Empty CSV file"
header_map = {}
for idx, h in enumerate(headers):
header_map[h.strip().lower()] = idx
if "problem" not in header_map:
return [], "Invalid CSV format: 'Problem' column is required."
parsed_items = []
for row in reader:
if not row:
continue
if len(row) < len(headers):
row = row + [""] * (len(headers) - len(row))
def get_val(name, default=""):
idx = header_map.get(name.lower())
if idx is not None and idx < len(row):
return row[idx]
return default
problem = get_val("Problem")
if not problem:
continue
def parse_bool(val):
val = str(val).strip().lower()
return val in ("true", "1", "yes", "y", "done", "t")
done = parse_bool(get_val("Done"))
bookmark = parse_bool(get_val("Bookmarked")) or parse_bool(get_val("Bookmark"))
skipped = parse_bool(get_val("Skipped"))
notes = get_val("Notes")
if notes.startswith("'") and len(notes) > 1 and notes[1] in ('=', '+', '-', '@'):
notes = notes[1:]
url = get_val("URL")
url2 = get_val("URL2")
parsed_items.append({
"problem": problem,
"url": url,
"url2": url2,
"done": done,
"bookmark": bookmark,
"skipped": skipped,
"notes": notes,
})
return parsed_items, None
def parse_json_backup(content_str):
try:
data = json.loads(content_str)
except Exception as e:
return [], f"Invalid JSON syntax: {str(e)}"
parsed_items = []
if isinstance(data, list):
items_list = data
elif isinstance(data, dict) and "progress" in data:
prog = data["progress"]
if isinstance(prog, list):
items_list = prog
elif isinstance(prog, dict):
items_list = []
for key, val in prog.items():
if isinstance(val, dict):
item = dict(val)
item["key"] = key
items_list.append(item)
else:
return [], "Invalid 'progress' format in JSON."
elif isinstance(data, dict):
items_list = []
for key, val in data.items():
if isinstance(val, dict):
item = dict(val)
item["key"] = key
items_list.append(item)
else:
return [], "Invalid JSON structure."
for item in items_list:
if not isinstance(item, dict):
continue
def parse_bool(val):
if isinstance(val, bool):
return val
val_str = str(val).strip().lower()
return val_str in ("true", "1", "yes", "y", "done", "t")
done = parse_bool(item.get("done") or item.get("Done"))
bookmark = parse_bool(
item.get("bookmark") or item.get("Bookmark") or item.get("bookmarked") or item.get("Bookmarked")
)
skipped = parse_bool(item.get("skipped") or item.get("Skipped"))
notes = str(item.get("notes") or item.get("Notes") or "")
problem = str(item.get("problem") or item.get("Problem") or "")
url = str(item.get("url") or item.get("URL") or "")
url2 = str(item.get("url2") or item.get("URL2") or "")
key = str(item.get("key") or item.get("id") or item.get("_id") or "")
if not problem and not url and not key:
continue
parsed_items.append({
"problem": problem,
"url": url,
"url2": url2,
"key": key,
"done": done,
"bookmark": bookmark,
"skipped": skipped,
"notes": notes,
})
return parsed_items, None
def process_dry_run(parsed_items, db_questions, current_progress):
by_id = {}
by_url = {}
by_name = {}
for q in db_questions:
q_id = str(q["_id"])
by_id[q_id] = q
u1 = normalize_url(q.get("url"))
if u1:
by_url[u1] = q
u2 = normalize_url(q.get("url2"))
if u2:
by_url[u2] = q
name = q.get("problem", "").strip().lower()
if name:
by_name[name] = q
matched_count = 0
unmatched_count = 0
changes = []
conflicts = []
mapped_progress = {}
for item in parsed_items:
target_question = None
key = item.get("key")
if key and key in by_id:
target_question = by_id[key]
if not target_question:
u = normalize_url(item.get("url"))
if u and u in by_url:
target_question = by_url[u]
if not target_question:
u2 = normalize_url(item.get("url2"))
if u2 and u2 in by_url:
target_question = by_url[u2]
if not target_question:
name = item.get("problem", "").strip().lower()
if name and name in by_name:
target_question = by_name[name]
if not target_question:
unmatched_count += 1
continue
matched_count += 1
q_id = str(target_question["_id"])
problem_title = target_question["problem"]
mapped_progress[q_id] = {
"done": item["done"],
"bookmark": item["bookmark"],
"skipped": item["skipped"],
"notes": item["notes"],
}
existing = current_progress.get(q_id, {})
change_desc = []
if item["done"] != bool(existing.get("done")):
change_desc.append(f"Done: {existing.get('done', False)} -> {item['done']}")
if item["bookmark"] != bool(existing.get("bookmark")):
change_desc.append(f"Bookmark: {existing.get('bookmark', False)} -> {item['bookmark']}")
if item["skipped"] != bool(existing.get("skipped")):
change_desc.append(f"Skipped: {existing.get('skipped', False)} -> {item['skipped']}")
db_notes = existing.get("notes") or ""
imp_notes = item["notes"]
if db_notes != imp_notes:
change_desc.append("Notes updated")
if db_notes and imp_notes:
conflicts.append({
"problem": problem_title,
"field": "notes",
"db_value": db_notes,
"import_value": imp_notes,
})
if change_desc:
changes.append({
"problem": problem_title,
"change": ", ".join(change_desc),
})
summary = {
"total_records": len(parsed_items),
"matched_records": matched_count,
"unmatched_records": unmatched_count,
"changes_detected": len(changes),
"conflicts_detected": len(conflicts),
}
return summary, changes, conflicts, mapped_progress