1
"""This module contains the GtkAttributesFacade class."""
3
from gobject import GObject
6
class GtkAttributesFacade:
8
"""Wrap a GTK instance to simplify the way attributes are referenced.
10
Given a GTK instance i, make it possible for any functions i.get_foo() and
11
i.set_foo(value) to be accessed via i.foo like a normal attribute.
13
The following attributes are used:
15
instance - This is the GTK instance that is being wrapped.
19
def __init__(self, instance):
20
"""Accept the instance."""
21
self.__dict__["instance"] = instance
23
def __setattr__(self, name, value):
24
"""Simplify the way attributes are referenced.
26
When trying to do self.foo = something, if there is a
27
self.instance.set_foo() method, use it. Otherwise, just set the
30
Return value so that chaining is possible.
33
setter = "set_" + name
34
if hasattr(self.instance, setter):
35
apply(getattr(self.instance, setter), [value])
37
self.__dict__[name] = value
40
def __getattr__(self, name):
41
"""Simplify the way attributes are referenced.
43
Remember that this method is called after a failed lookup in self. Try
44
looking for self.instance.foo. Next, try looking for a
45
self.instance.get_foo() method, and call it if it exists. Otherwise,
48
If a value is successfully looked up, and if it is a subclass of
49
GObject, wrap it in a GtkAttributesFacade before returning it.
52
getter = "get_" + name
53
if hasattr(self.instance, name):
54
ret = getattr(self.instance, name)
55
elif hasattr(self.instance, getter):
56
ret = apply(getattr(self.instance, getter))
59
"%s instance has no attribute '%s'" %
60
(self.instance.__class__.__name__, name))
61
if isinstance(ret, GObject):
62
ret = GtkAttributesFacade(ret)