Move info and warn to zavai lib
[gregoa/zavai.git] / zavai / registry.py
1 # registry - zavai resource registry
2 #
3 # Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
4 #
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.
9 #
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.
14 #
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
18
19 import zavai
20
21 class Registry(object):
22     """Collection of resources.
23
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.
27     """
28
29     def __init__(self):
30         self.factories = dict()
31         self.objects = dict()
32
33     def resource(self, name):
34         """Get a resource, instantiating it if it has not been done yet.
35
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).
39
40         If no suitable factory has been found, returns None.
41         """
42         res = self.objects.get(name, None)
43         if res is None:
44             fac = self.factories.get(name, None)
45             if fac is None:
46                 root = name
47                 while True:
48                     pos = root.rfind(".")
49                     if pos == -1: break
50                     root = root[:pos]
51                     fac = self.factories.get(root, None)
52                 if fac is None:
53                     return None
54             res = fac(self, name)
55             if res is not None:
56                 self.objects[name] = res
57         return res
58
59     def menu(self, name):
60         """Get a menu resource, instantiating it if it has not been done yet.
61
62         All menu resources share the same factory, which builds zavai.Menu
63         objects.
64         """
65         return self.resource("menu." + name)
66
67     def menu_link(self, name, label):
68         """Return a MenuLink to the menu with the given name
69         """
70         return zavai.MenuLink(self, name, label)
71
72     def get_existing(self, name):
73         """Get a menu resource, but only if it has been already instantiated.
74
75         If the resource has not been instantiated yet, returns None.
76         """
77         return self.objects.get(name, None)
78
79     def register(self, name, factory):
80         """Register a factory for this registry.
81
82         Name is a name for this factory, like "menu.gps.monitor".
83
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
89
90     def shutdown(self):
91         """Shut down all objects in this Registry.
92
93         After shutting down, all objects cannot be used anymore"""
94         for o in self.objects.itervalues():
95             o.shutdown()
96         self.objects.clear()
97
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
102         constructor"""
103         pass
104
105     def shutdown(self):
106         """Shut down this resource.
107
108         Normally one does nothing here, but it is important to give resources a
109         chance to do cleanup when the program quits.
110
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.
113         """
114         pass