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
21 class Registry(object):
22 """Collection of resources.
24 Various factories can be registered by name on the registry. Then when an
25 object is requested for the first time, it is created using the factory.
26 When it is requested again, the existing object is reused.
30 self.factories = dict()
33 def resource(self, name):
34 """Get a resource, instantiating it if it has not been done yet.
36 First it tries to use the factory registered with `name` itself. If it
37 fails, it tries to use the factory registered one level above (using
38 the dot '.' as a separator).
40 If no suitable factory has been found, returns None.
42 res = self.objects.get(name, None)
44 fac = self.factories.get(name, None)
51 fac = self.factories.get(root, None)
56 self.objects[name] = res
60 """Get a menu resource, instantiating it if it has not been done yet.
62 All menu resources share the same factory, which builds zavai.Menu
65 return self.resource("menu." + name)
67 def menu_link(self, name, label):
68 """Return a MenuLink to the menu with the given name
70 return zavai.MenuLink(self, name, label)
72 def get_existing(self, name):
73 """Get a menu resource, but only if it has been already instantiated.
75 If the resource has not been instantiated yet, returns None.
77 return self.objects.get(name, None)
79 def register(self, name, factory):
80 """Register a factory for this registry.
82 Name is a name for this factory, like "menu.gps.monitor".
84 Factory is a function that creates a Resource object. The function will
85 be passed the Registry itself as the only parameter"""
86 if name in self.factories:
87 return KeyError("% is already registered as a factory", name)
88 self.factories[name] = factory
91 """Shut down all objects in this Registry.
93 After shutting down, all objects cannot be used anymore"""
94 for o in self.objects.itervalues():
98 class Resource(object):
99 def __init__(self, registry, name, *args, **kw):
100 """The default constructor does nothing, but is there to eat the
101 parameters passed by Registry in case subclassers do not need a
106 """Shut down this resource.
108 Normally one does nothing here, but it is important to give resources a
109 chance to do cleanup when the program quits.
111 This can be used for tasks like closing the tags on a GPX track,
112 releasing a FSO resource, restoring mixer settings and so on.