-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.py
More file actions
79 lines (72 loc) · 3.34 KB
/
generator.py
File metadata and controls
79 lines (72 loc) · 3.34 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
import argparse
import json
import copy
class_vertex = 'Vertex'
vector_vertexes = 'vertexes_'
vector_edges = 'edges_'
emplace_back = 'emplace_back'
map_name = 'vertexes_by_name_'
def generator(vertex_file, edge_file, output):
if output is None:
output = "automaticInitGeneration.h"
with open('sources/defaultPosition.json') as f:
default_dict_motors = json.load(f)
with open('sources/defaultOrder.json') as f:
default_list_order = json.load(f)
out = open(output, 'w', encoding='utf-8')
out.write('// generated code begins\n\n')
vertex_numbers = dict()
with open(vertex_file) as file:
cur_vertex_name = None
cur_vertex_dict = copy.deepcopy(default_dict_motors)
number = 0
for line in file:
line_copy = line
if line == '\n' or line.strip()[0] == '/' and line.strip()[1] == '/':
continue
line = line.split()
if cur_vertex_name is None:
if len(line) != 2 or line[1] != '{':
print('Incorrect line: ' + line_copy + '\n' + 'Should be: \'NAME {\'')
raise KeyError
cur_vertex_name = line[0]
vertex_numbers[cur_vertex_name] = number
out.write(map_name + '["' + cur_vertex_name + '"] = ' + str(number) + ';\n')
number = number + 1
elif line[0] != '}':
if line[0] not in default_list_order:
print('No such motor: ' + line[0] + '. In vertex ' + cur_vertex_name + '.')
raise KeyError
cur_vertex_dict[line[0]] = line[2]
else:
s = ''
for name in default_list_order:
s = s + str(cur_vertex_dict[name]) + ', '
s = s[:-2]
out.write(vector_vertexes + '.' + emplace_back + '(std::vector<float>({' + s + '}));\n')
# МИША) ЗАГЛЯНИ СЮДА
out.write(vector_vertexes + '.back().SetName("' + cur_vertex_name + '");\n')
cur_vertex_name = None
cur_vertex_dict = copy.deepcopy(default_dict_motors)
out.write('\n')
with open(edge_file) as file:
for line in file:
line_copy = line
if line == '\n' or line.strip()[0] == '/' and line.strip()[1] == '/':
continue
line = line.split()
try:
vertex_from = '&' + vector_vertexes + '[' + str(vertex_numbers[line[0]]) + ']'
vertex_to = '&' + vector_vertexes + '[' + str(vertex_numbers[line[1]]) + ']'
out.write(vector_edges + '.' + emplace_back + '(' + vertex_from + ', ' + vertex_to + ', ' + line[2] + ');\n')
except KeyError as error:
print('Sorry, there is no such vertex ' + error.args[0] + ', like in line: ' + line_copy)
raise error
out.write('// generated code ends\n\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--vertex', action='store', type=str, help='file with vertexes', required=True)
parser.add_argument('--edges', action='store', type=str, help='file with edges', required=True)
parser.add_argument('--out', action='store', type=str, help='first word')
args = parser.parse_args()
generator(args.vertex, args.edges, args.out)