-
Notifications
You must be signed in to change notification settings - Fork 1
/
tc_extract_from_gvl.py
165 lines (136 loc) · 6.39 KB
/
tc_extract_from_gvl.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
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
import os
import re
import definitions
def extract_from_string(text_to_search, search_for_json, namespace):
token_specification = [
('VARIABLE', definitions.find_var),
('ARRAY', definitions.find_var_array),
('TO_JSON', definitions.find_to_json_attribute),
('EXTENDED', definitions.find_extends)
]
tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
flag_to_json = False
for mo in re.finditer(tok_regex, text_to_search):
kind = mo.lastgroup
index = mo.lastindex
if kind == 'VARIABLE':
if flag_to_json or not search_for_json:
print(f'VARIABLE type found with name {mo.groups()[index]} and type {mo.groups()[index + 2]}')
yield definitions.SimpleVariable(namespace, mo.groups()[index], mo.groups()[index + 1],
mo.groups()[index + 2])
elif kind == 'ARRAY':
if flag_to_json or not search_for_json:
print(f'ARRAY type found with name {mo.groups()[index]}')
yield definitions.ArrayVariable(namespace, mo.groups()[index], mo.groups()[index + 1],
mo.groups()[index + 2], mo.groups()[index + 3], mo.groups()[index + 4])
elif kind == 'EXTENDED' and not search_for_json:
print(f'Extended type found with name {mo.groups()[index]}')
namespace_split, var_name, *garbage = namespace.rsplit('.', 2)
yield definitions.ExtendedVariable(namespace_split + '.', var_name, mo.groups()[index])
if kind == 'TO_JSON':
flag_to_json = True
elif flag_to_json:
flag_to_json = False
def search_file(path, type_name):
name_to_search = type_name + '.TcDUT'
for root, d_names, f_names in os.walk(path):
if name_to_search in f_names:
return name_to_search
else:
for directory in d_names:
file_name_tmp = search_file(path + '/' + str(directory), type_name)
if file_name_tmp is not None:
return str(directory) + '/' + file_name_tmp
def add_end_object(object_local, var_list):
if isinstance(object_local, definitions.ArrayVariable):
var_list.append(definitions.ArrayVariableEnd())
else:
var_list.append(definitions.ObjectVariableEnd())
def add_start_object(object_local, var_list):
if isinstance(object_local, definitions.ArrayVariable):
var_list.append(
definitions.ArrayVariableStart(object_local.namespace, object_local.name, object_local.start_at,
object_local.stop_at))
else:
var_list.append(
definitions.ObjectVariableStart(object_local.namespace, object_local.name, object_local.var_type))
def check_if_type_known_from_token(token_local, var_list, searched_path):
for sublist in definitions.keywords_supported:
if token_local.var_type in sublist:
var_list.append(token_local)
return
print('search type ' + token_local.var_type)
file_name = search_file(searched_path, token_local.var_type)
if file_name is None:
print('search in other libraries')
try:
f = open('additionalLib.txt')
except FileNotFoundError:
print('additionalLib.txt not found')
return None
for lib_path in f.readlines():
lib_path = lib_path.split('\n')[0]
print('search in: ' + lib_path)
file_name = search_file(lib_path, token_local.var_type)
if file_name is not None:
print('file name found is: ' + file_name)
searched_path = lib_path
break
if file_name is not None:
if isinstance(token_local, definitions.ExtendedVariable):
print(f'file name of extended file is {file_name}')
print(f'namespace is {token_local.namespace}')
print(f'end_name is {token_local.end_name}')
get_token_from_files(file_name, token_local.namespace + token_local.end_name + '.', var_list, searched_path)
else:
is_enum, type_enum = check_isEnum(file_name, searched_path)
if is_enum:
enum = definitions.Enum(token_local.namespace, token_local.name, token_local.address, type_enum)
var_list.append(enum)
else:
add_start_object(token_local, var_list)
get_token_from_files(file_name, token_local.namespace + token_local.name + '.', var_list, searched_path)
add_end_object(token_local, var_list)
else:
print('file not found')
def get_token_from_files(type_file_name, namespace, variable_to_parse, search_path):
file_to_extract = [type_file_name]
extract_index = 0
try:
while extract_index < 100:
my_file_local = open(search_path + file_to_extract[extract_index], mode='rt')
extract_index = extract_index + 1
text_local = my_file_local.read()
my_file_local.close()
for token_r in extract_from_string(text_local, False, namespace):
check_if_type_known_from_token(token_r, variable_to_parse, search_path)
# Since we append periodically, we search until the end
except IndexError:
print(f'{type_file_name.rsplit("/", 1)[1]} extraction complete. {extract_index} type has been extracted')
pass
def extract_token_from_file(tcgvl_file_name, search_path):
''' extract all {attribute \'to_json\'} '''
variable_to_parse = []
my_file = open(tcgvl_file_name, mode='rt')
text = my_file.read()
my_file.close()
namespace = my_file.name.rsplit('.', 1)[0]
namespace = namespace.rsplit('/', 1)[1]
for token in extract_from_string(text, True, namespace + '.'):
check_if_type_known_from_token(token, variable_to_parse, search_path)
return variable_to_parse, namespace
def check_isEnum(type_file_name, search_path):
typeEnum = ""
file_to_extract = [type_file_name]
extract_index = 0
my_file_local = open(search_path + file_to_extract[extract_index], mode='rt')
extract_index = extract_index + 1
text_local = my_file_local.read()
my_file_local.close()
find = re.findall(definitions.find_enum, text_local)
if (find != []):
typeEnum = find[0][1]
result = True
else:
result = False
return result, typeEnum