2
* pythonscriptingplugin.h - Psi plugin providing Python scripting
3
* Copyright (C) 2006 Kevin Smith
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* You can also redistribute and/or modify this program under the
11
* terms of the Psi License, specified in the accompanied COPYING
12
* file, as published by the Psi Project; either dated January 1st,
13
* 2005, or (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31
#include "psiplugin.h"
33
extern int Py_VerboseFlag;
36
* Function to obtain all the directories in which plugins can be stored
37
* \return List of plugin directories
39
static QStringList scriptDirs()
42
l += "/Users/kismith/.psi/python";
46
class PythonPlugin : public PsiPlugin
49
Q_INTERFACES(PsiPlugin)
54
virtual QString name() const;
55
virtual QString shortName() const;
56
virtual QString version() const;
57
virtual bool processEvent( const QString& account, QDomNode &event );
60
QStringList scriptObjects_;
62
QString loadScript( const QString& fileName);
63
PyObject* main_module_;
68
Q_EXPORT_PLUGIN(PythonPlugin);
70
PythonPlugin::PythonPlugin()
73
main_module_ = PyImport_AddModule("__main__");
74
main_dict_ = PyModule_GetDict(main_module_);
75
QString command="print \"Python running\"";
76
PyRun_SimpleString(qPrintable(command));
81
PythonPlugin::~PythonPlugin()
86
QString PythonPlugin::name() const
88
return "Python Scripting Plugin";
91
QString PythonPlugin::shortName() const
96
QString PythonPlugin::version() const
101
bool PythonPlugin::processEvent( const QString& account, QDomNode &event )
103
foreach (QString script, scriptObjects_)
105
qDebug() << (qPrintable(QString("running it on script %1").arg(script)));
106
QString scriptCall=QString("%1.processEvent(\"\"\"%2\"\"\")").arg(script).arg(toString(event));
107
QString command=QString("%1").arg(scriptCall);
108
qDebug() << (qPrintable(QString("Running python command:\n%1").arg(command)));
109
PyRun_SimpleString(qPrintable(command));
114
void PythonPlugin::loadScripts()
116
foreach(QString d, scriptDirs()) {
118
foreach(QString file, dir.entryList()) {
119
file=dir.absoluteFilePath(file);
120
if (file.contains(".py"))
126
QString PythonPlugin::loadScript(const QString& fileName)
129
if ((file = fopen(qPrintable(fileName),"r")) == NULL )
131
qDebug() << (qPrintable(QString("Found script file %1").arg(fileName)));
132
PyObject* pyName = PyRun_File(file, qPrintable(fileName), Py_file_input, main_dict_, main_dict_);
133
qDebug("well, we got this far");
135
pyName = PyDict_GetItemString( main_dict_, "name" );
136
if (pyName == NULL || !PyString_Check(pyName))
138
qWarning(qPrintable(QString("Tried to load %1 but it didn't return a string for its name").arg(fileName)));
141
QString name( PyString_AsString( pyName ) );
142
scriptObjects_.append(name);
143
qDebug() << (qPrintable(QString("Found script %1 in the file").arg(name)));
147
#include "pythonplugin.moc"