| 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 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_blob_Type; |
|---|
| 27 |
|
|---|
| 28 |
#define BLOB_DEBUG(__name) \ |
|---|
| 29 |
CHECK_MGD; \ |
|---|
| 30 |
CLASS_METHOD_DEBUG(Pymidgard_blob_Type.tp_name, __name); |
|---|
| 31 |
|
|---|
| 32 |
#define __GET_BLOB \ |
|---|
| 33 |
MidgardBlob *blob = MIDGARD_BLOB(self->obj); \ |
|---|
| 34 |
g_assert(blob != NULL); |
|---|
| 35 |
|
|---|
| 36 |
static int |
|---|
| 37 |
__blob_constructor(PyGObject *self, PyObject *args, PyObject *kwargs) |
|---|
| 38 |
{ |
|---|
| 39 |
BLOB_DEBUG("__init__"); |
|---|
| 40 |
PyObject *pvalue = NULL; |
|---|
| 41 |
PyTypeObject *pytype = |
|---|
| 42 |
py_midgard_lookup_schema_type("midgard_attachment"); |
|---|
| 43 |
if(!PyArg_ParseTuple(args,"O!:__init__", pytype, &pvalue)) |
|---|
| 44 |
return -1; |
|---|
| 45 |
|
|---|
| 46 |
MgdObject *att = MIDGARD_OBJECT(((PyGObject *)pvalue)->obj); |
|---|
| 47 |
MidgardBlob *blob = midgard_blob_new(att); |
|---|
| 48 |
|
|---|
| 49 |
if(!blob) |
|---|
| 50 |
return -1; |
|---|
| 51 |
|
|---|
| 52 |
self->obj = G_OBJECT(blob); |
|---|
| 53 |
|
|---|
| 54 |
return 0; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
static PyObject * |
|---|
| 58 |
pymidgard_blob_read_content(PyGObject *self, PyObject *args) |
|---|
| 59 |
{ |
|---|
| 60 |
BLOB_DEBUG("read_content"); |
|---|
| 61 |
|
|---|
| 62 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 63 |
return NULL; |
|---|
| 64 |
|
|---|
| 65 |
__GET_BLOB; |
|---|
| 66 |
|
|---|
| 67 |
gsize bytes_read = 0; |
|---|
| 68 |
gchar *content = midgard_blob_read_content(blob, &bytes_read); |
|---|
| 69 |
|
|---|
| 70 |
PyObject *ret = Py_BuildValue("s", content); |
|---|
| 71 |
g_free(content); |
|---|
| 72 |
|
|---|
| 73 |
return ret; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
static PyObject * |
|---|
| 77 |
pymidgard_blob_write_content(PyGObject *self, PyObject *args) |
|---|
| 78 |
{ |
|---|
| 79 |
BLOB_DEBUG("write_content"); |
|---|
| 80 |
const gchar *content = NULL; |
|---|
| 81 |
gboolean written = FALSE; |
|---|
| 82 |
|
|---|
| 83 |
if(!PyArg_ParseTuple(args, "s", &content)) |
|---|
| 84 |
return NULL; |
|---|
| 85 |
|
|---|
| 86 |
__GET_BLOB; |
|---|
| 87 |
|
|---|
| 88 |
written = midgard_blob_write_content(blob, content); |
|---|
| 89 |
|
|---|
| 90 |
if(written) |
|---|
| 91 |
Py_RETURN_TRUE; |
|---|
| 92 |
|
|---|
| 93 |
Py_RETURN_FALSE; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
static PyObject * |
|---|
| 97 |
pymidgard_blob_remove_file(PyGObject *self, PyObject *args) |
|---|
| 98 |
{ |
|---|
| 99 |
BLOB_DEBUG("remove_file"); |
|---|
| 100 |
gboolean removed = FALSE; |
|---|
| 101 |
|
|---|
| 102 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 103 |
return NULL; |
|---|
| 104 |
|
|---|
| 105 |
__GET_BLOB; |
|---|
| 106 |
|
|---|
| 107 |
removed = midgard_blob_remove_file(blob); |
|---|
| 108 |
|
|---|
| 109 |
if(removed) |
|---|
| 110 |
Py_RETURN_TRUE; |
|---|
| 111 |
|
|---|
| 112 |
Py_RETURN_FALSE; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
static PyObject * |
|---|
| 116 |
pymidgard_blob_get_path(PyGObject *self, PyObject *args) |
|---|
| 117 |
{ |
|---|
| 118 |
BLOB_DEBUG("get_path"); |
|---|
| 119 |
|
|---|
| 120 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 121 |
return NULL; |
|---|
| 122 |
|
|---|
| 123 |
__GET_BLOB; |
|---|
| 124 |
|
|---|
| 125 |
const gchar *path = midgard_blob_get_path(blob); |
|---|
| 126 |
|
|---|
| 127 |
PyObject *ret = Py_BuildValue("s", path); |
|---|
| 128 |
|
|---|
| 129 |
return ret; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
static PyObject * |
|---|
| 133 |
pymidgard_blob_get_handler(PyGObject *self, PyObject *args) |
|---|
| 134 |
{ |
|---|
| 135 |
BLOB_DEBUG("get_handler"); |
|---|
| 136 |
|
|---|
| 137 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 138 |
return NULL; |
|---|
| 139 |
|
|---|
| 140 |
__GET_BLOB; |
|---|
| 141 |
|
|---|
| 142 |
const gchar *path = midgard_blob_get_path(blob); |
|---|
| 143 |
|
|---|
| 144 |
PyObject *fh = PyFile_FromString((gchar *)path, "w+"); |
|---|
| 145 |
|
|---|
| 146 |
return fh; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
static PyObject * |
|---|
| 150 |
pymidgard_blob_exists(PyGObject *self, PyObject *args) |
|---|
| 151 |
{ |
|---|
| 152 |
BLOB_DEBUG("exists"); |
|---|
| 153 |
gboolean exists = FALSE; |
|---|
| 154 |
|
|---|
| 155 |
if(!PyArg_ParseTuple(args, "")) |
|---|
| 156 |
return NULL; |
|---|
| 157 |
|
|---|
| 158 |
__GET_BLOB; |
|---|
| 159 |
|
|---|
| 160 |
exists = midgard_blob_exists(blob); |
|---|
| 161 |
|
|---|
| 162 |
if(exists) |
|---|
| 163 |
Py_RETURN_TRUE; |
|---|
| 164 |
|
|---|
| 165 |
Py_RETURN_FALSE; |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
static PyMethodDef pymidgard_blob_methods[] = { |
|---|
| 169 |
{ "read_content", (PyCFunction)pymidgard_blob_read_content, METH_VARARGS }, |
|---|
| 170 |
{ "write_content", (PyCFunction)pymidgard_blob_write_content, METH_VARARGS }, |
|---|
| 171 |
{ "remove_file", (PyCFunction)pymidgard_blob_remove_file, METH_VARARGS }, |
|---|
| 172 |
{ "get_path", (PyCFunction)pymidgard_blob_get_path, METH_VARARGS }, |
|---|
| 173 |
{ "get_handler", (PyCFunction)pymidgard_blob_get_handler, METH_VARARGS }, |
|---|
| 174 |
{ "exists", (PyCFunction)pymidgard_blob_exists, METH_VARARGS }, |
|---|
| 175 |
{ NULL, NULL, 0 } |
|---|
| 176 |
}; |
|---|
| 177 |
|
|---|
| 178 |
PyTypeObject G_GNUC_INTERNAL Pymidgard_blob_Type = { |
|---|
| 179 |
PyObject_HEAD_INIT(NULL) |
|---|
| 180 |
0, |
|---|
| 181 |
"blob", |
|---|
| 182 |
sizeof(PyGObject), |
|---|
| 183 |
0, |
|---|
| 184 |
|
|---|
| 185 |
(destructor)0, |
|---|
| 186 |
(printfunc)0, |
|---|
| 187 |
(getattrfunc)0, |
|---|
| 188 |
(setattrfunc)0, |
|---|
| 189 |
(cmpfunc)0, |
|---|
| 190 |
(reprfunc)0, |
|---|
| 191 |
(PyNumberMethods*)0, |
|---|
| 192 |
(PySequenceMethods*)0, |
|---|
| 193 |
(PyMappingMethods*)0, |
|---|
| 194 |
(hashfunc)0, |
|---|
| 195 |
(ternaryfunc)0, |
|---|
| 196 |
(reprfunc)0, |
|---|
| 197 |
(getattrofunc)0, |
|---|
| 198 |
(setattrofunc)0, |
|---|
| 199 |
(PyBufferProcs*)0, |
|---|
| 200 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|---|
| 201 |
NULL, |
|---|
| 202 |
(traverseproc)0, |
|---|
| 203 |
(inquiry)0, |
|---|
| 204 |
(richcmpfunc)0, |
|---|
| 205 |
offsetof(PyGObject, weakreflist), |
|---|
| 206 |
(getiterfunc)0, |
|---|
| 207 |
(iternextfunc)0, |
|---|
| 208 |
pymidgard_blob_methods, |
|---|
| 209 |
(struct PyMemberDef*)0, |
|---|
| 210 |
(struct PyGetSetDef*)0, |
|---|
| 211 |
NULL, |
|---|
| 212 |
NULL, |
|---|
| 213 |
(descrgetfunc)0, |
|---|
| 214 |
(descrsetfunc)0, |
|---|
| 215 |
offsetof(PyGObject, inst_dict), |
|---|
| 216 |
(initproc)__blob_constructor, |
|---|
| 217 |
(allocfunc)0, |
|---|
| 218 |
(newfunc)0, |
|---|
| 219 |
(freefunc)0, |
|---|
| 220 |
(inquiry)0 |
|---|
| 221 |
}; |
|---|
| 222 |
|
|---|
| 223 |
void py_midgard_blob_register_class( |
|---|
| 224 |
PyObject *d, gpointer pygobject_type) |
|---|
| 225 |
{ |
|---|
| 226 |
pygobject_register_class(d, |
|---|
| 227 |
"blob", |
|---|
| 228 |
MIDGARD_TYPE_BLOB, |
|---|
| 229 |
&Pymidgard_blob_Type, |
|---|
| 230 |
Py_BuildValue("(O)", pygobject_type)); |
|---|
| 231 |
} |
|---|