init
This commit is contained in:
150
customize-WebUI.py
Normal file
150
customize-WebUI.py
Normal file
@ -0,0 +1,150 @@
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
def load_configuration(config_path):
|
||||
"""
|
||||
Load configuration from a YAML file.
|
||||
|
||||
:param config_path: Path to the configuration file
|
||||
:return: Loaded configuration dictionary
|
||||
"""
|
||||
with open(config_path, 'r', encoding='utf-8') as config_file:
|
||||
return yaml.safe_load(config_file)
|
||||
|
||||
def matches_file_pattern(filename, pattern):
|
||||
"""
|
||||
Check if filename matches the given pattern.
|
||||
|
||||
:param filename: Name of the file
|
||||
:param pattern: Regex pattern to match
|
||||
:return: Boolean indicating if filename matches
|
||||
"""
|
||||
return re.search(pattern, filename) is not None
|
||||
|
||||
def apply_insert_rules(content, insert_rules):
|
||||
"""
|
||||
Apply insertion rules to the content.
|
||||
|
||||
:param content: Original file content
|
||||
:param insert_rules: List of insertion rules
|
||||
:return: Modified content and modification flag
|
||||
"""
|
||||
modified_content = content
|
||||
modified = False
|
||||
|
||||
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 specific 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
|
||||
|
||||
return modified_content, modified
|
||||
|
||||
def apply_replace_rules(content, replace_rules):
|
||||
"""
|
||||
Apply replacement rules to the content.
|
||||
|
||||
:param content: Original file content
|
||||
:param replace_rules: List of replacement rules
|
||||
:return: Modified content and modification flag
|
||||
"""
|
||||
modified_content = content
|
||||
modified = False
|
||||
|
||||
for rule in replace_rules:
|
||||
old_text = rule.get('old_text')
|
||||
new_text = rule.get('new_text')
|
||||
|
||||
# Replace text if found
|
||||
if old_text in modified_content:
|
||||
modified_content = modified_content.replace(old_text, new_text)
|
||||
modified = True
|
||||
|
||||
return modified_content, modified
|
||||
|
||||
def modify_files(config):
|
||||
"""
|
||||
Modify files based on configuration rules.
|
||||
|
||||
:param config: Configuration dictionary
|
||||
:return: Number of modified files
|
||||
"""
|
||||
files_modified = 0
|
||||
start_directory = config.get('start_directory', '.')
|
||||
|
||||
# Process each modification entry
|
||||
for entry in config.get('modifications', []):
|
||||
search_pattern = entry.get('file_pattern')
|
||||
insert_rules = entry.get('insert_rules', [])
|
||||
replace_rules = entry.get('replace_rules', [])
|
||||
|
||||
# Walk through directory
|
||||
for root, _, files in os.walk(start_directory):
|
||||
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)
|
||||
|
||||
files_modified += 1
|
||||
print(f'Modified: {full_path}')
|
||||
|
||||
except Exception as e:
|
||||
print(f'Error processing file {full_path}: {e}')
|
||||
|
||||
return files_modified
|
||||
|
||||
def main():
|
||||
# Check command-line argument
|
||||
if len(sys.argv) < 2:
|
||||
print("Please provide the path to the configuration file.")
|
||||
sys.exit(1)
|
||||
|
||||
config_path = sys.argv[1]
|
||||
|
||||
# Load configuration and modify files
|
||||
config = load_configuration(config_path)
|
||||
total_modified = modify_files(config)
|
||||
|
||||
print(f'\nTotal files modified: {total_modified}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user