diff --git a/openerp/__init__.py b/openerp/__init__.py index a3cd4b3c00a..63016a520ee 100644 --- a/openerp/__init__.py +++ b/openerp/__init__.py @@ -32,6 +32,7 @@ import netsvc import osv import pooler import pychart +import registry import release import report import run_tests diff --git a/openerp/pooler.py b/openerp/pooler.py index fb47a948752..c3b7b6f2eba 100644 --- a/openerp/pooler.py +++ b/openerp/pooler.py @@ -19,66 +19,50 @@ # ############################################################################## -import sql_db +""" Functions kept for backward compatibility. + + They are simple wrappers around a global RegistryManager methods. + +""" + +from openerp.registry.manager import RegistryManager + +_Registries = None + + +def ensure_registries(): + global _Registries + if _Registries is None: + _Registries = RegistryManager() -# Mapping between db name and osv_pool. -# Accessed through the functions below. -pool_dic = {} def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False, pooljobs=True): - """Return a database connection and an initialized osv_pool.""" - - db = sql_db.db_connect(db_name) - - if db_name in pool_dic: - pool = pool_dic[db_name] - else: - import openerp.modules - import openerp.osv.osv as osv_osv - pool = osv_osv.osv_pool() - - # Initializing an osv_pool will call general code which will in turn - # call get_db_and_pool (this function) to obtain the osv_pool being - # initialized. Make it available in the pool_dic then remove it if - # an exception is raised. - pool_dic[db_name] = pool - try: - openerp.modules.load_modules(db, force_demo, status, update_module) - except Exception: - del pool_dic[db_name] - raise - - cr = db.cursor() - try: - pool.init_set(cr, False) - pool.get('ir.actions.report.xml').register_all(cr) - cr.commit() - finally: - cr.close() - - if pooljobs: - pool.get('ir.cron').restart(db.dbname) - return db, pool + """Create and return a database connection and a newly initialized registry.""" + ensure_registries() + bound_registry = _Registries.get(db_name, force_demo, status, update_module, pooljobs) + return bound_registry.db, bound_registry.registry def delete_pool(db_name): - """Delete an existing osv_pool.""" - if db_name in pool_dic: - del pool_dic[db_name] + """Delete an existing registry.""" + ensure_registries() + _Registries.delete(db_name) + def restart_pool(db_name, force_demo=False, status=None, update_module=False): - """Delete an existing osv_pool and return a database connection and a newly initialized osv_pool.""" - delete_pool(db_name) - return get_db_and_pool(db_name, force_demo, status, update_module=update_module) + """Delete an existing registry and return a database connection and a newly initialized registry.""" + ensure_registries() + bound_registry = _Registries.new(db_name, force_demo, status, update_module, pooljobs) + return bound_registry.db, bound_registry.registry def get_db(db_name): - """Return a database connection. The corresponding osv_pool is initialize.""" + """Return a database connection. The corresponding registry is initialized.""" return get_db_and_pool(db_name)[0] def get_pool(db_name, force_demo=False, status=None, update_module=False): - """Return an osv_pool.""" + """Return a model registry.""" return get_db_and_pool(db_name, force_demo, status, update_module)[1] # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/registry/__init__.py b/openerp/registry/__init__.py new file mode 100644 index 00000000000..5ac650bc771 --- /dev/null +++ b/openerp/registry/__init__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2011 OpenERP s.a. (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +""" Modules registries. + +""" + +import manager + + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/registry/manager.py b/openerp/registry/manager.py new file mode 100644 index 00000000000..ff2cc235bb2 --- /dev/null +++ b/openerp/registry/manager.py @@ -0,0 +1,108 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +""" Model registries manager. + + The manager is responsible for creation and deletion of bound model + registries (essentially database connection/model registry pairs). + +""" + +import openerp.sql_db + + +class BoundRegistry(object): + """ Model registry/database connection pair.""" + def __init__(self, db, registry): + self.db = db + self.registry = registry + + +class RegistryManager(object): + + + # TODO maybe should receive the addons paths + def __init__(self): + # Mapping between db name and bound model registry. + # Accessed through the methods below. + self.bound_registries = {} + + + def get(self, db_name, force_demo=False, status=None, update_module=False, + pooljobs=True): + """ Return a bound registry for a given database name.""" + + if db_name in self.bound_registries: + bound_registry = self.bound_registries[db_name] + else: + bound_registry = self.new(db_name, force_demo, status, + update_module, pooljobs) + return bound_registry + + + def new(self, db_name, force_demo=False, status=None, + update_module=False, pooljobs=True): + """ Create and return a new bound registry for a given database name. + + The (possibly) previous bound registry for that database name is + discarded. + + """ + + import openerp.modules + import openerp.osv.osv as osv_osv + db = openerp.sql_db.db_connect(db_name) + pool = osv_osv.osv_pool() + + # Initializing a registry will call general code which will in turn + # call registries.get (this object) to obtain the registry being + # initialized. Make it available in the bound_registries dictionary + # then remove it if an exception is raised. + self.delete(db_name) + bound_registry = BoundRegistry(db, pool) + self.bound_registries[db_name] = bound_registry + try: + # This should be a method on BoundRegistry + openerp.modules.load_modules(db, force_demo, status, update_module) + except Exception: + del self.bound_registries[db_name] + raise + + cr = db.cursor() + try: + pool.init_set(cr, False) + pool.get('ir.actions.report.xml').register_all(cr) + cr.commit() + finally: + cr.close() + + if pooljobs: + pool.get('ir.cron').restart(db.dbname) + + return bound_registry + + + def delete(self, db_name): + if db_name in self.bound_registries: + del self.bound_registries[db_name] + + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: