root/trunk/midgard/apis/python/py_midgard_blob.c

Revision 16508, 6.1 kB (checked in by piotras, 6 months ago)

Defined NO_IMPORT_PYGOBJECT...

Line 
1 /*
2  * Copyright (C) 2007 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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,                                 /* ob_size */
181     "blob",                   /* tp_name */
182     sizeof(PyGObject),          /* tp_basicsize */
183     0,                                 /* tp_itemsize */
184     /* methods */
185     (destructor)0,        /* tp_dealloc */
186     (printfunc)0,                      /* tp_print */
187     (getattrfunc)0,       /* tp_getattr */
188     (setattrfunc)0,       /* tp_setattr */
189     (cmpfunc)0,           /* tp_compare */
190     (reprfunc)0,             /* tp_repr */
191     (PyNumberMethods*)0,     /* tp_as_number */
192     (PySequenceMethods*)0, /* tp_as_sequence */
193     (PyMappingMethods*)0,   /* tp_as_mapping */
194     (hashfunc)0,             /* tp_hash */
195     (ternaryfunc)0,          /* tp_call */
196     (reprfunc)0,              /* tp_str */
197     (getattrofunc)0,     /* tp_getattro */
198     (setattrofunc)0,     /* tp_setattro */
199     (PyBufferProcs*)0,  /* tp_as_buffer */
200     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
201     NULL,                        /* Documentation string */
202     (traverseproc)0,     /* tp_traverse */
203     (inquiry)0,             /* tp_clear */
204     (richcmpfunc)0,   /* tp_richcompare */
205     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
206     (getiterfunc)0,          /* tp_iter */
207     (iternextfunc)0,     /* tp_iternext */
208     pymidgard_blob_methods, /* tp_methods */
209     (struct PyMemberDef*)0,              /* tp_members */
210     (struct PyGetSetDef*)0,  /* tp_getset */
211     NULL,                              /* tp_base */
212     NULL,                              /* tp_dict */
213     (descrgetfunc)0,    /* tp_descr_get */
214     (descrsetfunc)0,    /* tp_descr_set */
215     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
216     (initproc)__blob_constructor,             /* tp_init */
217     (allocfunc)0,           /* tp_alloc */
218     (newfunc)0,               /* tp_new */
219     (freefunc)0,             /* tp_free */
220     (inquiry)0              /* tp_is_gc */
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 }
Note: See TracBrowser for help on using the browser.