mirror of
https://github.com/kevinveenbirkenbach/bulk-string-replacer.git
synced 2025-08-01 18:11:09 +02:00
Added path recognition for python files
This commit is contained in:
parent
de4f9511d5
commit
c54cb81812
19
main.py
19
main.py
@ -30,7 +30,8 @@ def process_directory(base_path, old_string, new_string, recursive,
|
|||||||
- replace_in_content: replace inside file contents
|
- replace_in_content: replace inside file contents
|
||||||
- rename_files: rename files whose names contain old_string
|
- rename_files: rename files whose names contain old_string
|
||||||
- rename_folders: rename folders whose names contain old_string
|
- rename_folders: rename folders whose names contain old_string
|
||||||
- rename_paths: match old_string as a relative path and move matching items to new_string
|
- rename_paths: match old_string as a relative path and move matching items to new_string path.
|
||||||
|
Additionally, when rename_paths is set, update module paths in Python files by replacing path separators with dots.
|
||||||
"""
|
"""
|
||||||
# Full-path move logic
|
# Full-path move logic
|
||||||
if rename_paths:
|
if rename_paths:
|
||||||
@ -50,6 +51,22 @@ def process_directory(base_path, old_string, new_string, recursive,
|
|||||||
os.rename(full_src, full_dst)
|
os.rename(full_src, full_dst)
|
||||||
if not recursive:
|
if not recursive:
|
||||||
break
|
break
|
||||||
|
# After moving, replace module paths in Python files
|
||||||
|
for root, dirs, files in os.walk(base_path):
|
||||||
|
if not include_hidden:
|
||||||
|
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
||||||
|
files = [f for f in files if not f.startswith('.')]
|
||||||
|
for f in files:
|
||||||
|
if f.endswith('.py'):
|
||||||
|
replace_content(
|
||||||
|
os.path.join(root, f),
|
||||||
|
old_string.replace('/', os.sep),
|
||||||
|
new_string.replace('/', '.'),
|
||||||
|
preview,
|
||||||
|
verbose
|
||||||
|
)
|
||||||
|
if not recursive:
|
||||||
|
break
|
||||||
# Only return early when only path-mode is active
|
# Only return early when only path-mode is active
|
||||||
if not (rename_files or replace_in_content):
|
if not (rename_files or replace_in_content):
|
||||||
return
|
return
|
||||||
|
49
test.py
49
test.py
@ -109,5 +109,54 @@ class TestBulkStringReplacer(unittest.TestCase):
|
|||||||
with open(f, 'r', encoding='utf-8') as fp:
|
with open(f, 'r', encoding='utf-8') as fp:
|
||||||
self.assertIn('OLD', fp.read())
|
self.assertIn('OLD', fp.read())
|
||||||
|
|
||||||
|
def test_module_path_replacement_in_python_files(self):
|
||||||
|
# Prepare a nested Python module under the old path
|
||||||
|
content = 'from old/path import func'
|
||||||
|
src = self.create_file('old/path/module.py', content)
|
||||||
|
|
||||||
|
# Run with -P: should move the file and replace path separators in .py files
|
||||||
|
process_directory(
|
||||||
|
base_path=self.base,
|
||||||
|
old_string='old/path',
|
||||||
|
new_string='old.path',
|
||||||
|
recursive=True,
|
||||||
|
rename_folders=False, rename_files=False,
|
||||||
|
replace_in_content=False,
|
||||||
|
preview=False, verbose=False,
|
||||||
|
include_hidden=True, rename_paths=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# The file should have been moved
|
||||||
|
new_path = os.path.join(self.base, 'old.path', 'module.py')
|
||||||
|
self.assertTrue(os.path.exists(new_path))
|
||||||
|
|
||||||
|
# Its content should have been updated
|
||||||
|
with open(new_path, 'r', encoding='utf-8') as fp:
|
||||||
|
self.assertIn('from old.path import func', fp.read())
|
||||||
|
|
||||||
|
def test_non_python_files_are_not_content_updated(self):
|
||||||
|
# Prepare a non-Python file under the old path
|
||||||
|
content = 'some reference to old/path in text'
|
||||||
|
txt = self.create_file('old/path/readme.txt', content)
|
||||||
|
|
||||||
|
# Run with -P: .txt files get moved but their content stays the same
|
||||||
|
process_directory(
|
||||||
|
base_path=self.base,
|
||||||
|
old_string='old/path',
|
||||||
|
new_string='old.path',
|
||||||
|
recursive=True,
|
||||||
|
rename_folders=False, rename_files=False,
|
||||||
|
replace_in_content=False,
|
||||||
|
preview=False, verbose=False,
|
||||||
|
include_hidden=True, rename_paths=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# The .txt should be moved but its content unchanged
|
||||||
|
new_txt = os.path.join(self.base, 'old.path', 'readme.txt')
|
||||||
|
self.assertTrue(os.path.exists(new_txt))
|
||||||
|
with open(new_txt, 'r', encoding='utf-8') as fp:
|
||||||
|
self.assertIn('old/path', fp.read())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user