Changeset 14813

Show
Ignore:
Timestamp:
02/08/08 14:49:04 (10 months ago)
Author:
piotras
Message:

blobs binded

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midgard/apis/python/py_midgard_blob.c

    r13659 r14813  
    2323#include "py_midgard.h" 
    2424 
     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 
     33static 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 
     54static PyObject * 
     55pymidgard_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 
     73static PyObject * 
     74pymidgard_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 
     93static PyObject * 
     94pymidgard_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 
     112static PyObject * 
     113pymidgard_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 
     129static PyObject * 
     130pymidgard_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 
     146static PyObject * 
     147pymidgard_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 
    25165static PyMethodDef pymidgard_blob_methods[] = { 
     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 }, 
    26172        { NULL, NULL, 0 } 
    27173}; 
     
    65211    (descrsetfunc)0,    /* tp_descr_set */ 
    66212    offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */ 
    67     (initproc)0,             /* tp_init */ 
     213    (initproc)__blob_constructor,             /* tp_init */ 
    68214    (allocfunc)0,           /* tp_alloc */ 
    69215    (newfunc)0,               /* tp_new */ 
  • trunk/midgard/apis/python/py_midgard_connection.c

    r14068 r14813  
    6565                _py_midgard_connection_singleton_set(MIDGARD_CONNECTION(self->obj)); 
    6666                /* get module object and set python object */ 
    67                 PyObject *module = PyImport_AddModule("midgard2");     
     67                PyObject *module = PyImport_AddModule("_midgard");     
    6868                PyObject *d = PyModule_GetDict(module); 
    6969                PyDict_DelItemString(d, "_connection");  
  • trunk/midgard/apis/python/tests/midgard_blob.py

    r13659 r14813  
    1 import midgard2 as midgard 
     1import _midgard as midgard 
     2import midgard_config 
    23 
    34class my_blob(midgard.blob): 
     
    56                midgard.blob.__init__(self) 
    67 
    7 blob = midgard.blob() 
     8att = midgard.mgdschema.midgard_attachment() 
     9blob = midgard.blob(att) 
     10 
     11content = "This is my note" 
     12blob.write_content(content) 
     13 
     14content = blob.read_content() 
     15print(content) 
     16