Skip to content

Commit 7f9290a

Browse files
author
piotrj
committed
search improvements
1 parent 3b079e4 commit 7f9290a

File tree

4 files changed

+129
-103
lines changed

4 files changed

+129
-103
lines changed

src/core.py

+3-55
Original file line numberDiff line numberDiff line change
@@ -971,17 +971,8 @@ def find_items(self,
971971

972972
if cd_func_to_call:
973973
try:
974-
if True: #entire file
975-
if not cd_func_to_call(cd_data):
976-
continue
977-
else:
978-
found=False
979-
for line in cd_data.splitlines():
980-
if cd_func_to_call(line):
981-
found=True
982-
break
983-
if not found:
984-
continue
974+
if not cd_func_to_call(cd_data):
975+
continue
985976
except Exception as e:
986977
print_info_fn(f'find_items(2):{e}' )
987978
continue
@@ -1470,49 +1461,6 @@ def threaded_read_records(self,load_errors):
14701461
self.records_to_show.append( (new_record,info_curr_quant,info_curr_size) )
14711462
self.update_sorted()
14721463

1473-
def find_items_in_records_check(self,
1474-
range_par,
1475-
size_min,size_max,
1476-
find_filename_search_kind,name_expr,name_case_sens,
1477-
find_cd_search_kind,cd_expr,cd_case_sens,
1478-
filename_fuzzy_threshold,cd_fuzzy_threshold):
1479-
1480-
sel_range = [range_par] if range_par else self.records
1481-
self.files_search_quant = sum([record.header.quant_files+record.header.quant_folders for record in sel_range])
1482-
1483-
if self.files_search_quant==0:
1484-
return 1
1485-
1486-
#name_regexp
1487-
#cd_regexp
1488-
if name_expr and find_filename_search_kind == 'regexp':
1489-
if res := test_regexp(name_expr):
1490-
return res
1491-
1492-
if cd_expr and find_cd_search_kind == 'regexp':
1493-
if res := test_regexp(cd_expr):
1494-
return res
1495-
1496-
if find_filename_search_kind == 'fuzzy':
1497-
if name_expr:
1498-
try:
1499-
float(filename_fuzzy_threshold)
1500-
except ValueError:
1501-
return f"wrong threshold value:{filename_fuzzy_threshold}"
1502-
else:
1503-
return "empty file expression"
1504-
1505-
if find_cd_search_kind == 'fuzzy':
1506-
if cd_expr:
1507-
try:
1508-
float(cd_fuzzy_threshold)
1509-
except ValueError:
1510-
return f"wrong threshold value:{cd_fuzzy_threshold}"
1511-
else:
1512-
return "empty cd expression"
1513-
1514-
return None
1515-
15161464
def find_results_clean(self):
15171465
for record in self.records:
15181466
record.find_results_clean()
@@ -1643,7 +1591,7 @@ def threaded_run(record_nr,commands_list,results_list,progress_list,info_list,pr
16431591
else:
16441592
if subprocess_poll() is not None:
16451593
break
1646-
sleep(0.01)
1594+
sleep(0.001)
16471595
sys.exit() #thread
16481596

16491597
#####################################################

src/dialogs.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,7 @@ def __init__(self,parent,icon,bg_color,pre_show=None,post_close=None,min_width=1
332332
self.text.frame.config(takefocus=False)
333333
self.text.vbar.config(takefocus=False)
334334

335-
#self.text.tag_configure('found', background='#F0D0D0')
336335
self.text.tag_configure('found', background='yellow')
337-
#self.text.tag_configure('found_sel', background='#D08080')
338336
self.text.tag_configure('found_sel', background='orange')
339337

340338
self.text.grid(row=1,column=0,padx=2,pady=5)
@@ -406,6 +404,8 @@ def key_press(self,event):
406404

407405
self.find_entry.focus_set()
408406
self.find_key_binding(event)
407+
elif key in ('Delete','BackSpace'):
408+
self.find_key_binding(event)
409409

410410
def find_next_prev(self,mod):
411411
text_search_pool_len = len(self.text_search_pool)
@@ -426,7 +426,6 @@ def find_next_prev(self,mod):
426426

427427
self.text.tag_remove("found", start_index, end_index)
428428
self_text.tag_add("found_sel", start_index, end_index)
429-
430429
else:
431430
self.find_info_var.set('-/-')
432431

src/librer.py

+112-33
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ def help_cascade_post():
730730
gc_collect()
731731
gc_enable()
732732

733+
self.search_results_reset=True
734+
733735
self_main.mainloop()
734736

735737

@@ -1509,12 +1511,12 @@ def ver_number(var):
15091511
(fuzzy_radio_name:=Radiobutton(find_filename_frame,text="by fuzzy match",variable=self.find_filename_search_kind_var,value='fuzzy',command=self.find_mod)).grid(row=4, column=0, sticky='news',padx=4,pady=4)
15101512

15111513
regexp_tooltip = "Regular expression\n"
1512-
regexp_tooltip_name = "checked on the file\nor folder name."
1513-
regexp_tooltip_cd = "checked on the entire\nCustom Data of a file."
1514+
regexp_tooltip_name = "Checked on the file\nor folder name."
1515+
regexp_tooltip_cd = "Checked on the entire\nCustom Data of a file."
15141516

1515-
glob_tooltip = "An expression containing wildcard characters\nsuch as '*','?' or character range '[a-c]'.\n"
1516-
glob_tooltip_name = 'checked on the file or folder name.'
1517-
glob_tooltip_cd = 'checked on the entire Custom Data of a file.'
1517+
glob_tooltip = "An expression containing wildcard characters\nsuch as '*','?' or character range '[a-c]'.\n\nPlace '*' at the beginning and end of an expression\nunless you want the expression to be found exactly\nat the beginning or end of a path element\n\n"
1518+
glob_tooltip_name = 'Checked on the file or folder name.'
1519+
glob_tooltip_cd = 'Checked on the entire Custom Data of a file.'
15181520

15191521
fuzzy_tooltip = 'Fuzzy matching is implemented using SequenceMatcher\nfrom the difflib module. Any file whose similarity\nscore exceeds the threshold will be classified as found.\nThe similarity score is calculated\n'
15201522
fuzzy_tooltip_name = 'based on the file or folder name.'
@@ -1643,9 +1645,9 @@ def validate_size_str(val):
16431645

16441646
self.search_butt = Button(self.find_dialog.area_buttons, text='Search', width=14, command=self.find_items )
16451647
self.search_butt.pack(side='left', anchor='n',padx=5,pady=5)
1646-
self.search_show_butt = Button(self.find_dialog.area_buttons, text='Show results', width=14, command=self.find_show_results )
1648+
self.search_show_butt = Button(self.find_dialog.area_buttons, text='Show results', width=14, command=self.find_show_results, state='disabled' )
16471649
self.search_show_butt.pack(side='left', anchor='n',padx=5,pady=5)
1648-
self.search_save_butt = Button(self.find_dialog.area_buttons, text='Save results', width=14, command=self.find_save_results )
1650+
self.search_save_butt = Button(self.find_dialog.area_buttons, text='Save results', width=14, command=self.find_save_results, state='disabled' )
16491651
self.search_save_butt.pack(side='left', anchor='n',padx=5,pady=5)
16501652

16511653
Button(self.find_dialog.area_buttons, text='Close', width=14, command=self.find_close ).pack(side='right', anchor='n',padx=5,pady=5)
@@ -1660,7 +1662,7 @@ def validate_size_str(val):
16601662

16611663
self.results_on_find = LabelDialogQuestion(self.find_dialog.widget,(self.ico_librer,self.ico_librer_small),self.bg_color,pre_show=lambda new_widget : self.pre_show(on_main_window_dialog=False,new_widget=new_widget),post_close=lambda : self.post_close(on_main_window_dialog=False))
16621664

1663-
self.results_on_find.cancel_button.configure(text='Continue search.',width=20)
1665+
self.results_on_find.cancel_button.configure(text='Continue search',width=20)
16641666
self.results_on_find.ok_button.configure(text='Close Search dialog',width=20)
16651667

16661668
self.find_dialog_created = True
@@ -2104,15 +2106,13 @@ def find_next(self):
21042106
self.select_find_result(1)
21052107

21062108
def find_save_results(self):
2107-
self.find_items()
2108-
21092109
if report_file_name := asksaveasfilename(parent = self.find_dialog.widget, initialfile = 'librer_search_report.txt',defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Files","*.txt")]):
21102110
self.status('saving file "%s" ...' % str(report_file_name))
21112111

21122112
with open(report_file_name,'w') as report_file:
21132113
report_file_write = report_file.write
2114-
#report_file_write('criteria: \n')
21152114

2115+
report_file_write('# ' + ('\n# ').join(self.search_info_lines) + '\n\n')
21162116
for record in librer_core.records:
21172117
if record.find_results:
21182118
report_file_write(f'record:{record.header.label}\n')
@@ -2124,9 +2124,9 @@ def find_save_results(self):
21242124
self.status('file saved: "%s"' % str(report_file_name))
21252125

21262126
def find_show_results(self):
2127-
self.find_items()
2127+
rest_txt_list = ['# ' + line for line in self.search_info_lines]
2128+
rest_txt_list.append('')
21282129

2129-
rest_txt_list = []
21302130
for record in librer_core.records:
21312131
if record.find_results:
21322132
rest_txt_list.append(f'record:{record.header.label}')
@@ -2248,13 +2248,14 @@ def find_mod(self):
22482248
self.search_show_butt.configure(state='disabled')
22492249
self.search_save_butt.configure(state='disabled')
22502250
else:
2251-
if self.searching_aborted:
2251+
if self.searching_aborted or self.search_results_reset:
22522252
self.search_butt.configure(state='normal')
22532253
else:
22542254
self.search_butt.configure(state='disabled')
22552255

2256-
self.search_show_butt.configure(state='normal')
2257-
self.search_save_butt.configure(state='normal')
2256+
if self.any_find_result:
2257+
self.search_show_butt.configure(state='normal')
2258+
self.search_save_butt.configure(state='normal')
22582259

22592260
self.find_dialog.widget.update()
22602261

@@ -2269,6 +2270,7 @@ def find_mod(self):
22692270
#@restore_status_line
22702271
def find_items(self):
22712272
if self.find_params_changed:
2273+
self.search_results_reset=False
22722274

22732275
self.find_clear()
22742276

@@ -2302,11 +2304,96 @@ def find_items(self):
23022304
filename_fuzzy_threshold = self.find_name_fuzzy_threshold.get()
23032305
cd_fuzzy_threshold = self.find_cd_fuzzy_threshold.get()
23042306

2307+
self.search_info_lines=[]
2308+
search_info_lines_append = self.search_info_lines.append
2309+
if find_range_all:
2310+
search_info_lines_append('Search in all records')
2311+
else:
2312+
search_info_lines_append(f'Search in record:{self.current_record.header.label}')
2313+
2314+
range_par = self.current_record if not find_range_all else None
2315+
2316+
sel_range = [range_par] if range_par else librer_core.records
2317+
files_search_quant = sum([record.header.quant_files+record.header.quant_folders for record in sel_range])
2318+
2319+
if files_search_quant==0:
2320+
return 1
2321+
2322+
if find_filename_search_kind == 'regexp':
2323+
if find_name_regexp:
2324+
if res := test_regexp(find_name_regexp):
2325+
self.info_dialog_on_find.show('regular expression error',res)
2326+
return
2327+
search_info_lines_append(f'Regular expression on path element:"{find_name_regexp}"')
2328+
else:
2329+
self.info_dialog_on_find.show('regular expression empty','(for path element)')
2330+
return
2331+
elif find_filename_search_kind == 'glob':
2332+
if find_name_glob:
2333+
info_str = f'Glob expression on path element:"{find_name_glob}"'
2334+
if find_name_case_sens:
2335+
search_info_lines_append(info_str + ' (Case sensitive)')
2336+
else:
2337+
search_info_lines_append(info_str)
2338+
else:
2339+
self.info_dialog_on_find.show('empty glob expression','(for path element)')
2340+
return
2341+
elif find_filename_search_kind == 'fuzzy':
2342+
if find_name_fuzz:
2343+
try:
2344+
float(filename_fuzzy_threshold)
2345+
except ValueError:
2346+
self.info_dialog_on_find.show('fuzzy threshold error',f"wrong threshold value:{filename_fuzzy_threshold}")
2347+
return
2348+
search_info_lines_append(f'Fuzzy match on path element:"{find_name_fuzz}" (...>{filename_fuzzy_threshold})')
2349+
else:
2350+
self.info_dialog_on_find.show('fuzzy expression error','empty expression')
2351+
return
2352+
2353+
if find_cd_search_kind == 'without':
2354+
search_info_lines_append(f'Files without Custom Data')
2355+
elif find_cd_search_kind == 'any':
2356+
search_info_lines_append(f'Files with any correct Custom Data')
2357+
elif find_cd_search_kind == 'error':
2358+
search_info_lines_append('Files with error on CD extraction')
2359+
elif find_cd_search_kind == 'regexp':
2360+
if find_cd_regexp:
2361+
if res := test_regexp(find_cd_regexp):
2362+
self.info_dialog_on_find.show('regular expression error',res)
2363+
return
2364+
search_info_lines_append(f'Regular expression on Custom Data:"{find_cd_regexp}"')
2365+
else:
2366+
self.info_dialog_on_find.show('regular expression empty','(for Custom Data)')
2367+
return
2368+
elif find_cd_search_kind == 'glob':
2369+
if find_cd_glob:
2370+
info_str = f'Glob expression on Custom Data:"{find_cd_glob}"'
2371+
if find_cd_case_sens:
2372+
search_info_lines_append(info_str + ' (Case sensitive)')
2373+
else:
2374+
search_info_lines_append(info_str)
2375+
else:
2376+
self.info_dialog_on_find.show('empty glob expression','(for Custom Data)')
2377+
return
2378+
elif find_cd_search_kind == 'fuzzy':
2379+
if find_cd_fuzz:
2380+
try:
2381+
float(cd_fuzzy_threshold)
2382+
except ValueError:
2383+
self.info_dialog_on_find.show('fuzzy threshold error',f"wrong threshold value:{cd_fuzzy_threshold}")
2384+
return
2385+
search_info_lines_append(f'Fuzzy match on Custom Data:"{find_cd_fuzz}" (...>{cd_fuzzy_threshold})')
2386+
2387+
else:
2388+
self.info_dialog_on_find.show('fuzzy expression error','empty expression')
2389+
return
2390+
23052391
if find_size_min:
23062392
min_num = str_to_bytes(find_size_min)
23072393
if min_num == -1:
23082394
self.info_dialog_on_find.show('min size value error',f'fix "{find_size_min}"')
23092395
return
2396+
search_info_lines_append(f'Min size:{find_size_min}')
23102397
else:
23112398
min_num = ''
23122399

@@ -2315,6 +2402,7 @@ def find_items(self):
23152402
if max_num == -1:
23162403
self.info_dialog_on_find.show('max size value error',f'fix "{find_size_max}"')
23172404
return
2405+
search_info_lines_append(f'Max size:{find_size_max}')
23182406
else:
23192407
max_num = ''
23202408

@@ -2330,25 +2418,15 @@ def find_items(self):
23302418
except Exception as te:
23312419
self.info_dialog_on_find.show('file modification time min error ',f'{find_modtime_min}\n{te}')
23322420
return
2333-
2421+
search_info_lines_append(f'Min modtime:{find_modtime_min}')
23342422
t_max=None
23352423
if find_modtime_max:
23362424
try:
23372425
t_max = int(mktime(parse_datetime(find_modtime_max).timetuple()))
23382426
except Exception as te:
23392427
self.info_dialog_on_find.show('file modification time max error ',f'{find_modtime_max}\n{te}')
23402428
return
2341-
2342-
range_par = self.current_record if not find_range_all else None
2343-
2344-
if check_res := librer_core.find_items_in_records_check(
2345-
range_par,
2346-
min_num,max_num,
2347-
find_filename_search_kind,find_name,find_name_case_sens,
2348-
find_cd_search_kind,find_cd,find_cd_case_sens,
2349-
filename_fuzzy_threshold,cd_fuzzy_threshold):
2350-
self.info_dialog_on_find.show('regular expression error',check_res)
2351-
return
2429+
search_info_lines_append(f'Max modtime:{find_modtime_max}')
23522430

23532431
self.cfg.set(CFG_KEY_find_range_all,find_range_all)
23542432
self.cfg.set(CFG_KEY_find_cd_search_kind,find_cd_search_kind)
@@ -2424,8 +2502,8 @@ def find_items(self):
24242502
self_progress_dialog_on_find_update_lab_image = self_progress_dialog_on_find.update_lab_image
24252503
self_get_hg_ico = self.get_hg_ico
24262504

2427-
librer_core_files_search_quant = librer_core.files_search_quant
2428-
fnumber_librer_core_files_search_quant = fnumber(librer_core_files_search_quant)
2505+
#librer_core_files_search_quant = librer_core.files_search_quant
2506+
fnumber_files_search_quant = fnumber(files_search_quant)
24292507
fnumber_records_len = fnumber(records_len)
24302508

24312509
time_without_busy_sign=0
@@ -2438,13 +2516,13 @@ def find_items(self):
24382516

24392517
curr_files = librer_core.total_search_progress
24402518

2441-
files_perc = curr_files * 100.0 / librer_core_files_search_quant
2519+
files_perc = curr_files * 100.0 / files_search_quant
24422520

24432521
self_progress_dialog_on_find_progr1var_set(librer_core.records_perc_info)
24442522
self_progress_dialog_on_find_progr2var_set(files_perc)
24452523

24462524
self_progress_dialog_on_find_lab_r1_config(text=fnumber(librer_core.search_record_nr) + '/' + fnumber_records_len)
2447-
self_progress_dialog_on_find_lab_r2_config(text=fnumber(curr_files) + '/' + fnumber_librer_core_files_search_quant)
2525+
self_progress_dialog_on_find_lab_r2_config(text=fnumber(curr_files) + '/' + fnumber_files_search_quant)
24482526

24492527
if self.action_abort:
24502528
librer_core.abort()
@@ -2501,7 +2579,8 @@ def find_items(self):
25012579

25022580
self.find_mod()
25032581

2504-
self.results_on_find.show('Search results',f"found: {find_results_quant_sum_format} items.\n\nNavigate search results by\n\'Find next (F3)\' & 'Find prev (Shift+F3)'\nactions." + abort_info)
2582+
search_info = '\n'.join(self.search_info_lines)
2583+
self.results_on_find.show('Search results',f"{search_info}\n\nfound: {find_results_quant_sum_format} items.\n\nNavigate search results by\n\'Find next (F3)\' & 'Find prev (Shift+F3)'\nactions." + abort_info)
25052584
self.status_find_tooltip(f"available search results: {find_results_quant_sum_format}")
25062585

25072586
if not self.searching_aborted and self.any_find_result:

0 commit comments

Comments
 (0)