rtdis.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 disconnect ports.
00018 
00019 '''
00020 
00021 
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.tree
00026 import rtctree.path
00027 import sys
00028 import traceback
00029 
00030 import path
00031 import rts_exceptions
00032 import rtshell
00033 
00034 
00035 def disconnect_all(cmd_path, full_path, options, tree=None):
00036     path, port = rtctree.path.parse_path(full_path)
00037     if not path[-1]:
00038         raise rts_exceptions.NoSuchObjectError(cmd_path)
00039     if not tree:
00040         tree = rtctree.tree.RTCTree(paths=path, filter=[path])
00041 
00042     object = tree.get_node(path)
00043     if not object:
00044         raise rts_exceptions.NoSuchObjectError(cmd_path)
00045     if not object.is_component:
00046         raise rts_exceptions.NotAComponentError(cmd_path)
00047 
00048     if port:
00049         # Disconnect a single port
00050         port_obj = object.get_port_by_name(port)
00051         if not port_obj:
00052             raise rts_exceptions.PortNotFoundError(path, port)
00053         if options.id:
00054             conn = port_obj.get_connection_by_id(options.id)
00055             if not conn:
00056                 raise rts_exceptions.ConnectionIDNotFoundError(options.id,
00057                         cmd_path)
00058             conn.disconnect()
00059         else:
00060             port_obj.disconnect_all()
00061     else:
00062         if options.id:
00063             # Hunt through the ports for the connection ID
00064             for p in object.ports:
00065                 conn = p.get_connection_by_id(options.id)
00066                 if not conn:
00067                     continue
00068                 conn.disconnect()
00069                 return
00070             raise rts_exceptions.ConnectionIDNotFoundError(options.id,
00071                     cmd_path)
00072         else:
00073             # Disconnect all ports
00074             object.disconnect_all()
00075 
00076 
00077 def disconnect_ports(paths, options, tree=None):
00078     cmd_paths, fps = zip(*paths)
00079     pathports = [rtctree.path.parse_path(fp) for fp in fps]
00080     for ii, p in enumerate(pathports):
00081         if not p[1]:
00082             raise rts_exceptions.NotAPortError(cmd_paths[ii])
00083         if not p[0][-1]:
00084             raise rts_exceptions.NotAPortError(cmd_paths[ii])
00085     paths, ports = zip(*pathports)
00086 
00087     if not tree:
00088         tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
00089 
00090     port_objs = []
00091     for ii, p in enumerate(pathports):
00092         obj = tree.get_node(p[0])
00093         if not obj:
00094             raise rts_exceptions.NoSuchObjectError(cmd_paths[ii])
00095         if obj.is_zombie:
00096             raise rts_exceptions.ZombieObjectError(cmd_paths[ii])
00097         if not obj.is_component:
00098             raise rts_exceptions.NotAComponentError(cmd_paths[ii])
00099         port_obj = obj.get_port_by_name(p[1])
00100         if not port_obj:
00101             raise rts_exceptions.PortNotFoundError(p[0], p[1])
00102         port_objs.append(port_obj)
00103     if len(port_objs) < 2:
00104         raise rts_exceptions.NoDestPortError
00105 
00106     if options.id:
00107         all_conns = port_objs[0].get_connections_by_dests(port_objs[1:])
00108         conns = []
00109         for c in all_conns:
00110             if c.id == options.id:
00111                 conns.append(c)
00112     else:
00113         conns = port_objs[0].get_connections_by_dests(port_objs[1:])
00114 
00115     if not conns:
00116         raise rts_exceptions.MultiConnectionNotFoundError
00117     for c in conns:
00118         c.disconnect()
00119 
00120 
00121 def main(argv=None, tree=None):
00122     usage = '''Usage: %prog [options] <source path> [destination path]
00123 Remove connections.'''
00124     version = rtshell.RTSH_VERSION
00125     parser = optparse.OptionParser(usage=usage, version=version)
00126     parser.add_option('-i', '--id', dest='id', action='store', type='string',
00127             default='', help='ID of the connection.')
00128     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00129             default=False,
00130             help='Output verbose information. [Default: %default]')
00131 
00132     if argv:
00133         sys.argv = [sys.argv[0]] + argv
00134     try:
00135         options, args = parser.parse_args()
00136     except optparse.OptionError, e:
00137         print >>sys.stderr, 'OptionError:', e
00138         return 1
00139 
00140     try:
00141         if len(args) == 1:
00142             # Disconnect all
00143             cmd_path = args[0]
00144             disconnect_all(cmd_path, path.cmd_path_to_full_path(cmd_path),
00145                                   options, tree)
00146         elif len(args) > 1:
00147             # Disconnect a set of ports
00148             paths = [(p, path.cmd_path_to_full_path(p)) for p in args]
00149             disconnect_ports(paths, options, tree)
00150         else:
00151             print >>sys.stderr, usage
00152             return 1
00153     except Exception, e:
00154         if options.verbose:
00155             traceback.print_exc()
00156         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00157         return 1
00158     return 0
00159 
00160 
00161 
00162 # vim: tw=79
00163 


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