File: pyremotequery.h

package info (click to toggle)
utopia-documents 2.4.4-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 30,560 kB
  • ctags: 24,084
  • sloc: cpp: 179,735; ansic: 16,208; python: 13,446; xml: 1,937; sh: 1,918; ruby: 1,594; makefile: 527; sql: 6
file content (232 lines) | stat: -rw-r--r-- 7,981 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*****************************************************************************
 *  
 *   This file is part of the Utopia Documents application.
 *       Copyright (c) 2008-2014 Lost Island Labs
 *           <info@utopiadocs.com>
 *   
 *   Utopia Documents is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU GENERAL PUBLIC LICENSE VERSION 3 as
 *   published by the Free Software Foundation.
 *   
 *   Utopia Documents 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.
 *   
 *   In addition, as a special exception, the copyright holders give
 *   permission to link the code of portions of this program with the OpenSSL
 *   library under certain conditions as described in each individual source
 *   file, and distribute linked combinations including the two.
 *   
 *   You must obey the GNU General Public License in all respects for all of
 *   the code used other than OpenSSL. If you modify file(s) with this
 *   exception, you may extend this exception to your version of the file(s),
 *   but you are not obligated to do so. If you do not wish to do so, delete
 *   this exception statement from your version.
 *   
 *   You should have received a copy of the GNU General Public License
 *   along with Utopia Documents. If not, see <http://www.gnu.org/licenses/>
 *  
 *****************************************************************************/

#include <Python.h>

#include <athenaeum/remotequery.h>
#include <string>
#include <iostream>

#include <boost/python.hpp>
#include <boost/mpl/vector.hpp>

#include "conversion.h"

#include <QRunnable>
#include <QWeakPointer>

#include <QDebug>



namespace python = boost::python;
namespace mpl = boost::mpl;



class PyRemoteQuery : public Athenaeum::RemoteQuery, public PyExtension
{
public:
    PyRemoteQuery(std::string extensionClassName);
    ~PyRemoteQuery();

    bool fetch(const QVariantMap & query, int offset, int limit);
    QString description();
    void run();
    QString title();

    void del_property(python::object key);
    python::object get_property(python::object key, python::object def = python::object());
    void set_property(python::object key, python::object value);

protected:
    long _thread_id;
    QVariantMap _query;
    int _offset;
    int _limit;
};




PyRemoteQuery::PyRemoteQuery(std::string extensionClassName)
    : Athenaeum::RemoteQuery(), PyExtension("utopia.library.RemoteQuery", extensionClassName), _thread_id(0)
{
    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();

    if (extensionObject()) {
        // Use boost::python to attach a method to the extension instance
        python::scope outer(python::object(python::handle<>(python::borrowed(extensionObject()))));
        python::def("get_property", python::make_function(bind(&PyRemoteQuery::get_property, this, _1, python::object()), python::default_call_policies(), mpl::vector< python::object, python::object >()));
        python::def("get_property", python::make_function(bind(&PyRemoteQuery::get_property, this, _1, _2), python::default_call_policies(), mpl::vector< python::object, python::object, python::object >()));
        python::def("set_property", python::make_function(bind(&PyRemoteQuery::set_property, this, _1, _2), python::default_call_policies(), mpl::vector< void, python::object, python::object >()));
        python::def("del_property", python::make_function(bind(&PyRemoteQuery::del_property, this, _1), python::default_call_policies(), mpl::vector< void, python::object >()));
    }

    PyGILState_Release(gstate);
}

PyRemoteQuery::~PyRemoteQuery()
{
    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();

    if (_thread_id > 0) {
        PyObject * exc = PyErr_NewException((char *) "utopia.Cancellation", 0, 0);
        int count = PyThreadState_SetAsyncExc(_thread_id, exc);
        _thread_id = 0;
    }

    PyGILState_Release(gstate);

    wait();
}

QString PyRemoteQuery::description()
{
    // FIXME get from somewhere else
    return QString::fromStdString(extensionDocString());
}

bool PyRemoteQuery::fetch(const QVariantMap & query, int offset, int limit)
{
    bool success = false;
    if (extensionObject())
    {
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        // Make sure fetch() is present and callable
        if (PyObject_HasAttrString(extensionObject(), "fetch") && PyCallable_Check(PyObject_GetAttrString(extensionObject(), "fetch"))) {
            _query = query;
            _offset = offset;
            _limit = limit;
            start();
            success = true;
        }

        PyGILState_Release(gstate);
    }
    return success;
}

void PyRemoteQuery::run()
{
    // Only bother to execute the query if the calling object still exists
    Athenaeum::RemoteQueryResultSet resultSet;

    bool success = false;
    if (extensionObject())
    {
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        PyObject * threadName = PyString_FromString("thread");
        PyObject * thread = PyImport_Import(threadName);
        Py_DECREF(threadName);
        PyObject * get_ident = PyObject_GetAttrString(thread, "get_ident");
        PyObject * ident = PyObject_CallObject(get_ident, 0);
        Py_DECREF(get_ident);
        _thread_id = PyInt_AsLong(ident);
        Py_DECREF(ident);

        // Make sure fetch() is present and callable
        if (PyObject_HasAttrString(extensionObject(), "fetch") && PyCallable_Check(PyObject_GetAttrString(extensionObject(), "fetch"))) {
            /* Invoke method on extension */
            PyObject * pyquery = convert(_query);
            if (pyquery) {
                PyObject * ret = PyObject_CallMethod(extensionObject(), (char *) "fetch", (char *) "(Oii)", pyquery, _offset, _limit);

                if (ret == 0) /* Exception*/ {
                    std::cerr << "Error in remote query " << extensionTypeName() << std::endl;
                    PyErr_PrintEx(0);
                } else {
                    PyObject * results;
                    if (ret == Py_None) {
                        success = true;
                    } else if (PyArg_ParseTuple(ret, "iiiO", &resultSet.offset, &resultSet.limit, &resultSet.count, &results)) {
                        resultSet.results = convert(results).toList();
                        success = true;
                    } else {
                        // FIXME error
                    }

                    Py_DECREF(ret);
                }
                Py_DECREF(pyquery);
            }
        }

        _thread_id = 0;

        PyGILState_Release(gstate);

        // Only bother to notify of the query's completion if the calling object still exists
        if (success) {
            qRegisterMetaType< Athenaeum::RemoteQueryResultSet >();
            emit fetched(resultSet);
        } else {
            // FIXME
        }
    }
}

void PyRemoteQuery::del_property(python::object key)
{
    //qDebug() << "del_property" << _thread_id << this;
    // Del value
    setPersistentProperty(convert(key).toString(), QVariant());
}

python::object PyRemoteQuery::get_property(python::object key, python::object def)
{
    //qDebug() << "get_property" << _thread_id << this;
    // Get value
    python::object value(def);
    PyObject * valueObj = convert(persistentProperty(convert(key).toString()));
    if (valueObj != Py_None) {
        value = python::object(python::handle<>(valueObj));
    }
    return value;
}

void PyRemoteQuery::set_property(python::object key, python::object value)
{
    //qDebug() << "set_property" << _thread_id << this;
    // Set value
    setPersistentProperty(convert(key).toString(), convert(value));
}

QString PyRemoteQuery::title()
{
    return QString::fromStdString(extensionDocString());
}