Skip to content

Commit

Permalink
core/scopymainwindow_api: Changed runScript method.
Browse files Browse the repository at this point in the history
The method no longer requires the contents of the script as a parameter
and does not automatically close the application.
Closing the application can now be managed by the user both from the
command line through the --keep-running (r) option and from ScopyJS through
runScript(QString scriptPath, bool exitApp) method.

Signed-off-by: andrei.danila <andrei.danila@analog.com>
  • Loading branch information
andreidanila1 committed Apr 8, 2024
1 parent 899bc34 commit ab50610
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
4 changes: 3 additions & 1 deletion core/include/core/scopymainwindow_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "scopy-core_export.h"
#include "scopymainwindow.h"
#include <QFile>

namespace scopy {
class SCOPY_CORE_EXPORT ScopyMainWindow_API : public ApiObject
Expand All @@ -20,10 +21,11 @@ class SCOPY_CORE_EXPORT ScopyMainWindow_API : public ApiObject
Q_INVOKABLE bool disconnectDevice();
Q_INVOKABLE void switchTool(QString devID, QString toolName);
Q_INVOKABLE void switchTool(QString toolName);
Q_INVOKABLE void runScript(QString content, QString fileName);
Q_INVOKABLE void runScript(QString scriptPath, bool exitApp = true);

private:
static bool sortByUUID(const QString &k1, const QString &k2);
const QString getScriptContent(QFile *file);
ScopyMainWindow *m_w;
};

Expand Down
23 changes: 8 additions & 15 deletions core/src/cmdlinehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,16 @@ int CmdLineHandler::handle(QCommandLineParser &parser, ScopyMainWindow_API &scop
}
}

bool keepRunning = parser.isSet("keep-running");
if(keepRunning) {
qInfo() << "keep-running option is only useful with a script!";
}

QString scriptPath = parser.value("script");
if(!scriptPath.isEmpty()) {
QFile file(scriptPath);
if(!file.open(QFile::ReadOnly)) {
qCritical() << "Unable to open script file";
return EXIT_FAILURE;
}

QTextStream stream(&file);
QString firstLine = stream.readLine();
if(!firstLine.startsWith("#!"))
stream.seek(0);

QString content = stream.readAll();
file.close();
QMetaObject::invokeMethod(&scopyApi, "runScript", Qt::QueuedConnection, Q_ARG(QString, content),
Q_ARG(QString, scriptPath));
bool exitApp = !keepRunning;
QMetaObject::invokeMethod(&scopyApi, "runScript", Qt::QueuedConnection, Q_ARG(QString, scriptPath),
Q_ARG(bool, exitApp));
}
return EXIT_SUCCESS;
}
Expand Down
26 changes: 23 additions & 3 deletions core/src/scopymainwindow_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@ void ScopyMainWindow_API::switchTool(QString toolName)
}
}

void ScopyMainWindow_API::runScript(QString content, QString fileName)
void ScopyMainWindow_API::runScript(QString scriptPath, bool exitApp)
{
QJSValue val = ScopyJS::GetInstance()->engine()->evaluate(content, fileName);
QFile file(scriptPath);
if(!file.open(QFile::ReadOnly)) {
qCritical(CAT_SCOPY_API) << "Unable to open the script file: " << scriptPath;
return;
}
const QString scriptContent = getScriptContent(&file);
QJSValue val = ScopyJS::GetInstance()->engine()->evaluate(scriptContent, scriptPath);
int ret = EXIT_SUCCESS;
if(val.isError()) {
qWarning(CAT_SCOPY_API) << "Exception:" << val.toString();
Expand All @@ -155,8 +161,22 @@ void ScopyMainWindow_API::runScript(QString content, QString fileName)
qWarning(CAT_SCOPY_API) << val.toString();
}

qInfo(CAT_SCOPY_API) << "Script finished with status" << ret;
/* Exit application */
qApp->exit(ret);
if(exitApp)
qApp->exit(ret);
}

const QString ScopyMainWindow_API::getScriptContent(QFile *file)
{
QTextStream stream(file);
QString firstLine = stream.readLine();
if(!firstLine.startsWith("#!"))
stream.seek(0);

QString content = stream.readAll();
file->close();
return content;
}

bool ScopyMainWindow_API::sortByUUID(const QString &k1, const QString &k2)
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"},
{{"r", "keep-running"}, "Keep the application session after running a certain script."},
{{"a", "accept-license"}, "Accept the license in advance."},
{{"l", "logfile"}, "Saves all the logging messages into a file.", "filename"},
{{"c", "connect"}, "Establish the connection to a given device by URI.", "URI"},
Expand Down
6 changes: 3 additions & 3 deletions tools/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ Scopy Scripting Guide is available at: https://wiki.analog.com/university/tools/
- devID: The device ID that was returned upon adding it to the device browser
- toolName: The name of the desired tool
5. Running a script from a given file
- Command: `scopy.runScript(QString content, QString fileName)`
- content: The content of the script
- fileName: The path to the file
- Command: `scopy.runScript(QString scriptPath, bool exitApp = true)`
- scriptPath: The path to the script
- exitApp: If set to true, the application will be closed after running the given script

0 comments on commit ab50610

Please sign in to comment.