rtcon.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Geoffrey Biggs
00009     RT-Synthesis Research Group
00010     Intelligent Systems Research Institute,
00011     National Institute of Advanced Industrial Science and Technology (AIST),
00012     Japan
00013     All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016 
00017 Implementation of the command to connect two ports.
00018 
00019 '''
00020 
00021 
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.exceptions
00026 import rtctree.tree
00027 import rtctree.path
00028 import sys
00029 import traceback
00030 
00031 import path
00032 import rts_exceptions
00033 import rtshell
00034 
00035 
00036 def connect_ports(paths, options, tree=None):
00037     cmd_paths, fps = zip(*paths)
00038     pathports = [rtctree.path.parse_path(fp) for fp in fps]
00039     for ii, p in enumerate(pathports):
00040         if not p[1]:
00041             raise rts_exceptions.NotAPortError(cmd_paths[ii])
00042         if not p[0][-1]:
00043             raise rts_exceptions.NotAPortError(cmd_paths[ii])
00044     paths, ports = zip(*pathports)
00045 
00046     if not tree:
00047         tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
00048 
00049     port_objs = []
00050     for ii, p in enumerate(pathports):
00051         obj = tree.get_node(p[0])
00052         if not obj:
00053             raise rts_exceptions.NoSuchObjectError(cmd_paths[ii])
00054         if obj.is_zombie:
00055             raise rts_exceptions.ZombieObjectError(cmd_paths[ii])
00056         if not obj.is_component:
00057             raise rts_exceptions.NotAComponentError(cmd_paths[ii])
00058         port_obj = obj.get_port_by_name(p[1])
00059         if not port_obj:
00060             raise rts_exceptions.PortNotFoundError(p[0], p[1])
00061         port_objs.append(port_obj)
00062 
00063     conn_name = options.name if options.name else None
00064     port_objs[0].connect(port_objs[1:], name=conn_name, id=options.id,
00065             props=options.properties)
00066 
00067 
00068 def main(argv=None, tree=None):
00069     def property_callback(option, opt, option_value, parser):
00070         if option_value.count('=') != 1:
00071             raise optparse.OptionValueError('Bad property format: {0}'.format(
00072                 option_value))
00073         key, equals, value = option_value.partition('=')
00074         if not getattr(parser.values, option.dest):
00075             setattr(parser.values, option.dest, {})
00076         if key in getattr(parser.values, option.dest):
00077             print >>sys.stderr, '{0}: Warning: duplicate property: {1}'.format(
00078                     sys.argv[0], option_value)
00079         getattr(parser.values, option.dest)[key] = value
00080 
00081     usage = '''Usage: %prog [options] <path 1> <path 2> [<path 3> ...]
00082 Connect two or more ports.'''
00083     version = rtshell.RTSH_VERSION
00084     parser = optparse.OptionParser(usage=usage, version=version)
00085     parser.add_option('-i', '--id', dest='id', action='store', type='string',
00086             default='', help='ID of the connection. [Default: %default]')
00087     parser.add_option('-n', '--name', dest='name', action='store',
00088             type='string', default=None,
00089             help='Name of the connection. [Default: %default]')
00090     parser.add_option('-p', '--property', dest='properties', action='callback',
00091             callback=property_callback, type='string',
00092             help='Connection properties.')
00093     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00094             default=False,
00095             help='Output verbose information. [Default: %default]')
00096 
00097     if argv:
00098         sys.argv = [sys.argv[0]] + argv
00099     try:
00100         options, args = parser.parse_args()
00101     except optparse.OptionError, e:
00102         print >>sys.stderr, 'OptionError:', e
00103         return 1
00104 
00105     if not getattr(options, 'properties'):
00106         setattr(options, 'properties', {})
00107 
00108     if not args:
00109         # If no paths given then can't do anything.
00110         print >>sys.stderr, '{0}: No ports specified.'.format(
00111                 os.path.basename(sys.argv[0]))
00112         return 1
00113     paths = [(p, path.cmd_path_to_full_path(p)) for p in args]
00114 
00115     try:
00116         connect_ports(paths, options, tree=tree)
00117     except Exception, e:
00118         if options.verbose:
00119             traceback.print_exc()
00120         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00121         return 1
00122     return 0
00123 
00124 
00125 # vim: tw=79
00126 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12