Skip to content

Commit

Permalink
Merge pull request #48 from mROS-base/argparse
Browse files Browse the repository at this point in the history
use argparse to specify file(s) to generate template.hpp
  • Loading branch information
takasehideki authored Sep 15, 2023
2 parents 4c4a97d + c446669 commit 14c4681
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions mros2_header_generator/templates_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import sys
import re
import argparse
from os import path
from jinja2 import Environment, FileSystemLoader

arg = sys.argv
app = arg[1]

includeFiles = []
pubMsgTypes = []
subMsgTypes = []
Expand All @@ -15,7 +13,21 @@ def toSnakeCase(string):
return re.sub("(.[A-Z])",lambda x:x.group(1)[0] + "_" +x.group(1)[1],string).lower()

def main():
with open(app + "/app.cpp", 'r') as m_f:
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('--file', nargs='*', type=str, default=['app.cpp'],
help='filename(s) of mros2 app code (default: \'app.cpp\')')

args = parser.parse_args()
outdir = args.outdir
file = args.file

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:
Expand All @@ -41,8 +53,10 @@ def main():
env = Environment(loader=FileSystemLoader(path.dirname(__file__)))
template = env.get_template('templates.tpl')
datatext = template.render({ "includeFiles":includeFiles, "pubMsgTypes":pubMsgTypes, "subMsgTypes":subMsgTypes })
with open(os.path.join(app+"/templates.hpp"), "wb") as f:
with open(os.path.join(outdir + "/templates.hpp"), "wb") as f:
f.write(datatext.encode('utf-8'))

print('Generate {}/template.hpp done.'.format(outdir))

if __name__ == "__main__":
main()

0 comments on commit 14c4681

Please # to comment.