pyrogenesis-migration-scripts/A26_A27/P265.py

71 lines
1.9 KiB
Python
Raw Normal View History

2023-01-17 00:32:20 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -*- mode: python-mode; python-indent-offset: 4; -*-
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: © 2023 Wildfire Games
# SPDX-FileCopyrightText: © 2023 Freagarach
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)