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
00032
00033
00034
00035 import roslib; roslib.load_manifest('rve_interface_gen')
00036 import roslib.msgs
00037 import roslib.packages
00038
00039 import rve_interface_gen
00040 from rve_interface_gen.InterfaceParser import InterfaceParser
00041 from rve_interface_gen.InterfaceLexer import InterfaceLexer
00042 from rve_interface_gen.antlr3 import ANTLRFileStream
00043 from rve_interface_gen.antlr3 import CommonTokenStream
00044
00045 from cStringIO import StringIO
00046
00047 import os
00048 import os.path
00049 import sys
00050
00051 def msg_prefix_from_interface_method(i, m):
00052 return '%s_%s'%(i.name, m.name)
00053
00054 def get_generated_messages(i):
00055 msgs = []
00056 for m in i.methods:
00057 request_message = '%sRequest'%(msg_prefix_from_interface_method(i, m))
00058 response_message = '%sResponse'%(msg_prefix_from_interface_method(i, m))
00059 msgs = msgs + [request_message, response_message]
00060
00061 return msgs
00062
00063 def write_message(i, members, pkg, pkg_path, name):
00064 output_dir = '%s/msg'%(pkg_path)
00065 if (not os.path.exists(output_dir)):
00066
00067
00068 try:
00069 os.makedirs(output_dir)
00070 except OSError, e:
00071 pass
00072
00073 f = open('%s/%s.msg'%(output_dir, name), 'w')
00074
00075 s = StringIO()
00076 for m in members:
00077 print >> s, '%s %s'%(m[0], m[1])
00078
00079 print >> f, s.getvalue()
00080 f.close()
00081
00082 def write_method_messages(i, m, pkg, pkg_path):
00083 request_message = '%sRequest'%(msg_prefix_from_interface_method(i, m))
00084 response_message = '%sResponse'%(msg_prefix_from_interface_method(i, m))
00085 write_message(i, m.fields, pkg, pkg_path, request_message)
00086 write_message(i, m.return_fields, pkg, pkg_path, response_message)
00087
00088 def generate_interface(i, pkg, pkg_path):
00089 for m in i.methods:
00090 write_method_messages(i, m, pkg, pkg_path)
00091
00092 def generate(interface_file, pkg, pkg_path):
00093 parser = load_from_file(interface_file)
00094 [generate_interface(i, pkg, pkg_path) for i in parser.interfaces]
00095
00096 def load_from_file(interface_file):
00097 char_stream = ANTLRFileStream(sys.argv[1])
00098 lexer = InterfaceLexer(char_stream)
00099 tokens = CommonTokenStream(lexer)
00100 parser = InterfaceParser(tokens);
00101 parser.main()
00102
00103 return parser