From c54cb818128f3d0f0d153f243cae0e9081764c97 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 10 Jul 2025 19:37:56 +0200 Subject: [PATCH] Added path recognition for python files --- main.py | 19 ++++++++++++++++++- test.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 50ded76..0eab753 100755 --- a/main.py +++ b/main.py @@ -30,7 +30,8 @@ def process_directory(base_path, old_string, new_string, recursive, - replace_in_content: replace inside file contents - rename_files: rename files 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 if rename_paths: @@ -50,6 +51,22 @@ def process_directory(base_path, old_string, new_string, recursive, os.rename(full_src, full_dst) if not recursive: 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 if not (rename_files or replace_in_content): return diff --git a/test.py b/test.py index c908778..27b0e92 100644 --- a/test.py +++ b/test.py @@ -109,5 +109,54 @@ class TestBulkStringReplacer(unittest.TestCase): with open(f, 'r', encoding='utf-8') as fp: 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__': unittest.main()