After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 532998 - add gtk.Builder.objects.foo as synonym for gtk.Builder.get_object('foo')
add gtk.Builder.objects.foo as synonym for gtk.Builder.get_object('foo')
Status: RESOLVED WONTFIX
Product: pygtk
Classification: Bindings
Component: gtk
Git Master
Other All
: Normal enhancement
: ---
Assigned To: Paul Pogonyshev
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2008-05-13 19:52 UTC by Paul Pogonyshev
Modified: 2011-08-06 00:46 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
proposed implementation + a test case (4.09 KB, patch)
2008-05-13 19:53 UTC, Paul Pogonyshev
none Details | Review
patch + test cases, 2nd iteration (5.74 KB, patch)
2008-05-14 19:22 UTC, Paul Pogonyshev
none Details | Review

Description Paul Pogonyshev 2008-05-13 19:52:34 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').
Comment 1 Paul Pogonyshev 2008-05-13 19:53:22 UTC
Created attachment 110871 [details] [review]
proposed implementation + a test case
Comment 2 Johan (not receiving bugmail) Dahlin 2008-05-13 23:28:23 UTC
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.
Comment 3 Paul Pogonyshev 2008-05-14 19:22:41 UTC
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.
Comment 4 Paul Pogonyshev 2008-05-14 19:24:23 UTC
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.
Comment 5 Paul Pogonyshev 2008-07-06 15:56:15 UTC
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.
Comment 6 John Stowers 2011-08-06 00:46:45 UTC
Closing API incompatible changes to pygtk - It is maintenance only now.