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 roslib, roslib.msgs
00035 import os, os.path
00036 import errno
00037
00038 IODELIM = '---'
00039 COMMENTCHAR = roslib.msgs.COMMENTCHAR
00040 AUTOGEN="# ====== DO NOT MODIFY! AUTOGENERATED FROM AN ACTION DEFINITION ======\n"
00041
00042 class ActionSpecException(Exception): pass
00043
00044 def parse_action_spec(text, package_context = ''):
00045 pieces = [cStringIO.StringIO()]
00046 for l in text.split('\n'):
00047 if l.startswith(IODELIM):
00048 pieces.append(cStringIO.StringIO())
00049 else:
00050 pieces[-1].write(l + '\n')
00051 return [p.getvalue() for p in pieces]
00052
00053 def write_file(filename, text):
00054 f = open(filename, 'w')
00055 f.write(text)
00056 f.close()
00057
00058 def main():
00059
00060 if len(sys.argv) < 3:
00061 print "Need to give a package path and file name"
00062 sys.exit(1)
00063 pkg = os.path.abspath(sys.argv[1])
00064 filename = sys.argv[2]
00065
00066 if not os.path.exists(os.path.join(pkg, 'manifest.xml')):
00067 print "Not a package %s" % pkg
00068 sys.exit(1)
00069
00070 output_dir = os.path.join(pkg, 'msg')
00071
00072
00073 try:
00074 os.makedirs(output_dir)
00075 except OSError, e:
00076 if e.errno == errno.EEXIST:
00077 pass
00078 else:
00079 raise
00080
00081 action_dir = os.path.join(pkg, 'action')
00082 action_file = os.path.join(action_dir, filename)
00083 if not action_file.endswith('.action'):
00084 print "The file specified has the wrong extension. It must end in .action"
00085 else:
00086 filename = os.path.join(action_dir, action_file)
00087
00088 f = open(filename)
00089 action_spec = f.read()
00090 f.close()
00091
00092 name = os.path.basename(filename)[:-7]
00093 print "Generating for action %s" % name
00094
00095 pieces = parse_action_spec(action_spec)
00096 if len(pieces) != 3:
00097 raise ActionSpecException("%s: wrong number of pieces, %d"%(filename,len(pieces)))
00098 goal, result, feedback = pieces
00099
00100 action_msg = AUTOGEN + """
00101 %sActionGoal action_goal
00102 %sActionResult action_result
00103 %sActionFeedback action_feedback
00104 """ % (name, name, name)
00105
00106 goal_msg = AUTOGEN + goal
00107 action_goal_msg = AUTOGEN + """
00108 Header header
00109 actionlib_msgs/GoalID goal_id
00110 %sGoal goal
00111 """ % name
00112
00113 result_msg = AUTOGEN + result
00114 action_result_msg = AUTOGEN + """
00115 Header header
00116 actionlib_msgs/GoalStatus status
00117 %sResult result
00118 """ % name
00119
00120 feedback_msg = AUTOGEN + feedback
00121 action_feedback_msg = AUTOGEN + """
00122 Header header
00123 actionlib_msgs/GoalStatus status
00124 %sFeedback feedback
00125 """ % name
00126
00127 write_file(os.path.join(output_dir, "%sAction.msg"%name), action_msg)
00128 write_file(os.path.join(output_dir, "%sGoal.msg"%name), goal_msg)
00129 write_file(os.path.join(output_dir, "%sActionGoal.msg"%name), action_goal_msg)
00130 write_file(os.path.join(output_dir, "%sResult.msg"%name), result_msg)
00131 write_file(os.path.join(output_dir, "%sActionResult.msg"%name), action_result_msg)
00132 write_file(os.path.join(output_dir, "%sFeedback.msg"%name), feedback_msg)
00133 write_file(os.path.join(output_dir, "%sActionFeedback.msg"%name), action_feedback_msg)
00134
00135
00136 if __name__ == '__main__': main()