| | 25 | #define BLOB_DEBUG(__name) \ |
|---|
| | 26 | CHECK_MGD; \ |
|---|
| | 27 | CLASS_METHOD_DEBUG(Pymidgard_blob_Type.tp_name, __name); |
|---|
| | 28 | |
|---|
| | 29 | #define __GET_BLOB \ |
|---|
| | 30 | MidgardBlob *blob = MIDGARD_BLOB(self->obj); \ |
|---|
| | 31 | g_assert(blob != NULL); |
|---|
| | 32 | |
|---|
| | 33 | static int |
|---|
| | 34 | __blob_constructor(PyGObject *self, PyObject *args, PyObject *kwargs) |
|---|
| | 35 | { |
|---|
| | 36 | BLOB_DEBUG("__init__"); |
|---|
| | 37 | PyObject *pvalue = NULL; |
|---|
| | 38 | PyTypeObject *pytype = |
|---|
| | 39 | py_midgard_lookup_schema_type("midgard_attachment"); |
|---|
| | 40 | if(!PyArg_ParseTuple(args,"O!:__init__", pytype, &pvalue)) |
|---|
| | 41 | return -1; |
|---|
| | 42 | |
|---|
| | 43 | MgdObject *att = MIDGARD_OBJECT(((PyGObject *)pvalue)->obj); |
|---|
| | 44 | MidgardBlob *blob = midgard_blob_new(att); |
|---|
| | 45 | |
|---|
| | 46 | if(!blob) |
|---|
| | 47 | return -1; |
|---|
| | 48 | |
|---|
| | 49 | self->obj = G_OBJECT(blob); |
|---|
| | 50 | |
|---|
| | 51 | return 0; |
|---|
| | 52 | } |
|---|
| | 53 | |
|---|
| | 54 | static PyObject * |
|---|
| | 55 | pymidgard_blob_read_content(PyGObject *self, PyObject *args) |
|---|
| | 56 | { |
|---|
| | 57 | BLOB_DEBUG("read_content"); |
|---|
| | 58 | |
|---|
| | 59 | if(!PyArg_ParseTuple(args, "")) |
|---|
| | 60 | return NULL; |
|---|
| | 61 | |
|---|
| | 62 | __GET_BLOB; |
|---|
| | 63 | |
|---|
| | 64 | gsize bytes_read = 0; |
|---|
| | 65 | gchar *content = midgard_blob_read_content(blob, &bytes_read); |
|---|
| | 66 | |
|---|
| | 67 | PyObject *ret = Py_BuildValue("s", content); |
|---|
| | 68 | g_free(content); |
|---|
| | 69 | |
|---|
| | 70 | return ret; |
|---|
| | 71 | } |
|---|
| | 72 | |
|---|
| | 73 | static PyObject * |
|---|
| | 74 | pymidgard_blob_write_content(PyGObject *self, PyObject *args) |
|---|
| | 75 | { |
|---|
| | 76 | BLOB_DEBUG("write_content"); |
|---|
| | 77 | const gchar *content = NULL; |
|---|
| | 78 | gboolean written = FALSE; |
|---|
| | 79 | |
|---|
| | 80 | if(!PyArg_ParseTuple(args, "s", &content)) |
|---|
| | 81 | return NULL; |
|---|
| | 82 | |
|---|
| | 83 | __GET_BLOB; |
|---|
| | 84 | |
|---|
| | 85 | written = midgard_blob_write_content(blob, content); |
|---|
| | 86 | |
|---|
| | 87 | if(written) |
|---|
| | 88 | Py_RETURN_TRUE; |
|---|
| | 89 | |
|---|
| | 90 | Py_RETURN_FALSE; |
|---|
| | 91 | } |
|---|
| | 92 | |
|---|
| | 93 | static PyObject * |
|---|
| | 94 | pymidgard_blob_remove_file(PyGObject *self, PyObject *args) |
|---|
| | 95 | { |
|---|
| | 96 | BLOB_DEBUG("remove_file"); |
|---|
| | 97 | gboolean removed = FALSE; |
|---|
| | 98 | |
|---|
| | 99 | if(!PyArg_ParseTuple(args, "")) |
|---|
| | 100 | return NULL; |
|---|
| | 101 | |
|---|
| | 102 | __GET_BLOB; |
|---|
| | 103 | |
|---|
| | 104 | removed = midgard_blob_remove_file(blob); |
|---|
| | 105 | |
|---|
| | 106 | if(removed) |
|---|
| | 107 | Py_RETURN_TRUE; |
|---|
| | 108 | |
|---|
| | 109 | Py_RETURN_FALSE; |
|---|
| | 110 | } |
|---|
| | 111 | |
|---|
| | 112 | static PyObject * |
|---|
| | 113 | pymidgard_blob_get_path(PyGObject *self, PyObject *args) |
|---|
| | 114 | { |
|---|
| | 115 | BLOB_DEBUG("get_path"); |
|---|
| | 116 | |
|---|
| | 117 | if(!PyArg_ParseTuple(args, "")) |
|---|
| | 118 | return NULL; |
|---|
| | 119 | |
|---|
| | 120 | __GET_BLOB; |
|---|
| | 121 | |
|---|
| | 122 | const gchar *path = midgard_blob_get_path(blob); |
|---|
| | 123 | |
|---|
| | 124 | PyObject *ret = Py_BuildValue("s", path); |
|---|
| | 125 | |
|---|
| | 126 | return ret; |
|---|
| | 127 | } |
|---|
| | 128 | |
|---|
| | 129 | static PyObject * |
|---|
| | 130 | pymidgard_blob_get_handler(PyGObject *self, PyObject *args) |
|---|
| | 131 | { |
|---|
| | 132 | BLOB_DEBUG("get_handler"); |
|---|
| | 133 | |
|---|
| | 134 | if(!PyArg_ParseTuple(args, "")) |
|---|
| | 135 | return NULL; |
|---|
| | 136 | |
|---|
| | 137 | __GET_BLOB; |
|---|
| | 138 | |
|---|
| | 139 | const gchar *path = midgard_blob_get_path(blob); |
|---|
| | 140 | |
|---|
| | 141 | PyObject *fh = PyFile_FromString((gchar *)path, "w+"); |
|---|
| | 142 | |
|---|
| | 143 | return fh; |
|---|
| | 144 | } |
|---|
| | 145 | |
|---|
| | 146 | static PyObject * |
|---|
| | 147 | pymidgard_blob_exists(PyGObject *self, PyObject *args) |
|---|
| | 148 | { |
|---|
| | 149 | BLOB_DEBUG("exists"); |
|---|
| | 150 | gboolean exists = FALSE; |
|---|
| | 151 | |
|---|
| | 152 | if(!PyArg_ParseTuple(args, "")) |
|---|
| | 153 | return NULL; |
|---|
| | 154 | |
|---|
| | 155 | __GET_BLOB; |
|---|
| | 156 | |
|---|
| | 157 | exists = midgard_blob_exists(blob); |
|---|
| | 158 | |
|---|
| | 159 | if(exists) |
|---|
| | 160 | Py_RETURN_TRUE; |
|---|
| | 161 | |
|---|
| | 162 | Py_RETURN_FALSE; |
|---|
| | 163 | } |
|---|
| | 164 | |
|---|
| | 166 | { "read_content", (PyCFunction)pymidgard_blob_read_content, METH_VARARGS }, |
|---|
| | 167 | { "write_content", (PyCFunction)pymidgard_blob_write_content, METH_VARARGS }, |
|---|
| | 168 | { "remove_file", (PyCFunction)pymidgard_blob_remove_file, METH_VARARGS }, |
|---|
| | 169 | { "get_path", (PyCFunction)pymidgard_blob_get_path, METH_VARARGS }, |
|---|
| | 170 | { "get_handler", (PyCFunction)pymidgard_blob_get_handler, METH_VARARGS }, |
|---|
| | 171 | { "exists", (PyCFunction)pymidgard_blob_exists, METH_VARARGS }, |
|---|