1
/***************************************************************************
2
File : PythonScripting.h
4
--------------------------------------------------------------------
5
Copyright : (C) 2006 by Knut Franke
6
Email (use @ for *) : knut.franke*gmx.de
7
Description : Execute Python code from within SciDAVis
9
***************************************************************************/
11
/***************************************************************************
13
* This program is free software; you can redistribute it and/or modify *
14
* it under the terms of the GNU General Public License as published by *
15
* the Free Software Foundation; either version 2 of the License, or *
16
* (at your option) any later version. *
18
* This program is distributed in the hope that it will be useful, *
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21
* GNU General Public License for more details. *
23
* You should have received a copy of the GNU General Public License *
24
* along with this program; if not, write to the Free Software *
25
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26
* Boston, MA 02110-1301 USA *
28
***************************************************************************/
29
#ifndef PYTHON_SCRIPTING_H
30
#define PYTHON_SCRIPTING_H
32
#include "ScriptingEnv.h"
33
#include "PythonScript.h"
38
typedef struct _object PyObject;
40
class PythonScripting: public ScriptingEnv
45
static const char *langName;
46
PythonScripting(ApplicationWindow *parent);
48
static ScriptingEnv *constructor(ApplicationWindow *parent) { return new PythonScripting(parent); }
51
void write(const QString &text) { emit print(text); }
53
//! like str(object) in Python
55
* Convert object to a string.
56
* Steals a reference to object if decref is true; borrows otherwise.
58
QString toString(PyObject *object, bool decref=false);
59
//! evaluate a Python expression
61
* Evaluates code, using argDict (borrowed reference) as local dictionary
62
* or an empty one if argDict==NULL. name is the filename Python uses when
63
* reporting errors. Returns a new reference; NULL means caller has to do
66
PyObject* eval(const QString &code, PyObject *argDict=NULL, const char *name="<scidavis>");
67
//! execute a sequence of Python statements
69
* Executes code, using argDict (borrowed reference) as local dictionary
70
* or an empty one if argDict==NULL. name is the filename Python uses when
71
* reporting errors. A false return value means caller has to do exception
74
bool exec(const QString &code, PyObject *argDict=NULL, const char *name="<scidavis>");
77
bool isRunning() const;
78
Script *newScript(const QString &code, QObject *context, const QString &name="<input>")
80
return new PythonScript(this, code, context, name);
83
bool setQObject(QObject*, const char*, PyObject *dict);
84
bool setQObject(QObject *val, const char *name) { return setQObject(val,name,NULL); }
85
bool setInt(int, const char*, PyObject *dict=NULL);
86
bool setDouble(double, const char*, PyObject *dict=NULL);
88
const QStringList mathFunctions() const;
89
const QString mathFunctionDoc (const QString &name) const;
90
const QStringList fileExtensions() const;
92
PyObject *globalDict() { return globals; }
93
PyObject *sysDict() { return sys; }
96
bool loadInitFile(const QString &path);
98
PyObject *globals; // PyDict of global environment
99
PyObject *math; // PyDict of math functions
100
PyObject *sys; // PyDict of sys module