genpair.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
00005 #
00006 
00007 ##############################################################################
00008 # Imports
00009 ##############################################################################
00010 
00011 import sys
00012 try:
00013     from cStringIO import StringIO
00014 except ImportError:
00015     from io import StringIO
00016 import re
00017 import os, os.path
00018 import errno
00019 import argparse
00020 
00021 IODELIM   = '---'
00022 AUTOGEN="# ====== DO NOT MODIFY! AUTOGENERATED FROM A SERVICE PAIR DEFINITION ======\n\n"
00023 
00024 class PairSpecException(Exception): pass
00025 
00026 def parse_service_pair_spec(text, package_context = ''):
00027     pieces = [StringIO()]
00028     for l in text.split('\n'):
00029         if l.startswith(IODELIM):
00030             pieces.append(StringIO())
00031         else:
00032             pieces[-1].write(l + '\n')
00033     return [p.getvalue() for p in pieces]
00034 
00035 def write_file(filename, text):
00036     f = open(filename, 'w')
00037     f.write(text)
00038     f.close()
00039 
00040 def main():
00041 
00042     parser = argparse.ArgumentParser(description='Service pair generator.')
00043     parser.add_argument('filename', nargs=1, type=str, help="filename of the .pair service")
00044     parser.add_argument('-o', '--output-dir', action='store', default='.', help='output directory')
00045     args = parser.parse_args()
00046 
00047     # Try to make the directory, but silently continue if it already exists
00048     try:
00049         os.makedirs(args.output_dir)
00050     except OSError as e:
00051         if e.errno == errno.EEXIST:
00052             pass
00053         else:
00054             raise
00055 
00056     service_pair_file = args.filename[0]
00057     if not service_pair_file.endswith('.pair'):
00058         print("The file specified has the wrong extension. It must end in .pair")
00059     else:
00060         filename = service_pair_file
00061 
00062         f = open(filename)
00063         service_pair_spec = f.read()
00064         f.close()
00065 
00066         name = os.path.basename(filename)[:-7]
00067         (name, unused_sep, unused_ext) = os.path.basename(filename).partition('.')
00068         #print("Generating for pair %s" % name)
00069 
00070         pieces = parse_service_pair_spec(service_pair_spec)
00071         if len(pieces) != 2:
00072             raise ActionSpecException("%s: wrong number of pieces, %d"%(filename,len(pieces)))
00073         service_request, service_response = pieces
00074         service_pair_msg = AUTOGEN + name + 'PairRequest pair_request\n' + name + 'PairResponse pair_response\n' 
00075         service_request_msg = AUTOGEN + service_request
00076         service_pair_request_msg = AUTOGEN + 'uuid_msgs/UniqueID id\n' + name + 'Request request\n'
00077         service_response_msg = AUTOGEN + service_response
00078         service_pair_response_msg = AUTOGEN + 'uuid_msgs/UniqueID id\n' + name + 'Response response\n'
00079 
00080         write_file(os.path.join(args.output_dir, "%sPair.msg"%name), service_pair_msg)
00081         write_file(os.path.join(args.output_dir, "%sRequest.msg"%name), service_request_msg)
00082         write_file(os.path.join(args.output_dir, "%sPairRequest.msg"%name), service_pair_request_msg)
00083         write_file(os.path.join(args.output_dir, "%sResponse.msg"%name), service_response_msg)
00084         write_file(os.path.join(args.output_dir, "%sPairResponse.msg"%name), service_pair_response_msg)
00085 
00086 
00087 if __name__ == '__main__': main()


rocon_service_pair_msgs
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 20:10:51