diff --git a/rtshell/rtcon.py b/rtshell/rtcon.py index c6320d6..701347e 100755 --- a/rtshell/rtcon.py +++ b/rtshell/rtcon.py @@ -85,6 +85,18 @@ def connect_ports(paths, options, tree=None): port_objs.append(port_obj) conn_name = options.name if options.name else None + + if options.no_duplicates: + for p in port_objs: + if p.get_connection_by_name(conn_name): + raise rts_exceptions.DuplicateConnectionNameError(conn_name, + p.name) + if options.id: + if p.get_connection_by_id(options.id): + raise rts_exceptions.DuplicateConnectionIDError(options.id) + if port_objs[0].get_connections_by_dests(port_objs[1:]): + raise rts_exceptions.DuplicateConnectionError(cmd_paths) + port_objs[0].connect(port_objs[1:], name=conn_name, id=options.id, props=options.properties) @@ -106,6 +118,8 @@ def property_callback(option, opt, option_value, parser): Connect two or more ports.''' version = rtshell.RTSH_VERSION parser = optparse.OptionParser(usage=usage, version=version) + parser.add_option('-d', '--no-duplicates', dest='no_dups', + action='store_true', help='Prevent duplicate connections') parser.add_option('-i', '--id', dest='id', action='store', type='string', default='', help='ID of the connection. [Default: %default]') parser.add_option('-n', '--name', dest='name', action='store', @@ -153,4 +167,3 @@ def property_callback(option, opt, option_value, parser): # vim: tw=79 - diff --git a/rtshell/rts_exceptions.py b/rtshell/rts_exceptions.py index d6fbf59..7eeb4c8 100644 --- a/rtshell/rts_exceptions.py +++ b/rtshell/rts_exceptions.py @@ -400,6 +400,37 @@ def __str__(self): self._id) +class DuplicateConnectionError(RtShellError): + '''An identical connection already exists.''' + def __init__(self, ports): + self._ports = ports + + def __str__(self): + return 'An identical connection already exists between ports {}'.format( + self._ports) + + +class DuplicateConnectionIDError(RtShellError): + '''A connection with that ID already exists.''' + def __init__(self, conn_id): + self._conn_id = conn_id + + def __str__(self): + return 'A connection with ID {} already exists between the ' \ + 'specified ports'.format(self._conn_id) + + +class DuplicateConnectionNameError(RtShellError): + '''A connection with that name already exists.''' + def __init__(self, conn_name, port_name): + self._conn_name = conn_name + self._port_name = port_name + + def __str__(self): + return 'A connection with name {} already exists from the ' \ + 'specified port {}'.format(self._conn_name, self._port_name) + + class BadPortTypeError(RtShellError): '''The port type is not defined.''' def __init__(self, rtc, port):