From 1423732563016dee08265161c759a1f349e1c571 Mon Sep 17 00:00:00 2001 From: Matic Kukovec Date: Fri, 17 Sep 2021 15:35:11 +0200 Subject: [PATCH] search/replace-in-files upgrades --- functions.py | 36 ++++++++++++++++++++++++++---------- gui/mainwindow.py | 20 ++++++++++++-------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/functions.py b/functions.py index 489cd89..930b48d 100644 --- a/functions.py +++ b/functions.py @@ -195,7 +195,8 @@ def find_files_with_text(search_text, search_dir, case_sensitive=False, search_subdirs=True, - break_on_find=False): + break_on_find=False, + file_filter=None): """ Search for the specified text in files in the specified directory and return a file list. """ @@ -213,6 +214,10 @@ def find_files_with_text(search_text, #"walk" through the directory tree and save the readable files to a list for root, subFolders, files in walk_tree: for file in files: + if file_filter is not None: + filename, file_extension = os.path.splitext(file) + if file_extension.lower() not in file_filter: + continue #Merge the path and filename full_with_path = os.path.join(root, file) if test_text_file(full_with_path) != None: @@ -232,7 +237,7 @@ def find_files_with_text(search_text, else: compare_file_text = file_text compare_search_text = search_text - print(compare_search_text) +# print(compare_search_text) #Check if file contains the search string if compare_search_text in compare_file_text: return_file_list.append(file) @@ -248,7 +253,8 @@ def find_files_with_text_enum(search_text, search_dir, case_sensitive=False, search_subdirs=True, - break_on_find=False): + break_on_find=False, + file_filter=None): """ Search for the specified text in files in the specified directory and return a file list and lines where the text was found at. @@ -270,9 +276,13 @@ def find_files_with_text_enum(search_text, #"walk" through the directory tree and save the readable files to a list for root, subFolders, files in walk_tree: for file in files: + if file_filter is not None: + filename, file_extension = os.path.splitext(file) + if file_extension.lower() not in file_filter: + continue #Merge the path and filename full_with_path = os.path.join(root, file) - if test_text_file(full_with_path) != None: + if test_text_file(full_with_path) is not None: #On windows, the function "os.path.join(root, file)" line gives a combination of "/" and "\\", #which looks weird but works. The replace was added to have things consistent in the return file list. full_with_path = full_with_path.replace("\\", "/") @@ -311,7 +321,8 @@ def replace_text_in_files(search_text, replace_text, search_dir, case_sensitive=False, - search_subdirs=True): + search_subdirs=True, + file_filter=None): """ Search for the specified text in files in the specified directory and replace all instances of the search_text with replace_text and save the changes back to the file. @@ -320,8 +331,10 @@ def replace_text_in_files(search_text, found_files = find_files_with_text( search_text, search_dir, - case_sensitive, - search_subdirs + case_sensitive=case_sensitive, + search_subdirs=search_subdirs, + break_on_find=False, + file_filter=file_filter ) if found_files == None: return [] @@ -345,7 +358,8 @@ def replace_text_in_files_enum(search_text, replace_text, search_dir, case_sensitive=False, - search_subdirs=True): + search_subdirs=True, + file_filter=None): """ The second version of replace_text_in_files, that goes line-by-line and replaces found instances and stores the line numbers, @@ -361,8 +375,10 @@ def replace_text_in_files_enum(search_text, found_files = find_files_with_text( search_text, search_dir, - case_sensitive, - search_subdirs + case_sensitive=case_sensitive, + search_subdirs=search_subdirs, + break_on_find=False, + file_filter=file_filter ) if found_files == None: return {} diff --git a/gui/mainwindow.py b/gui/mainwindow.py index de00347..c0e98d9 100644 --- a/gui/mainwindow.py +++ b/gui/mainwindow.py @@ -1603,13 +1603,13 @@ def construct_system_menu(): system_menu.installEventFilter(click_filter) def special_find_in(): #The second argument is raw, so that single backslashes work for windows paths - self.repl.setText('find_in_files("",r"directory",case_sensitive=False,search_subdirs=True,break_on_find=False)') + self.repl.setText('find_in_files("",r"directory",case_sensitive=False,search_subdirs=True,break_on_find=False,file_filter=None)') self.view.set_repl_type(data.ReplType.SINGLE_LINE) self.repl.setFocus() self.repl.setSelection(self.repl.text().index("directory"), len("directory")) def special_find_in_with_dialog(): #The second argument is raw, so that single backslashes work for windows paths - self.repl.setText('find_in_files("",case_sensitive=False,search_subdirs=True,break_on_find=False)') + self.repl.setText('find_in_files("",case_sensitive=False,search_subdirs=True,break_on_find=False,file_filter=None)') self.view.set_repl_type(data.ReplType.SINGLE_LINE) self.repl.setFocus() self.repl.setCursorPosition(self.repl.text().find('",case_sensitive')) @@ -1648,7 +1648,7 @@ def special_find_file_with_dialog(): def special_replace_in_files(): #The second argument is raw, so that single backslashes work for windows paths temp_string = 'replace_in_files("search_text","replace_text",' - temp_string += 'r"directory",case_sensitive=False,search_subdirs=True)' + temp_string += 'r"directory",case_sensitive=False,search_subdirs=True,file_filter=None)' self.repl.setText(temp_string) self.view.set_repl_type(data.ReplType.SINGLE_LINE) self.repl.setFocus() @@ -1656,7 +1656,7 @@ def special_replace_in_files(): def special_replace_in_files_with_dialog(): #The second argument is raw, so that single backslashes work for windows paths temp_string = 'replace_in_files("search_text","replace_text",' - temp_string += 'case_sensitive=False,search_subdirs=True)' + temp_string += 'case_sensitive=False,search_subdirs=True,file_filter=None)' self.repl.setText(temp_string) self.view.set_repl_type(data.ReplType.SINGLE_LINE) self.repl.setFocus() @@ -3873,7 +3873,8 @@ def find_in_files(self, search_dir=None, case_sensitive=False, search_subdirs=True, - break_on_find=False): + break_on_find=False, + file_filter=None): """Return a list of files that contain the searched text as a list and display it""" #Check if the search directory is none, then use a dialog window #to select the real search directory @@ -3889,7 +3890,8 @@ def find_in_files(self, search_dir, case_sensitive, search_subdirs, - break_on_find + break_on_find, + file_filter ) # Check of the function return is valid if result == -1: @@ -3931,7 +3933,8 @@ def replace_in_files(self, replace_text, search_dir=None, case_sensitive=False, - search_subdirs=True): + search_subdirs=True, + file_filter=None): """ Same as the function in the 'functions' module. Replaces all instances of search_string with the replace_string in the files, @@ -3959,7 +3962,8 @@ def replace_in_files(self, replace_text, search_dir, case_sensitive, - search_subdirs + search_subdirs, + file_filter ) if result == -1: self._parent.display.repl_display_message(