3 class Registry(object):
4 """Collection of resources.
6 Various factories can be registered by name on the registry. Then when an
7 object is requested for the first time, it is created using the factory.
8 When it is requested again, the existing object is reused.
12 self.factories = dict()
15 def resource(self, name):
16 """Get a resource, instantiating it if it has not been done yet.
18 First it tries to use the factory registered with `name` itself. If it
19 fails, it tries to use the factory registered one level above (using
20 the dot '.' as a separator).
22 If no suitable factory has been found, returns None.
24 res = self.objects.get(name, None)
26 fac = self.factories.get(name, None)
33 fac = self.factories.get(root, None)
38 self.objects[name] = res
42 """Get a menu resource, instantiating it if it has not been done yet.
44 All menu resources share the same factory, which builds zavai.Menu
47 return self.resource("menu." + name)
49 def menu_link(self, name, label):
50 """Return a MenuLink to the menu with the given name
52 return zavai.MenuLink(self, name, label)
54 def get_existing(self, name):
55 """Get a menu resource, but only if it has been already instantiated.
57 If the resource has not been instantiated yet, returns None.
59 return self.objects.get(name, None)
61 def register(self, name, factory):
62 """Register a factory for this registry.
64 Name is a name for this factory, like "menu.gps.monitor".
66 Factory is a function that creates a Resource object. The function will
67 be passed the Registry itself as the only parameter"""
68 if name in self.factories:
69 return KeyError("% is already registered as a factory", name)
70 self.factories[name] = factory
73 """Shut down all objects in this Registry.
75 After shutting down, all objects cannot be used anymore"""
76 for o in self.objects.itervalues():
80 class Resource(object):
81 def __init__(self, registry, name, *args, **kw):
82 """The default constructor does nothing, but is there to eat the
83 parameters passed by Registry in case subclassers do not need a
88 """Shut down this resource.
90 Normally one does nothing here, but it is important to give resources a
91 chance to do cleanup when the program quits.
93 This can be used for tasks like closing the tags on a GPX track,
94 releasing a FSO resource, restoring mixer settings and so on.