Skip to content

Commit

Permalink
Option to prevent duplicate connections
Browse files Browse the repository at this point in the history
  • Loading branch information
gbiggs committed Jun 20, 2017
1 parent d8158cd commit 9e4e7ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion rtshell/rtcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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',
Expand Down Expand Up @@ -153,4 +167,3 @@ def property_callback(option, opt, option_value, parser):


# vim: tw=79

31 changes: 31 additions & 0 deletions rtshell/rts_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 9e4e7ef

Please sign in to comment.