summaryrefslogtreecommitdiffstats
path: root/src/extension/script/InkscapePython.cpp
blob: c5835441bda4a29dac88c85353fbf8a3363e9fe1 (plain)
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/**
 * Python Interpreter wrapper for Inkscape
 *
 * Authors:
 *   Bob Jamison <ishmalius@gmail.com>
 *
 * Copyright (C) 2004-2007 Authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */


#include <Python.h>

#include "InkscapePython.h"


#include <stdio.h>
#include "CXX/Extensions.hxx"
#include "CXX/Objects.hxx"


#include <inkscape.h>
#include <desktop.h>
#include <document.h>
#include <ui/dialog/dialog-manager.h>

//only for sp_help_about()
#include "help.h"

namespace Inkscape
{
namespace Extension
{
namespace Script
{




//########################################################################
//# D I A L O G    M A N A G E R
//########################################################################

class PyDialogManager : public Py::PythonExtension<PyDialogManager>
{
public:

    PyDialogManager(Inkscape::UI::Dialog::DialogManager *dm) :
                            dialogManager(dm)
        {
        }

    virtual ~PyDialogManager()
        {
        }

    virtual Py::Object getattr(const char *name)
        {
        /*
        if (strcmp(name, "activeDesktop")==0)
            {
            PyDesktop obj(SP_ACTIVE_DESKTOP);
            return obj;
            }
        */
        return getattr_methods(name);
        }
        
    virtual Py::Object showAbout(const Py::Tuple &args)
        {
        sp_help_about();
        return Py::Nothing();
        }


    static void init_type()
        {
        behaviors().name("DialogManager");
        behaviors().doc("dialogManager objects: ");
        behaviors().supportRepr();
        behaviors().supportGetattr();

        add_varargs_method("showAbout", &PyDialogManager::showAbout,
             "Shows a silly dialog");
        }

private:

    Inkscape::UI::Dialog::DialogManager *dialogManager;
};



//########################################################################
//# D E S K T O P
//########################################################################

class PyDesktop : public Py::PythonExtension<PyDesktop>
{
public:

    PyDesktop(SPDesktop *dt) : desktop(dt)
        {
        }

    virtual ~PyDesktop()
        {
        }

    virtual Py::Object getattr(const char *name)
        {
        if (strcmp(name, "dialogManager")==0)
            {
            Py::Object obj(Py::asObject(
                  new PyDialogManager(desktop->_dlg_mgr)));
            return obj;
            }
        return getattr_methods(name);
        }
        
    static void init_type()
        {
        behaviors().name("Desktop");
        behaviors().doc("desktop objects: dialogManager");
        behaviors().supportRepr();
        behaviors().supportGetattr();
        }



private:

    SPDesktop *desktop;

};



//########################################################################
//# D O C U M E N T
//########################################################################


class PyDocument : public Py::PythonExtension<PyDocument>
{
public:

    PyDocument(SPDocument *doc) : document(doc)
        {
        }

    virtual ~PyDocument()
        {
        }

    virtual Py::Object getattr(const char *name)
        {
        return getattr_methods(name);
        }
        

    static void init_type()
        {
        behaviors().name("Document");
        behaviors().doc("document objects: ");
        behaviors().supportRepr();
        behaviors().supportGetattr();
        }

private:

    SPDocument *document;
};







//########################################################################
//# I N K S C A P E    and siblings
//#
//# The following are children of PyInkscapeModule and are spawned
//# by its methods.  The classes above are spawned by PyInkscape and
//# it descendants.
//#
//########################################################################


class PyInkscape : public Py::PythonExtension<PyInkscape>
{
public:

    PyInkscape(InkscapePython &par) : parent(par)
        {
        inkscape = INKSCAPE;
        }
        
    virtual ~PyInkscape()
        {
        }


    virtual Py::Object getattr(const char *name)
        {
        if (strcmp(name, "activeDesktop")==0)
            {
            Py::Object obj(Py::asObject(
                  new PyDesktop(SP_ACTIVE_DESKTOP)));
            return obj;
            }
        else if (strcmp(name, "activeDocument")==0)
            {
            Py::Object obj(Py::asObject(
                  new PyDocument(SP_ACTIVE_DOCUMENT)));
            return obj;
            }
        return getattr_methods(name);
        }
        
    virtual Py::Object exit(const Py::Tuple &args)
        {
        //exit();
        return Py::Nothing(); //like a void
        }

    virtual Py::Object hello(const Py::Tuple &args)
        {
        //exit();
        //throw Py::RuntimeError("some error message");        
        return Py::String("Hello, world!");
        }

    static void init_type()
        {
        behaviors().name("Inkscape");
        behaviors().doc("inkscape objects: activeDesktop activeDocument");
        behaviors().supportRepr();
        behaviors().supportGetattr();

        add_varargs_method("hello", &PyInkscape::hello,
             "Does a hello, world");
        add_varargs_method("exit", &PyInkscape::exit,
             "exit from the current application");
        }

private:

    InkscapePython &parent;

    Inkscape::Application *inkscape;

};




//########################################################################
//# O U T P U T S
//########################################################################



class PyStdOut : public Py::PythonExtension<PyStdOut>
{
public:

    PyStdOut(InkscapePython &par) : parent(par)
        {
        }
        
    virtual ~PyStdOut()
        {
        }

        
    virtual Py::Object write(const Py::Tuple &args)
        {
        for(unsigned int i=0 ; i<args.length() ; i++)
            {
            Py::String str(args[i]);
            parent.writeStdOut(str.as_std_string());
            }
        return Py::Nothing();
        }

    static void init_type()
        {
        behaviors().name("PyStdOut");

        add_varargs_method("write", &PyStdOut::write,
             "redirects stdout");
        }

private:

    InkscapePython &parent;

};




class PyStdErr : public Py::PythonExtension<PyStdErr>
{
public:

    PyStdErr(InkscapePython &par) : parent(par)
        {
        }
        
    virtual ~PyStdErr()
        {
        }

        
    virtual Py::Object write(const Py::Tuple &args)
        {
        for(unsigned int i=0 ; i<args.length() ; i++)
            {
            Py::String str(args[i]);
            parent.writeStdErr(str.as_std_string());
            }
        return Py::Nothing();
        }

    static void init_type()
        {
        behaviors().name("PyStdErr");

        add_varargs_method("write", &PyStdErr::write,
             "redirects stderr");
        }

private:

    InkscapePython &parent;


};



//########################################################################
//# M O D U L E
//########################################################################



class PyInkscapeModule : public Py::ExtensionModule<PyInkscapeModule>
{
public:
    PyInkscapeModule(InkscapePython &par)
        : Py::ExtensionModule<PyInkscapeModule>( "PyInkscapeModule" ),
          parent(par)
        {
        //# Init our module's classes
        PyInkscape::init_type();
        PyDocument::init_type();
        PyDesktop::init_type();
        PyDialogManager::init_type();
        PyStdOut::init_type();
        PyStdErr::init_type();

        add_varargs_method("getInkscape", 
            &PyInkscapeModule::getInkscape, "returns global inkscape app");
        add_varargs_method("getStdOut", 
            &PyInkscapeModule::getStdOut, "gets redirected output");
        add_varargs_method("getStdErr", 
            &PyInkscapeModule::getStdErr, "gets redirected output");

        initialize( "main Inkscape module" );
        }

    virtual ~PyInkscapeModule()
        {
        }

    virtual Py::Object getInkscape(const Py::Tuple &args)
        {
        Py::Object obj(Py::asObject(new PyInkscape(parent)));
        return obj;
        }
    virtual Py::Object getStdOut(const Py::Tuple &args)
        {
        Py::Object obj(Py::asObject(new PyStdOut(parent)));
        return obj;
        }
    virtual Py::Object getStdErr(const Py::Tuple &args)
        {
        Py::Object obj(Py::asObject(new PyStdErr(parent)));
        return obj;
        }

private:

    InkscapePython &parent;


};



//########################################################################
//# M A I N
//########################################################################




/**
 *  Interpret an in-memory string
 */
bool InkscapePython::interpretScript(const Glib::ustring &script,
          Glib::ustring &output, Glib::ustring &error)
{

    stdOut.clear();
    stdErr.clear();

    //## First bind our classes
    Py_Initialize();
    

    //# Init our custom objects
    PyInkscapeModule inkscapeModule(*this);

    PyObject *globalMod  = PyImport_AddModule("__main__");
    PyObject *globalDict = PyModule_GetDict(globalMod);
    PyObject *localDict  = inkscapeModule.moduleDictionary().ptr();

    Glib::ustring buf =
    "import sys\n"
    "sys.stdout = getStdOut()\n"
    "sys.stderr = getStdErr()\n"
    "\n"
    "inkscape = getInkscape()\n"
    "\n";
    buf.append(script);


    char *codeStr = (char *)buf.c_str();
    PyRun_String(codeStr, Py_file_input, globalDict, localDict);

    output = stdOut;
    //output = "hello, world\n";
    error  = stdErr;


    //## Check for errors
    if (PyErr_Occurred())
        {
        PyObject *errtype      = NULL;
        PyObject *errval       = NULL;
        PyObject *errtraceback = NULL;

        PyErr_Fetch(&errtype, &errval, &errtraceback);
        //PyErr_Clear();

        if (errval && PyString_Check(errval))
            {
            PyObject *pystring = PyObject_Str(errval);
            char *errStr = PyString_AsString(pystring);
            int line = ((PyTracebackObject*)errtraceback)->tb_lineno;
            //error = "Line ";
            //error.append(line);
            //error.append(" : ");
            error.append(errStr);
            Py_XDECREF(pystring);
            }
        else
            {
            error = "Error occurred";
            }
        Py_XDECREF(errtype);
        Py_XDECREF(errval);
        Py_XDECREF(errtraceback);
        Py_Finalize();
        return false;
        }


    Py_Finalize();

    return true;
}







}  // namespace Script
}  // namespace Extension
}  // namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :