forked from Archive/PX4-Autopilot
px_generate_uorb_topic_files.py: beautify
This commit is contained in:
parent
cafc2f5e61
commit
4137517d12
|
@ -85,11 +85,13 @@ PACKAGE = 'px4'
|
|||
TOPICS_TOKEN = '# TOPICS '
|
||||
IDL_TEMPLATE_FILE = 'msg.idl.template'
|
||||
|
||||
|
||||
class MsgScope:
|
||||
NONE = 0
|
||||
SEND = 1
|
||||
RECEIVE = 2
|
||||
|
||||
|
||||
def get_multi_topics(filename):
|
||||
"""
|
||||
Get TOPICS names from a "# TOPICS" line
|
||||
|
@ -105,6 +107,7 @@ def get_multi_topics(filename):
|
|||
ofile.close()
|
||||
return result
|
||||
|
||||
|
||||
def get_msgs_list(msgdir):
|
||||
"""
|
||||
Makes list of msg files in the given directory
|
||||
|
@ -117,8 +120,10 @@ def generate_output_from_file(format_idx, filename, outputdir, package, template
|
|||
Converts a single .msg file to an uorb header/source file
|
||||
"""
|
||||
msg_context = genmsg.msg_loader.MsgContext.create_default()
|
||||
full_type_name = genmsg.gentools.compute_full_type_name(package, os.path.basename(filename))
|
||||
spec = genmsg.msg_loader.load_msg_from_file(msg_context, filename, full_type_name)
|
||||
full_type_name = genmsg.gentools.compute_full_type_name(
|
||||
package, os.path.basename(filename))
|
||||
spec = genmsg.msg_loader.load_msg_from_file(
|
||||
msg_context, filename, full_type_name)
|
||||
field_name_and_type = {}
|
||||
for field in spec.parsed_fields():
|
||||
field_name_and_type.update({field.name: field.type})
|
||||
|
@ -126,13 +131,15 @@ def generate_output_from_file(format_idx, filename, outputdir, package, template
|
|||
try:
|
||||
assert 'timestamp' in field_name_and_type
|
||||
except AssertionError:
|
||||
print("[ERROR] uORB topic files generator:\n\tgenerate_output_from_file:\tNo 'timestamp' field found in " + spec.short_name + " msg definition!")
|
||||
print("[ERROR] uORB topic files generator:\n\tgenerate_output_from_file:\tNo 'timestamp' field found in " +
|
||||
spec.short_name + " msg definition!")
|
||||
exit(1)
|
||||
# assert if the timestamp field is of type uint64
|
||||
try:
|
||||
assert field_name_and_type.get('timestamp') == 'uint64'
|
||||
except AssertionError:
|
||||
print("[ERROR] uORB topic files generator:\n\tgenerate_output_from_file:\t'timestamp' field in " + spec.short_name + " msg definition is not of type uint64 but rather of type " + field_name_and_type.get('timestamp') + "!")
|
||||
print("[ERROR] uORB topic files generator:\n\tgenerate_output_from_file:\t'timestamp' field in " + spec.short_name +
|
||||
" msg definition is not of type uint64 but rather of type " + field_name_and_type.get('timestamp') + "!")
|
||||
exit(1)
|
||||
topics = get_multi_topics(filename)
|
||||
if includepath:
|
||||
|
@ -162,11 +169,13 @@ def generate_output_from_file(format_idx, filename, outputdir, package, template
|
|||
|
||||
return generate_by_template(output_file, template_file, em_globals)
|
||||
|
||||
|
||||
def generate_idl_file(filename_msg, outputdir, templatedir, package, includepath):
|
||||
"""
|
||||
Generates an .idl from .msg file
|
||||
"""
|
||||
em_globals = get_em_globals(filename_msg, package, includepath, MsgScope.NONE)
|
||||
em_globals = get_em_globals(
|
||||
filename_msg, package, includepath, MsgScope.NONE)
|
||||
spec_short_name = em_globals["spec"].short_name
|
||||
|
||||
# Make sure output directory exists:
|
||||
|
@ -174,10 +183,12 @@ def generate_idl_file(filename_msg, outputdir, templatedir, package, includepath
|
|||
os.makedirs(outputdir)
|
||||
|
||||
template_file = os.path.join(templatedir, IDL_TEMPLATE_FILE)
|
||||
output_file = os.path.join(outputdir, IDL_TEMPLATE_FILE.replace("msg.idl.template", str(spec_short_name + "_.idl")))
|
||||
output_file = os.path.join(outputdir, IDL_TEMPLATE_FILE.replace(
|
||||
"msg.idl.template", str(spec_short_name + "_.idl")))
|
||||
|
||||
return generate_by_template(output_file, template_file, em_globals)
|
||||
|
||||
|
||||
def generate_uRTPS_general(filename_send_msgs, filename_received_msgs,
|
||||
outputdir, templatedir, package, includepath, template_name):
|
||||
"""
|
||||
|
@ -185,10 +196,12 @@ def generate_uRTPS_general(filename_send_msgs, filename_received_msgs,
|
|||
"""
|
||||
em_globals_list = []
|
||||
if filename_send_msgs:
|
||||
em_globals_list.extend([get_em_globals(f, package, includepath, MsgScope.SEND) for f in filename_send_msgs])
|
||||
em_globals_list.extend([get_em_globals(
|
||||
f, package, includepath, MsgScope.SEND) for f in filename_send_msgs])
|
||||
|
||||
if filename_received_msgs:
|
||||
em_globals_list.extend([get_em_globals(f, package, includepath, MsgScope.RECEIVE) for f in filename_received_msgs])
|
||||
em_globals_list.extend([get_em_globals(
|
||||
f, package, includepath, MsgScope.RECEIVE) for f in filename_received_msgs])
|
||||
|
||||
merged_em_globals = merge_em_globals_list(em_globals_list)
|
||||
# Make sure output directory exists:
|
||||
|
@ -196,15 +209,18 @@ def generate_uRTPS_general(filename_send_msgs, filename_received_msgs,
|
|||
os.makedirs(outputdir)
|
||||
|
||||
template_file = os.path.join(templatedir, template_name)
|
||||
output_file = os.path.join(outputdir, template_name.replace(".template", ""))
|
||||
output_file = os.path.join(
|
||||
outputdir, template_name.replace(".template", ""))
|
||||
|
||||
return generate_by_template(output_file, template_file, merged_em_globals)
|
||||
|
||||
|
||||
def generate_topic_file(filename_msg, outputdir, templatedir, package, includepath, template_name):
|
||||
"""
|
||||
Generates an .idl from .msg file
|
||||
"""
|
||||
em_globals = get_em_globals(filename_msg, package, includepath, MsgScope.NONE)
|
||||
em_globals = get_em_globals(
|
||||
filename_msg, package, includepath, MsgScope.NONE)
|
||||
spec_short_name = em_globals["spec"].short_name
|
||||
|
||||
# Make sure output directory exists:
|
||||
|
@ -212,17 +228,21 @@ def generate_topic_file(filename_msg, outputdir, templatedir, package, includepa
|
|||
os.makedirs(outputdir)
|
||||
|
||||
template_file = os.path.join(templatedir, template_name)
|
||||
output_file = os.path.join(outputdir, spec_short_name + "_" + template_name.replace(".template", ""))
|
||||
output_file = os.path.join(
|
||||
outputdir, spec_short_name + "_" + template_name.replace(".template", ""))
|
||||
|
||||
return generate_by_template(output_file, template_file, em_globals)
|
||||
|
||||
|
||||
def get_em_globals(filename_msg, package, includepath, scope):
|
||||
"""
|
||||
Generates em globals dictionary
|
||||
"""
|
||||
msg_context = genmsg.msg_loader.MsgContext.create_default()
|
||||
full_type_name = genmsg.gentools.compute_full_type_name(package, os.path.basename(filename_msg))
|
||||
spec = genmsg.msg_loader.load_msg_from_file(msg_context, filename_msg, full_type_name)
|
||||
full_type_name = genmsg.gentools.compute_full_type_name(
|
||||
package, os.path.basename(filename_msg))
|
||||
spec = genmsg.msg_loader.load_msg_from_file(
|
||||
msg_context, filename_msg, full_type_name)
|
||||
topics = get_multi_topics(filename_msg)
|
||||
if includepath:
|
||||
search_path = genmsg.command_line.includepath_to_dict(includepath)
|
||||
|
@ -244,6 +264,7 @@ def get_em_globals(filename_msg, package, includepath, scope):
|
|||
|
||||
return em_globals
|
||||
|
||||
|
||||
def merge_em_globals_list(em_globals_list):
|
||||
"""
|
||||
Merges a list of em_globals to a single dictionary where each attribute is a list
|
||||
|
@ -253,12 +274,12 @@ def merge_em_globals_list(em_globals_list):
|
|||
|
||||
merged_em_globals = {}
|
||||
for name in em_globals_list[0]:
|
||||
merged_em_globals[name] = [em_globals[name] for em_globals in em_globals_list]
|
||||
merged_em_globals[name] = [em_globals[name]
|
||||
for em_globals in em_globals_list]
|
||||
|
||||
return merged_em_globals
|
||||
|
||||
|
||||
|
||||
def generate_by_template(output_file, template_file, em_globals):
|
||||
"""
|
||||
Invokes empy intepreter to geneate output_file by the
|
||||
|
@ -271,7 +292,8 @@ def generate_by_template(output_file, template_file, em_globals):
|
|||
|
||||
ofile = open(output_file, 'w')
|
||||
# todo, reuse interpreter
|
||||
interpreter = em.Interpreter(output=ofile, globals=em_globals, options={em.RAW_OPT:True,em.BUFFERED_OPT:True})
|
||||
interpreter = em.Interpreter(output=ofile, globals=em_globals, options={
|
||||
em.RAW_OPT: True, em.BUFFERED_OPT: True})
|
||||
try:
|
||||
interpreter.file(open(template_file))
|
||||
except OSError as e:
|
||||
|
@ -325,7 +347,8 @@ def convert_dir(format_idx, inputdir, outputdir, package, templatedir):
|
|||
if fn[-4:].lower() != '.msg':
|
||||
continue
|
||||
|
||||
generate_output_from_file(format_idx, fn, outputdir, package, templatedir, includepath)
|
||||
generate_output_from_file(
|
||||
format_idx, fn, outputdir, package, templatedir, includepath)
|
||||
return True
|
||||
|
||||
|
||||
|
@ -375,6 +398,7 @@ def convert_dir_save(format_idx, inputdir, outputdir, package, templatedir, temp
|
|||
# Copy changed headers from temporary dir to output dir
|
||||
copy_changed(temporarydir, outputdir, prefix, quiet)
|
||||
|
||||
|
||||
def generate_topics_list_file(msgdir, outputdir, templatedir):
|
||||
# generate cpp file with topics list
|
||||
msgs = get_msgs_list(msgdir)
|
||||
|
@ -384,24 +408,30 @@ def generate_topics_list_file(msgdir, outputdir, templatedir):
|
|||
multi_topics.extend(get_multi_topics(msg_filename))
|
||||
tl_globals = {"msgs": msgs, "multi_topics": multi_topics}
|
||||
tl_template_file = os.path.join(templatedir, TOPICS_LIST_TEMPLATE_FILE)
|
||||
tl_out_file = os.path.join(outputdir, TOPICS_LIST_TEMPLATE_FILE.replace(".template", ""))
|
||||
tl_out_file = os.path.join(
|
||||
outputdir, TOPICS_LIST_TEMPLATE_FILE.replace(".template", ""))
|
||||
generate_by_template(tl_out_file, tl_template_file, tl_globals)
|
||||
|
||||
|
||||
def generate_topics_list_file_from_files(files, outputdir, templatedir):
|
||||
# generate cpp file with topics list
|
||||
filenames = [os.path.basename(p) for p in files if os.path.basename(p).endswith(".msg")]
|
||||
filenames = [os.path.basename(
|
||||
p) for p in files if os.path.basename(p).endswith(".msg")]
|
||||
multi_topics = []
|
||||
for msg_filename in files:
|
||||
multi_topics.extend(get_multi_topics(msg_filename))
|
||||
tl_globals = {"msgs": filenames, "multi_topics": multi_topics}
|
||||
tl_template_file = os.path.join(templatedir, TOPICS_LIST_TEMPLATE_FILE)
|
||||
tl_out_file = os.path.join(outputdir, TOPICS_LIST_TEMPLATE_FILE.replace(".template", ""))
|
||||
tl_out_file = os.path.join(
|
||||
outputdir, TOPICS_LIST_TEMPLATE_FILE.replace(".template", ""))
|
||||
generate_by_template(tl_out_file, tl_template_file, tl_globals)
|
||||
|
||||
|
||||
def append_to_include_path(path_to_append, curr_include, package):
|
||||
for p in path_to_append:
|
||||
curr_include.append('%s:%s' % (package, p))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Convert msg files to uorb headers/sources')
|
||||
|
@ -444,10 +474,13 @@ if __name__ == "__main__":
|
|||
exit(-1)
|
||||
if args.file is not None:
|
||||
for f in args.file:
|
||||
generate_output_from_file(generate_idx, f, args.temporarydir, args.package, args.templatedir, INCL_DEFAULT)
|
||||
generate_output_from_file(
|
||||
generate_idx, f, args.temporarydir, args.package, args.templatedir, INCL_DEFAULT)
|
||||
if generate_idx == 1:
|
||||
generate_topics_list_file_from_files(args.file, args.outputdir, args.templatedir)
|
||||
copy_changed(args.temporarydir, args.outputdir, args.prefix, args.quiet)
|
||||
generate_topics_list_file_from_files(
|
||||
args.file, args.outputdir, args.templatedir)
|
||||
copy_changed(args.temporarydir, args.outputdir,
|
||||
args.prefix, args.quiet)
|
||||
elif args.dir is not None:
|
||||
convert_dir_save(
|
||||
generate_idx,
|
||||
|
|
Loading…
Reference in New Issue