From 76570ee6884c828745637b5feec4a8f2b58bc147 Mon Sep 17 00:00:00 2001 From: takasehideki Date: Wed, 13 Sep 2023 22:59:57 +0900 Subject: [PATCH 1/2] use argparse to specify file(s) to generate template.hpp ``` $ python3 ../mros2/mros2_header_generator/templates_generator.py --app echoback_string --file app.cpp bbb.cpp -h Generate template.hpp from mros2 app code file. usage: templates_generator.py [-h] [--app APP] [--file [FILE [FILE ...]]] Generate template.hpp from mros2 app code file. optional arguments: -h, --help show this help message and exit --app APP application name (default: 'echoreply_string') --file [FILE [FILE ...]] filename(s) of mros2 app code (default: 'app.cpp') ``` --- mros2_header_generator/templates_generator.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/mros2_header_generator/templates_generator.py b/mros2_header_generator/templates_generator.py index cc2e17d..f166d60 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('--app', default='echoreply_string', + help='application name (default: \'echoreply_string\')') + parser.add_argument('--file', nargs='*', type=str, default=['app.cpp'], + help='filename(s) of mros2 app code (default: \'app.cpp\')') + + args = parser.parse_args() + app = args.app + file = args.file + + for f in file: + print(' Analyzing {}/{} file to generate...'.format(app, f)) + with open(app + "/" + f, 'r') as m_f: arr = m_f.readlines() for m_line in arr: if "create_publisher" in m_line: @@ -44,5 +56,7 @@ def main(): with open(os.path.join(app+"/templates.hpp"), "wb") as f: f.write(datatext.encode('utf-8')) + print('Generate {}/template.hpp done.'.format(app)) + if __name__ == "__main__": main() From c4466697cc661b89ee77d35e06a8faa71a7e9e90 Mon Sep 17 00:00:00 2001 From: takasehideki Date: Fri, 15 Sep 2023 11:31:05 +0900 Subject: [PATCH 2/2] change location to execute template_gen and output dir of template.hpp also check: https://github.com/mROS-base/mros2-mbed/pull/45/commits/b120d7c423f9abe8fead87975c5a24eb9885d1ca ``` $ pwd //mros2-mbed/workspace/echoback_string $ python3 ../../mros2/mros2_header_generator/templates_generator.py -h Generate template.hpp from mros2 app code file. usage: templates_generator.py [-h] [--outdir OUTDIR] [--file [FILE [FILE ...]]] Generate template.hpp from mros2 app code file. optional arguments: -h, --help show this help message and exit --outdir OUTDIR directry name to output template.hpp (default: '.' (current dir)) --file [FILE [FILE ...]] filename(s) of mros2 app code (default: 'app.cpp') ``` --- mros2_header_generator/templates_generator.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mros2_header_generator/templates_generator.py b/mros2_header_generator/templates_generator.py index f166d60..793d5d1 100644 --- a/mros2_header_generator/templates_generator.py +++ b/mros2_header_generator/templates_generator.py @@ -16,18 +16,18 @@ 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('--app', default='echoreply_string', - help='application name (default: \'echoreply_string\')') + 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() - app = args.app + outdir = args.outdir file = args.file for f in file: - print(' Analyzing {}/{} file to generate...'.format(app, f)) - with open(app + "/" + f, 'r') as m_f: + 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: @@ -53,10 +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(app)) + print('Generate {}/template.hpp done.'.format(outdir)) if __name__ == "__main__": main()