2 from ConfigParser
import DuplicateSectionError
, NoSectionError
3 from widget_state
import dump_state
, load_state
5 class GladeXML(gtk
.glade
.XML
):
7 """Return an iterator which produces (widget_name, widget) pairs.
9 glade.XML.get_widget_prefix returns all widgets with names starting with the
10 supplied string; so if we call get_widget_prefix(''), we get all the widgets
11 in the loaded glade file!
13 return ((gtk
.glade
.get_widget_name(w
), w
) for w
in self
.get_widget_prefix(''))
16 def dump_gtk_state(self
, cfg
):
17 """Enumerate the widgets in the loaded glade file.
19 For each widget, create a section in the configuration file represented by 'cfg'.
20 Then, write the widget state using the dictionary produced by dump_state to the
21 widget's section in the 'cfg' file.
23 for widget_name
, widget
in self
.get_widgets():
25 cfg
.add_section(widget_name
)
26 except DuplicateSectionError
, e
:
29 for key
, val
in dump_state(widget
).iteritems():
30 cfg
.set(widget_name
, key
, str(val
))
33 def load_gtk_state(self
, cfg
):
34 for widget_name
, widget
in self
.get_widgets():
36 load_state(widget
, dict(cfg
.items(widget_name
)))
39 except NoSectionError
, e
:
43 def connect(self
, context
):
44 """Enumerate the methods in the object 'context''s class. For each method, create a
45 (name, function) pair, with the function name and a bound method (binding is done to
46 the object 'context' via getattr(context, name)).
48 Now, create a dictionary out of these pairs. Fincally pass the dictionary to
49 signal_autoconnect, which will bind the signals defined in the glade file to our
51 handlers
= dict((name
, getattr(context
, name
)) for name
in context
.__class
__.__dict
__)
52 self
.signal_autoconnect(handlers
)