Example #1
0
def execute_1():
    """
    after 3 seconds, deactivate and re-activate the app
    after 3 seconds, re-open and re-activate the app
    after 3 more seconds, quit
    :return:
    """
    logger.info("execute_1:begin")
    cfg = Services.getService("Configuration")
    rc = Services.getService("RecordingControl")

    logger.info("app activated")
    app = cfg.configuration().applicationByName("myApp")

    t = QTimer()
    t.setSingleShot(True)
    t.setInterval(3000)
    t.start()
    waitForSignal(t.timeout)

    execute_1.i = MethodInvoker(cfg.deactivate, Qt.QueuedConnection)
    waitForSignal(app.activeApplication.stateChanged,
                  lambda s: s == FilterState.CONSTRUCTED)
    logger.info("app deactivated")

    execute_1.i = MethodInvoker(cfg.activate, Qt.QueuedConnection)
    waitForSignal(cfg.configuration().appActivated)
    if app.activeApplication.getState() != FilterState.ACTIVE:
        waitForSignal(app.activeApplication.stateChanged,
                      lambda s: s == FilterState.ACTIVE)

    execute_1.i = MethodInvoker(rc.startRecording, Qt.QueuedConnection, ".")
    logger.info("app activated")

    t = QTimer()
    t.setSingleShot(True)
    t.setInterval(3000)
    t.start()
    waitForSignal(t.timeout)

    execute_1.i = MethodInvoker(cfg.deactivate, Qt.QueuedConnection)
    waitForSignal(app.activeApplication.stateChanged,
                  lambda s: s == FilterState.CONSTRUCTED)
    logger.info("app deactivated")

    # re-open this application
    execute_1.i = MethodInvoker(cfg.loadConfig, Qt.QueuedConnection,
                                "basicworkflow.json")
    waitForSignal(cfg.configuration().configNameChanged)
    logger.info("config loaded")

    # activate
    execute_1.i = MethodInvoker(cfg.changeActiveApp, Qt.QueuedConnection,
                                "myApp")
    waitForSignal(cfg.configuration().appActivated)
    execute_1.i = MethodInvoker(cfg.activate, Qt.QueuedConnection)
    waitForSignal(cfg.configuration().appActivated)
    if app.activeApplication.getState() != FilterState.ACTIVE:
        waitForSignal(app.activeApplication.stateChanged,
                      lambda s: s == FilterState.ACTIVE)
    logger.info("app activated")

    t = QTimer()
    t.setSingleShot(True)
    t.setInterval(3000)
    t.start()
    waitForSignal(t.timeout)

    execute_1.i = MethodInvoker(QCoreApplication.quit, Qt.QueuedConnection)
    logger.info("execute_1:end")
def execute_2():
    logger.info("execute_2:begin")
    cfg = Services.getService("Configuration")
    pbc = Services.getService("PlaybackControl")

    app = cfg.configuration().applicationByName("pbApp")

    logger.info("app activated")
    execute_2.i = MethodInvoker(pbc.setSequence, Qt.QueuedConnection,
                                glob.glob("./*.h5")[0])
    _, tbegin, tend, _ = waitForSignal(pbc.sequenceOpened)

    execute_2.i = MethodInvoker(pbc.startPlayback, Qt.QueuedConnection)
    waitForSignal(pbc.playbackStarted)
    waitForSignal(pbc.playbackPaused)
    logger.info("played")

    execute_2.i = MethodInvoker(pbc.seekBeginning, Qt.QueuedConnection)
    waitForSignal(pbc.currentTimestampChanged)
    logger.info("seekBeginning")

    for i in range(10):
        execute_2.i = MethodInvoker(pbc.stepForward, Qt.QueuedConnection, None)
        waitForSignal(pbc.playbackPaused)
        logger.info("stepForward[None]")

    for i in range(10):
        execute_2.i = MethodInvoker(pbc.stepForward, Qt.QueuedConnection,
                                    "stream1")
        waitForSignal(pbc.playbackPaused)
        logger.info("stepForward[stream1]")

    for i in range(2):
        execute_2.i = MethodInvoker(pbc.stepForward, Qt.QueuedConnection,
                                    "stream2")
        waitForSignal(pbc.playbackPaused)
        logger.info("stepForward[stream2]")

    execute_2.i = MethodInvoker(
        pbc.seekTime, Qt.QueuedConnection,
        QDateTime.fromMSecsSinceEpoch(
            (tbegin.toMSecsSinceEpoch() + tend.toMSecsSinceEpoch()) // 2))
    waitForSignal(pbc.currentTimestampChanged)
    logger.info("seekTime")

    execute_2.i = MethodInvoker(pbc.seekEnd, Qt.QueuedConnection)
    waitForSignal(pbc.currentTimestampChanged)
    logger.info("seekEnd")

    for i in range(10):
        execute_2.i = MethodInvoker(pbc.stepBackward, Qt.QueuedConnection,
                                    None)
        waitForSignal(pbc.playbackPaused)
        logger.info("stepBackward[None]")

    execute_2.i = MethodInvoker(
        pbc.seekTime, Qt.QueuedConnection,
        QDateTime.fromMSecsSinceEpoch(tbegin.toMSecsSinceEpoch() - 1000))
    waitForSignal(pbc.currentTimestampChanged)
    logger.info("seekTimeBegin")

    execute_2.i = MethodInvoker(
        pbc.seekTime, Qt.QueuedConnection,
        QDateTime.fromMSecsSinceEpoch(tend.toMSecsSinceEpoch() + 1000))
    waitForSignal(pbc.currentTimestampChanged)
    logger.info("seekTimeEnd")

    execute_2.i = MethodInvoker(QCoreApplication.quit, Qt.QueuedConnection)
    logger.info("execute_2:end")
def execute_0():
    """
    Creates a new application with an app and a composite filter and saves it to a file; finally activate the
    created app.
    :return:
    """
    logger.info("execute_0:begin")

    cfg = Services.getService("Configuration")

    # create a new configuration with a composite graph and an application
    execute_0.i = MethodInvoker(cfg.newConfig, Qt.QueuedConnection,
                                "basicworkflow.json")
    waitForSignal(cfg.configuration().configNameChanged)

    # create simple composite filter
    cfg.configuration().renameComposite(
        cfg.configuration().addNewCompositeFilter(), "composite")
    comp = cfg.configuration().compositeFilterByName("composite")
    node = comp.getGraph().addNode(library="pyfile://./SimpleStaticFilter.py",
                                   factoryFunction="SimpleStaticFilter")
    idx = _getIndex(cfg.model, ["composite", "composite", node, "sleep_time"])
    cfg.model.setData(idx.siblingAtColumn(1), 0.01, Qt.EditRole)

    execute_0.i = MethodInvoker(comp.getGraph().addDynamicInputPort,
                                Qt.QueuedConnection, "CompositeOutput", "out")
    waitForSignal(comp.getGraph().dynInputPortAdded)
    execute_0.i = MethodInvoker(comp.getGraph().addDynamicOutputPort,
                                Qt.QueuedConnection, "CompositeInput", "in")
    waitForSignal(comp.getGraph().dynOutputPortAdded)
    comp.getGraph().addConnection("CompositeInput", "in", node, "inPort")
    comp.getGraph().addConnection(node, "outPort", "CompositeOutput", "out")

    # create simple application (live/recorder)
    cfg.configuration().renameApp(cfg.configuration().addNewApplication(),
                                  "myApp")
    app = cfg.configuration().applicationByName("myApp")
    import nexxT
    if nexxT.useCImpl:
        src = app.getGraph().addNode(
            library="entry_point://tests.nexxT.CSimpleSource",
            factoryFunction="entry_point")
    else:
        src = app.getGraph().addNode(
            library="pyfile://./SimpleStaticFilter.py",
            factoryFunction="SimpleSource")
    src2 = app.getGraph().addNode(library="pyfile://./SimpleStaticFilter.py",
                                  factoryFunction="SimpleSource")
    idx = _getIndex(cfg.model, ["apps", "myApp", src, "frequency"])
    cfg.model.setData(idx.siblingAtColumn(1), 10.0, Qt.EditRole)
    idx = _getIndex(cfg.model, ["apps", "myApp", src2, "frequency"])
    cfg.model.setData(idx.siblingAtColumn(1), 2.0, Qt.EditRole)
    idx = _getIndex(cfg.model, ["apps", "myApp", src2, "log_tr"])
    cfg.model.setData(idx.siblingAtColumn(1), False, Qt.EditRole)

    flt = app.getGraph().addNode(library=comp, factoryFunction="compositeNode")
    app.getGraph().addConnection(src, "outPort", flt, "in")

    # add a recorder
    rec = app.getGraph().addNode(library="pymod://nexxT.filters.hdf5",
                                 factoryFunction="Hdf5Writer")
    execute_0.i = MethodInvoker(app.getGraph().addDynamicInputPort,
                                Qt.QueuedConnection, rec, "stream1")
    waitForSignal(app.getGraph().dynInputPortAdded)
    execute_0.i = MethodInvoker(app.getGraph().addDynamicInputPort,
                                Qt.QueuedConnection, rec, "stream2")
    waitForSignal(app.getGraph().dynInputPortAdded)
    app.getGraph().addConnection(src, "outPort", rec, "stream1")
    app.getGraph().addConnection(src2, "outPort", rec, "stream2")

    # assign threads
    app.getGraph().getMockup(src).propertyCollection().getChildCollection(
        "_nexxT").setProperty("thread", "source-thread")
    app.getGraph().getMockup(src2).propertyCollection().getChildCollection(
        "_nexxT").setProperty("thread", "source2-thread")
    app.getGraph().getMockup(rec).propertyCollection().getChildCollection(
        "_nexxT").setProperty("thread", "rec-thread")

    # create another application (playback)
    # hdf5 reader
    cfg.configuration().renameApp(cfg.configuration().addNewApplication(),
                                  "pbApp")
    app = cfg.configuration().applicationByName("pbApp")
    pb = app.getGraph().addNode(library="pymod://nexxT.filters.hdf5",
                                factoryFunction="Hdf5Reader",
                                suggestedName="player")
    assert pb == "player"
    execute_0.i = MethodInvoker(app.getGraph().addDynamicOutputPort,
                                Qt.QueuedConnection, pb, "stream1")
    waitForSignal(app.getGraph().dynOutputPortAdded)
    execute_0.i = MethodInvoker(app.getGraph().addDynamicOutputPort,
                                Qt.QueuedConnection, pb, "stream2")
    waitForSignal(app.getGraph().dynOutputPortAdded)
    # 2 filters
    flt1 = app.getGraph().addNode(library="pyfile://./SimpleStaticFilter.py",
                                  factoryFunction="SimpleStaticFilter",
                                  suggestedName="flt1")
    assert flt1 == "flt1"
    idx = _getIndex(cfg.model, ["apps", "pbApp", "flt1", "sleep_time"])
    cfg.model.setData(idx.siblingAtColumn(1), 0.01, Qt.EditRole)
    idx = _getIndex(cfg.model, ["apps", "pbApp", "flt1", "log_rcv"])
    cfg.model.setData(idx.siblingAtColumn(1), True, Qt.EditRole)
    idx = _getIndex(cfg.model, ["apps", "pbApp", "flt1", "log_prefix"])
    cfg.model.setData(idx.siblingAtColumn(1), "(flt1) ", Qt.EditRole)

    flt2 = app.getGraph().addNode(library="pyfile://./SimpleStaticFilter.py",
                                  factoryFunction="SimpleStaticFilter",
                                  suggestedName="flt2")
    assert flt2 == "flt2"
    idx = _getIndex(cfg.model, ["apps", "pbApp", "flt2", "sleep_time"])
    cfg.model.setData(idx.siblingAtColumn(1), 0.01, Qt.EditRole)
    idx = _getIndex(cfg.model, ["apps", "pbApp", "flt2", "log_rcv"])
    cfg.model.setData(idx.siblingAtColumn(1), True, Qt.EditRole)
    idx = _getIndex(cfg.model, ["apps", "pbApp", "flt2", "log_prefix"])
    cfg.model.setData(idx.siblingAtColumn(1), "(flt2) ", Qt.EditRole)

    app.getGraph().addConnection("player", "stream1", "flt1", "inPort")
    app.getGraph().addConnection("player", "stream2", "flt2", "inPort")

    # save application
    execute_0.i = MethodInvoker(cfg.saveConfig, Qt.QueuedConnection)
    waitForSignal(cfg.configuration().configNameChanged)

    # change active app
    execute_1.i = MethodInvoker(cfg.changeActiveApp, Qt.QueuedConnection,
                                "myApp")
    waitForSignal(cfg.configuration().appActivated)

    # activate
    execute_1.i = MethodInvoker(cfg.activate, Qt.QueuedConnection)
    waitForSignal(cfg.configuration().appActivated)
    if app.activeApplication.getState() != FilterState.ACTIVE:
        waitForSignal(app.activeApplication.stateChanged,
                      lambda s: s == FilterState.ACTIVE)
    logger.info("execute_0:end")
Example #4
0
def startNexT(cfgfile, active, execScripts, execCode, withGui):
    """
    Starts next with the given config file and activates the given application.
    :param cfgfile: path to config file
    :param active: active application (if None, the first application in the config will be used)
    :return: None
    """
    logger.debug("Starting nexxT...")
    config = Configuration()
    lcl = QLocale.system()
    lcl.setNumberOptions(QLocale.c().numberOptions())
    QLocale.setDefault(lcl)
    if withGui:
        app = QApplication(
        ) if QApplication.instance() is None else QApplication.instance()
        app.setWindowIcon(QIcon(":icons/nexxT.svg"))
        app.setOrganizationName("nexxT")
        app.setApplicationName("nexxT")
        setupGuiServices(config)
    else:
        app = QCoreApplication() if QCoreApplication.instance(
        ) is None else QCoreApplication.instance()
        app.setOrganizationName("nexxT")
        app.setApplicationName("nexxT")
        setupConsoleServices(config)

    if cfgfile is not None:
        ConfigFileLoader.load(config, cfgfile)
    if withGui:
        mainWindow = Services.getService("MainWindow")
        mainWindow.restoreState()
        mainWindow.show()
        # the reference will still be held by the service, but here we don't need it anymore
        del mainWindow
    if active is not None:
        config.activate(active)
        # pylint: disable=unused-variable
        # need to hold the reference of this until the method is called
        i2 = MethodInvoker(
            dict(object=Application, method="initialize", thread=app.thread()),
            MethodInvoker.IDLE_TASK)  # pylint: disable=unused-variable
        waitForSignal(config.appActivated)
        if Application.activeApplication.getState() != FilterState.ACTIVE:
            waitForSignal(Application.activeApplication.stateChanged,
                          lambda s: s == FilterState.ACTIVE)
        logger.info("done")

    def cleanup():
        logger.debug("cleaning up loaded services")
        Services.removeAll()
        logger.debug("cleaning up loaded plugins")
        for v in ("last_traceback", "last_type", "last_value"):
            if hasattr(sys, v):
                del sys.__dict__[v]
        #PluginManager.singleton().unloadAll()
        logger.debug("cleaning up complete")

    code_globals = {}
    for c in execCode:
        logger.info("Executing code '%s'", c)
        # note that exec is used intentionally here to provide the user with scripting posibilities
        exec(compile(c, "<string>", 'exec'), code_globals)  # pylint: disable=exec-used
        logger.debug("Executing code done")

    for s in execScripts:
        logger.info("Executing script '%s'", s)
        with open(s) as fscript:
            # note that exec is used intentionally here to provide the user with scripting possibilities
            exec(compile(fscript.read(), s, 'exec'), code_globals)  # pylint: disable=exec-used
        logger.debug("Executing script done")

    res = app.exec_()
    logger.debug("closing config")
    config.close()
    cleanup()

    logger.internal("app.exec_ returned")

    return res