[FIX] registry.py: typos (invalid syntax).
[odoo/odoo.git] / openerp / modules / registry.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 """ Models registries.
23
24 """
25
26 import openerp.sql_db
27 import openerp.osv.orm
28
29
30 class Registry(object):
31     """ Model registry for a particular database.
32
33     The registry is essentially a mapping between model names and model
34     instances. There is one registry instance per database.
35
36     """
37
38     def __init__(self, db_name):
39         self.models = {} # model name/model instance mapping
40         self._sql_error = {}
41         self._store_function = {}
42         self._init = True
43         self._init_parent = {}
44         self.db_name = db_name
45         self.db = openerp.sql_db.db_connect(db_name)
46
47     def do_parent_store(self, cr):
48         for o in self._init_parent:
49             self.get(o)._parent_store_compute(cr)
50         self._init = False
51
52     def obj_list(self):
53         """ Return the list of model names in this registry."""
54         return self.models.keys()
55
56     def add(self, model_name, model):
57         """ Add or replace a model in the registry."""
58         self.models[model_name] = model
59
60     def get(self, model_name):
61         """ Return a model for a given name or None if it doesn't exist."""
62         return self.models.get(model_name)
63
64     def __getitem__(self, model_name):
65         """ Return a model for a given name or raise KeyError if it doesn't exist."""
66         return self.models[model_name]
67
68     def instanciate(self, module, cr):
69         """ Instanciate all the classes of a given module for a particular db."""
70
71         res = []
72
73         # Instanciate classes registered through their constructor and
74         # add them to the pool.
75         for klass in openerp.osv.orm.module_class_list.get(module, []):
76             res.append(klass.createInstance(self, cr))
77
78         # Instanciate classes automatically discovered.
79         for cls in openerp.osv.orm.MetaModel.module_to_models.get(module, []):
80             if cls not in openerp.osv.orm.module_class_list.get(module, []):
81                 res.append(cls.createInstance(self, cr))
82
83         return res
84
85     def clear_caches():
86         """ Clear the caches
87
88         This clears the caches associated to methods decorated with
89         ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models.
90         """
91         for model in self.models.itervalues():
92             model.clear_caches()
93
94
95 class RegistryManager(object):
96     """ Model registries manager.
97
98         The manager is responsible for creation and deletion of model
99         registries (essentially database connection/model registry pairs).
100
101     """
102
103     # Mapping between db name and model registry.
104     # Accessed through the methods below.
105     registries = {}
106
107
108     @classmethod
109     def get(cls, db_name, force_demo=False, status=None, update_module=False,
110             pooljobs=True):
111         """ Return a registry for a given database name."""
112
113         if db_name in cls.registries:
114             registry = cls.registries[db_name]
115         else:
116             registry = cls.new(db_name, force_demo, status,
117                 update_module, pooljobs)
118         return registry
119
120
121     @classmethod
122     def new(cls, db_name, force_demo=False, status=None,
123             update_module=False, pooljobs=True):
124         """ Create and return a new registry for a given database name.
125
126         The (possibly) previous registry for that database name is discarded.
127
128         """
129
130         import openerp.modules
131         registry = Registry(db_name)
132
133         # Initializing a registry will call general code which will in turn
134         # call registries.get (this object) to obtain the registry being
135         # initialized. Make it available in the registries dictionary then
136         # remove it if an exception is raised.
137         cls.delete(db_name)
138         cls.registries[db_name] = registry
139         try:
140             # This should be a method on Registry
141             openerp.modules.load_modules(registry.db, force_demo, status, update_module)
142         except Exception:
143             del cls.registries[db_name]
144             raise
145
146         cr = registry.db.cursor()
147         try:
148             registry.do_parent_store(cr)
149             registry.get('ir.actions.report.xml').register_all(cr)
150             cr.commit()
151         finally:
152             cr.close()
153
154         if pooljobs:
155             registry.get('ir.cron').restart(registry.db.dbname)
156
157         return registry
158
159
160     @classmethod
161     def delete(cls, db_name):
162         """ Delete the registry linked to a given database. """
163         if db_name in cls.registries:
164             del cls.registries[db_name]
165
166
167     @classmethod
168     def clear_caches(db_name):
169         """ Clear the caches
170
171         This clears the caches associated to methods decorated with
172         ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models
173         of the given database name.
174
175         This method is given to spare you a ``RegistryManager.get(db_name)``
176         that would loads the given database if it was not already loaded.
177         """
178         if db_name in cls.registries:
179             cls.registries[db_name].clear_caches()
180
181
182 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: