1 # registry - zavai resource registry
3 # Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 "Get the parent name for s"
25 if pos == -1: return None
27 if res == "menu": return None
31 "Compute a default label given the last element of a path"
33 if pos == -1: return s.capitalize()
34 return s[pos+1:].capitalize()
37 class Registry(object):
38 """Collection of resources.
40 Various factories can be registered by name on the registry. Then when an
41 object is requested for the first time, it is created using the factory.
42 When it is requested again, the existing object is reused.
46 self.factories = dict()
50 def register(self, name, obj):
51 """Register an object at the given path.
53 Name the path to this object, like "menu.gps.monitor".
55 if name in self.objects:
56 return KeyError("%s is already registered", name)
57 zavai.info("Registering", name)
58 self.objects[name] = obj
60 if name.startswith("menu."):
61 self.add_to_menu(name)
63 def register_factory(self, name, fac, label = None):
64 """Register an object factory at the given path.
66 Name the path to this object, like "menu.gps.monitor".
68 if name in self.factories:
69 return KeyError("Factory %s is already registered", name)
70 zavai.info("Registering factory", name)
71 self.factories[name] = fac
72 if label is not None: self.labels[name] = label
74 def register_action(self, name, obj):
75 """Register an object at the given path.
77 Name the path to this object, like "menu.gps.monitor".
79 if name in self.objects:
80 return KeyError("%s is already registered", name)
81 zavai.info("Registering action", name)
82 self.objects[name] = obj
84 if name.startswith("menu."):
85 parent = get_parent(name)
86 if parent is not None:
87 zavai.info("Add action to menu", name, parent)
88 menu = self.menu(parent)
89 if isinstance(obj, gtk.ToggleAction):
90 menu.add_child(zavai.ToggleButton(self, name, action=obj))
92 menu.add_child(zavai.LinkButton(self, name, action=obj))
94 def add_to_menu(self, name):
95 "Add the applet with the given name to the menu structure"
96 parent = get_parent(name)
97 if parent is not None:
98 zavai.info("Add to menu", name, parent)
99 menu = self.menu(parent)
100 menu.add_child(zavai.LinkButton(self, name, self.label(name)))
102 def label(self, name):
103 "Return the label for the object with the given name"
104 res = self.labels.get(name)
108 obj = self.resource(name)
109 return obj.props.label
111 return default_label(name)
113 def resource(self, name):
114 """Get a resource from the registry.
116 If no resource exists at `name` but there is a factory, instantiate the
117 object using the factory.
119 If not even a factory exists at `name`, returns None.
121 res = self.objects.get(name, None)
123 fac = self.factories.get(name, None)
125 res = self.objects[name] = fac(self, name)
128 def menu(self, name):
129 """Get a menu resource, automatically creating it if it is missing.
131 Menus are created automatically linked to a parent menu, according to
132 the hierarchy in `name`.
134 res = self.resource(name)
136 # Check if it is a toplevel menu
137 if name.startswith("menu."):
138 parent = get_parent(name[5:])
139 if parent is not None:
140 parent = "menu." + parent
142 parent = get_parent(name)
144 res = zavai.Menu(self, name, parent)
145 self.register(name, res)
149 """Shut down all objects in this Registry.
151 After shutting down, all objects cannot be used anymore"""
152 for o in self.objects.itervalues():
153 if isinstance(o, Resource):
157 class Resource(object):
159 """Shut down this resource.
161 Normally one does nothing here, but it is important to give resources a
162 chance to do cleanup when the program quits.
164 This can be used for tasks like closing the tags on a GPX track,
165 releasing a FSO resource, restoring mixer settings and so on.