File: pythonplugin.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (128 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * pythonscriptingplugin.h - Psi plugin providing Python scripting
 * Copyright (C) 2006  Kevin Smith
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "psiplugin.h"

#include <Python.h>
#include <QDebug>
#include <QtCore>

extern int Py_VerboseFlag;

/**
 * Function to obtain all the directories in which plugins can be stored
 * \return List of plugin directories
 */
static QStringList scriptDirs()
{
    QStringList l;
    l += "/Users/kismith/.psi/python";
    return l;
}

class PythonPlugin : public PsiPlugin {
    Q_OBJECT
    Q_INTERFACES(PsiPlugin)

public:
    PythonPlugin();
    ~PythonPlugin();
    virtual QString name() const;
    virtual QString shortName() const;
    virtual QString version() const;
    virtual bool    processEvent(const QString &account, QDomNode &event);

private:
    QStringList scriptObjects_;
    void        loadScripts();
    QString     loadScript(const QString &fileName);
    PyObject *  main_module_;
    PyObject *  main_dict_;
};

Q_EXPORT_PLUGIN(PythonPlugin);

PythonPlugin::PythonPlugin()
{
    Py_Initialize();
    main_module_    = PyImport_AddModule("__main__");
    main_dict_      = PyModule_GetDict(main_module_);
    QString command = "print \"Python running\"";
    PyRun_SimpleString(qPrintable(command));
    loadScripts();
}

PythonPlugin::~PythonPlugin() { Py_Finalize(); }

QString PythonPlugin::name() const { return "Python Scripting Plugin"; }

QString PythonPlugin::shortName() const { return "python"; }

QString PythonPlugin::version() const { return "0.0"; }

bool PythonPlugin::processEvent(const QString &account, QDomNode &event)
{
    foreach (QString script, scriptObjects_) {
        qDebug() << (qPrintable(QString("running it on script %1").arg(script)));
        QString scriptCall = QString("%1.processEvent(\"\"\"%2\"\"\")").arg(script).arg(toString(event));
        QString command    = QString("%1").arg(scriptCall);
        qDebug() << (qPrintable(QString("Running python command:\n%1").arg(command)));
        PyRun_SimpleString(qPrintable(command));
    }
    return true;
}

void PythonPlugin::loadScripts()
{
    foreach (QString d, scriptDirs()) {
        QDir dir(d);
        foreach (QString file, dir.entryList()) {
            file = dir.absoluteFilePath(file);
            if (file.contains(".py"))
                loadScript(file);
        }
    }
}

QString PythonPlugin::loadScript(const QString &fileName)
{
    FILE *file;
    if ((file = fopen(qPrintable(fileName), "r")) == NULL)
        return "";
    qDebug() << (qPrintable(QString("Found script file %1").arg(fileName)));
    PyObject *pyName = PyRun_File(file, qPrintable(fileName), Py_file_input, main_dict_, main_dict_);
    qDebug("well, we got this far");
    fclose(file);
    pyName = PyDict_GetItemString(main_dict_, "name");
    if (pyName == NULL || !PyString_Check(pyName)) {
        qWarning(qPrintable(QString("Tried to load %1 but it didn't return a string for its name").arg(fileName)));
        return "";
    }
    QString name(PyString_AsString(pyName));
    scriptObjects_.append(name);
    qDebug() << (qPrintable(QString("Found script %1 in the file").arg(name)));
    return name;
}

#include "pythonplugin.moc"