Skip to content

Commit

Permalink
search/replace-in-files upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
matkuki committed Sep 17, 2021
1 parent 403d299 commit 1423732
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
36 changes: 26 additions & 10 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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("\\", "/")
Expand Down Expand Up @@ -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.
Expand All @@ -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 []
Expand All @@ -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,
Expand All @@ -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 {}
Expand Down
20 changes: 12 additions & 8 deletions gui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -1648,15 +1648,15 @@ 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()
self.repl.setSelection(self.repl.text().index("directory"), len("directory"))
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()
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 1423732

Please sign in to comment.