AvogadroLibs 2.1.0-20261111
Loading...
Searching...
No Matches
PythonScript Class Reference

The PythonScript class implements a interface for calling short-lived python utility scripts.

#include <pythonscript.h>

Inheritance diagram for PythonScript:

Public Slots

void setDebug (bool d)
void processFinished (int exitCode, QProcess::ExitStatus exitStatus)

Signals

void finished ()
void asyncProgress (const QJsonObject &payload)

Public Member Functions

bool debug () const
QString scriptFilePath () const
void setScriptFilePath (const QString &scriptFile)
void setPackageInfo (const QString &packageDir, const QString &command, const QString &identifier, const QString &displayName=QString())
bool isPackageMode () const
QString packageDir () const
QString packageCommand () const
QString packageIdentifier () const
QString packageDisplayName () const
bool hasErrors () const
void clearErrors ()
QStringList errorList () const
void setDefaultPythonInterpreter ()
QByteArray execute (const QStringList &args, const QByteArray &scriptStdin=QByteArray())
bool asyncExecute (const QStringList &args, const QByteArray &scriptStdin=QByteArray(), bool mergedChannels=true, bool closeWriteChannel=true)
void setProgressScanning (bool enable)
bool progressScanning () const
QByteArray asyncWriteAndResponse (QByteArray input)
QByteArray asyncWriteAndResponseRaw (const QByteArray &input, int timeoutMs=5000)
void asyncTerminate ()
QByteArray asyncResponse ()
QByteArray asyncStandardError () const
 PythonScript (const QString &scriptFilePath_, QObject *parent_=nullptr)
 PythonScript (QObject *parent_=nullptr)

Static Public Member Functions

static bool parseProgressEnvelope (const QByteArray &line, QJsonObject &payload)
static void filterProgressLines (QByteArray &buffer, QByteArray &output, QList< QJsonObject > &payloads)

Protected Attributes

bool m_debug
bool m_packageMode = false
QString m_pythonInterpreter
QString m_pixi
QString m_scriptFilePath
QString m_packageDir
QString m_packageCommand
QString m_packageIdentifier
QString m_packageDisplayName
QStringList m_errors
QProcess * m_process

Constructor & Destructor Documentation

◆ PythonScript() [1/2]

PythonScript ( const QString & scriptFilePath_,
QObject * parent_ = nullptr )
explicit

Constructors

Parameters
scriptFilePath_Absolute path to python script.

◆ PythonScript() [2/2]

PythonScript ( QObject * parent_ = nullptr)
explicit

Constructors

Parameters
scriptFilePath_Absolute path to python script.

Member Function Documentation

◆ debug()

bool debug ( ) const
Returns
True if debugging of python I/O is enabled.

◆ scriptFilePath()

QString scriptFilePath ( ) const
Returns
The path to the generator file.

◆ setScriptFilePath()

void setScriptFilePath ( const QString & scriptFile)

Set the path to the input generator script file. This will reset any cached data held by this class.

◆ setPackageInfo()

void setPackageInfo ( const QString & packageDir,
const QString & command,
const QString & identifier,
const QString & displayName = QString() )

Enable package mode. In this mode, execute() runs "pixi run <command> <identifier> [args]" with packageDir as the working directory, instead of launching a script file via python.

◆ isPackageMode()

bool isPackageMode ( ) const
Returns
True if this script is in package mode.

◆ packageDir()

QString packageDir ( ) const
Returns
The package directory (only meaningful in package mode).

◆ packageCommand()

QString packageCommand ( ) const
Returns
The package command (only meaningful in package mode).

◆ packageIdentifier()

QString packageIdentifier ( ) const
Returns
The package identifier (only meaningful in package mode).

◆ packageDisplayName()

QString packageDisplayName ( ) const
Returns
The package display name from metadata, if one was provided.

◆ hasErrors()

bool hasErrors ( ) const
Returns
True if an error is set.

◆ clearErrors()

void clearErrors ( )

Reset the error counter.

◆ errorList()

QStringList errorList ( ) const
Returns
A QStringList containing all errors that occurred in the last call to the input generator script.

◆ setDefaultPythonInterpreter()

void setDefaultPythonInterpreter ( )

Reset the python interpreter path. The following are checked, in order:

  • The AVO_PYTHON_INTERPRETER environment variable
  • The "interpreters/python" QSettings value
  • The path specified in avogadropython.h.

