Skip to content

Instantly share code, notes, and snippets.

@stoyanK7
Created June 23, 2023 12:50
Show Gist options
  • Save stoyanK7/9519c7c1358ffdc8ab3998cecc3b774d to your computer and use it in GitHub Desktop.
Save stoyanK7/9519c7c1358ffdc8ab3998cecc3b774d to your computer and use it in GitHub Desktop.
# 1. Place in src/xdocs folder
# 2. Run python3 script.py
import os
import re
from lxml import etree
folders = ['checks', 'filters', 'filefilters']
for folder in folders:
dir = os.path.join(folder)
for root, dirs, files in os.walk(dir):
for filename in files:
if filename == "index.xml":
continue
file_path = os.path.join(root, filename)
file_tree = etree.parse(file_path)
# Get module name from title, i.e. AbstractClassName
module_name = file_tree.xpath('//*[local-name()="title"]')[0].text
# Read file contents
with open(file_path, 'r') as input_file:
contents = input_file.read()
# Replace id="AbstractClassName_ with id="
# Essentially removing the module name and underscore after from the id
pattern = re.compile(rf'id="{module_name}_')
new_contents = pattern.sub('id="', contents)
# Write back to file
with open(file_path, 'w') as output_file:
output_file.write(new_contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment