Skip to content

Commit

Permalink
generic: scopy api add function to run specific function of script
Browse files Browse the repository at this point in the history
To be able to call a function from Qt the function must be "export"
example: export function test()


Signed-off-by: Ionut Muthi <[email protected]>
  • Loading branch information
IonutMuthi committed Jul 8, 2024
1 parent 44d548c commit a4a681a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions core/include/core/scopymainwindow_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 12 additions & 3 deletions core/src/cmdlinehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}

Expand Down
27 changes: 27 additions & 0 deletions core/src/scopymainwindow_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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."},
Expand Down
1 change: 1 addition & 0 deletions pluginbase/src/scopyjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit a4a681a

Please sign in to comment.