This commit is contained in:
MLH
2024-12-09 21:56:21 +01:00
parent cd6400e494
commit b33375f7a6
5 changed files with 140 additions and 326 deletions

View File

@ -83,7 +83,7 @@ def apply_insert_rules(content, insert_rules):
if insert_text not in modified_content:
modified_content = modified_content.replace(
search_text,
f'{search_text} {insert_text}'
f'{search_text}\n{insert_text}'
)
modified = True
@ -93,7 +93,7 @@ def apply_insert_rules(content, insert_rules):
if insert_text not in modified_content:
modified_content = modified_content.replace(
search_text,
f'{insert_text} {search_text}'
f'{insert_text}\n{search_text}'
)
modified = True
@ -167,8 +167,46 @@ def process_copy_and_modify_rules(config):
if copy_with_mode(matched_src, full_dest, mode):
successful_operations['copies'] += 1
# Rest of the function remains the same as in the original script
# ... (modification rules processing)
# Process modification rules
destination_dir = config.get('destination_directory', '.')
for mod_rule in config.get('modification_rules', []):
search_pattern = mod_rule.get('file_pattern')
insert_rules = mod_rule.get('insert_rules', [])
replace_rules = mod_rule.get('replace_rules', [])
# Walk through destination directory
for root, _, files in os.walk(destination_dir):
for filename in files:
# Check if file matches pattern
if matches_file_pattern(filename, search_pattern):
full_path = os.path.join(root, filename)
try:
# Read file content
with open(full_path, 'r', encoding='utf-8') as file:
content = file.read()
# Apply insert rules
content_after_insert, insert_modified = apply_insert_rules(
content, insert_rules
)
# Apply replace rules
final_content, replace_modified = apply_replace_rules(
content_after_insert, replace_rules
)
# Save modified file
if insert_modified or replace_modified:
with open(full_path, 'w', encoding='utf-8') as file:
file.write(final_content)
successful_operations['modifications'] += 1
print(f'Modified: {full_path}')
except Exception as e:
print(f'Error processing file {full_path}: {e}')
return successful_operations