diff --git a/mros2_header_generator/templates_generator.py b/mros2_header_generator/templates_generator.py index cc2e17d..793d5d1 100644 --- a/mros2_header_generator/templates_generator.py +++ b/mros2_header_generator/templates_generator.py @@ -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 = [] @@ -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: @@ -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()