[REF] osv: moved osv_pool to modules/registry.

bzr revid: vmt@openerp.com-20110614142226-yd3y39a8z3ubwvxm
This commit is contained in:
Vo Minh Thu 2011-06-14 16:22:26 +02:00
parent 4432d6a9f2
commit e375049414
5 changed files with 90 additions and 103 deletions

View File

@ -24,87 +24,130 @@
"""
import openerp.sql_db
import openerp.osv.orm
class BoundRegistry(object):
""" Model registry/database connection pair."""
def __init__(self, db, registry):
self.db = db
self.registry = registry
class Registry(object):
""" Model registry for a particular database.
The registry is essentially a mapping between model names and model
instances. There is one registry instance per database.
"""
def __init__(self, db_name):
self.models = {} # model name/model instance mapping
self._sql_error = {}
self._store_function = {}
self._init = True
self._init_parent = {}
self.db_name = db_name
self.db = openerp.sql_db.db_connect(db_name)
def do_parent_store(self, cr):
for o in self._init_parent:
self.get(o)._parent_store_compute(cr)
self._init = False
def obj_list(self):
""" Return the list of model names in this registry."""
return self.models.keys()
def add(self, model_name, model):
""" Add or replace a model in the registry."""
self.models[model_name] = model
def get(self, model_name):
""" Return a model for a given name or None if it doesn't exist."""
return self.models.get(model_name)
def __getitem__(self, model_name):
""" Return a model for a given name or raise KeyError if it doesn't exist."""
return self.models[model_name]
def instanciate(self, module, cr):
""" Instanciate all the classes of a given module for a particular db."""
res = []
# Instanciate classes registered through their constructor and
# add them to the pool.
for klass in openerp.osv.orm.module_class_list.get(module, []):
res.append(klass.createInstance(self, cr))
return res
class RegistryManager(object):
""" Model registries manager.
The manager is responsible for creation and deletion of bound model
The manager is responsible for creation and deletion of model
registries (essentially database connection/model registry pairs).
"""
# 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 = {}
# Mapping between db name and model registry.
# Accessed through the methods below.
registries = {}
def get(self, db_name, force_demo=False, status=None, update_module=False,
@classmethod
def get(cls, db_name, force_demo=False, status=None, update_module=False,
pooljobs=True):
""" Return a bound registry for a given database name."""
""" Return a registry for a given database name."""
if db_name in self.bound_registries:
bound_registry = self.bound_registries[db_name]
if db_name in cls.registries:
registry = cls.registries[db_name]
else:
bound_registry = self.new(db_name, force_demo, status,
registry = cls.new(db_name, force_demo, status,
update_module, pooljobs)
return bound_registry
return registry
def new(self, db_name, force_demo=False, status=None,
@classmethod
def new(cls, db_name, force_demo=False, status=None,
update_module=False, pooljobs=True):
""" Create and return a new bound registry for a given database name.
""" Create and return a new registry for a given database name.
The (possibly) previous bound registry for that database name is
discarded.
The (possibly) previous 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()
registry = Registry(db_name)
# 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
# initialized. Make it available in the registries dictionary then
# remove it if an exception is raised.
cls.delete(db_name)
cls.registries[db_name] = registry
try:
# This should be a method on BoundRegistry
openerp.modules.load_modules(db, force_demo, status, update_module)
# This should be a method on Registry
openerp.modules.load_modules(registry.db, force_demo, status, update_module)
except Exception:
del self.bound_registries[db_name]
del cls.registries[db_name]
raise
cr = db.cursor()
cr = registry.db.cursor()
try:
pool.do_parent_store(cr)
pool.get('ir.actions.report.xml').register_all(cr)
registry.do_parent_store(cr)
registry.get('ir.actions.report.xml').register_all(cr)
cr.commit()
finally:
cr.close()
if pooljobs:
pool.get('ir.cron').restart(db.dbname)
pool.get('ir.cron').restart(registry.db.dbname)
return bound_registry
return registry
def delete(self, db_name):
if db_name in self.bound_registries:
del self.bound_registries[db_name]
@classmethod
def delete(cls, db_name):
""" Delete the registry linked to a given database. """
if db_name in cls.registries:
del cls.registries[db_name]
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -3074,7 +3074,7 @@ class orm(orm_template):
#
def _inherits_reload_src(self):
for obj in self.pool.obj_pool.values():
for obj in self.pool.models.values():
if self._name in obj._inherits:
obj._inherits_reload()

View File

@ -34,7 +34,7 @@ import logging
from psycopg2 import IntegrityError, errorcodes
from openerp.tools.func import wraps
from openerp.tools.translate import translate
from openerp.osv.orm import module_class_list
class except_osv(Exception):
def __init__(self, name, value, exc_type='warning'):
@ -203,51 +203,6 @@ class object_proxy(netsvc.Service):
return res
class osv_pool(object):
""" Model registry for a particular database.
The registry is essentially a mapping between model names and model
instances. There is one registry instance per database.
"""
def __init__(self):
self.obj_pool = {} # model name/model instance mapping
self._sql_error = {}
self._store_function = {}
self._init = True
self._init_parent = {}
def do_parent_store(self, cr):
for o in self._init_parent:
self.get(o)._parent_store_compute(cr)
self._init = False
def obj_list(self):
""" Return the list of model names in this registry."""
return self.obj_pool.keys()
def add(self, model_name, model):
""" Add or replace a model in the registry."""
self.obj_pool[model_name] = model
def get(self, name):
""" Return a model for a given name or None if it doesn't exist."""
return self.obj_pool.get(name)
def instanciate(self, module, cr):
""" Instanciate all the classes of a given module for a particular db."""
res = []
# Instanciate classes registered through their constructor and
# add them to the pool.
for klass in module_class_list.get(module, []):
res.append(klass.createInstance(self, cr))
return res
class osv_memory(orm.orm_memory):
""" Deprecated class. """
pass

View File

@ -27,33 +27,22 @@
from openerp.modules.registry import RegistryManager
_Registries = None
def ensure_registries():
global _Registries
if _Registries is None:
_Registries = RegistryManager()
def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False, pooljobs=True):
"""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
registry = RegistryManager.get(db_name, force_demo, status, update_module, pooljobs)
return registry.db, registry
def delete_pool(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 registry and return a database connection and a newly initialized registry."""
ensure_registries()
bound_registry = _Registries.new(db_name, force_demo, status, update_module, True)
return bound_registry.db, bound_registry.registry
registry = RegistryManager.new(db_name, force_demo, status, update_module, True)
return registry.db, registry
def get_db(db_name):

View File

@ -536,7 +536,7 @@ def trans_generate(lang, modules, cr):
trans_obj = pool.get('ir.translation')
model_data_obj = pool.get('ir.model.data')
uid = 1
l = pool.obj_pool.items()
l = pool.models.items()
l.sort()
query = 'SELECT name, model, res_id, module' \