init
This commit is contained in:
95
customize-WebUI-V3.py
Normal file
95
customize-WebUI-V3.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
def modify_files(config_path):
|
||||
"""
|
||||
Modifiziert Dateien basierend auf einer YAML-Konfigurationsdatei.
|
||||
|
||||
:param config_path: Pfad zur Konfigurationsdatei
|
||||
"""
|
||||
# load config
|
||||
with open(config_path, 'r', encoding='utf-8') as config_file:
|
||||
config = yaml.safe_load(config_file)
|
||||
|
||||
files_modified = 0
|
||||
|
||||
# iterate trough every config
|
||||
for entry in config['modifications']:
|
||||
search_pattern = entry.get('file_pattern')
|
||||
insert_rules = entry.get('insert_rules', [])
|
||||
replace_rules = entry.get('replace_rules', [])
|
||||
|
||||
# recursiv search starting at start dir
|
||||
for root, dirs, files in os.walk(config.get('start_directory', '.')):
|
||||
for filename in files:
|
||||
# check filenamepattern
|
||||
if re.search(search_pattern, filename):
|
||||
full_path = os.path.join(root, filename)
|
||||
|
||||
try:
|
||||
with open(full_path, 'r', encoding='utf-8') as file:
|
||||
content = file.read()
|
||||
|
||||
modified = False
|
||||
modified_content = content
|
||||
|
||||
# iterate trough insert rules
|
||||
for rule in insert_rules:
|
||||
insert_text = rule.get('insert_text')
|
||||
|
||||
# insert after specific text
|
||||
if 'after_text' in rule:
|
||||
search_text = rule.get('after_text')
|
||||
if insert_text not in modified_content:
|
||||
modified_content = modified_content.replace(
|
||||
search_text,
|
||||
f'{search_text}\n{insert_text}'
|
||||
)
|
||||
modified = True
|
||||
|
||||
# insert before spicific text
|
||||
elif 'before_text' in rule:
|
||||
search_text = rule.get('before_text')
|
||||
if insert_text not in modified_content:
|
||||
modified_content = modified_content.replace(
|
||||
search_text,
|
||||
f'{insert_text}\n{search_text}'
|
||||
)
|
||||
modified = True
|
||||
|
||||
# iterate replace rules
|
||||
for rule in replace_rules:
|
||||
old_text = rule.get('old_text')
|
||||
new_text = rule.get('new_text')
|
||||
|
||||
# check, if the text to be replaced, exists
|
||||
if old_text in modified_content:
|
||||
modified_content = modified_content.replace(old_text, new_text)
|
||||
modified = True
|
||||
|
||||
# save changed files
|
||||
if modified:
|
||||
with open(full_path, 'w', encoding='utf-8') as file:
|
||||
file.write(modified_content)
|
||||
|
||||
files_modified += 1
|
||||
print(f'Modified: {full_path}')
|
||||
|
||||
except Exception as e:
|
||||
print(f'Error modifying file {full_path}: {e}')
|
||||
|
||||
print(f'\nAnzahl modifizierter Dateien: {files_modified}')
|
||||
|
||||
def main():
|
||||
# path of config file as argument
|
||||
if len(sys.argv) < 2:
|
||||
print("Error: Provide a path to the config.yaml file")
|
||||
sys.exit(1)
|
||||
|
||||
config_path = sys.argv[1]
|
||||
modify_files(config_path)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user