From a4a681aa24edf82e2c59e2a872189c9295f4f5e3 Mon Sep 17 00:00:00 2001 From: Ionut Muthi Date: Mon, 8 Jul 2024 11:37:58 +0300 Subject: [PATCH] generic: scopy api add function to run specific function of script To be able to call a function from Qt the function must be "export" example: export function test() Signed-off-by: Ionut Muthi --- core/include/core/scopymainwindow_api.h | 1 + core/src/cmdlinehandler.cpp | 15 +++++++++++--- core/src/scopymainwindow_api.cpp | 27 +++++++++++++++++++++++++ main.cpp | 1 + pluginbase/src/scopyjs.cpp | 1 + 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/core/include/core/scopymainwindow_api.h b/core/include/core/scopymainwindow_api.h index 927759d26..a20d758df 100644 --- a/core/include/core/scopymainwindow_api.h +++ b/core/include/core/scopymainwindow_api.h @@ -22,6 +22,7 @@ class SCOPY_CORE_EXPORT ScopyMainWindow_API : public ApiObject Q_INVOKABLE void switchTool(QString devID, QString toolName); Q_INVOKABLE void switchTool(QString toolName); Q_INVOKABLE void runScript(QString scriptPath, bool exitApp = true); + Q_INVOKABLE void runFunctionOfScript(QString scriptPath, QStringList scriptFunctions, bool exitApp = true); Q_INVOKABLE void runScriptList(QStringList scriptPathList, bool exitApp = true); private: diff --git a/core/src/cmdlinehandler.cpp b/core/src/cmdlinehandler.cpp index 66f0fd738..f206ab12d 100644 --- a/core/src/cmdlinehandler.cpp +++ b/core/src/cmdlinehandler.cpp @@ -30,9 +30,17 @@ int CmdLineHandler::handle(QCommandLineParser &parser, ScopyMainWindow_API &scop QString scriptPath = parser.value("script"); if(!scriptPath.isEmpty()) { - bool exitApp = !keepRunning; - QMetaObject::invokeMethod(&scopyApi, "runScript", Qt::QueuedConnection, Q_ARG(QString, scriptPath), - Q_ARG(bool, exitApp)); + //check if script is run with specific function + QStringList scriptFunctions = parser.values("script-functions"); + if(!scriptFunctions.isEmpty()) { + bool exitApp = !keepRunning; + QMetaObject::invokeMethod(&scopyApi, "runFunctionOfScript", Qt::QueuedConnection, Q_ARG(QString, scriptPath), Q_ARG(QStringList, scriptFunctions), + Q_ARG(bool, exitApp)); + } else { + bool exitApp = !keepRunning; + QMetaObject::invokeMethod(&scopyApi, "runScript", Qt::QueuedConnection, Q_ARG(QString, scriptPath), + Q_ARG(bool, exitApp)); + } } QStringList scriptListPath = parser.values("script-list"); @@ -41,6 +49,7 @@ int CmdLineHandler::handle(QCommandLineParser &parser, ScopyMainWindow_API &scop QMetaObject::invokeMethod(&scopyApi, "runScriptList", Qt::QueuedConnection, Q_ARG(QStringList, scriptListPath), Q_ARG(bool, exitApp)); } + return EXIT_SUCCESS; } diff --git a/core/src/scopymainwindow_api.cpp b/core/src/scopymainwindow_api.cpp index f67828839..64a5fd891 100644 --- a/core/src/scopymainwindow_api.cpp +++ b/core/src/scopymainwindow_api.cpp @@ -167,9 +167,36 @@ void ScopyMainWindow_API::runScript(QString scriptPath, bool exitApp) qApp->exit(ret); } +void ScopyMainWindow_API::runFunctionOfScript(QString scriptPath, QStringList scriptFunctions, bool exitApp) +{ + qInfo(CAT_SCOPY_API) << "Run Script with functions" ; + QJSValue module = ScopyJS::GetInstance()->engine()->importModule(scriptPath); + + int ret = EXIT_SUCCESS; + + foreach (QString function, scriptFunctions) { + + QJSValue scriptFunction = module.property(function); + QJSValue result = scriptFunction.call(); + qInfo(CAT_SCOPY_API) <<"FUNCTION IS " << function << " RESTUL IS " << result.toString(); + + if(result.isError()) { + qWarning(CAT_SCOPY_API) << "Exception:" << result.toString(); + ret = EXIT_FAILURE; + } else if(!result.isUndefined()) { + qWarning(CAT_SCOPY_API) << result.toString(); + } + } + + + /* Exit application */ + if(exitApp) + qApp->exit(ret); +} void ScopyMainWindow_API::runScriptList(QStringList scriptPathList, bool exitApp) { foreach (QString scriptPath, scriptPathList) { + runScript(scriptPath, false); } diff --git a/main.cpp b/main.cpp index 130d8ece4..e2dea29dd 100644 --- a/main.cpp +++ b/main.cpp @@ -88,6 +88,7 @@ int main(int argc, char *argv[]) parser.addVersionOption(); parser.addOptions({ {{"s", "script"}, "Run given script.", "script"}, + {{"f", "script-functions"}, "Run given script list.", "script-functions"}, {{"S", "script-list"}, "Run given script list.", "script-list"}, {{"r", "keep-running"}, "Keep the application session after running a certain script."}, {{"a", "accept-license"}, "Accept the license in advance."}, diff --git a/pluginbase/src/scopyjs.cpp b/pluginbase/src/scopyjs.cpp index 6fe1c4961..09980e8b9 100644 --- a/pluginbase/src/scopyjs.cpp +++ b/pluginbase/src/scopyjs.cpp @@ -73,6 +73,7 @@ void ScopyJS::init() m_engine.globalObject().setProperty("inspect()", m_engine.evaluate("(function(o) { for (each in o) { print(each); } })")); m_engine.installExtensions(QJSEngine::ConsoleExtension); + m_engine.installExtensions(QJSEngine::AllExtensions); m_engine.globalObject().setProperty("fileIO", m_engine.newQObject(new JsFileIo(this))); if(isatty(STDIN_FILENO)) {