-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarccompare.py
More file actions
304 lines (241 loc) · 11.7 KB
/
marccompare.py
File metadata and controls
304 lines (241 loc) · 11.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
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from pymarc import MARCReader
import os
import threading
import pickle
import queue as q
#THIS CODE WAS WRITTEN WITH MICROSOFT COPILOT AND META AI IN SEPTEMBER-NOVEMBER 2024. USE WITH CAUTION!
# Constant values
FILE_TYPES = {
"MARC Files": "*.mrc",
"Text Files": "*.txt"
}
CACHE_EXTENSION = ".cache"
SEARCH_INPUT_MESSAGE = "Search by Unique Identifiers (comma-separated or line-breaks):"
UPLOAD_ORIGINAL_MESSAGE = "Original records in MARC Binary (.mrc)"
UPLOAD_UPDATED_MESSAGE = "Updated records in MARC Binary (.mrc)"
UPLOAD_TXT_MESSAGE = "In .txt with no whitespaces or line-breaks, only commas"
class MARCComparer:
def __init__(self, root):
self.root = root
self.root.title("MARC Record Comparer")
self.original_file = None
self.updated_file = None
self.identifiers = []
self.current_index = 0
self.create_widgets()
def create_widgets(self):
# Create frames to organize widgets
self.top_frame = tk.Frame(self.root)
self.top_frame.pack(pady=10)
self.upload_frame = tk.Frame(self.top_frame)
self.upload_frame.pack()
self.search_frame = tk.Frame(self.top_frame)
self.search_frame.pack(pady=10)
self.button_frame = tk.Frame(self.top_frame)
self.button_frame.pack(pady=10)
self.text_frame = tk.Frame(self.root)
self.text_frame.pack(fill=tk.BOTH, expand=True)
self.bottom_frame = tk.Frame(self.root)
self.bottom_frame.pack(pady=10)
# Upload buttons
self.upload_original_button = tk.Button(self.upload_frame, text="Upload Original MARC File", command=self.upload_original)
self.upload_original_button.pack(side=tk.LEFT, padx=5)
self.upload_original_button.bind("<Enter>", lambda e: self.show_tooltip(e, UPLOAD_ORIGINAL_MESSAGE))
self.upload_original_button.bind("<Leave>", self.hide_tooltip)
self.upload_updated_button = tk.Button(self.upload_frame, text="Upload Updated MARC File", command=self.upload_updated)
self.upload_updated_button.pack(side=tk.LEFT, padx=5)
self.upload_updated_button.bind("<Enter>", lambda e: self.show_tooltip(e, UPLOAD_UPDATED_MESSAGE))
self.upload_updated_button.bind("<Leave>", self.hide_tooltip)
self.upload_txt_button = tk.Button(self.upload_frame, text="MMS IDs .txt (comma-separated or line-breaks)", command=self.upload_txt)
self.upload_txt_button.pack(side=tk.LEFT, padx=5)
self.upload_txt_button.bind("<Enter>", lambda e: self.show_tooltip(e, UPLOAD_TXT_MESSAGE))
self.upload_txt_button.bind("<Leave>", self.hide_tooltip)
# Search label and text
self.search_label = tk.Label(self.search_frame, text=SEARCH_INPUT_MESSAGE)
self.search_label.pack()
self.search_text = tk.Text(self.search_frame, height=10, width=50)
self.search_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.search_scrollbar = tk.Scrollbar(self.search_frame)
self.search_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.search_text.config(yscrollcommand=self.search_scrollbar.set)
self.search_scrollbar.config(command=self.search_text.yview)
# Buttons
self.search_button = tk.Button(self.button_frame, text="Search", command=self.search_records)
self.search_button.pack(side=tk.LEFT, padx=5)
self.next_button = tk.Button(self.button_frame, text="Next", command=self.next_record)
self.next_button.pack(side=tk.LEFT, padx=5)
self.prev_button = tk.Button(self.button_frame, text="Previous", command=self.prev_record)
self.prev_button.pack(side=tk.LEFT, padx=5)
# Text boxes
self.original_text = tk.Text(self.text_frame, wrap=tk.NONE)
self.original_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.updated_text = tk.Text(self.text_frame, wrap=tk.NONE)
self.updated_text.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
# Counter label and spinner
self.counter_label = tk.Label(self.bottom_frame, text="")
self.counter_label.pack()
self.spinner = ttk.Progressbar(self.bottom_frame, mode='indeterminate')
self.spinner.pack()
self.spinner.pack_forget() # Hide spinner initially
self.tooltip = None
def show_tooltip(self, event, text):
if self.tooltip:
self.tooltip.destroy()
self.tooltip = tk.Toplevel(self.root)
self.tooltip.wm_overrideredirect(True)
self.tooltip.attributes("-alpha", 0.5)
label = tk.Label(self.tooltip, text=text, background="black", foreground="white", relief=tk.SOLID, borderwidth=1)
label.pack()
x = event.x_root + 20
y = event.y_root + 20
self.tooltip.wm_geometry(f"+{x}+{y}")
def hide_tooltip(self, event):
if self.tooltip:
self.tooltip.destroy()
self.tooltip = None
def upload_marc_file(self, attr_name):
"""Upload MARC file and delete cached files"""
file_path = filedialog.askopenfilename(filetypes=[("MARC Files", FILE_TYPES["MARC Files"])])
if file_path:
# Delete cached files
cache_file = f"{os.path.basename(file_path)}.cache"
if os.path.exists(cache_file):
os.remove(cache_file)
setattr(self, attr_name, file_path)
messagebox.showinfo("File Selected", f"MARC file selected: {os.path.basename(file_path)}")
def upload_original(self):
self.upload_marc_file('original_file')
def upload_updated(self):
self.upload_marc_file('updated_file')
def upload_txt(self):
txt_file = filedialog.askopenfilename(filetypes=[("Text Files", FILE_TYPES["Text Files"])])
if txt_file:
with open(txt_file, 'r') as f:
content = f.read()
self.identifiers = [id.strip() for id in content.replace('\n', ',').split(',') if id.strip()]
print("Identifiers from TXT:") # Debugging line
for identifier in self.identifiers:
print(identifier) # Debugging line
messagebox.showinfo("File Selected", f"TXT file selected: {os.path.basename(txt_file)}")
self.search_records()
def next_record(self):
if self.current_index < len(self.matched_records) - 1:
self.current_index += 1
self.display_current_record()
def prev_record(self):
if self.current_index > 0:
self.current_index -= 1
self.display_current_record()
def display_current_record(self):
original_record, updated_record = self.matched_records[self.current_index]
self.original_text.delete(1.0, tk.END)
self.original_text.insert(tk.END, original_record)
self.updated_text.delete(1.0, tk.END)
self.updated_text.insert(tk.END, updated_record)
self.counter_label.config(text=f"Record Pair {self.current_index + 1} of {len(self.matched_records)}")
def index_marc_records(self, file_path):
"""Create an index mapping IDs to record positions and cache records."""
cache_file = f"{os.path.basename(file_path)}{CACHE_EXTENSION}"
# Check if cache exists
if os.path.exists(cache_file):
with open(cache_file, 'rb') as fh:
return pickle.load(fh)
index = {}
records = {}
with open(file_path, 'rb') as fh:
reader = MARCReader(fh)
for i, record in enumerate(reader):
if record['001']:
index[record['001'].data] = i
records[record['001'].data] = record
# Store cache
with open(cache_file, 'wb') as fh:
pickle.dump((index, records), fh)
return index, records
def get_record_by_id(self, index, records, unique_id):
"""Retrieve a record by ID using the index and cache."""
return records.get(unique_id)
def print_marc_ids(self, file_path):
"""Print MARC IDs from a file."""
index, _ = self.index_marc_records(file_path)
print(f"Identifiers in {os.path.basename(file_path)}:", list(index.keys()))
def _perform_search(self):
print("Search thread started")
try:
matches_found = 0
self.matched_records = []
for unique_id in self.identifiers:
# Retrieve records using indexes and cache
original_record = self.get_record_by_id(self.original_index, self.original_records, unique_id)
updated_record = self.get_record_by_id(self.updated_index, self.updated_records, unique_id)
if original_record and updated_record:
matches_found += 1
self.matched_records.append((original_record, updated_record))
# Display results
self.spinner.stop()
self.spinner.pack_forget()
messagebox.showinfo("Search Complete", f"Total IDs: {len(self.identifiers)}\nMatches Found: {len(self.matched_records)}")
self.current_index = 0
self.display_current_record() if self.matched_records else None
except Exception as e:
# Display error message
self.spinner.stop()
self.spinner.pack_forget()
messagebox.showerror("Search Error", str(e))
print("Search thread finished")
def search_records(self):
# Validate file uploads
if not all([self.original_file, self.updated_file]):
messagebox.showwarning("Files Missing", "Please upload both original and updated MARC files.")
return
# Parse search input
search_input = self.search_text.get('1.0', tk.END).strip()
if not search_input:
messagebox.showwarning("Invalid Input", "Please enter search identifiers.")
return
self.identifiers = [id.strip() for id in search_input.replace('\n', ',').split(',') if id.strip()]
# Show spinner
self.show_spinner()
# Create queue and thread
queue = q.Queue()
thread = threading.Thread(target=self._create_indexes_thread, args=(queue,))
thread.start()
# Check queue for completion
self.check_queue(queue)
def _create_indexes_thread(self, queue):
try:
self._create_indexes()
self._perform_search() # Only call here
queue.put(None) # Signal completion
except Exception as e:
queue.put(e) # Propagate exception
def check_queue(self, queue):
try:
result = queue.get(block=False)
if isinstance(result, Exception):
self.hide_spinner()
messagebox.showerror("Search Error", str(result))
else:
self.hide_spinner()
self._perform_search()
except q.Empty:
self.root.after(100, lambda: self.check_queue(queue))
def show_spinner(self):
"""Display the spinner."""
self.spinner.pack()
self.spinner.start()
def hide_spinner(self):
"""Hide the spinner."""
self.spinner.pack_forget()
def _create_indexes(self):
"""Create indexes and cache records"""
self.original_index, self.original_records = self.index_marc_records(self.original_file)
self.updated_index, self.updated_records = self.index_marc_records(self.updated_file)
# Show spinner
self.show_spinner()
if __name__ == "__main__":
root = tk.Tk()
app = MARCComparer(root)
root.mainloop()