-
Notifications
You must be signed in to change notification settings - Fork 10
/
script.cpp
285 lines (210 loc) · 7.55 KB
/
script.cpp
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
#ifdef SPROXEL_USE_PYTHON
#include <QtDebug>
#include <QFileInfo>
#include <QDir>
#include <QMessageBox>
#include "MainWindow.h"
#include "script.h"
#include "pyConsole.h"
#include "pyBindings.h"
extern void init_sproxel_bindings();
#define GLUE(a, b) a##b
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
QDir exe_dir;
static PyObject *glue=NULL;
PyObject *py_save_project=NULL, *py_load_project=NULL, *py_init_plugin_pathes=NULL,
*py_scan_plugins=NULL, *py_register_plugins=NULL, *py_unregister_plugins=NULL;
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
#define TOPYT(cls) \
static PyObject * GLUE(cls, _toPy_py) = NULL; \
PyObject * GLUE(cls, _toPy) (class cls *p) \
{ \
if (!p) { Py_RETURN_NONE; } \
if (!GLUE(cls, _toPy_py)) { PyErr_SetString(PyExc_StandardError, "No " #cls "_toPy in SproxelGlue"); return NULL; } \
PyObject *c=PyCapsule_New(p, NULL, NULL); if (!c) return NULL; \
PyObject *w=PyObject_CallFunctionObjArgs(GLUE(cls, _toPy_py), c, NULL); \
Py_DECREF(c); \
return w; \
}
#define TOCPP(cls) \
static PyObject * GLUE(cls, _toCpp_py) = NULL; \
class cls * GLUE(cls, _toCpp) (PyObject *w) \
{ \
if (!w) { PyErr_SetString(PyExc_StandardError, "NULL PyObject in " #cls "_toCpp"); return NULL; } \
if (w==Py_None) return NULL; \
if (!GLUE(cls, _toCpp_py)) { PyErr_SetString(PyExc_StandardError, "No " #cls "_toCpp in SproxelGlue"); return NULL; } \
PyObject *c=PyObject_CallFunctionObjArgs(GLUE(cls, _toCpp_py), w, NULL); if (!c) return NULL; \
void *p=PyCapsule_GetPointer(c, NULL); \
Py_DECREF(c); \
return (class cls*)p; \
}
#include "glue/classGlue.h"
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
void init_script(const char *exe_path)
{
exe_dir=QFileInfo(exe_path).absoluteDir();
qDebug() << "prog name:" << exe_path;
Py_SetProgramName((char*)exe_path);
qDebug() << "init py...";
Py_Initialize();
qDebug() << "init py ok";
init_python_console();
init_sproxel_bindings();
pycon("exe path: %s", exe_path);
pycon("exe dir: %s", exe_dir.absolutePath().toLocal8Bit().data());
#ifndef _WIN32
QString code="import sys\nsys.path.insert(0, \"";
code.append(exe_dir.absolutePath());
code.append("\")\nprint 'sys.path:', sys.path\n");
PyRun_SimpleString(code.toLocal8Bit().data());
#endif
pycon("Importing PySide.SproxelGlue...");
glue=PyImport_ImportModule("PySide.SproxelGlue");
if (!glue)
{
pycon("Failed to import PySide.SproxelGlue");
#ifdef _WIN32
QMessageBox::critical(NULL, "Sproxel Error", "Failed to import PySide.SproxelGlue");
#endif
}
else
{
pycon("Imported PySide.SproxelGlue, getting methods...");
bool gotErrors=false;
#define TOPYT(cls) \
GLUE(cls, _toPy_py ) = PyObject_GetAttrString(glue, #cls "_toPy" ); \
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
#define TOCPP(cls) \
GLUE(cls, _toCpp_py) = PyObject_GetAttrString(glue, #cls "_toCpp"); \
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
#include "glue/classGlue.h"
pycon("Glue methods: %s", gotErrors ? "some missing" : "all OK");
if (gotErrors) QMessageBox::critical(NULL, "Sproxel Error", "Some PySide.SproxelGlue methods are missing.");
}
PyObject *mod=PyImport_ImportModule("sproxel_utils");
if (!mod)
{
PyErr_Print();
QMessageBox::critical(NULL, "Sproxel Error", "Failed to import sproxel_utils");
}
else
{
// check required methods
bool gotErrors=false;
py_save_project=PyObject_GetAttrString(mod, "save_project");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_load_project=PyObject_GetAttrString(mod, "load_project");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_init_plugin_pathes=PyObject_GetAttrString(mod, "init_plugin_pathes");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_scan_plugins=PyObject_GetAttrString(mod, "scan_plugins");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_register_plugins=PyObject_GetAttrString(mod, "register_plugins");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
py_unregister_plugins=PyObject_GetAttrString(mod, "unregister_plugins");
if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }
pycon("Scripted methods: %s", gotErrors ? "some missing" : "all OK");
if (gotErrors) QMessageBox::critical(NULL, "Sproxel Error", "Some scripted methods are missing.");
Py_DECREF(mod); mod=NULL;
}
}
void close_script()
{
close_python_console();
Py_XDECREF(py_unregister_plugins); py_unregister_plugins=NULL;
Py_XDECREF(py_register_plugins); py_register_plugins=NULL;
Py_XDECREF(py_scan_plugins); py_scan_plugins=NULL;
Py_XDECREF(py_init_plugin_pathes); py_init_plugin_pathes=NULL;
Py_XDECREF(py_save_project); py_save_project=NULL;
Py_XDECREF(py_load_project); py_load_project=NULL;
#define TOPYT(cls) Py_XDECREF(GLUE(cls, _toPy_py )); GLUE(cls, _toPy_py ) = NULL;
#define TOCPP(cls) Py_XDECREF(GLUE(cls, _toCpp_py)); GLUE(cls, _toCpp_py) = NULL;
#include "glue/classGlue.h"
Py_XDECREF(glue); glue=NULL;
qDebug() << "fin py...";
Py_Finalize();
qDebug() << "fin py ok";
}
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
void script_set_main_window(MainWindow *mwin)
{
pycon("Adding Sproxel objects to script...");
PyObject *mod=PyImport_AddModule("sproxel");
PyObject *o;
bool gotErrors=false;
o=QMainWindow_toPy(mwin);
if (!o) { gotErrors=true; PyErr_Print(); o=Py_None; Py_INCREF(o); }
PyModule_AddObject(mod, "main_window", o);
o=PyList_New(1);
PyList_SetItem(o, 0, qstr_to_py(exe_dir.absoluteFilePath("plugins")));
PyModule_AddObject(mod, "plugin_pathes", o);
PyModule_AddObject(mod, "undo", undo_manager_to_py(mwin->undoManager()));
//== TODO: add more components to script
// init plugin pathes
if (py_init_plugin_pathes)
{
PyObject *res=PyObject_CallFunctionObjArgs(py_init_plugin_pathes, NULL);
if (!res) PyErr_Print();
else Py_DECREF(res);
}
pycon("Sproxel objects: %s", gotErrors ? "FAILED to add to script" : "all added");
}
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
void scan_plugins()
{
if (py_scan_plugins)
{
PyObject *res=PyObject_CallFunctionObjArgs(py_scan_plugins, NULL);
if (!res) PyErr_Print();
else Py_DECREF(res);
}
}
void register_plugins()
{
if (py_register_plugins)
{
PyObject *res=PyObject_CallFunctionObjArgs(py_register_plugins, NULL);
if (!res) PyErr_Print();
else Py_DECREF(res);
}
}
void unregister_plugins()
{
if (py_unregister_plugins)
{
PyObject *res=PyObject_CallFunctionObjArgs(py_unregister_plugins, NULL);
if (!res) PyErr_Print();
else Py_DECREF(res);
}
}
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
bool run_script(const QString &fn)
{
QString filename=exe_dir.filePath(fn);
#ifdef _WIN32
FILE *file=_wfopen(filename.utf16(), L"r");
#else
FILE *file=fopen(filename.toUtf8(), "r");
#endif
if (file)
{
pycon("Starting script %S", filename.unicode());
PyObject *mod=PyImport_AddModule("__main__");
PyObject *modDict=PyModule_GetDict(mod);
PyObject *o=PyRun_File(file, qPrintable(filename), Py_file_input, modDict, modDict);
fclose(file);
Py_XDECREF(o);
if (PyErr_Occurred())
{
PyErr_Print();
return false;
}
return true;
}
else
{
pycon("Failed to open script file %S", filename.unicode());
return false;
}
}
#endif