File: pyannotator.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 (487 lines) | stat: -rw-r--r-- 18,190 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*****************************************************************************
 *  
 *   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 <papyro/annotator.h>
#include <papyro/utils.h>
#include <spine/spineapi.h>
#include <spine/spineapi_internal.h>
#include <utopia2/configuration.h>
#include <utopia2/busagent.h>

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

#include "conversion.h"
#include "spine/pyspineapi.h"

#include <string>
#include <iostream>

#include <QDebug>
#include <QVariant>

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




static QMap< QString, QString > event_name_to_legacy_method_name;

QString event_name_to_method_name(const QString & event)
{
    QRegExp parse("(?:(\\w+):)?(\\w+)");
    QString name;
    if (parse.exactMatch(event)) {
        QString timing(parse.cap(1));
        QString type(parse.cap(2));
        if (timing.isEmpty()) { timing = "on"; }
        name = QString("%1_%2_event").arg(timing).arg(type);
    }
    return name;
}


class PyAnnotator : public Papyro::Annotator, public PyExtension
{
public:
    PyAnnotator(std::string extensionClassName)
        : PyExtension("utopia.document.Annotator", extensionClassName)
    {
        if (event_name_to_legacy_method_name.isEmpty()) {
            event_name_to_legacy_method_name["on:load"] = "prepare";
            event_name_to_legacy_method_name["after:load"] = "reducePrepare";
            event_name_to_legacy_method_name["on:ready"] = "populate";
            event_name_to_legacy_method_name["after:ready"] = "reducePopulate";
            event_name_to_legacy_method_name["on:filter"] = "filter";
            event_name_to_legacy_method_name["after:filter"] = "reduceFilter";
            event_name_to_legacy_method_name["on:activate"] = "annotate";
            event_name_to_legacy_method_name["after:activate"] = "reduceAnnotate";
            event_name_to_legacy_method_name["on:marshal"] = "marshal";
            event_name_to_legacy_method_name["after:marshal"] = "reduceMarshal";
            event_name_to_legacy_method_name["on:persist"] = "persist";
            event_name_to_legacy_method_name["on:explore"] = "lookup";
        }




        // Acquire Python's global interpreter lock
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        // Ensure the extension object instantiated correctly, then tailor this object
        if (extensionObject()) {
            // Get UUID
            if (PyObject * uuidret = PyObject_CallMethod(extensionObject(), (char *) "uuid", NULL)) {
                _uuid = PyString_AsString(uuidret);
                Py_DECREF(uuidret);

                // Use boost::python to attach a method to the extension instance
                python::scope outer(python::object(python::handle<>(python::borrowed(extensionObject()))));
                python::def("get_config", python::make_function(bind(&PyAnnotator::get_config, this, _1, python::object()), python::default_call_policies(), mpl::vector< python::object, python::object >()));
                python::def("get_config", python::make_function(bind(&PyAnnotator::get_config, this, _1, _2), python::default_call_policies(), mpl::vector< python::object, python::object, python::object >()));
                python::def("set_config", python::make_function(bind(&PyAnnotator::set_config, this, _1, _2), python::default_call_policies(), mpl::vector< void, python::object, python::object >()));
                python::def("del_config", python::make_function(bind(&PyAnnotator::del_config, this, _1), python::default_call_policies(), mpl::vector< void, python::object >()));
            }

            // Get BusId
            if (PyObject * busidret = PyObject_CallMethod(extensionObject(), (char *) "busId", NULL)) {
                _busId = PyString_AsString(busidret);
                Py_DECREF(busidret);

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

            // Get handleable events timing:name/weight
            if (PyObject * eventsret = PyObject_CallMethod(extensionObject(), (char *) "handleableEvents", NULL)) {
                _handleableEvents = convert(eventsret).toStringList();
                foreach (const QString & event, _handleableEvents) {
                    _handleableEventNames << event.mid(0, event.indexOf('/'));
                }
                Py_DECREF(eventsret);
            } else {
                PyErr_Clear();
            }

            // Work out handleable events timing:name
            if (PyObject * dir = PyObject_Dir(extensionObject())) {
                foreach (const QString & attr, convert(dir).toStringList()) {
                    std::string std_attr = Papyro::unicodeFromQString(attr);
                    const char * c_attr = std_attr.c_str();
                    if (PyObject_HasAttrString(extensionObject(), (char *) c_attr)) {
                        if (PyObject * py_attr = PyObject_GetAttrString(extensionObject(), (char *) c_attr)) {
                            QRegExp parse("(before|on|after)_(\\w+)_event");
                            if (PyCallable_Check(py_attr) && parse.exactMatch(attr)) {
                                int weight = 0;
                                if (PyObject * doc = PyObject_GetAttrString(py_attr, (char *) "__doc__")) {
                                    QRegExp parseWeight(".*\\[(?:.+;)?\\s*weight=(-?\\d+)\\s*(?:;.+)?\\].*");
                                    if (parseWeight.exactMatch(convert(doc).toString())) {
                                        weight = parseWeight.cap(1).toInt();
                                    }
                                    Py_DECREF(doc);
                                }

                                QString event(QString("%1:%2").arg(parse.cap(1)).arg(parse.cap(2)));
                                _handleableEventNames << event;
                                event += QString("/%1").arg(weight);
                                _handleableEvents << event;
                            }
                            Py_DECREF(py_attr);
                        }
                    }
                }
                Py_DECREF(dir);
            } else {
                PyErr_PrintEx(0);
            }

            // Register legacy method names to event names
            QMapIterator< QString, QString > liter(event_name_to_legacy_method_name);
            while (liter.hasNext()) {
                liter.next();
                std::string legacy_method_name(Papyro::unicodeFromQString(liter.value()));
                if (PyObject_HasAttrString(extensionObject(), legacy_method_name.c_str()) &&
                    PyCallable_Check(PyObject_GetAttrString(extensionObject(), legacy_method_name.c_str()))) {
                    _handleableLegacyEvents << liter.key();
                }
            }
        }

        // Release Python's global interpreter lock
        PyGILState_Release(gstate);
    }

    bool _annotate(std::string name, Spine::DocumentHandle document, const QVariantMap & kwargs = QVariantMap())
    {
        bool success = true;

        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        PyObject * method = PyString_FromString(name.c_str());

        /* Get python wrapper of document */
        PyObject * pydoc = 0;
        if (document) {
            Document * doc = static_cast<Document *>(malloc(sizeof(Document)));
            doc->_doc = Spine::share_SpineDocument(document, 0);
            doc->_err = SpineError_NoError;
            pydoc = SWIG_NewPointerObj(static_cast<void *>(doc),
                                       SWIG_TypeQuery("_p_Document"),
                                       SWIG_POINTER_OWN);
        }

        if (extensionObject()) {
            PyObject * ret = 0;

            // Build the *args and *kwargs objects for this invocation
            PyObject * pyargs = PyTuple_New(0);
            PyObject * pykwargs = convert(kwargs);

            if (pydoc) {
                // This will overwrite whatever kwarg has been passed in
                PyDict_SetItemString(pykwargs, (char *) "document", pydoc);
            }

            /* Invoke method on extension */
            if (PyObject * callable = PyObject_GetAttrString(extensionObject(), name.c_str())) {
                ret = PyObject_Call(callable, pyargs, pykwargs);
                Py_DECREF(callable);
            }

            Py_DECREF(pyargs);
            Py_DECREF(pykwargs);

            if (ret == 0) { /* Exception*/
                PyObject * ptype = 0;
                PyObject * pvalue = 0;
                PyObject * ptraceback = 0;
                PyErr_Fetch(&ptype, &pvalue, &ptraceback);
                // Set this annotator's error message
                if (pvalue) {
                    PyObject * msg = PyObject_Str(pvalue);
                    setErrorString(PyString_AsString(msg));
                    Py_DECREF(msg);
                } else if (ptype) {
                    PyObject * msg = PyObject_Str(ptype);
                    setErrorString(PyString_AsString(msg));
                    Py_DECREF(msg);
                } else {
                    setErrorString("An unknown error occurred");
                }
                PyErr_Restore(ptype, pvalue, ptraceback);
                PyErr_PrintEx(0);

                success = false;
            } else {
                // Don't care about the return value
                Py_DECREF(ret);
            }
        }

        /*  Clean up */
        Py_XDECREF(pydoc);
        Py_DECREF(method);

        PyGILState_Release(gstate);

        return success;
    }

    bool canHandleEvent(const QString & event)
    {
        foreach (const QString & candidate, handleableEvents()) {
            if (candidate == event ||
                candidate.startsWith(event + "/")) {
                return true;
            }
        }
        return false;
    }

    QStringList handleableEvents()
    {
        QStringList unique(_handleableEvents + _handleableLegacyEvents);
        unique.removeDuplicates();
        return unique;
    }

    bool handleEvent(const QString & event, Spine::DocumentHandle document, const QVariantMap & kwargs)
    {
        // Only attempt events we've registered
        if (_handleableEventNames.contains(event)) {
            QString name(event_name_to_method_name(event));
            return _annotate(Papyro::unicodeFromQString(name), document, kwargs);
        }
        if (_handleableLegacyEvents.contains(event)) {
            // Map event name to legacy method name
            QString legacy(event_name_to_legacy_method_name.value(event));
            return _annotate(Papyro::unicodeFromQString(legacy), document, kwargs);
        }
        return false;
    }

    std::set< Spine::AnnotationHandle > lookup(Spine::DocumentHandle document, const std::string & phrase)
    {
        std::set< Spine::AnnotationHandle > derived;

        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        /* Get python wrapper of document */
        PyObject * pydoc = 0;
        if (document) {
            Document * doc = static_cast<Document *>(malloc(sizeof(Document)));
            doc->_doc = Spine::share_SpineDocument(document, 0);
            doc->_err = SpineError_NoError;
            pydoc = SWIG_NewPointerObj(static_cast<void *>(doc),
                                       SWIG_TypeQuery("_p_Document"),
                                       SWIG_POINTER_OWN);
        }

        /* Get python wrapper of annotation */
        PyObject * input = PyUnicode_DecodeUTF8(phrase.data(), phrase.length(), 0);

        if (input) {
            PyObject * pyargs = PyTuple_New(0);
            PyObject * pykwargs = PyDict_New();
            PyDict_SetItemString(pykwargs, "phrase", input);

            if (pydoc) {
                // This will overwrite whatever kwarg has been passed in
                PyDict_SetItemString(pykwargs, "document", pydoc);
            }

            /* Invoke method on extension */
            PyObject * ret = 0;

            /* Invoke method on extension */
            PyObject * callable = PyObject_GetAttrString(extensionObject(), "on_explore_event");
            if (callable == 0) {
                PyObject * callable = PyObject_GetAttrString(extensionObject(), "lookup");
            }
            if (callable) {
                ret = PyObject_Call(callable, pyargs, pykwargs);
                Py_DECREF(callable);
            }

            Py_DECREF(pyargs);
            Py_DECREF(pykwargs);

            if (ret == 0) { /* Exception */
                PyErr_PrintEx(0);
            } else {
                // ret is now a sequence of annotation objects
                if (PySequence_Check(ret)) {
                    for (Py_ssize_t i = 0; i < PySequence_Size(ret); ++i) {
                        PyObject * pyAnnotation = PySequence_GetItem(ret, i);
                        Annotation * ann = 0;
                        if (SWIG_ConvertPtr(pyAnnotation,
                                            (void**) &ann,
                                            SWIG_TypeQuery("_p_Annotation"),
                                            0) == 0) {
                            derived.insert(ann->_ann->_handle);
                        }
                    }
                } else { /* Not a sequence! */
                    PyErr_PrintEx(0);
                }

                Py_DECREF(ret);
            }
        }

        Py_XDECREF(pydoc);

        PyGILState_Release(gstate);

        return derived;
    }




    //**** Message bus methods ****//

    void resubscribeToBus()
    {
        // Register on message bus
        if (!_busId.isEmpty()) {
            subscribeToBus();
        }
    }

    void receiveFromBus(const QString & sender, const QVariant & data)
    {
        BusAgent::receiveFromBus(sender, data); // DEBUG

        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        PyObject * recipient = convert(sender);
        PyObject * kwargs = convert(data);
        PyObject * method = PyString_FromString("event");
        PyObject * ret = PyObject_CallMethodObjArgs(extensionObject(), method, recipient, kwargs, NULL);
        Py_XDECREF(ret);
        Py_XDECREF(method);
        Py_XDECREF(kwargs);
        Py_XDECREF(recipient);

        PyGILState_Release(gstate);
    }

    QString busId() const
    {
        if (_busId.isEmpty()) {
            QString uuid = QString::fromStdString(_uuid);
            return uuid.mid(1, uuid.size()-2);
        } else {
            return _busId;
        }
    }

    void postToBusFromPython(python::object first, python::object second = python::object())
    {
        QString recipient;
        QVariant data;
        if (second.ptr()) {
            recipient = convert(first).toString();
            data = convert(second);
            postToBus(recipient, data);
        } else {
            data = convert(first);
            postToBus(data);
        }
    }




    //**** Configuration methods ****//

    QUuid configurationId() const
    {
        return _uuid.c_str();
    }

    void del_config(python::object key)
    {
        // Del value
        configuration()->del(convert(key).toString());
    }

    python::object get_config(python::object key, python::object def = python::object())
    {
        // Resolve key
        python::object value(def);
        PyObject * valueObj = convert(configuration()->get(convert(key).toString()));
        if (valueObj != Py_None) {
            value = python::object(python::handle<>(valueObj));
        }
        return value;
    }

    void set_config(python::object key, python::object value)
    {
        // Set value
        configuration()->set(convert(key).toString(), convert(value));
    }




    std::string uuid()
    {
        return _uuid;
    }

    std::string title()
    {
        return extensionDocString();
    }

protected:
    std::string _uuid;
    QString _busId;

    QStringList _handleableEvents;
    QStringList _handleableLegacyEvents;
    QStringList _handleableEventNames;
};


#undef ANNOTATOR_METHOD