QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
20
22// static methods
23
25{
26 return nullptr != sInstance;
27}
28
29bool QgsPythonRunner::run( const QString &command, const QString &messageOnError )
30{
31 if ( sInstance )
32 {
33 QgsDebugMsgLevel( "Running " + command, 3 );
34 return sInstance->runCommand( command, messageOnError );
35 }
36 else
37 {
38 QgsDebugError( QStringLiteral( "Unable to run Python command: runner not available!" ) );
39 return false;
40 }
41}
42
43bool QgsPythonRunner::runFile( const QString &filename, const QString &messageOnError )
44{
45 if ( sInstance )
46 {
47 QgsDebugMsgLevel( "Running " + filename, 3 );
48 return sInstance->runFileCommand( filename, messageOnError );
49 }
50 else
51 {
52 QgsDebugError( QStringLiteral( "Unable to run Python command: runner not available!" ) );
53 return false;
54 }
55}
56
57bool QgsPythonRunner::eval( const QString &command, QString &result )
58{
59 if ( sInstance )
60 {
61 return sInstance->evalCommand( command, result );
62 }
63 else
64 {
65 QgsDebugError( QStringLiteral( "Unable to run Python command: runner not available!" ) );
66 return false;
67 }
68}
69
70bool QgsPythonRunner::setArgv( const QStringList &arguments, const QString &messageOnError )
71{
72 if ( sInstance )
73 {
74 return sInstance->setArgvCommand( arguments, messageOnError );
75 }
76 else
77 {
78 QgsDebugError( QStringLiteral( "Unable to run Python command: runner not available!" ) );
79 return false;
80 }
81}
82
84{
85 delete sInstance;
86 sInstance = runner;
87}
88
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:61
#define QgsDebugError(str)
Definition qgslogger.h:57