◆ execute()

QByteArray execute ( const QStringList & args,
const QByteArray & scriptStdin = QByteArray() )

Start a new process to execute: "<m_pythonInterpreter> <scriptFilePath()> [args ...]", optionally passing scriptStdin to the processes standard input. Returns the standard output of the process when finished.

◆ asyncExecute()

bool asyncExecute ( const QStringList & args,
const QByteArray & scriptStdin = QByteArray(),
bool mergedChannels = true,
bool closeWriteChannel = true )

Start a new process to execute asynchronously "<m_pythonInterpreter> <scriptFilePath()> [args ...]", optionally passing scriptStdin to the processes standard input. If closeWriteChannel is false, stdin remains open for follow-up writes.

Will send asyncFinished() signal when finished

Returns true if the process was started; false if it could not be launched, in which case finished() will never be emitted and errorList() describes the failure.

◆ setProgressScanning()

void setProgressScanning ( bool enable)

Enable incremental scanning of the asynchronous process' standard output for progress envelopes, i.e. single lines of the form {"avogadro": { ... }}. Matching lines are removed from the output and reported through the asyncProgress() signal; every other line is kept and returned by asyncResponse() as usual.

This must be set before asyncExecute(). It is opt-in because scanning drains the process' output as it arrives, which is incompatible with callers that read the process directly (e.g. asyncWriteAndResponse() in "server mode").

When enabled, the script is run with AVO_PROGRESS_PROTOCOL set in its environment so it can tell that Avogadro is listening. Scripts must check for it before printing envelopes: releases up to and including Avogadro 2.0.0 read the script's whole standard output as a single JSON document, and the extra lines make that parse fail outright.

◆ progressScanning()

bool progressScanning ( ) const
Returns
True if standard output is being scanned for progress envelopes.

◆ parseProgressEnvelope()

bool parseProgressEnvelope ( const QByteArray & line,
QJsonObject & payload )
static

Test whether a single line of script output is a progress envelope.

To avoid mistaking part of a pretty-printed result for an envelope, the line must parse as a complete JSON object with exactly one member, named "avogadro".

Parameters
lineThe line to test, without its trailing newline.
payloadSet to the contents of the "avogadro" member on success.
Returns
True if line is a progress envelope.

◆ filterProgressLines()

void filterProgressLines ( QByteArray & buffer,
QByteArray & output,
QList< QJsonObject > & payloads )
static

Split buffer into complete lines, appending the payload of any progress envelope to payloads and every other line (newline included) verbatim to output. A trailing partial line is left in buffer for the next call.

◆ asyncWriteAndResponse()

QByteArray asyncWriteAndResponse ( QByteArray input)

Write input to the asynchronous process' standard input and return the standard output when ready. Does not wait for the process to terminate before returning (e.g. "server mode").

Parameters
inputThe input to write to the process' standard input
Returns
The standard output of the process

◆ asyncWriteAndResponseRaw()

QByteArray asyncWriteAndResponseRaw ( const QByteArray & input,
int timeoutMs = 5000 )

Write input to the asynchronous process' standard input and return raw bytes from stdout. Unlike asyncWriteAndResponse(), this does not require line-oriented output and is suitable for framed binary protocols.

◆ asyncTerminate()

void asyncTerminate ( )

Terminate the asynchronous process.

◆ asyncResponse()

QByteArray asyncResponse ( )

Returns the standard output of the asynchronous process when finished. If progress scanning is enabled, progress envelope lines have been removed and the remainder is returned even after the process is gone.

◆ asyncStandardError()

QByteArray asyncStandardError ( ) const
Returns
The standard error collected from the asynchronous process. Only populated when the process was started with separate channels; useful for reporting a python traceback that never reached standard output.

◆ finished

void finished ( )
signal

The asynchronous execution is finished or timed out

◆ asyncProgress

void asyncProgress ( const QJsonObject & payload)
signal

A progress envelope was read from the asynchronous process' standard output. Only emitted when setProgressScanning(true) was called before asyncExecute().

Parameters
payloadThe contents of the "avogadro" member of the envelope.

◆ setDebug

void setDebug ( bool d)
slot

Enable/disable debugging.

◆ processFinished

void processFinished ( int exitCode,
QProcess::ExitStatus exitStatus )
slot

Handle a finished process;


The documentation for this class was generated from the following file: