QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgspythonrunner.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspythonrunner.cpp
3 ---------------------
4 begin : May 2011
5 copyright : (C) 2011 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgspythonrunner.h"
16
17#include "qgslogger.h"
18
19#include <QString>
20
21using namespace Qt::StringLiterals;
22
24
26// static methods
27
29{
30 return nullptr != sInstance;
31}
32
33bool QgsPythonRunner::run( const QString &command, const QString &messageOnError )
34{
35 if ( sInstance )
36 {
37 QgsDebugMsgLevel( "Running " + command, 3 );
38 return sInstance->runCommand( command, messageOnError );
39 }
40 else
41 {
42 QgsDebugError( u"Unable to run Python command: runner not available!"_s );
43 return false;
44 }
45}
46
47bool QgsPythonRunner::runFile( const QString &filename, const QString &messageOnError )
48{
49 if ( sInstance )
50 {
51 QgsDebugMsgLevel( "Running " + filename, 3 );
52 return sInstance->runFileCommand( filename, messageOnError );
53 }
54 else
55 {
56 QgsDebugError( u"Unable to run Python command: runner not available!"_s );
57 return false;
58 }
59}
60
61bool QgsPythonRunner::eval( const QString &command, QString &result )
62{
63 if ( sInstance )
64 {
65 return sInstance->evalCommand( command, result );
66 }
67 else
68 {
69 QgsDebugError( u"Unable to run Python command: runner not available!"_s );
70 return false;
71 }
72}
73
74bool QgsPythonRunner::setArgv( const QStringList &arguments, const QString &messageOnError )
75{
76 if ( sInstance )
77 {
78 return sInstance->setArgvCommand( arguments, messageOnError );
79 }
80 else
81 {
82 QgsDebugError( u"Unable to run Python command: runner not available!"_s );
83 return false;
84 }
85}
86
88{
89 delete sInstance;
90 sInstance = runner;
91}
92
Utility class for running Python commands from various parts of QGIS.
static bool setArgv(const QStringList &arguments, const QString &messageOnError=QString())
Set sys.argv.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.
static void setInstance(QgsPythonRunner *runner)
Assign an instance of Python runner so that run() can be used.
static bool eval(const QString &command, QString &result)
Eval a Python statement.
static QgsPythonRunner * sInstance
static bool runFile(const QString &filename, const QString &messageOnError=QString())
Execute a Python filename, showing an error message if one occurred.
static bool isValid()
Returns true if the runner has an instance (and thus is able to run commands).
QgsPythonRunner()=default
Protected constructor: can be instantiated only from children.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59