Changeset 16662

Show
Ignore:
Timestamp:
06/20/08 15:03:20 (4 months ago)
Author:
piotras
Message:

Implemented properties' getter and setter.

Files:

Legend:

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

    r16656 r16662  
    148148                        /* Py_DECREF(pyval); */ 
    149149                } 
    150         } 
     150        }       
    151151 
    152152        return (self->obj) ? 0 : -1; 
     
    590590} 
    591591 
     592static PyObject * 
     593__get_attro(PyObject *self, PyObject *attr) 
     594{ 
     595        GObject *object = G_OBJECT(((PyGObject*)self)->obj); 
     596 
     597        /* Now we do fallback to generic GetAttr */ 
     598        if(!object)  
     599                return PyObject_GenericGetAttr(self, attr); 
     600 
     601        /* There's underlying GObject so we can run custom routines and get property*/ 
     602        GObjectClass *klass = G_OBJECT_GET_CLASS(object); 
     603        GParamSpec *pspec; 
     604        const gchar *attr_name = PyString_AsString(attr); 
     605        PyTypeObject *tp = self->ob_type; 
     606 
     607        pspec = g_object_class_find_property(klass, attr_name); 
     608 
     609        if(!pspec) { 
     610                 
     611                PyErr_Format(PyExc_AttributeError, 
     612                                "'%.50s' object has no attribute '%.400s'", 
     613                                tp->tp_name, attr_name); 
     614                return NULL; 
     615        } 
     616 
     617        GValue pval = {0, }; 
     618        g_value_init(&pval, pspec->value_type); 
     619        g_object_get_property(object, attr_name, &pval); 
     620 
     621        PyObject *pvalue = pyg_value_as_pyobject((const GValue *)&pval, FALSE); 
     622 
     623        g_value_unset(&pval); 
     624 
     625        return pvalue; 
     626} 
     627 
     628static int 
     629__set_attro(PyObject *self, PyObject *attr, PyObject *value) 
     630{ 
     631        GObject *object = G_OBJECT(((PyGObject*)self)->obj); 
     632 
     633        /* Now we do fallback to generic SetAttr */ 
     634        if(!object)  
     635                return PyObject_GenericSetAttr(self, attr, value); 
     636 
     637        /* There's underlying GObject so we can run custom routines and set property*/ 
     638        GObjectClass *klass = G_OBJECT_GET_CLASS(object); 
     639        GParamSpec *pspec; 
     640        const gchar *attr_name = PyString_AsString(attr); 
     641        PyTypeObject *tp = self->ob_type; 
     642 
     643        pspec = g_object_class_find_property(klass, attr_name); 
     644 
     645        if(!pspec) { 
     646                 
     647                PyErr_Format(PyExc_AttributeError, 
     648                                "'%.50s' object has no attribute '%.400s'", 
     649                                tp->tp_name, attr_name); 
     650                return -1; 
     651        } 
     652 
     653        GValue pval = {0, }; 
     654        g_value_init(&pval, pspec->value_type); 
     655        pyg_value_from_pyobject(&pval, value); 
     656 
     657        g_object_set_property(object, attr_name, &pval); 
     658 
     659        g_value_unset(&pval); 
     660 
     661        return 0; 
     662} 
     663 
    592664PyTypeObject *__new_object_struct(const gchar *typename) 
    593665{ 
     
    617689        ot->tp_call = 0; 
    618690        ot->tp_str = 0; 
    619         ot->tp_getattro = 0
    620         ot->tp_setattro = 0
     691        ot->tp_getattro = __get_attro
     692        ot->tp_setattro = __set_attro
    621693        ot->tp_as_buffer = 0; 
    622694        ot->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; 
     
    668740 
    669741        for (i = 0; i < n_types; i++) { 
    670                  
     742 
    671743                typename = g_type_name(all_types[i]); 
     744                /* g_warning("Trying to register '%s'", typename); */ 
     745 
     746                if(!G_TYPE_IS_CLASSED(all_types[i])) 
     747                        continue; 
     748 
    672749                PyTypeObject *ot = __new_object_struct(typename); 
    673750                pygobject_register_class(d, 
    674                                 typename, all_types[i], ot, NULL); 
     751                                typename, all_types[i], ot, NULL);  
    675752                                /* FIXME, What is a static bases in this case? */ 
    676753                                /* Py_BuildValue("(O)", pygobject_type)); */