Repository navigation
Expand file tree
/
Copy pathwtx_parser.py
More file actions
119 lines (98 loc) · 3.09 KB
/
Copy pathwtx_parser.py
File metadata and controls
119 lines (98 loc) · 3.09 KB
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
111
112
113
114
115
116
117
118
119
from lxml import etree
import os
class WTXParser:
def parse_file(self, file_path, file_type):
"""
Parse WTX file based on its type and return structured content
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
file_extension = os.path.splitext(file_path)[1].lower()
if file_type == "map":
return self._parse_map(file_path)
elif file_type == "system":
return self._parse_system(file_path)
elif file_type == "typetree":
return self._parse_typetree(file_path)
else:
raise ValueError(f"Unsupported file type: {file_type}")
def _parse_map(self, file_path):
"""Parse WTX map file (.mmc)"""
tree = etree.parse(file_path)
root = tree.getroot()
map_data = {
'name': root.get('name', ''),
'inputs': [],
'outputs': [],
'functions': [],
'transformations': []
}
# Parse input cards
for input_card in root.findall(".//InputCard"):
map_data['inputs'].append({
'name': input_card.get('name', ''),
'type': input_card.get('type', ''),
'format': input_card.get('format', '')
})
# Parse output cards
for output_card in root.findall(".//OutputCard"):
map_data['outputs'].append({
'name': output_card.get('name', ''),
'type': output_card.get('type', ''),
'format': output_card.get('format', '')
})
# Parse functions and transformations
for function in root.findall(".//Function"):
map_data['functions'].append({
'name': function.get('name', ''),
'type': function.get('type', ''),
'inputs': [input.get('ref') for input in function.findall('Input')],
'outputs': [output.get('ref') for output in function.findall('Output')]
})
return map_data
def _parse_system(self, file_path):
"""Parse WTX system file (.sys)"""
tree = etree.parse(file_path)
root = tree.getroot()
system_data = {
'name': root.get('name', ''),
'components': [],
'connections': []
}
# Parse components
for component in root.findall(".//Component"):
system_data['components'].append({
'name': component.get('name', ''),
'type': component.get('type', ''),
'properties': {prop.get('name'): prop.get('value')
for prop in component.findall('Property')}
})
# Parse connections
for connection in root.findall(".//Connection"):
system_data['connections'].append({
'from': connection.get('from', ''),
'to': connection.get('to', ''),
'type': connection.get('type', '')
})
return system_data
def _parse_typetree(self, file_path):
"""Parse WTX type tree file (.tts)"""
tree = etree.parse(file_path)
root = tree.getroot()
type_data = {
'name': root.get('name', ''),
'types': []
}
def parse_type(type_elem):
type_info = {
'name': type_elem.get('name', ''),
'datatype': type_elem.get('datatype', ''),
'children': []
}
for child in type_elem.findall('./Type'):
type_info['children'].append(parse_type(child))
return type_info
# Parse all top-level types
for type_elem in root.findall('./Type'):
type_data['types'].append(parse_type(type_elem))
return type_data