GNOME Bugzilla – Bug 532998
add gtk.Builder.objects.foo as synonym for gtk.Builder.get_object('foo')
Last modified: 2011-08-06 00:46:45 UTC
This was previously discussed on IRC with Johan. This is in the same spirit as GtkXXX.props.foo is the same as GtkXXX.get_property('foo').
Created attachment 110871 [details] [review] proposed implementation + a test case
Comment on attachment 110871 [details] [review] proposed implementation + a test case >Index: gtk/gtkbuilder.override >+static PyObject * >+pygtk_builder_objects_getattr(PyGtkBuilderObjects *self, char *name) >+{ >+ GtkBuilder *builder = GTK_BUILDER(((PyGObject *) self->builder)->obj); >+ return pygobject_new(gtk_builder_get_object(builder, name)); >+} AttributeError should be raised if gtk_builder_get_objects() returns NULL. >+ "gtk.GtkBuilderObjects", /* tp_name */ gtk.BuilderObjects no? >+ "GtkBuilder objects", /* tp_doc */ gtk.Builder.objects, a description here would be nice if this actually shows up in the docstring >Index: gtk/gtk-2.12-types.defs > (parent "GObject") > (c-name "GtkBuilder") > (gtype-id "GTK_TYPE_BUILDER") >+ (fields >+ '("PyObject*" "objects") This is not necessary AFAICT. >Index: tests/test_builder.py >+ self.assertNotEqual(builder.get_object ('main'), None) >+ self.assertEqual(builder.objects.main, builder.get_object ('main')) You can remove the spaces before '('. >+ self.assertEqual(builder.objects.this_object_doesnt_exist, None) Last test should be updated to raise AttributeError object.__members__ should be mapped to get_objects()/buildable_get_name(), dir(builder.objects) works. There needs to be some name mangling done, for instance, converting dashes to underscores.
Created attachment 110926 [details] [review] patch + test cases, 2nd iteration Here is the second iteration of the patch. * AttributeError should be raised if gtk_builder_get_objects() returns NULL. * gtk.BuilderObjects no? * gtk.Builder.objects, a description here would be nice if this actually shows up in the docstring * You can remove the spaces before '('. * Last test should be updated to raise AttributeError These are done. > >+ (fields > >+ '("PyObject*" "objects") > > This is not necessary AFAICT. No, it is. Otherwise the relevant part of .override file isn't copied into .c, I actually tried. Though it may also highlight a problem in .override... > object.__members__ should be mapped to get_objects()/buildable_get_name(), > dir(builder.objects) works. Should __members__ be a list of names or a list of gobject.GObject's? (Currently I implemented the latter.) > There needs to be some name mangling done, for instance, converting dashes to > underscores. This might be not trivial to do. E.g. what if names clash (id="foo_bar" and id="foo-bar" in one gtk.Builder)? Should implementation try all possible demangling (since we are speaking the way round here)? E.g. objects.spam_ham_egg should try get_object('spam_ham_egg'), get_object('spam_ham-egg'), get_object('spam-ham_egg') and get_object('spam-ham-egg')? Or should we try only two possibilities: AFAIK GLib allows only dashes or only underscores in a given signal/property/etc. name.
BTW, forgot to say that some tests in the patch currently fail because of the unresolved issues I mentioned. I also forgot about dir(). AFAIK __dir__ only exists in SVN Python. I'll look what gobject.GObject.props does, though.
One more possibility is to have gtk.Builder implement dict protocol, but only its read part (no modifications allowed). That automatically solves all problems with name demanglings. I.e.: ui = gtk.Builder() ui.add_from_file(...) And after that: 'id1' in ui: whether there is an object/widget with that id defined; len(ui): number of currently defined objects; ui['id1']: object/widget 'id1', else AttributeError; ui.get('id1', [default]):object/widget 'id1', else 'default' or None; ui.items(), ui.keys(), ui.values(): just as in dict. I guess iteritems() etc. are an overkill. And anyway they are retired in Python 3.
Closing API incompatible changes to pygtk - It is maintenance only now.