-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePartMatcher.py
More file actions
554 lines (460 loc) · 17.9 KB
/
FilePartMatcher.py
File metadata and controls
554 lines (460 loc) · 17.9 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
import os
import re
import time
import keyboard
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from collections import namedtuple
# This is a simple GUI that allows you to browse a directory and then search
# for files by part of the name. Parts are delimited by non-alphanumeric
# characters and lowercased. To deal with the proliferation of parts differing
# only by a numeric suffix, these are truncated.
#
# The autocomplete textbox selects parts that start with the text you enter.
# If you prefix that text with a space, it searchs for parts containing that
# text. In other words, it changes from "part*" to "*part*".
#
# Selecting the part shows all files that contain it. Double-clicking on a
# file opens it. Right-clicking on it opens a menu. The first two options
# are self-explanatory. The next fills the parts list based on the selected
# file, letting you search laterally.
#
# In lateral search mode, the part list contains only the ones relevant to the
# file that you had selected and the autocomplete textbox has a leading ">" to
# indicate that it's scoped to this partial list. Clearing the textbox
# restores regular mode.
#
# The final option moves the selected file to a target directory. If no target
# directory is selected, it prompts you to browse for one. This is no
# replacement for opening the directory in Explorer, but it fits in with the
# mission of helping the user find and harvest files. Note that you can also
# right-click on the browse button to select the target directory.
#
# To run it conveniently under Windows, create a shortcut to this file. It
# needs to launch python, passing the full path to this file as a parameter.
# Top-level directory being searched.
directory = ""
# Directory to move files to.
destination_directory = ""
# Maps parts to the FileInfo's that contain them.
file_dict = {}
# Holds currently-displayed parts list.
part_list = []
# Counter used for title display
title_counter = 0
# Named tuple to hold file info for each row in the tree.
FileInfo = namedtuple("FileInfo", ["key", "path", "name", "extension", "size"])
# Shows help messagebox.
def show_help():
messagebox.showinfo(
"Help",
"Enter start of part to autocomplete. Prefix with space for wildcard search. Double-click on file for launch. Right-click for menu.",
)
# Control for auto-complete.
class AutocompleteEntry(tk.Entry):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.var = tk.StringVar()
self.configure(textvariable=self.var)
self.var.trace_add("write", self.on_change)
# Updates part list display based on current text.
def on_change(self, *args):
current_text = self.var.get().lower()
temp_list = []
if current_text and current_text[0].isspace():
# If there's leading whitespace, search `*text*`.
current_text = current_text.lstrip()
for part in file_dict:
if current_text in part:
temp_list.append(part)
elif current_text and current_text[0] == ">":
# If we're in lateral search mode, search within partial list.
current_text = current_text[1:]
for part in part_list:
if part.startswith(current_text):
temp_list.append(part)
else:
# Otherwise, search `text*`.
for part in file_dict:
if part.startswith(current_text):
temp_list.append(part)
# Display list and cascade changes.
temp_list.sort()
listbox_parts.delete(0, "end")
for part in temp_list:
listbox_parts.insert(tk.END, part)
if listbox_parts.size() > 0:
listbox_parts.selection_set(0)
listbox_parts.see(0)
listbox_parts.event_generate("<<ListboxSelect>>")
else:
show_files_with_selected_part()
def set_text(self, text=""):
self.var.set(text)
# Updates tree with files that contain selected part.
def show_files_with_selected_part(event=None):
if tree.get_children():
tree.delete(*tree.get_children())
if not listbox_parts.curselection():
return
# Reset headings to remove sorting indicators and timer.
update_title()
set_headings()
selected_part = listbox_parts.get(listbox_parts.curselection())
# Can be missing after we move/delete a file.
if not selected_part in file_dict:
return
for file in file_dict[selected_part]:
tree.insert(
"",
"end",
text=file.key,
values=(file.path, file.extension, format(file.size, ",")),
)
# Helper to extract selected file from tree.
def get_selected_file(event=None):
items = tree.selection()
if not items:
return None
item_text = tree.item(items[0])["text"]
if len(item_text) == 0:
return None
selected_file = os.path.join(directory, item_text)
return selected_file
# Opens selected file.
def open_selected_file(event=None):
selected_file = get_selected_file(event)
perform(lambda: os.startfile(selected_file))
# Opens selected file's directory.
def open_selected_directory(event=None):
selected_file = get_selected_file(event)
if selected_file:
selected_directory = os.path.dirname(selected_file)
perform(lambda: os.startfile(selected_directory))
# Opens lateral search on the parts from the selected file.
def open_laterally(event=None):
global part_list
selected_file = get_selected_file(event)
selected_file = os.path.relpath(selected_file, directory)
if selected_file:
part_list = list(set(get_parts(selected_file)))
show_part_list(True)
# Get information about selected file.
def get_selected_info():
items = tree.selection()
if not items:
return None, None, None
item = items[0]
path = tree.item(item)["text"]
fullpath = os.path.normpath(os.path.join(directory, path))
return fullpath, item, path
# Move selected file to destination directory, if one is selected. Otherwise, browse for one.
def move_to_target(event=None):
if len(destination_directory) == 0:
messagebox.showinfo("Help", "Right-click on Browse button to change target")
browse_target()
return
fullpath, item, path = get_selected_info()
if not fullpath:
return
new_path = os.path.join(destination_directory, os.path.basename(fullpath))
if os.path.exists(new_path):
messagebox.showerror("Error", f"File already exists: {new_path}")
return
message = f"Move {fullpath} to {new_path}?"
confirm_and_remove(
fullpath, item, path, lambda: os.rename(fullpath, new_path), message
)
# Delete selected file.
def delete_file(event=None):
fullpath, item, path = get_selected_info()
if not fullpath:
return
message = f"Delete {fullpath} permanently?"
confirm_and_remove(fullpath, item, path, lambda: os.remove(fullpath), message)
# Perform a file operation lambda.
def perform(file_op):
try:
file_op()
return True
except OSError as e:
messagebox.showerror("Error", f"Error: {e.strerror}")
return False
# Perform file move/remove operation lambda after prompting for permission.
def confirm_and_remove(fullpath, item=None, path=None, file_op=None, message=None):
if messagebox.askyesno("Confirm", message) and perform(file_op):
remove_missing_file(item, path)
# Remove file entry from tree and dictionary, after file was moved or deleted.
def remove_missing_file(item, path):
tree.delete(item)
parts = get_parts(path)
for part in parts:
if not part in file_dict:
continue
list = file_dict[part]
matching_element = next((x for x in list if x.key == path), None)
if matching_element:
list.remove(matching_element)
if len(list) == 0:
del file_dict[part]
# Pop up context menu on right-click in tree. Doubles as a properties box to
# see attributes that aren't visible without resizing columns.
def show_context_menu(event):
global destination_directory
item = tree.identify_row(event.y)
if not item:
return
path = tree.item(item)["text"]
filename = os.path.basename(path)
dirname = os.path.dirname(path)
tree_menu.entryconfig(0, label=f"Open File: {filename}")
tree_menu.entryconfig(1, label=f"Open Directory: {dirname}")
if len(destination_directory) > 0:
tree_menu.entryconfig(3, label=f"Move File to: {destination_directory}")
else:
tree_menu.entryconfig(3, label=f"Browse target")
tree.selection_set(item)
tree_menu.post(event.x_root, event.y_root)
non_alphanumeric_regex = re.compile(r"[^a-zA-Z0-9\u0080-\uFFFF]")
trailing_numbers_regex = re.compile(r"\d*$")
# Breaks file path into parts, normalizing them for searching.
# Dedupes but does not sort.
def get_parts(file_path):
# Split on anything that's not alphanumeric, but leave high Unicode alone.
parts = re.split(non_alphanumeric_regex, file_path)
# Remove trailing numbers and (subsequent) empty parts.
parts = [trailing_numbers_regex.sub("", part.lower()) for part in parts]
parts = [part for part in parts if len(part) != 0]
return parts
# Fill part list with sub-list. Pass True to start a lateral search.
def show_part_list(start_exploring=False):
listbox_parts.delete(0, "end")
for part in part_list:
listbox_parts.insert(tk.END, part)
if start_exploring:
entry_autocomplete.set_text(">")
else:
entry_autocomplete.set_text()
entry_autocomplete.focus()
# Update title, potentially resetting or incrementing counter.
def update_title(increment=0, msg=""):
global title_counter
if not increment:
title_counter = 0
else:
title_counter += increment
if title_counter == 0:
root.title(f"File Part Matcher: {directory} {msg}")
return
if title_counter % 100 == 0:
root.title(f"File Part Matcher: {directory}, {title_counter} {msg}")
if title_counter % 1000 == 0:
root.update()
# Find files and sizes recursively.
def find_files(directory):
file_list = []
for entry in os.scandir(directory):
if keyboard.is_pressed("esc"):
file_list.clear()
return file_list
if entry.is_symlink():
continue
if entry.is_file():
update_title(1, "files found")
file_list.append((entry.path, entry.stat().st_size))
elif entry.is_dir():
file_list.extend(find_files(entry.path))
return file_list
# Pop up a directory dialog and then find all files recursively.
def browse_directory():
global directory
new_directory = os.path.normpath(
filedialog.askdirectory(title="Select directory to analyze")
)
if not new_directory or new_directory == ".":
return
directory = new_directory
update_title()
root.config(cursor="wait")
root.update_idletasks()
root.after(100, process_files)
# Choose target directory.
def browse_target():
global destination_directory
destination_directory = os.path.normpath(
filedialog.askdirectory(title="Select directory to move files to")
)
if not destination_directory or destination_directory == ".":
destination_directory = ""
# Extract parts from files.
def extract_parts(files):
global file_dict
for file_path, file_size in files:
rel_path = os.path.relpath(file_path, directory)
file_info = FileInfo(
rel_path.lower(),
rel_path,
os.path.split(rel_path)[-1],
os.path.splitext(rel_path)[1][1:].upper(),
file_size,
)
update_title(-1, "files to extract parts from")
if keyboard.is_pressed("esc"):
file_dict.clear()
return
parts = get_parts(rel_path)
for part in parts:
if part not in file_dict:
file_dict[part] = set()
file_dict[part].add(file_info)
# Sort file list by key.
def sort_file_list(file_list):
file_list.sort(key=lambda x: x.key)
update_title(-1, " parts to sort")
return file_list
# Process files into parts.
def process_files():
global directory
global part_list
global file_dict
start_time = time.time()
file_dict.clear()
update_title()
files = find_files(directory)
extract_parts(files)
update_title(0, ", sorting...")
update_title(len(file_dict), " parts to sort")
# Sort file lists for each part.
update_title(len(file_dict), " parts to sort")
file_dict = {key: sort_file_list(list(file_dict[key])) for key in file_dict}
# Sort parts.
update_title(0, ", sorting list of {len(file_dict)} parts")
part_list = [key for key in sorted(file_dict.keys())]
update_title(0, f", displaying")
show_part_list()
root.config(cursor="")
root.update_idletasks()
elapsed_time = time.time() - start_time
formatted_part_count = format(len(part_list), ",")
formatted_file_count = format(len(files), ",")
update_title(
0,
f", found {formatted_part_count} parts among "
f"{formatted_file_count} files in {elapsed_time:.2f} seconds",
)
# Create the window.
root = tk.Tk()
custom_font = ("TkDefaultFont", 16, "normal")
update_title()
root.state("zoomed")
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)
root.bind("<F1>", lambda event: show_help())
# Create the left frame, which holds the autocomplete entry and the file part
# listbox, as well as the browse button.
frame_left = tk.Frame(root)
frame_left.grid(row=1, column=0, sticky="nsew")
frame_left.columnconfigure(0, weight=1)
frame_left.rowconfigure(1, weight=1)
entry_autocomplete = AutocompleteEntry(frame_left, width=40, font=custom_font)
entry_autocomplete.grid(row=0, column=0, sticky="ew")
browse_button = tk.Button(frame_left, text="Browse", command=browse_directory)
browse_button.bind("<Button-3>", lambda event: browse_target())
browse_button.grid(row=0, column=1, padx=10, sticky="w")
listbox_parts = tk.Listbox(frame_left, width=40, font=custom_font)
listbox_parts.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
scrollbar_parts = tk.Scrollbar(frame_left, command=listbox_parts.yview)
scrollbar_parts.grid(row=1, column=2, sticky="ns")
listbox_parts.config(yscrollcommand=scrollbar_parts.set)
# Create the right frame, which holds the tree control with file info.
frame_right = tk.Frame(root)
frame_right.grid(row=1, column=1, sticky="nsew")
frame_right.configure(bg="#ffff00")
style = ttk.Style()
style.theme_use("clam")
style.configure("Custom.Treeview", font=custom_font, rowheight=30)
style.configure("Custom.Treeview.Heading", background="gray", foreground="white")
style.map("Custom.Treeview.Heading", background=[("active", "#005ca3")])
tree = ttk.Treeview(
frame_right, columns=("name", "type", "size"), style="Custom.Treeview"
)
# Set up the sortable columns, removing sort indicators.
def set_headings():
tree.heading("name", text="Name", command=lambda c=0: sortby(tree, c))
tree.heading("type", text="Type", command=lambda c=1: sortby(tree, c))
tree.heading("size", text="Size", command=lambda c=2: sortby(tree, c))
# Set tree headings and column widths.
screen_width = root.winfo_screenwidth()
tree.heading("#0", text="File Path")
set_headings()
tree.column("#0", width=0, stretch=False)
tree.column(
"name", minwidth=int(screen_width / 2.5), width=int(screen_width / 2), anchor="w"
)
tree.column("type", width=160, anchor="w")
tree.column("size", width=160, anchor="e")
tree.grid(row=0, column=1, sticky="nsew")
# Sort when tree heading is clicked.
def sortby(tree, column_number):
column_id = tree.column(column_number, option="id")
column_text = tree.heading(column_id, option="text")
last_char = column_text[-1]
if last_char == "▲":
descending = True
column_text = column_text[:-1]
elif last_char == "▼":
descending = False
column_text = column_text[:-1]
else:
descending = True
set_headings()
tree.heading(
column_id,
text=column_text + ("▼" if descending else "▲"),
command=lambda c=column_number: sortby(tree, c),
)
# Special-case the size column so that it sorts numerically.
if column_text == "Size":
data = [
(float(tree.set(child, column_number).replace(",", "")), child)
for child in tree.get_children("")
]
else:
data = [
(tree.set(child, column_number), child) for child in tree.get_children("")
]
# Sort the items in place
data.sort(reverse=descending)
for index, item in enumerate(data):
tree.move(item[1], "", index)
# Create the scrollbar widget and link it to the treeview widget
vsb = ttk.Scrollbar(frame_right, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=vsb.set)
vsb.grid(row=0, column=2, sticky="ns")
# Configure the size of the rows and columns
frame_right.rowconfigure(0, weight=1)
frame_right.columnconfigure(1, weight=1)
# Bind file cascade to parts and double-click to tree rows.
listbox_parts.bind("<<ListboxSelect>>", show_files_with_selected_part)
tree.bind(
"<Double-Button-1>",
lambda event: open_selected_file(event) if tree.identify_row(event.y) else None,
)
# Create context menu for tree and bind it to right-click.
tree_menu = tk.Menu(root, tearoff=0)
tree_menu.add_command(label="Open File", command=open_selected_file, font=custom_font)
tree_menu.add_command(
label="Open Directory", command=open_selected_directory, font=custom_font
)
tree_menu.add_command(
label="Explore Laterally", command=open_laterally, font=custom_font
)
tree_menu.add_command(label="Browse target", command=move_to_target, font=custom_font)
tree_menu.add_command(label="Delete file", command=delete_file, font=custom_font)
tree.bind("<Button-3>", show_context_menu)
# Go.
browse_button.focus()
root.mainloop()