-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patha2lstream.py
More file actions
200 lines (184 loc) · 8.21 KB
/
a2lstream.py
File metadata and controls
200 lines (184 loc) · 8.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
import enum
import pdb
class a2l_nodetype(enum.Enum):
Empty = 1
Element = 2
Text = 3
Comment = 4
A2LDeclaration = 5
EndElement = 6
Whitespace = 7
class a2l_stream:
start_tag = '/begin'
end_tag = '/end'
void_chars = [' ','\n','\t','\r']
def __init__(self, file_path='', encoding='latin-1', white_space=False):
"""Initialise the A2L StreamReader"""
self.base_uri = file_path
if file_path != '':
self.a2l_file = open(self.base_uri,'rb')
self.name = ''
self.node_type = a2l_nodetype.Empty
self.node_value = ''
self.return_white_space = white_space
self.encoding = encoding
self.stored_char = ''
def __str__(self):
return f'Name: {self.name}\nNode type: {self.node_type}\nNode value: {self.node_value}'
def peek(self):
#try:
#next_char = unicode(self.a2l_file.peek(1)[:1],self.encoding)
next_char = self.a2l_file.peek(1)[:1].decode(self.encoding)
if next_char == '':
next_char = None
#except:
# next_char = None
return next_char
def readchar(self):
#try:
#char = unicode(self.a2l_file.read(1),self.encoding)
char = self.a2l_file.read(1).decode(self.encoding)
if char == '':
char = None
#except:
# char = None
return char
def close(self):
self.a2l_file.close()
def read(self):
next_char = ''
read_char = self.stored_char
string_read = ''
self.node_type = a2l_nodetype.Empty
self.name = ''
self.node_value = ''
while True:
if read_char == '':
read_char = self.readchar()
read_next_char_flag = False
if read_char != None:
if read_char in self.void_chars and self.node_type != a2l_nodetype.Comment:
if len(string_read) == 0:
self.node_type = a2l_nodetype.Whitespace
string_read += read_char
while True:
read_char = self.readchar()
if not read_char in self.void_chars and read_char != None:
if self.return_white_space == True:
self.node_value = string_read
self.stored_char = read_char
return True
else:
string_read = ''
self.node_type = a2l_nodetype.Empty
self.name = ''
self.stored_char = read_char
read_next_char_flag = False
break
else:
if read_char == None:
return False
else:
string_read += read_char
else:
self.node_value = string_read
self.stored_char = read_char
return True
else:
string_read += read_char
if string_read == '/*' or string_read == '//':
self.node_type = a2l_nodetype.Comment
if string_read == '/*':
while True:
read_char = self.readchar()
string_read += read_char
if string_read[-2:] == '*/':
self.node_value = string_read
self.stored_char = self.readchar()
return True
else:
while True:
read_char = self.readchar()
string_read += read_char
if string_read[-1] == '\n':
self.node_value = string_read[0:-1]
self.stored_char = read_char
return True
read_next_char_flag = True
elif string_read == self.start_tag or string_read == self.end_tag:
next_char = self.readchar()
if next_char in self.void_chars:
if string_read == self.start_tag:
self.node_type = a2l_nodetype.Element
else:
self.node_type = a2l_nodetype.EndElement
self.stored_char = ''
new_a2lstream = self
while new_a2lstream.node_type != a2l_nodetype.Text:
new_a2lstream.read()
self.name = new_a2lstream.node_value
self.node_value = string_read
if string_read == self.start_tag:
self.node_type = a2l_nodetype.Element
else:
self.node_type = a2l_nodetype.EndElement
return True
else:
string_read += next_char
read_next_char_flag = True
elif string_read == '"':
self.node_type = a2l_nodetype.Text
while True:
read_char = self.readchar()
string_read += read_char
if read_char == '"':
if string_read[-2:-1] != '\\':
string_read = string_read[1:-1]
string_read = string_read.replace('""','"')
string_read = string_read.replace('\\"','"')
self.node_value = string_read
self.stored_char = self.readchar()
return True
else:
self.node_type = a2l_nodetype.Text
read_next_char_flag = True
if read_next_char_flag == True:
read_char = self.readchar()
else:
return False
def debug(self,iter=10):
"""Output the values parsed for debugging purpose"""
print("Debugger mode for A2LStreamReader. Type exit to end the debugging mode.")
print(' '*7 +' - Node value - Node type')
print('='*71)
print()
increment = 0
while True:
for i in range(iter):
self.read()
print(str(increment).ljust(7),self.node_value,self.name,self.node_type,sep=' - ')
increment += 1
debug_input = input("> ")
if debug_input == 'exit':
break
def debug_full(self,param):
"""Output the values parsed for debugging purpose"""
print("Debugger mode for A2LStreamReader. Type exit to end the debugging mode.")
print(' '*7 +' - Node value - Node type')
print('='*71)
print()
increment = 0
while True:
while True:
self.read()
if self.node_value == param:
print(str(increment).ljust(7),self.node_value,self.name,self.node_type,sep=' - ')
break
increment += 1
self.read()
print(str(increment).ljust(7),self.node_value,self.name,self.node_type,sep=' - ')
increment += 1
debug_input = input("> ")
if debug_input == 'exit':
break