Page MenuHomeWildfire Games
Paste P265

Fix D4514.
ActivePublic

Authored by Freagarach on Mar 2 2022, 6:25 PM.
#!/usr/bin/env python3
import fileinput
import glob
import os
from lxml import etree as ET
class StyleFixer:
def fix_template_style(template_path):
changes = [
["version='1.0'", 'version="1.0"'],
["'UTF-8'", '"utf-8"']
]
StyleFixer.sed(template_path, changes)
def sed(path, changes):
for line in fileinput.input(path, inplace=True):
for change in changes:
line = line.replace(change[0], change[1])
print(line, end="")
class TemplateFixer:
def __init__(self, vfs_root):
self.template_folder = os.path.join(vfs_root, 'simulation', 'templates')
def fix_template(self, template_path):
tree = ET.parse(template_path)
root = tree.getroot()
changed = False
cmp_identity = root.find('Identity')
if cmp_identity != None:
if self.fix_requirements(cmp_identity):
changed = True
cmp_upgrade = root.find('Upgrade')
if cmp_upgrade != None:
for upgrade in cmp_upgrade.iterfind("./"):
if self.fix_requirements(upgrade):
changed = True
if not changed:
return False
root[:] = sorted(root, key=lambda x: x.tag)
ET.indent(tree)
tree.write(template_path, xml_declaration=True, pretty_print=True, encoding='utf-8')
return True
def fix_requirements(self, cmp):
req_tech = cmp.find('RequiredTechnology')
if req_tech == None:
return False
ET.SubElement(ET.SubElement(cmp, 'Requirements'), 'Techs').text = req_tech.text
cmp.remove(req_tech)
return True
def run(self):
for template in glob.iglob(self.template_folder + '/**/*.xml', recursive=True):
if self.fix_template(template):
StyleFixer.fix_template_style(template)
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.realpath(__file__))
template_fixer = TemplateFixer(script_dir)
template_fixer.run()

Event Timeline

Freagarach created this paste.Mar 2 2022, 6:25 PM
Freagarach changed the title of this paste from Command-Line Input to Fix D4514..Mar 2 2022, 6:25 PM
Freagarach updated the paste's language from autodetect to python.
Freagarach edited the content of this paste. (Show Details)Apr 3 2022, 8:29 PM
Freagarach changed the visibility from "All Users" to "Public (No Login Required)".
Freagarach edited the content of this paste. (Show Details)Apr 4 2022, 8:31 AM
  • The execution of the script from the public folder worked.
    • Testing if comments will be removed from the .xml file.
  • The comment was still there after running the script.
  • The only negative side effect is that the comment did not move with the renamed component.

asciinema

  • Related script: P238