[gedit/gnome-2-28] Override setattr/getattr for gedit.Message to return message values



commit d8f483a84075cc6227e797a36fb01901b420c480
Author: Jesse van den Kieboom <jesse icecrew nl>
Date:   Sun Oct 11 02:15:55 2009 +0200

    Override setattr/getattr for gedit.Message to return message values
    
    By default, an attribute is now looked up in the message first before
    calling the default attribute handlers of python. This makes the message
    API a bit more pythonic (you can do message.<key> instead of
    message.get_value(<key>)).

 .../python/bindings/geditmessage.override          |   60 ++++++++++++++++++++
 1 files changed, 60 insertions(+), 0 deletions(-)
---
diff --git a/plugin-loaders/python/bindings/geditmessage.override b/plugin-loaders/python/bindings/geditmessage.override
index c352ae8..244d60c 100644
--- a/plugin-loaders/python/bindings/geditmessage.override
+++ b/plugin-loaders/python/bindings/geditmessage.override
@@ -494,3 +494,63 @@ _wrap_gedit_message_bus_send_sync(PyGObject *self, PyObject *args, PyObject *kwa
     return pygobject_new((GObject *)message);
 }
 %%
+override-slot GeditMessage.tp_getattro
+static PyObject *
+_wrap_gedit_message_tp_getattro(PyObject *self, PyObject *attrname)
+{
+	GeditMessage *message = GEDIT_MESSAGE(((PyGObject *)self)->obj);
+	GeditMessageType *type;
+
+	gchar *name = _helper_wrap_get_string (attrname);
+	gboolean exists;
+	gboolean intype;
+	PyObject *ret;
+	
+	if (name == NULL)
+	{
+		PyErr_SetString(PyExc_TypeError, "attr name somehow not a string");
+		return NULL;
+	}
+	
+	g_object_get (message, "type", &type, NULL);
+	intype = gedit_message_type_lookup (type, name) != G_TYPE_INVALID;
+	gedit_message_type_unref (type);
+	
+	exists = gedit_message_has_key (message, name);
+	
+	if (!intype)
+	{
+		ret = PyObject_GenericGetAttr(self, attrname);
+	}
+	else if (exists)
+	{
+		GValue value = { 0, };
+		gedit_message_get_value (message, name, &value);
+		ret = pyg_value_as_pyobject(&value, TRUE);
+		g_value_unset (&value);
+	}
+	else
+	{
+		Py_INCREF(Py_None);
+		ret = Py_None;
+	}
+
+	g_free (name);
+	return ret;
+}
+%%
+override-slot GeditMessage.tp_setattro
+static int
+_wrap_gedit_message_tp_setattro(PyObject *self, PyObject *attrname, PyObject *value)
+{
+	GeditMessage *message = GEDIT_MESSAGE(((PyGObject *)self)->obj);
+
+	if (!_helper_wrap_message_set_value(message, attrname, value))
+	{
+		return PyObject_GenericSetAttr(self, attrname, value);
+	}
+	else
+	{
+		return 1;
+	}
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]