-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_import_xml.py
110 lines (79 loc) · 3.31 KB
/
utility_import_xml.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import subprocess
import tempfile
import xml.etree.ElementTree as ET
from .utility_print import LogFormatter as Logger
from . import utility_data as Data
# Call CfgConvert.exe and produce config in .xml format
def CfgToXml(filePath,exePath):
print(Logger.Log("Parsing .cfg to .xml (CfgConvert.exe)",2))
destFolder = tempfile.gettempdir()
destName = "blendermodelcfgeditor_model_cfg_temp.xml"
destPath = os.path.join(destFolder,destName)
subprocess.run([exePath,"-xml","-dst",destPath,filePath])
return destPath
# Parse xml to an element tree
def XmlToET(xmlPath):
print(Logger.Log("Translating .xml to ElementTree",2))
xmlFile = open(xmlPath,"r")
xmlstring = xmlFile.read()
xmlFile.close()
os.remove(xmlPath) # delete temporary file
xmlstring = xmlstring.splitlines()
xmlstring = ["<config>"] + xmlstring[2:len(xmlstring)] + ["</config>"]
xmlstring = "\n".join(xmlstring)
xmlTree = ET.ElementTree(ET.fromstring(xmlstring))
return xmlTree
# Parse element tree to custom class structure
def ETToClasses(elementClass):
className = elementClass.tag.lower()
classParent = ""
print(Logger.Log("Creating class from ElementTree: {}".format(className),3))
if len(elementClass.attrib) != 0:
classParent = elementClass.attrib["base"].lower()
newClass = Data.ImportClass(className,classParent)
for subelement in elementClass:
propName = subelement.tag.lower()
propValue = ""
if "type" in subelement.attrib.keys(): # array property
propValue = []
for item in subelement:
propValue.append(item.text.lower() if item.text is not None else "")
elif len(subelement) != 0 or "base" in subelement.attrib.keys(): # class
propValue = ETToClasses(subelement)
elif len(subelement) == 0: # simple property
propValue = subelement.text
if propValue is None:
propValue = ""
else:
propValue = propValue.lower()
newClass.AddElement(propName,propValue)
return newClass
# Read an convert model.cfg to custom class structure
def ReadConfig(filePath,exePath):
xmlPath = CfgToXml(filePath,exePath)
if not os.path.isfile(xmlPath):
print(Logger.Log("CfgConvert.exe failed to convert the config to XML format",2))
return None
xmlTree = XmlToET(xmlPath)
print(Logger.Log("Translating ElementTree to class structure",2))
classTree = ETToClasses(xmlTree.getroot())
print(Logger.Log("Finished translating ElementTree to class structure",2))
return classTree
# Debug function to browse the resulting class tree
def BrowseClassTree(classTree):
print(classTree.name)
print(classTree.elements)
nextElementName = ""
while nextElementName not in classTree.elements and nextElementName != "exit":
nextElementName = input("Open element: ")
if nextElementName == "exit":
exit()
nextElement = getattr(classTree,nextElementName)
if type(nextElement) is Data.ImportClass:
BrowseClassTree(nextElement)
elif type(nextElement) is list:
for item in nextElement:
print(item)
else:
print(nextElement)