1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 """ Models registries.
27 import openerp.osv.orm
30 class Registry(object):
31 """ Model registry for a particular database.
33 The registry is essentially a mapping between model names and model
34 instances. There is one registry instance per database.
38 def __init__(self, db_name):
39 self.models = {} # model name/model instance mapping
41 self._store_function = {}
43 self._init_parent = {}
44 self.db_name = db_name
45 self.db = openerp.sql_db.db_connect(db_name)
47 def do_parent_store(self, cr):
48 for o in self._init_parent:
49 self.get(o)._parent_store_compute(cr)
53 """ Return the list of model names in this registry."""
54 return self.models.keys()
56 def add(self, model_name, model):
57 """ Add or replace a model in the registry."""
58 self.models[model_name] = model
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)
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]
68 def instanciate(self, module, cr):
69 """ Instanciate all the classes of a given module for a particular db."""
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))
81 class RegistryManager(object):
82 """ Model registries manager.
84 The manager is responsible for creation and deletion of model
85 registries (essentially database connection/model registry pairs).
89 # Mapping between db name and model registry.
90 # Accessed through the methods below.
95 def get(cls, db_name, force_demo=False, status=None, update_module=False,
97 """ Return a registry for a given database name."""
99 if db_name in cls.registries:
100 registry = cls.registries[db_name]
102 registry = cls.new(db_name, force_demo, status,
103 update_module, pooljobs)
108 def new(cls, db_name, force_demo=False, status=None,
109 update_module=False, pooljobs=True):
110 """ Create and return a new registry for a given database name.
112 The (possibly) previous registry for that database name is discarded.
116 import openerp.modules
117 registry = Registry(db_name)
119 # Initializing a registry will call general code which will in turn
120 # call registries.get (this object) to obtain the registry being
121 # initialized. Make it available in the registries dictionary then
122 # remove it if an exception is raised.
124 cls.registries[db_name] = registry
126 # This should be a method on Registry
127 openerp.modules.load_modules(registry.db, force_demo, status, update_module)
129 del cls.registries[db_name]
132 cr = registry.db.cursor()
134 registry.do_parent_store(cr)
135 registry.get('ir.actions.report.xml').register_all(cr)
141 registry.get('ir.cron').restart(registry.db.dbname)
147 def delete(cls, db_name):
148 """ Delete the registry linked to a given database. """
149 if db_name in cls.registries:
150 del cls.registries[db_name]
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: