Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import sys
00032 import cStringIO
00033 import re
00034 import os, os.path
00035 import errno
00036 from optparse import OptionParser
00037
00038 IODELIM = '---'
00039 AUTOGEN="# ====== DO NOT MODIFY! AUTOGENERATED FROM AN ACTION DEFINITION ======\n"
00040
00041 class ActionSpecException(Exception): pass
00042
00043 def parse_action_spec(text, package_context = ''):
00044 pieces = [cStringIO.StringIO()]
00045 for l in text.split('\n'):
00046 if l.startswith(IODELIM):
00047 pieces.append(cStringIO.StringIO())
00048 else:
00049 pieces[-1].write(l + '\n')
00050 return [p.getvalue() for p in pieces]
00051
00052 def write_file(filename, text):
00053 f = open(filename, 'w')
00054 f.write(text)
00055 f.close()
00056
00057 def main():
00058
00059 parser = OptionParser("Actionlib generator")
00060 parser.add_option('-o', dest='output_dir',
00061 help='output directory')
00062
00063 (options, args) = parser.parse_args(sys.argv)
00064
00065 pkg = os.path.abspath(sys.argv[1])
00066 filename = sys.argv[2]
00067
00068 output_dir = options.output_dir
00069
00070
00071 try:
00072 os.makedirs(output_dir)
00073 except OSError, e:
00074 if e.errno == errno.EEXIST:
00075 pass
00076 else:
00077 raise
00078
00079 action_file = args[1]
00080 if not action_file.endswith('.action'):
00081 print "The file specified has the wrong extension. It must end in .action"
00082 else:
00083 filename = action_file
00084
00085 f = open(filename)
00086 action_spec = f.read()
00087 f.close()
00088
00089 name = os.path.basename(filename)[:-7]
00090 print "Generating for action %s" % name
00091
00092 pieces = parse_action_spec(action_spec)
00093 if len(pieces) != 3:
00094 raise ActionSpecException("%s: wrong number of pieces, %d"%(filename,len(pieces)))
00095 goal, result, feedback = pieces
00096
00097 action_msg = AUTOGEN + """
00098 %sActionGoal action_goal
00099 %sActionResult action_result
00100 %sActionFeedback action_feedback
00101 """ % (name, name, name)
00102
00103 goal_msg = AUTOGEN + goal
00104 action_goal_msg = AUTOGEN + """
00105 Header header
00106 actionlib_msgs/GoalID goal_id
00107 %sGoal goal
00108 """ % name
00109
00110 result_msg = AUTOGEN + result
00111 action_result_msg = AUTOGEN + """
00112 Header header
00113 actionlib_msgs/GoalStatus status
00114 %sResult result
00115 """ % name
00116
00117 feedback_msg = AUTOGEN + feedback
00118 action_feedback_msg = AUTOGEN + """
00119 Header header
00120 actionlib_msgs/GoalStatus status
00121 %sFeedback feedback
00122 """ % name
00123
00124 write_file(os.path.join(output_dir, "%sAction.msg"%name), action_msg)
00125 write_file(os.path.join(output_dir, "%sGoal.msg"%name), goal_msg)
00126 write_file(os.path.join(output_dir, "%sActionGoal.msg"%name), action_goal_msg)
00127 write_file(os.path.join(output_dir, "%sResult.msg"%name), result_msg)
00128 write_file(os.path.join(output_dir, "%sActionResult.msg"%name), action_result_msg)
00129 write_file(os.path.join(output_dir, "%sFeedback.msg"%name), feedback_msg)
00130 write_file(os.path.join(output_dir, "%sActionFeedback.msg"%name), action_feedback_msg)
00131
00132
00133 if __name__ == '__main__': main()