-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_qt_cleanup.py
101 lines (76 loc) · 3.02 KB
/
sc_qt_cleanup.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
#!/usr/bin/python3
import os, sys
import xml.etree.ElementTree as ET
from xml.dom import minidom
#---------------------------------------------------------------
# PrettyFormat
#---------------------------------------------------------------
def PrettyFormat(root):
"""
Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(root, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
#---------------------------------------------------------------
# ParseState
#---------------------------------------------------------------
def ParseState(curr_state, root_state):
states = []
internals = []
externals = []
# Create a new state.
new_curr_state = ET.SubElement(root_state, 'state')
new_curr_state.attrib['id'] = curr_state.attrib['id']
if 'initial' in curr_state.attrib:
new_curr_state.attrib['initial'] = curr_state.attrib['initial']
# Find all states and create a temporary list.
for node in curr_state:
if 'state' in node.tag:
states.append(node)
# Sort states by ids.
states = sorted(states, key = lambda i: i.attrib['id'])
for node in states:
# Make a recursive call.
ParseState(node, new_curr_state)
# Internal transitions.
for node in curr_state:
if 'transition' in node.tag and node.attrib['type'] == 'internal':
internals.append( (node.attrib['type'], node.attrib['event']) )
# External transitions.
for node in curr_state:
if 'transition' in node.tag and node.attrib['type'] == 'external':
externals.append( (node.attrib['type'], node.attrib['event'], node.attrib['target']) )
# Sort transitions by events.
internals.sort(key = lambda x: x[1])
externals.sort(key = lambda x: x[1])
# Create transitions and add to the current state.
for internal in internals:
new_transition = ET.SubElement(new_curr_state, 'transition')
new_transition.attrib['type'] = internal[0]
new_transition.attrib['event'] = internal[1]
for external in externals:
new_transition = ET.SubElement(new_curr_state, 'transition')
new_transition.attrib['type'] = external[0]
new_transition.attrib['event'] = external[1]
new_transition.attrib['target'] = external[2]
return
#---------------------------------------------------------------
# ParseQtScxml
#---------------------------------------------------------------
def ParseQtScxml(input_file):
tree = ET.parse(input_file)
root = tree.getroot()
statemachine = None
for child in root:
statemachine = child
root_state = ET.Element("statemachine")
ParseState(statemachine, root_state)
print(PrettyFormat(root_state))
return root_state
#---------------------------------------------------------------
# Main
#---------------------------------------------------------------
if __name__=="__main__":
input_file = sys.argv[1]
state_machine = ParseQtScxml(input_file)