-
Notifications
You must be signed in to change notification settings - Fork 15
/
templates_generator.py
77 lines (65 loc) · 2.83 KB
/
templates_generator.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
import os
import glob
import re
import argparse
import filecmp
import shutil
from os import path
from jinja2 import Environment, FileSystemLoader
includeFiles = []
pubMsgTypes = []
subMsgTypes = []
def toSnakeCase(string):
return re.sub("(.[A-Z])",lambda x:x.group(1)[0] + "_" +x.group(1)[1],string).lower()
def main():
print('Generate template.hpp from mros2 app code file.')
parser = argparse.ArgumentParser(description='Generate template.hpp from mros2 app code file.')
parser.add_argument('--outdir', default='.',
help='directry name to output template.hpp (default: \'.\' (current dir))')
parser.add_argument('--indir', nargs='*', type=str, required=True,
help='input dir(s) that contains mros2 app code (required)')
args = parser.parse_args()
indir = args.indir
outdir = args.outdir
file = []
for id in indir:
file = file + glob.glob(os.path.join(id, "*.cpp"))
for f in file:
print(' Analyzing \'{}\' to generate...'.format(f))
with open(f, 'r') as m_f:
arr = m_f.readlines()
for m_line in arr:
if "create_publisher" in m_line:
line = m_line.strip()
left_idx = line.index('<')
right_idx =line.index('>')
pubMsgTypes.append(line[left_idx+1:right_idx])
if "create_subscription" in m_line:
line = m_line.strip()
left_idx = line.index('<')
right_idx =line.index('>')
subMsgTypes.append(line[left_idx+1:right_idx])
pubSubUnion = list(set(pubMsgTypes + subMsgTypes))
for msgType in pubSubUnion:
typeArr = msgType.split('::')
typeArr[2] = toSnakeCase(typeArr[2])
includeFile = '/'.join(typeArr)
includeFile = '#include "' + includeFile + '.hpp"'
includeFiles.append(includeFile)
env = Environment(loader=FileSystemLoader(path.dirname(__file__)))
template = env.get_template('templates.tpl')
datatext = template.render({ "includeFiles":includeFiles, "pubMsgTypes":pubMsgTypes, "subMsgTypes":subMsgTypes })
outfile_path = os.path.join(outdir, "templates.hpp")
outtemp_path = os.path.join(outdir, "templates.hpp.tmp")
if (not os.path.isfile(outfile_path)):
with open(outfile_path, "wb") as f:
f.write(datatext.encode('utf-8'))
with open(outtemp_path, "wb") as f:
f.write(datatext.encode('utf-8'))
if filecmp.cmp(outtemp_path, outfile_path, shallow=True):
os.remove(outtemp_path)
else:
shutil.move(outtemp_path, outfile_path)
print('Generate {}/template.hpp done.'.format(outdir))
if __name__ == "__main__":
main()