| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
#ifdef HAVE_CONFIG_H |
|---|
| 20 |
#include "config.h" |
|---|
| 21 |
#endif |
|---|
| 22 |
|
|---|
| 23 |
#define NO_IMPORT_PYGOBJECT |
|---|
| 24 |
#include "py_midgard.h" |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_replicator_Type; |
|---|
| 28 |
|
|---|
| 29 |
#define REPLICATOR_DEBUG(__name) \ |
|---|
| 30 |
CHECK_MGD; \ |
|---|
| 31 |
CLASS_METHOD_DEBUG(Pymidgard_replicator_Type.tp_name, __name); |
|---|
| 32 |
|
|---|
| 33 |
static int |
|---|
| 34 |
__replicator_constructor(PyGObject *self, PyObject *args, PyObject *kwargs) |
|---|
| 35 |
{ |
|---|
| 36 |
g_warning("FIXME, it's not implemented"); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
static PyObject * |
|---|
| 40 |
pymidgard_replicator_serialize(PyGObject *self, PyObject *args) |
|---|
| 41 |
{ |
|---|
| 42 |
REPLICATOR_DEBUG("serialize"); |
|---|
| 43 |
MgdObject *mobject; |
|---|
| 44 |
PyObject *pobject; |
|---|
| 45 |
MidgardReplicator *mr = NULL; |
|---|
| 46 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 47 |
|
|---|
| 48 |
if(!PyArg_ParseTuple(args, "O", &pobject)) |
|---|
| 49 |
return NULL; |
|---|
| 50 |
|
|---|
| 51 |
gchar *srld = |
|---|
| 52 |
midgard_replicator_serialize(mr, |
|---|
| 53 |
MIDGARD_OBJECT(((PyGObject *)pobject)->obj)); |
|---|
| 54 |
|
|---|
| 55 |
PyObject *ret = Py_BuildValue("s", srld); |
|---|
| 56 |
g_free(srld); |
|---|
| 57 |
|
|---|
| 58 |
return ret; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
static PyObject * |
|---|
| 62 |
pymidgard_replicator_export(PyGObject *self, PyObject *args) |
|---|
| 63 |
{ |
|---|
| 64 |
REPLICATOR_DEBUG("export"); |
|---|
| 65 |
PyObject *pobject; |
|---|
| 66 |
MidgardReplicator *mr = NULL; |
|---|
| 67 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 68 |
|
|---|
| 69 |
if(!PyArg_ParseTuple(args, "O", &pobject)) |
|---|
| 70 |
return NULL; |
|---|
| 71 |
|
|---|
| 72 |
gboolean exported = |
|---|
| 73 |
midgard_replicator_export(mr, |
|---|
| 74 |
MIDGARD_OBJECT(((PyGObject *)pobject)->obj)); |
|---|
| 75 |
|
|---|
| 76 |
if(exported) |
|---|
| 77 |
Py_RETURN_TRUE; |
|---|
| 78 |
|
|---|
| 79 |
Py_RETURN_FALSE; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
static PyObject * |
|---|
| 83 |
pymidgard_replicator_export_purged(PyGObject *self, PyObject *args) |
|---|
| 84 |
{ |
|---|
| 85 |
REPLICATOR_DEBUG("export_purged"); |
|---|
| 86 |
const gchar *classname, *startdate = NULL, *enddate = NULL; |
|---|
| 87 |
MidgardReplicator *mr = NULL; |
|---|
| 88 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 89 |
|
|---|
| 90 |
if(!PyArg_ParseTuple(args, "s|ss", &classname, &startdate, &enddate)) |
|---|
| 91 |
return NULL; |
|---|
| 92 |
|
|---|
| 93 |
MidgardObjectClass *klass = |
|---|
| 94 |
MIDGARD_OBJECT_GET_CLASS_BY_NAME(classname); |
|---|
| 95 |
|
|---|
| 96 |
if(!klass) { |
|---|
| 97 |
PyErr_SetString(PyExc_SystemError, "Can not replicate object. Invalid class name"); |
|---|
| 98 |
return NULL; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
MidgardConnection *mgd = |
|---|
| 102 |
_py_midgard_connection_singleton_get(); |
|---|
| 103 |
|
|---|
| 104 |
gchar *xml = midgard_replicator_export_purged(mr, klass, mgd, startdate, enddate); |
|---|
| 105 |
|
|---|
| 106 |
PyObject *ret = Py_BuildValue("s", xml); |
|---|
| 107 |
g_free(xml); |
|---|
| 108 |
|
|---|
| 109 |
return ret; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
static PyObject * |
|---|
| 113 |
pymidgard_replicator_export_blob(PyGObject *self, PyObject *args) |
|---|
| 114 |
{ |
|---|
| 115 |
REPLICATOR_DEBUG("export_blob"); |
|---|
| 116 |
PyObject *pobject; |
|---|
| 117 |
MidgardReplicator *mr = NULL; |
|---|
| 118 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 119 |
|
|---|
| 120 |
if(!PyArg_ParseTuple(args, "O", &pobject)) |
|---|
| 121 |
return NULL; |
|---|
| 122 |
|
|---|
| 123 |
gchar *xml = midgard_replicator_export_blob(mr, |
|---|
| 124 |
MIDGARD_OBJECT(((PyGObject *)pobject)->obj)); |
|---|
| 125 |
|
|---|
| 126 |
PyObject *ret = Py_BuildValue("s", xml); |
|---|
| 127 |
g_free(xml); |
|---|
| 128 |
|
|---|
| 129 |
return ret; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
static PyObject * |
|---|
| 133 |
pymidgard_replicator_export_media(PyGObject *self, PyObject *args) |
|---|
| 134 |
{ |
|---|
| 135 |
REPLICATOR_DEBUG("export_media"); |
|---|
| 136 |
g_warning("FIXME, it's not implemented"); |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
static PyObject * |
|---|
| 140 |
pymidgard_replicator_unserialize(PyGObject *self, PyObject *args) |
|---|
| 141 |
{ |
|---|
| 142 |
REPLICATOR_DEBUG("unserialize"); |
|---|
| 143 |
const gchar *xml; |
|---|
| 144 |
gboolean force = FALSE; |
|---|
| 145 |
PyObject *list; |
|---|
| 146 |
MidgardReplicator *mr = NULL; |
|---|
| 147 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 148 |
|
|---|
| 149 |
if(!PyArg_ParseTuple(args, "s|b", &xml, &force)) |
|---|
| 150 |
return NULL; |
|---|
| 151 |
|
|---|
| 152 |
MidgardConnection *mgd = |
|---|
| 153 |
_py_midgard_connection_singleton_get(); |
|---|
| 154 |
|
|---|
| 155 |
guint i = 0; |
|---|
| 156 |
GObject **objects = midgard_replicator_unserialize(mr, mgd, xml, force); |
|---|
| 157 |
|
|---|
| 158 |
if(objects == NULL) { |
|---|
| 159 |
|
|---|
| 160 |
list = PyTuple_New(i); |
|---|
| 161 |
return list; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
while(objects[i] != NULL) |
|---|
| 165 |
i++; |
|---|
| 166 |
|
|---|
| 167 |
list = PyTuple_New(i); |
|---|
| 168 |
OBJECTS2LIST(objects, list); |
|---|
| 169 |
g_free(objects); |
|---|
| 170 |
|
|---|
| 171 |
return list; |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
int _py_midgard_convert_dbobject(PyObject *object, void *arg) |
|---|
| 175 |
{ |
|---|
| 176 |
PyObject *pclass = NULL, *pcname = NULL; |
|---|
| 177 |
int ret = 0; |
|---|
| 178 |
|
|---|
| 179 |
pclass = PyObject_GetAttrString(object, "__class__"); |
|---|
| 180 |
if (!pclass) { |
|---|
| 181 |
|
|---|
| 182 |
PyErr_SetString(PyExc_TypeError, "Didn't find a class for given object."); |
|---|
| 183 |
return ret; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
pcname = PyObject_GetAttrString(pclass, "__name__"); |
|---|
| 187 |
if(!pcname) { |
|---|
| 188 |
|
|---|
| 189 |
PyErr_SetString(PyExc_TypeError, "Didn't find a class name for given object."); |
|---|
| 190 |
return ret; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
GType type = g_type_from_name(PyString_AS_STRING(pcname)); |
|---|
| 194 |
|
|---|
| 195 |
ret = 1; |
|---|
| 196 |
|
|---|
| 197 |
if(g_type_parent(type) != MIDGARD_TYPE_DBOBJECT) { |
|---|
| 198 |
|
|---|
| 199 |
if(g_type_parent(type) != MIDGARD_TYPE_OBJECT) { |
|---|
| 200 |
|
|---|
| 201 |
PyErr_SetString(PyExc_TypeError, "Expected midgard_dbobject (or derived) class instance."); |
|---|
| 202 |
ret = 0; |
|---|
| 203 |
|
|---|
| 204 |
} else { |
|---|
| 205 |
|
|---|
| 206 |
ret = 1; |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
if(ret == 1) { |
|---|
| 211 |
|
|---|
| 212 |
if(G_OBJECT(((PyGObject *)object)->obj) == NULL) { |
|---|
| 213 |
|
|---|
| 214 |
PyErr_SetString(PyExc_TypeError, "Can not find underlying GObject object."); |
|---|
| 215 |
ret = 0; |
|---|
| 216 |
} |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
arg = (void *)MIDGARD_OBJECT(((PyGObject *)object)->obj); |
|---|
| 220 |
g_assert(arg != NULL); |
|---|
| 221 |
|
|---|
| 222 |
return ret; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
static int __py_midgard_parse_dbobject(PyObject *object) |
|---|
| 226 |
{ |
|---|
| 227 |
PyObject *pclass = NULL, *pcname = NULL; |
|---|
| 228 |
int ret = 0; |
|---|
| 229 |
|
|---|
| 230 |
pclass = PyObject_GetAttrString(object, "__class__"); |
|---|
| 231 |
if (!pclass) { |
|---|
| 232 |
|
|---|
| 233 |
PyErr_SetString(PyExc_TypeError, "Didn't find a class for given argument object."); |
|---|
| 234 |
return ret; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
pcname = PyObject_GetAttrString(pclass, "__name__"); |
|---|
| 238 |
if(!pcname) { |
|---|
| 239 |
|
|---|
| 240 |
PyErr_SetString(PyExc_TypeError, "Didn't find a class name for given argument object."); |
|---|
| 241 |
return ret; |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
GType type = g_type_from_name(PyString_AS_STRING(pcname)); |
|---|
| 245 |
|
|---|
| 246 |
if(!type) { |
|---|
| 247 |
|
|---|
| 248 |
PyErr_SetString(PyExc_TypeError, "Expected argument object registered in GType system."); |
|---|
| 249 |
return ret; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
if(g_type_parent(type) != MIDGARD_TYPE_DBOBJECT) { |
|---|
| 253 |
|
|---|
| 254 |
if(g_type_parent(type) != MIDGARD_TYPE_OBJECT) { |
|---|
| 255 |
|
|---|
| 256 |
PyErr_SetString(PyExc_TypeError, "Expected argument of type midgard_dbobject (or derived class)."); |
|---|
| 257 |
ret = 0; |
|---|
| 258 |
|
|---|
| 259 |
} else { |
|---|
| 260 |
|
|---|
| 261 |
ret = 1; |
|---|
| 262 |
} |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
if(ret == 1) { |
|---|
| 266 |
|
|---|
| 267 |
if(G_OBJECT(((PyGObject *)object)->obj) == NULL) { |
|---|
| 268 |
|
|---|
| 269 |
PyErr_SetString(PyExc_TypeError, "Can not find underlying GObject object."); |
|---|
| 270 |
ret = 0; |
|---|
| 271 |
} |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
return ret; |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
static PyObject * |
|---|
| 278 |
pymidgard_replicator_import_object(PyGObject *self, PyObject *args) |
|---|
| 279 |
{ |
|---|
| 280 |
REPLICATOR_DEBUG("import_object"); |
|---|
| 281 |
PyObject *pobject; |
|---|
| 282 |
gboolean force; |
|---|
| 283 |
MidgardReplicator *mr = NULL; |
|---|
| 284 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 285 |
|
|---|
| 286 |
PyTypeObject mdbo = py_midgard_dbobject_get_type_object(); |
|---|
| 287 |
|
|---|
| 288 |
if(!PyArg_ParseTuple(args, "O|b", &pobject, &force)) |
|---|
| 289 |
return NULL; |
|---|
| 290 |
|
|---|
| 291 |
if(!__py_midgard_parse_dbobject(pobject)) |
|---|
| 292 |
return NULL; |
|---|
| 293 |
|
|---|
| 294 |
gboolean imported = |
|---|
| 295 |
midgard_replicator_import_object(mr, |
|---|
| 296 |
MIDGARD_OBJECT(((PyGObject *)pobject)->obj), force); |
|---|
| 297 |
|
|---|
| 298 |
if(imported) |
|---|
| 299 |
Py_RETURN_TRUE; |
|---|
| 300 |
|
|---|
| 301 |
Py_RETURN_FALSE; |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
static PyObject * |
|---|
| 305 |
pymidgard_replicator_import_from_xml(PyGObject *self, PyObject *args) |
|---|
| 306 |
{ |
|---|
| 307 |
REPLICATOR_DEBUG("import_from_xml"); |
|---|
| 308 |
const gchar *xml; |
|---|
| 309 |
gboolean force = FALSE; |
|---|
| 310 |
MidgardConnection *mgd = NULL; |
|---|
| 311 |
MidgardReplicator *mr = NULL; |
|---|
| 312 |
if(self) mr = MIDGARD_REPLICATOR(self->obj); |
|---|
| 313 |
|
|---|
| 314 |
if(!PyArg_ParseTuple(args, "s|b", &xml, &force)) |
|---|
| 315 |
return NULL; |
|---|
| 316 |
|
|---|
| 317 |
if(!mr) mgd = _py_midgard_connection_singleton_get(); |
|---|
| 318 |
|
|---|
| 319 |
midgard_replicator_import_from_xml(mr, mgd, xml, force); |
|---|
| 320 |
|
|---|
| 321 |
return; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
static PyMethodDef pymidgard_replicator_methods[] = { |
|---|
| 325 |
{ "serialize", (PyCFunction)pymidgard_replicator_serialize, METH_VARARGS | METH_STATIC}, |
|---|
| 326 |
{ "export", (PyCFunction)pymidgard_replicator_export, METH_VARARGS | METH_STATIC}, |
|---|
| 327 |
{ "export_purged", (PyCFunction)pymidgard_replicator_export_purged, METH_VARARGS | METH_STATIC}, |
|---|
| 328 |
{ "export_blob", (PyCFunction)pymidgard_replicator_export_blob, METH_VARARGS | METH_STATIC}, |
|---|
| 329 |
{ "export_media", (PyCFunction)pymidgard_replicator_export_media, METH_VARARGS | METH_STATIC}, |
|---|
| 330 |
{ "unserialize", (PyCFunction)pymidgard_replicator_unserialize, METH_VARARGS | METH_STATIC}, |
|---|
| 331 |
{ "import_object", (PyCFunction)pymidgard_replicator_import_object, METH_VARARGS | METH_STATIC}, |
|---|
| 332 |
{ "import_from_xml", (PyCFunction)pymidgard_replicator_import_from_xml, METH_VARARGS | METH_STATIC}, |
|---|
| 333 |
{ NULL, NULL, 0 } |
|---|
| 334 |
}; |
|---|
| 335 |
|
|---|
| 336 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_replicator_Type = { |
|---|
| 337 |
PyObject_HEAD_INIT(NULL) |
|---|
| 338 |
0, |
|---|
| 339 |
"replicator", |
|---|
| 340 |
sizeof(PyGObject), |
|---|
| 341 |
0, |
|---|
| 342 |
|
|---|
| 343 |
(destructor)0, |
|---|
| 344 |
(printfunc)0, |
|---|
| 345 |
(getattrfunc)0, |
|---|
| 346 |
(setattrfunc)0, |
|---|
| 347 |
(cmpfunc)0, |
|---|
| 348 |
(reprfunc)0, |
|---|
| 349 |
(PyNumberMethods*)0, |
|---|
| 350 |
(PySequenceMethods*)0, |
|---|
| 351 |
(PyMappingMethods*)0, |
|---|
| 352 |
(hashfunc)0, |
|---|
| 353 |
(ternaryfunc)0, |
|---|
| 354 |
(reprfunc)0, |
|---|
| 355 |
(getattrofunc)0, |
|---|
| 356 |
(setattrofunc)0, |
|---|
| 357 |
(PyBufferProcs*)0, |
|---|
| 358 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|---|
| 359 |
NULL, |
|---|
| 360 |
(traverseproc)0, |
|---|
| 361 |
(inquiry)0, |
|---|
| 362 |
(richcmpfunc)0, |
|---|
| 363 |
offsetof(PyGObject, weakreflist), |
|---|
| 364 |
(getiterfunc)0, |
|---|
| 365 |
(iternextfunc)0, |
|---|
| 366 |
pymidgard_replicator_methods, |
|---|
| 367 |
(struct PyMemberDef*)0, |
|---|
| 368 |
(struct PyGetSetDef*)0, |
|---|
| 369 |
NULL, |
|---|
| 370 |
NULL, |
|---|
| 371 |
(descrgetfunc)0, |
|---|
| 372 |
(descrsetfunc)0, |
|---|
| 373 |
offsetof(PyGObject, inst_dict), |
|---|
| 374 |
(initproc)__replicator_constructor, |
|---|
| 375 |
(allocfunc)0, |
|---|
| 376 |
(newfunc)0, |
|---|
| 377 |
(freefunc)0, |
|---|
| 378 |
(inquiry)0 |
|---|
| 379 |
}; |
|---|
| 380 |
|
|---|
| 381 |
void py_midgard_replicator_register_class( |
|---|
| 382 |
PyObject *d, gpointer pygobject_type) |
|---|
| 383 |
{ |
|---|
| 384 |
pygobject_register_class(d, |
|---|
| 385 |
"replicator", |
|---|
| 386 |
MIDGARD_TYPE_REPLICATOR, |
|---|
| 387 |
&Pymidgard_replicator_Type, |
|---|
| 388 |
Py_BuildValue("(O)", pygobject_type)); |
|---|
| 389 |
} |
|---|