-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsort.py
27 lines (22 loc) · 901 Bytes
/
sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import xml.etree.ElementTree as ET
def sortchildrenby(parent, attr):
try:
parent[:] = sorted(parent, key=lambda child: child.get(attr))
except TypeError as e:
raise(e)
def sort(filename):
ET.register_namespace("", "http://kayak.2codeornot2code.org/1.0")
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
with open(filename, "r") as infile:
tree = ET.parse(infile, parser)
root = tree.getroot()
for bus in root.findall("{http://kayak.2codeornot2code.org/1.0}Bus"):
print(bus.tag, bus.attrib)
sortchildrenby(bus, "id")
for child in bus:
if not child.attrib['name']:
child.attrib['name'] = f"Unknown_{child.attrib['id']}"
# print(child.tag, child.attrib)
tree.write(filename)
sort("skyactive_new.kcd")
sort("Vehicle_Settings.kcd")