Skip to content

Commit

Permalink
Plugs clean up Connections properly if reconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSchweizer committed Sep 2, 2018
1 parent f68dfd7 commit f7d6a32
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
10 changes: 8 additions & 2 deletions flowpipe/plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ def value(self, value):
plug.value = value

def connect(self, plug):
"""Connect this Plug to the given Plug.
"""Connect this Plug to the given InputPlug.
Set both participating Plugs dirty.
"""
if plug.node is self.node:
raise Exception(
'Can\'t connect Plugs that are part of the same Node.')

for connection in plug.connections:
plug.disconnect(connection)

if plug not in self.connections:
self.connections.append(plug)
plug.value = self.value
Expand Down Expand Up @@ -172,14 +175,17 @@ def __unicode__(self):
return pretty

def connect(self, plug):
"""Connect this Plug to the given Plug.
"""Connect this Plug to the given OutputPlug.
Set both participating Plugs dirty.
"""
if plug.node is self.node:
raise Exception(
'Can\'t connect Plugs that are part of the same Node.')

for connection in self.connections:
self.disconnect(connection)

self.connections = [plug]
self.is_dirty = True
if self not in plug.connections:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def test_evaluate_sets_all_inputs_clean():
assert not node.is_dirty


def test_cannot_connect_node_to_it():
"""A node can not create a cycle by connecting to it."""
def test_cannot_connect_node_to_itself():
"""A node can not create a cycle by connecting to itself."""
node = SquareNode()
with pytest.raises(Exception):
node.outputs['out'] >> node.inputs['in1']
Expand Down
41 changes: 33 additions & 8 deletions tests/test_plugs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import print_function

from flowpipe.node import INode
from flowpipe.node import INode, Node
from flowpipe.plug import InputPlug, OutputPlug


Expand All @@ -10,12 +10,43 @@ def compute(self):
pass


def test_connecting_different_input_disconnects_existing_ones():

@Node(outputs=["a_out"])
def A(a):
pass

@Node(outputs=["b_out"])
def B(b):
pass

@Node(outputs=["c_out"])
def C(c):
pass

a = A()
b = B()
c = C()

a.outputs["a_out"].connect(b.inputs["b"])
c.outputs["c_out"].connect(b.inputs["b"])

assert not a.outputs["a_out"].connections

b.inputs["b"].connect(a.outputs["a_out"])

assert a.outputs["a_out"].connections

b.inputs["b"].connect(c.outputs["c_out"])

assert not a.outputs["a_out"].connections


def test_connect_and_dicsonnect_nodes():
"""Connect and disconnect nodes."""
n1 = NodeForTesting()
n2 = NodeForTesting()
out_plug_a = OutputPlug('out', n1)
out_plug_b = OutputPlug('out', n1)
in_plug_a = InputPlug('in', n2)
in_plug_b = InputPlug('in', n2)

Expand All @@ -39,12 +70,6 @@ def test_connect_and_dicsonnect_nodes():
assert 2 == len(out_plug_a.connections)
assert 1 == len(in_plug_b.connections)

# Connecting a different input disconnects the existing one
assert out_plug_a == in_plug_a.connections[0]
out_plug_b >> in_plug_a
print(in_plug_a.connections)
assert out_plug_b == in_plug_a.connections[0]


def test_change_connections_sets_plug_dirty():
"""Connecting and disconnecting sets the plug dirty."""
Expand Down

0 comments on commit f7d6a32

Please sign in to comment.