msgs.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2010, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
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         # if we're being run concurrently, the above test can report false but os.makedirs can still fail if
00067         # another copy just created the directory
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


rve_interface_gen
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:00