def __init__(self, environment):
     super().__init__(True, False, environment)
     self.outPort = OutputPort(False, "outPort", environment)
     self.addStaticPort(self.outPort)
     self.dynInPorts = None
     self.sleep_time = self.propertyCollection().defineProperty(
         "sleep_time", 0.0, "sleep time to simulate computational load [s]")
Exemple #2
0
def test_dynamic_out_filter():
    with FilterEnvironment(
            "pyfile://" + os.path.dirname(__file__) +
            "/../interface/SimpleDynamicFilter.py", "SimpleDynOutFilter",
            PropertyCollectionImpl("root", None)) as dynOutPyFilter:
        f = dynOutPyFilter.getPlugin()

        dip = InputPort(True, "dynInPort", dynOutPyFilter)
        expect_exception(dynOutPyFilter.addPort, dip)
        dop = OutputPort(True, "dynOutPort", dynOutPyFilter)
        dynOutPyFilter.addPort(dop)
        expect_exception(dynOutPyFilter.addPort, dop)

        assert dynOutPyFilter.getDynamicInputPorts() == []
        if useCImpl:
            assert [p.data() for p in dynOutPyFilter.getDynamicOutputPorts()
                    ] == [dop.data()]
        else:
            assert dynOutPyFilter.getDynamicOutputPorts() == [dop]
        sip = dynOutPyFilter.getStaticInputPorts()
        assert len(sip) == 1
        assert sip[0].name() == "inPort"
        assert sip[0] is dynOutPyFilter.getInputPort("inPort")
        sop = dynOutPyFilter.getStaticOutputPorts()
        assert len(sop) == 1
        assert sop[0].name() == "outPort"
        assert sop[0] is dynOutPyFilter.getOutputPort("outPort")
        assert dynOutPyFilter.state() == FilterState.CONSTRUCTED

        dynOutPyFilter.init()
        dop2 = OutputPort(True, "dynOutPort2", dynOutPyFilter)
        expect_exception(dynOutPyFilter.addPort, dop2)

        dynOutPyFilter.open()
        expect_exception(dynOutPyFilter.addPort, dop2)
 def __init__(self, environment):
     Filter.__init__(self, False, False, environment)
     self.inPort = InputPort(False, "inPort", environment)
     self.addStaticPort(self.inPort)
     self.outPort = OutputPort(False, "outPort", environment)
     self.addStaticPort(self.outPort)
     self.sleep_time = self.propertyCollection().defineProperty(
         "sleep_time",
         0.0,
         "sleep time to simulate computational load [s]",
         options=dict(min=0.0, max=1.0))
     self.log_rcv = self.propertyCollection().defineProperty(
         "log_rcv", True, "whether or not to log receive events")
     self.log_prefix = self.propertyCollection().defineProperty(
         "log_prefix", "", "a prefix for log messages")
     self.propertyCollection().defineProperty(
         "an_int_property",
         4223,
         "to have coverage for the integer props",
         options=dict(min=1234, max=5000))
     self.propertyCollection().defineProperty(
         "an_enum_property",
         "e1",
         "to have coverage for the integer props",
         options=dict(enum=["e1", "e2"]))
Exemple #4
0
 def __init__(self, environment):
     super().__init__(False, False, environment)
     self.video_out = OutputPort(False, "video", environment)
     self.audio_out = OutputPort(False, "audio", environment)
     self.addStaticPort(self.video_out)
     self.addStaticPort(self.audio_out)
     self.filename = self.propertyCollection().defineProperty("filename", "", "avi file name")
 def __init__(self, environment):
     Filter.__init__(self, False, False, environment)
     self.outPort = OutputPort(False, "outPort", environment)
     self.addStaticPort(self.outPort)
     self.timeout_ms = int(1000 / self.propertyCollection().defineProperty(
         "frequency", 1.0, "frequency of data generation [Hz]"))
     self.log_tr = self.propertyCollection().defineProperty(
         "log_tr", True, "whether or not to log transmit events")
class SimpleStaticFilter(Filter):
    def __init__(self, environment):
        Filter.__init__(self, False, False, environment)
        self.inPort = InputPort(False, "inPort", environment)
        self.addStaticPort(self.inPort)
        self.outPort = OutputPort(False, "outPort", environment)
        self.addStaticPort(self.outPort)
        self.sleep_time = self.propertyCollection().defineProperty(
            "sleep_time",
            0.0,
            "sleep time to simulate computational load [s]",
            options=dict(min=0.0, max=1.0))
        self.log_rcv = self.propertyCollection().defineProperty(
            "log_rcv", True, "whether or not to log receive events")
        self.log_prefix = self.propertyCollection().defineProperty(
            "log_prefix", "", "a prefix for log messages")
        self.propertyCollection().defineProperty(
            "an_int_property",
            4223,
            "to have coverage for the integer props",
            options=dict(min=1234, max=5000))
        self.propertyCollection().defineProperty(
            "an_enum_property",
            "e1",
            "to have coverage for the integer props",
            options=dict(enum=["e1", "e2"]))

    def onStart(self):
        self.log_rcv = self.propertyCollection().getProperty("log_rcv")
        self.log_prefix = self.propertyCollection().getProperty("log_prefix")
        self.propertyCollection().getProperty("an_int_property")
        self.propertyCollection().getProperty("an_enum_property")

    def onPortDataChanged(self, inputPort):
        dataSample = inputPort.getData()
        self.afterReceive(dataSample)
        if dataSample.getDatatype() == "text/utf8" and self.log_rcv:
            logging.getLogger(__name__).info(
                "%sreceived: %s", self.log_prefix,
                dataSample.getContent().data().decode("utf8"))
        newSample = DataSample.copy(dataSample)
        time.sleep(self.sleep_time)
        self.beforeTransmit(dataSample)
        self.outPort.transmit(dataSample)
        self.afterTransmit()

    # used by tests
    def afterReceive(self, dataSample):
        pass

    def beforeTransmit(self, dataSample):
        pass

    def afterTransmit(self):
        pass
class SimpleSource(Filter):
    def __init__(self, environment):
        Filter.__init__(self, False, False, environment)
        self.outPort = OutputPort(False, "outPort", environment)
        self.addStaticPort(self.outPort)
        self.timeout_ms = int(1000 / self.propertyCollection().defineProperty(
            "frequency", 1.0, "frequency of data generation [Hz]"))
        self.log_tr = self.propertyCollection().defineProperty(
            "log_tr", True, "whether or not to log transmit events")

    def onStart(self):
        self.timer = QTimer()
        self.timer.timeout.connect(self.newDataEvent)
        # prevent different behaviour between linux and windows (the timer will fire 10 times as fast as needed
        # and the events are filtered in newDataEvent)
        self.timer.start(self.timeout_ms / 10)
        self.counter = 0
        self.lastSendTime = None
        self.log_tr = self.propertyCollection().getProperty("log_tr")

    def newDataEvent(self):
        t = time.monotonic()
        if self.lastSendTime is not None:
            if t - self.lastSendTime < self.timeout_ms * 1e-3:
                # we are still earlier than the requested framerate
                return
        self.lastSendTime = t
        self.counter += 1
        c = "Sample %d" % self.counter
        s = DataSample(c.encode("utf8"), "text/utf8",
                       int(time.time() / DataSample.TIMESTAMP_RES))
        if self.log_tr:
            logging.getLogger(__name__).info("transmit: %s", c)
        self.beforeTransmit(s)
        self.outPort.transmit(s)
        self.afterTransmit()

    def onStop(self):
        self.timer.stop()
        del self.timer

    def onPortDataChanged(self, inputPort):
        dataSample = inputPort.getData()
        newSample = DataSample.copy(dataSample)
        self.outPort.transmit(dataSample)

    # overwritten by tests
    def beforeTransmit(self, dataSample):
        pass

    def afterTransmit(self):
        pass
 def __init__(self, environment):
     super().__init__(False, True, environment)
     self.inPort = InputPort(False, "inPort", environment)
     self.addStaticPort(self.inPort)
     self.outPort = OutputPort(False, "outPort", environment)
     self.addStaticPort(self.outPort)
     self.dynOutPorts = None
Exemple #9
0
 def __init__(self, env, envCInput, envCOutput, parent):
     Filter.__init__(self, False, False, env)
     self._parent = parent
     for src in envCInput.getDynamicOutputPorts():
         dest = InputPort(False, src.name(), env)
         self.addStaticPort(dest)
     for src in envCOutput.getDynamicInputPorts():
         dest = OutputPort(False, src.name(), env)
         self.addStaticPort(dest)
Exemple #10
0
    def addStaticOutputPort(self, name):
        """
        Shortcut for generating a static output port and adding it to the filter. See also
        :py:func:`nexxT.interface.Ports.OutputPort`

        :param name: The name of the output port
        :return: the new port instance
        """
        port = OutputPort(False, name, self._environment)
        self.addStaticPort(port)
        return port
class SimpleDynInFilter(Filter):
    def __init__(self, environment):
        super().__init__(True, False, environment)
        self.outPort = OutputPort(False, "outPort", environment)
        self.addStaticPort(self.outPort)
        self.dynInPorts = None
        self.sleep_time = self.propertyCollection().defineProperty(
            "sleep_time", 0.0, "sleep time to simulate computational load [s]")

    def onInit(self):
        self.dynInPorts = self.getDynamicInputPorts()
        assert len(self.getDynamicOutputPorts()) == 0

    def onPortDataChanged(self, inputPort):
        dataSample = inputPort.getData()
        self.afterReceive(dataSample)
        if dataSample.getDatatype() == "text/utf8":
            logging.getLogger(__name__).info(
                "received: %s",
                dataSample.getContent().data().decode("utf8"))
        newSample = DataSample.copy(dataSample)
        time.sleep(self.sleep_time)
        self.beforeTransmit(dataSample)
        self.outPort.transmit(dataSample)
        self.afterTransmit()

    def onDeinit(self):
        self.dynInPorts = None

    # used by tests
    def afterReceive(self, dataSample):
        pass

    def beforeTransmit(self, dataSample):
        pass

    def afterTransmit(self):
        pass
Exemple #12
0
def test_static_filter():
    function_calls = []
    with FilterEnvironment(
            "pyfile://" + os.path.dirname(__file__) +
            "/../interface/SimpleStaticFilter.py", "SimpleStaticFilter",
            PropertyCollectionImpl("root", None)) as staticPyFilter:
        f = staticPyFilter.getPlugin()

        # call the methods to check everything is sane
        assert f.environment() is staticPyFilter
        assert len(f.onSuggestDynamicPorts()[0]) == 0
        assert len(f.onSuggestDynamicPorts()[1]) == 0

        origCallbacks = dict(onInit=f.onInit,
                             onOpen=f.onOpen,
                             onStart=f.onStart,
                             onStop=f.onStop,
                             onClose=f.onClose,
                             onDeinit=f.onDeinit,
                             onPortDataChanged=f.onPortDataChanged)
        for callback in origCallbacks:
            setattr(f,
                    callback,
                    lambda *args, callback=callback: function_calls.append(
                        (callback, origCallbacks[callback](*args))))

        def exceptionCallback(*args):
            raise RuntimeError()

        assert staticPyFilter.getDynamicInputPorts() == []
        assert staticPyFilter.getDynamicOutputPorts() == []
        sip = staticPyFilter.getStaticInputPorts()
        assert len(sip) == 1
        assert sip[0].name() == "inPort"
        assert sip[0] is staticPyFilter.getInputPort("inPort")
        sop = staticPyFilter.getStaticOutputPorts()
        assert len(sop) == 1
        assert sop[0].name() == "outPort"
        assert sop[0] is staticPyFilter.getOutputPort("outPort")
        expect_exception(staticPyFilter.addPort,
                         InputPort(True, "dynOutPort", staticPyFilter))
        expect_exception(staticPyFilter.addPort,
                         OutputPort(True, "dynOutPort", staticPyFilter))
        assert staticPyFilter.state() == FilterState.CONSTRUCTED

        assert len(function_calls) == 0
        expect_exception(staticPyFilter.start)
        expect_exception(staticPyFilter.open)
        expect_exception(staticPyFilter.stop)
        expect_exception(staticPyFilter.close)
        expect_exception(staticPyFilter.deinit)
        staticPyFilter.init()
        assert function_calls == [("onInit", None)]
        assert staticPyFilter.state() == FilterState.INITIALIZED
        function_calls.clear()

        expect_exception(staticPyFilter.init)
        expect_exception(staticPyFilter.stop)
        expect_exception(staticPyFilter.start)
        expect_exception(staticPyFilter.close)
        staticPyFilter.open()
        assert function_calls == [("onOpen", None)]
        assert staticPyFilter.state() == FilterState.OPENED
        function_calls.clear()

        expect_exception(staticPyFilter.init)
        expect_exception(staticPyFilter.open)
        expect_exception(staticPyFilter.stop)
        expect_exception(staticPyFilter.deinit)
        staticPyFilter.start()
        assert function_calls == [("onStart", None)]
        assert staticPyFilter.state() == FilterState.ACTIVE
        function_calls.clear()

        assert len(function_calls) == 0
        expect_exception(staticPyFilter.init)
        expect_exception(staticPyFilter.open)
        expect_exception(staticPyFilter.start)
        expect_exception(staticPyFilter.close)
        expect_exception(staticPyFilter.deinit)
        staticPyFilter.stop()
        assert function_calls == [("onStop", None)]
        assert staticPyFilter.state() == FilterState.OPENED
        function_calls.clear()

        assert len(function_calls) == 0
        expect_exception(staticPyFilter.init)
        expect_exception(staticPyFilter.open)
        expect_exception(staticPyFilter.stop)
        expect_exception(staticPyFilter.deinit)
        staticPyFilter.close()
        assert function_calls == [("onClose", None)]
        assert staticPyFilter.state() == FilterState.INITIALIZED
        function_calls.clear()

        assert len(function_calls) == 0
        staticPyFilter.deinit()
        assert function_calls == [("onDeinit", None)]
        assert staticPyFilter.state() == FilterState.CONSTRUCTED
        function_calls.clear()

        # check exception call backs
        f.onInit = exceptionCallback
        staticPyFilter.init()
        assert staticPyFilter.state() == FilterState.INITIALIZED
        staticPyFilter.deinit()
        function_calls.clear()

    # check auto cleanup functionality
    with FilterEnvironment(
            "pyfile://" + os.path.dirname(__file__) +
            "/../interface/SimpleStaticFilter.py", "SimpleStaticFilter",
            PropertyCollectionImpl("root", None)) as staticPyFilter:
        f = staticPyFilter.getPlugin()
        origCallbacks = dict(onInit=f.onInit,
                             onOpen=f.onOpen,
                             onStart=f.onStart,
                             onStop=f.onStop,
                             onClose=f.onClose,
                             onDeinit=f.onDeinit,
                             onPortDataChanged=f.onPortDataChanged)
        for callback in origCallbacks:
            setattr(f,
                    callback,
                    lambda *args, callback=callback: function_calls.append(
                        (callback, origCallbacks[callback](*args))))

    assert len(function_calls) == 0

    with FilterEnvironment(
            "pyfile://" + os.path.dirname(__file__) +
            "/../interface/SimpleStaticFilter.py", "SimpleStaticFilter",
            PropertyCollectionImpl("root", None)) as staticPyFilter:
        f = staticPyFilter.getPlugin()
        origCallbacks = dict(onInit=f.onInit,
                             onOpen=f.onOpen,
                             onStart=f.onStart,
                             onStop=f.onStop,
                             onClose=f.onClose,
                             onDeinit=f.onDeinit,
                             onPortDataChanged=f.onPortDataChanged)
        for callback in origCallbacks:
            setattr(f,
                    callback,
                    lambda *args, callback=callback: function_calls.append(
                        (callback, origCallbacks[callback](*args))))
        staticPyFilter.init()

    assert function_calls == [("onInit", None), ("onDeinit", None)]
    function_calls.clear()

    with FilterEnvironment(
            "pyfile://" + os.path.dirname(__file__) +
            "/../interface/SimpleStaticFilter.py", "SimpleStaticFilter",
            PropertyCollectionImpl("root", None)) as staticPyFilter:
        f = staticPyFilter.getPlugin()
        origCallbacks = dict(onInit=f.onInit,
                             onStart=f.onStart,
                             onOpen=f.onOpen,
                             onStop=f.onStop,
                             onClose=f.onClose,
                             onDeinit=f.onDeinit,
                             onPortDataChanged=f.onPortDataChanged)
        for callback in origCallbacks:
            setattr(f,
                    callback,
                    lambda *args, callback=callback: function_calls.append(
                        (callback, origCallbacks[callback](*args))))
        staticPyFilter.init()
        staticPyFilter.open()
        staticPyFilter.start()

    assert function_calls == [("onInit", None), ("onOpen", None),
                              ("onStart", None), ("onStop", None),
                              ("onClose", None), ("onDeinit", None)]
    function_calls.clear()

    expect_exception(FilterEnvironment, "weird.plugin.extension", "factory",
                     PropertyCollectionImpl("root", None))