Moving more modules
[apertium.git] / trunk / apertium-tools / apertium-tolk / glade.py
blob0acf98bc9287895f76385f5e15956bf1afb5bb5f
1 import gtk.glade
2 from ConfigParser import DuplicateSectionError, NoSectionError
3 from widget_state import dump_state, load_state
5 class GladeXML(gtk.glade.XML):
6 def get_widgets(self):
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!
12 """
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.
22 """
23 for widget_name, widget in self.get_widgets():
24 try:
25 cfg.add_section(widget_name)
26 except DuplicateSectionError, e:
27 pass
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():
35 try:
36 load_state(widget, dict(cfg.items(widget_name)))
37 except KeyError, e:
38 pass
39 except NoSectionError, e:
40 pass
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
50 methods."""
51 handlers = dict((name, getattr(context, name)) for name in context.__class__.__dict__)
52 self.signal_autoconnect(handlers)