Example #1
0
    def __init__(self,
                 port: 'Port' = None,
                 allowOptional: bool = True) -> 'PortEditorWidget':
        """
		Creates a port editor widget.
		
		:param port: The port to edit.
		:type port: Port
		:param allowOptional: if True, the optional checkbox will be shown.
		:type allowOptional: bool
		"""
        QWidget.__init__(self)
        self.ui = Ui_PortEditorWidget()
        self.ui.setupUi(self)

        if port is not None:
            self._port = port
        else:
            self._port = Port()
        self.updateEditor()

        if allowOptional:
            self.ui.optionalButton.clicked.connect(
                self.onOptionalButtonClicked)

        self.updateDefaultEditorState()
Example #2
0
	def test_ActionWrapper(self):
		# make an action pipeline
		ap = ActionPipeline()
		p1 = Port()
		p2 = Port()
		ap.addInputPort(p1)
		ap.addOutputPort(p2)
		
		# make a wrapper for it
		parent = ActionPipeline()
		w = ActionWrapper(ap, parent)
		p1Mirror, = w.getInputPorts()
		p2Mirror, = w.getOutputPorts()
		
		self.assertTrue(p1Mirror is not p1)
		self.assertTrue(p2Mirror is not p2)
		self.assertTrue(type(p1Mirror) == Port)
		self.assertTrue(type(p2Mirror) == Port)
		self.assertTrue(p1Mirror.isOptional() == p1.isOptional())
		self.assertTrue(p1Mirror.getDataType() == p1.getDataType())
		self.assertTrue(p2Mirror.isOptional() == p2.isOptional())
		self.assertTrue(p2Mirror.getDataType() == p2.getDataType())
		
		# Add another port to the action pipeline and make sure the wrapper is updated
		ap.addInputPort(Port())
		self.assertTrue(len(ap.getInputPorts()) == len(w.getInputPorts()))
		
		ap.removePort(p1)
		self.assertTrue(len(ap.getInputPorts()) == len(w.getInputPorts()))
		
		ap.removePort(p2)
		self.assertTrue(len(ap.getInputPorts()) == len(w.getInputPorts()))
		
		self.assertRaises(ActionException, lambda: ActionPipeline().registerWrapper(w))
		self.assertRaises(ActionException, lambda: ActionPipeline().unRegisterWrapper(w))
		self.assertRaises(ActionException, lambda: ap.unRegisterWrapper(w))
		
		w.forgetActionReference()
		ap.unRegisterWrapper(w)
		
		self.assertRaises(ActionException, lambda: ActionPipeline().addAction(w))
		self.assertRaises(ActionException, lambda: parent.addAction(w))
		
Example #3
0
class PortEditorWidget(QWidget):
    """
	The PortEditorWidget is used to edit a single port object.
	"""
    def __init__(self,
                 port: 'Port' = None,
                 allowOptional: bool = True) -> 'PortEditorWidget':
        """
		Creates a port editor widget.
		
		:param port: The port to edit.
		:type port: Port
		:param allowOptional: if True, the optional checkbox will be shown.
		:type allowOptional: bool
		"""
        QWidget.__init__(self)
        self.ui = Ui_PortEditorWidget()
        self.ui.setupUi(self)

        if port is not None:
            self._port = port
        else:
            self._port = Port()
        self.updateEditor()

        if allowOptional:
            self.ui.optionalButton.clicked.connect(
                self.onOptionalButtonClicked)

        self.updateDefaultEditorState()

    def updateDefaultEditorState(self) -> None:
        """
		If the port is required, set the default editor text to "None" and disable.
		Else, enable it and set it to the port's default value.
		
		:return: None
		:rtype: NoneType
		"""
        if self.ui.optionalButton.text() == "Optional":
            if self._port._default:
                self.ui.defaultEdit.setText(str(self._port._default))
            else:
                self.ui.defaultEdit.setText("")
                self.ui.defaultEdit.setPlaceholderText("Default Value")
            self.ui.defaultEdit.setEnabled(True)
        else:
            self.ui.defaultEdit.setText('None')
            self.ui.defaultEdit.setEnabled(False)

    def onOptionalButtonClicked(self) -> None:
        """
		Switch the text on the button between "Required" and "Optional"
		
		:return: None
		:rtype: NoneType
		"""
        if self.ui.optionalButton.text() == "Required":
            self.ui.optionalButton.setText("Optional")
        else:
            self.ui.optionalButton.setText("Required")
        self.updateDefaultEditorState()

    def getPort(self) -> 'Port':
        """
		Get's the port object associated with this port editor.
		
		:return: The Port being edited.
		:rtype:
		"""
        return self._port

    def updateEditor(self) -> None:
        """
		Updates the editor widget to reflect the port values
		
		:return: None
		:rtype: NoneType
		"""
        self.ui.nameEdit.setText(self._port.getName())
        self.ui.typeEdit.setText(str(self._port.getDataType().__name__))

        if self._port.isOptional():
            self.ui.optionalButton.setText("Optional")
            self.ui.defaultEdit.setText(str(self._port.getDefaultValue()))
        else:
            self.ui.optionalButton.setText("Required")
            self.ui.defaultEdit.setText("None")

    def updatePort(self) -> None:
        """
		Sets all the values of the port values to the values of the editors
		
		.. note:: This function will update the underlying port. Don't call it unless you're
			absolutely certain that the values in the editors are good.
		
		:return: None
		:rtype: NoneType
		"""
        p = self.previewPort()
        self._port.mirror(p)

    def previewPort(self) -> 'Port':
        """
		Returns a new port with all values from editors set.
		
		:return: The new port object
		:rtype: Port
		"""
        p = Port()
        p.setName(self.ui.nameEdit.text())

        if self.ui.optionalButton.text() == 'Optional':
            p.setOptional(True)
        else:
            p.setOptional(False)

        try:
            dataType = eval(self.ui.typeEdit.text())
            assert (type(dataType) == type)
        except:
            # TODO: Figure out what to do in the case that we didn't recognize it as a type.
            pass
        else:
            p.setDataType(dataType)

        if p.isOptional():
            p.setDefaultValue(self.ui.defaultEdit.text())

        return p
Example #4
0
    def previewPort(self) -> 'Port':
        """
		Returns a new port with all values from editors set.
		
		:return: The new port object
		:rtype: Port
		"""
        p = Port()
        p.setName(self.ui.nameEdit.text())

        if self.ui.optionalButton.text() == 'Optional':
            p.setOptional(True)
        else:
            p.setOptional(False)

        try:
            dataType = eval(self.ui.typeEdit.text())
            assert (type(dataType) == type)
        except:
            # TODO: Figure out what to do in the case that we didn't recognize it as a type.
            pass
        else:
            p.setDataType(dataType)

        if p.isOptional():
            p.setDefaultValue(self.ui.defaultEdit.text())

        return p
Example #5
0
                    return

        spread(-self._height / 2, self._inputPortGraphics)
        spread(self._height / 2, self._outputPortGraphics)

    def getHeight(self):
        self.updateActionRect()
        return self._height

    def getWidth(self):
        self.updateActionRect()
        return self._width


if __name__ == "__main__":
    app = QApplication()
    v = QGraphicsView()
    s = QGraphicsScene()
    v.setScene(s)
    action = ComponentAction()
    p1 = Port()
    p2 = Port()
    p3 = Port()
    action.addInputPort(p1)
    action.addInputPort(p2)
    action.addOutputPort(p3)

    A = ActionGraphics(action)
    s.addItem(A)
    v.show()
    sys.exit(app.exec_())
Example #6
0
	def test_Port(self):
		myWireSet = WireSet()
		portA = Port(dataType=int)
		portB = Port()
		portC = Port()
		
		# Make some wires.
		myWireSet.addWire(portA, portB)
		myWireSet.addWire(portA, portC)
		wires = myWireSet.getWires()
		
		AtoB = wires[0]
		portA.addOutputWire(AtoB)
		
		self.assertRaises(TypeError, lambda: portB.setDataType("not a type"))
		
		portA.setOptional(False)
		self.assertRaises(PortException, lambda: portA.setDefaultValue(1))
		portA.setOptional(True)
		portA.setDefaultValue(1)
Example #7
0
	def test_ActionPipeline(self):
		
		# create top-level action pipeline
		readAccountBalance = ActionPipeline()
		self.assertTrue(readAccountBalance.getActions() == [])
		self.assertTrue(readAccountBalance.getInputPorts() == [])
		self.assertTrue(readAccountBalance.getOutputPorts() == [])
		
		# create ports and add them to the top-level action pipeline
		username = Port()
		username.setOptional(False)
		username.setDataType(str)
		self.assertFalse(username.isOptional())
		self.assertTrue(username.getDataType() == str)
		pwd = Port()
		pwd.setOptional(False)
		pwd.setDataType(str)
		self.assertFalse(pwd.isOptional())
		self.assertTrue(pwd.getDataType() == str)
		accountBalance = Port()
		readAccountBalance.addInputPort(username)
		readAccountBalance.addInputPort(pwd)
		readAccountBalance.addOutputPort(accountBalance)
		self.assertTrue(readAccountBalance.getActions() == [])
		self.assertTrue(readAccountBalance.getInputPorts() == [username, pwd])
		self.assertTrue(readAccountBalance.getOutputPorts() == [accountBalance])
		
		# create another action pipeline that won't be a child of the top-level
		temp = ActionPipeline()
		tempPort = Port()
		temp.addInputPort(tempPort)
		
		# Create a sub-action pipeline with ports
		login = ActionPipeline()
		uname = Port()
		password = Port()
		login.addInputPort(uname)
		login.addInputPort(password)
		self.assertTrue(login.getActions() == [])
		self.assertTrue(login.getInputPorts() == [uname, password])
		self.assertTrue(login.getOutputPorts() == [])
		
		# Create a wrapper for the sub-action pipeline
		loginWrapper = ActionWrapper(login, readAccountBalance)
		w_uname, w_password = loginWrapper.getInputPorts()
		
		self.assertTrue(type(w_uname) == Port)
		self.assertTrue(type(w_password) == Port)
		self.assertTrue(w_uname.isOptional() == uname.isOptional())
		self.assertTrue(w_uname.getDataType() == uname.getDataType())
		self.assertTrue(w_password.isOptional() == password.isOptional())
		self.assertTrue(w_password.getDataType() == password.getDataType())
		
		uname = w_uname
		password = w_password
		
		# Add sub-action pipeline to top-level action pipeline.
		#readAccountBalance.addAction(loginWrapper)
		self.assertTrue(readAccountBalance.getActions() == [loginWrapper])
		
		# Make sure we can't add a non-wrapper action to an action pipeline
		self.assertRaises(ActionException, lambda: readAccountBalance.addAction(login))
		
		# make sure we can't add the uname port again
		self.assertRaises(PortException, lambda: login.addInputPort(uname))
		self.assertRaises(PortException, lambda: login.addOutputPort(uname))
		self.assertRaises(PortException, lambda: readAccountBalance.addInputPort(uname))
		self.assertRaises(PortException, lambda: readAccountBalance.addOutputPort(uname))
		self.assertRaises(PortException, lambda: ActionPipeline().addInputPort(uname))
		self.assertRaises(PortException, lambda: ActionPipeline().addOutputPort(uname))
		
		# connect wires
		readAccountBalance.connect(username, uname)
		readAccountBalance.connect(pwd, password)
		
		# make sure wires exist
		self.assertTrue(readAccountBalance.getWireSet().containsWire(username, uname))
		self.assertTrue(readAccountBalance.getWireSet().containsWire(pwd, password))
		
		# make sure that you can't connect the wires again.
		self.assertRaises(PortException, lambda: readAccountBalance.connect(username, uname))
		self.assertRaises(PortException, lambda: readAccountBalance.connect(pwd, uname))
		
		# disconnect the username wire
		readAccountBalance.disconnect(username, uname)
		self.assertRaises(WireException, lambda: readAccountBalance.disconnect(username, uname))
		
		# remove the ports from the top-level action, add them to the sub-action, then move them
		# back.
		readAccountBalance.removePort(username)
		readAccountBalance.removePort(accountBalance)
		self.assertTrue(readAccountBalance.getInputPorts() == [pwd])
		self.assertTrue(readAccountBalance.getOutputPorts() == [])
		self.assertTrue(username.getAction() is None)
		self.assertTrue(accountBalance.getAction() is None)
		self.assertRaises(PortException, lambda: readAccountBalance.removePort(username))
		self.assertRaises(PortException, lambda: readAccountBalance.removePort(accountBalance))
		self.assertRaises(PortException, lambda: readAccountBalance.removePort(Port()))
		self.assertRaises(PortException, lambda: readAccountBalance.removePort(tempPort))
		self.assertFalse(readAccountBalance.getWireSet().containsWire(username, uname))
		login.addInputPort(username)
		login.addOutputPort(accountBalance)
		login.removePort(username)
		login.removePort(accountBalance)
		readAccountBalance.addInputPort(username)
		readAccountBalance.addOutputPort(accountBalance)
		readAccountBalance.connect(username, uname)
		self.assertTrue(readAccountBalance.getWireSet().containsWire(username, uname))
		self.assertTrue(readAccountBalance.getWireSet().containsWire(pwd, password))
		
		# change sequence of actions
		readAccountBalance.changeSequence([loginWrapper])
		self.assertRaises(ActionException, lambda: readAccountBalance.changeSequence([loginWrapper,ActionPipeline]))
		self.assertRaises(ActionException, lambda: readAccountBalance.changeSequence([loginWrapper,loginWrapper]))
		self.assertRaises(ActionException, lambda: readAccountBalance.changeSequence([]))
		
		# Make sure we can't connect/disconnect ports that we don't have access to.
		self.assertRaises(PortException, lambda: readAccountBalance.connect(username, Port()))
		self.assertRaises(PortException, lambda: readAccountBalance.connect(Port(), username))
		self.assertRaises(PortException, lambda: readAccountBalance.disconnect(username, Port()))
		self.assertRaises(PortException, lambda: readAccountBalance.disconnect(Port(), username))
		
		# make sure we can't connect the username ports backwards
		self.assertRaises(WireException, lambda: readAccountBalance.connect(uname, username))
	
		# action deletion
		self.assertRaises(ActionException, lambda: readAccountBalance.removeAction(login))
		readAccountBalance.removeAction(loginWrapper)
		self.assertFalse(readAccountBalance.getWireSet().containsWire(username, uname))
		self.assertFalse(readAccountBalance.getWireSet().containsWire(pwd, password))
		
		# Add a component action to read from the account balance field

		# TODO: Update ComponentAction constructor call
		#readField = ComponentAction(None, "This is a bullshit component action")
		readField = ActionPipeline()
		balanceStr = Port()
		readField.addOutputPort(balanceStr)
		
		readFieldWrapper = ActionWrapper(readField, readAccountBalance)
		balanceStr2, = readFieldWrapper.getOutputPorts()
		
		#readAccountBalance.addAction(readFieldWrapper)
		readAccountBalance.connect(balanceStr2, accountBalance)
		self.assertRaises(PortException, lambda: readAccountBalance.connect(balanceStr, accountBalance))
Example #8
0
	def test_WireSet(self):
		myWireSet = WireSet()
		portA = Port(dataType=int)
		portB = Port()
		portC = Port()
		
		# Make some wires.
		myWireSet.addWire(portA, portB)
		myWireSet.addWire(portA, portC)
		wires1 = myWireSet.getWires()
		
		self.assertTrue(portA.getInputWire() is None)
		self.assertTrue(portC.getOutputWires() == [])
		self.assertTrue(portA.getOutputWires()[0].getDestPort() == portB)
		self.assertTrue(portA.getOutputWires()[1].getDestPort() == portC)
		
		# make sure that duplicates can't be added
		myWireSet.addWire(portA, portB)
		wires2 = myWireSet.getWires()
		self.assertTrue(wires1 == wires2)
		
		# delete port A and make sure all wires are deleted.
		myWireSet.deleteWiresConnectedToPort(portA)
		self.assertTrue(myWireSet.getWires() == [])
		self.assertTrue(portA.getInputWire() is None)
		self.assertTrue(portB.getInputWire() is None)
		self.assertTrue(portC.getInputWire() is None)
		self.assertTrue(portA.getOutputWires() == [])
		self.assertTrue(portB.getOutputWires() == [])
		self.assertTrue(portC.getOutputWires() == [])
		
		# make sure we can't force two wires into one port
		myWireSet.addWire(portA, portB)
		self.assertRaises(PortException, lambda: myWireSet.addWire(portC, portB))
		self.assertTrue(myWireSet.containsWire(portA, portB))
		self.assertFalse(myWireSet.containsWire(portB, portC))
		
		# chain ports together and delete the middle one.
		myWireSet.addWire(portA, portB)
		myWireSet.addWire(portB, portC)
		myWireSet.deleteWiresConnectedToPort(portB)
		self.assertTrue(myWireSet.getWires() == [])
		self.assertTrue(portA.getInputWire() is None)
		self.assertTrue(portB.getInputWire() is None)
		self.assertTrue(portC.getInputWire() is None)
		self.assertTrue(portA.getOutputWires() == [])
		self.assertTrue(portB.getOutputWires() == [])
		self.assertTrue(portC.getOutputWires() == [])
Example #9
0
    def setLabelText(self, text: str) -> None:
        """
		Setting the label for the menu's description.
		
		:param text: Text that will be in the label.
		:type text: str
		:return: None
		:rtype: Nonetype
		"""
        self.ui.menuLabel.setText(text)


if __name__ == "__main__":
    app = QApplication()
    w = ActionMenu()
    p1 = Port()
    p2 = Port()
    p3 = Port()
    p4 = Port()
    p5 = Port()
    ap = ActionPipeline()
    ap.addInputPort(p1)
    ap.addInputPort(p2)
    ap.addOutputPort(p3)
    ap.addOutputPort(p4)
    ap.addOutputPort(p5)

    for i in range(10):
        ap.getProperties().getProperty("Name")[1].setValue("Action Pipeline" +
                                                           str(i))
        w.addAction(ap)
Example #10
0
        self.menu.prerequest()
        self.menu.exec_(event.screenPos())


if __name__ == "__main__":
    app = QApplication()
    v = QGraphicsView()
    v.setGeometry(500, 500, 500, 500)
    s = QGraphicsScene()
    v.setScene(s)

    # Create top-level actionPipeline.
    actPipeline = ActionPipeline()

    # Add some ports to it.
    inPort1 = Port()
    inPort2 = Port()
    outPort1 = Port()
    actPipeline.addInputPort(inPort1)
    actPipeline.addInputPort(inPort2)
    actPipeline.addOutputPort(outPort1)

    #Sub-Actions
    act1 = ActionPipeline()
    act1.setName("action pipeline 1")
    act2 = ActionPipeline()
    act2.setName("action pipeline 2")

    prt1_1 = Port()
    prt2_1 = Port()
    prt3_1 = Port()