[MERGE] [osv: moved osv_pool to modules/registry.
bzr revid: vmt@openerp.com-20110615153954-d0fbws57z2w2r6db
This commit is contained in:
+83
-40
@@ -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)
|
||||
registry.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:
|
||||
|
||||
+2
-8
@@ -830,9 +830,6 @@ class orm_template(object):
|
||||
if x=='.id': return [x]
|
||||
return x.replace(':id','/id').replace('.id','/.id').split('/')
|
||||
fields_to_export = map(fsplit, fields_to_export)
|
||||
fields_export = fields_to_export + []
|
||||
warning = ''
|
||||
warning_fields = []
|
||||
datas = []
|
||||
for row in self.browse(cr, uid, ids, context):
|
||||
datas += self.__export_row(cr, uid, row, fields_to_export, context)
|
||||
@@ -1020,7 +1017,6 @@ class orm_template(object):
|
||||
|
||||
if config.get('import_partial', False) and filename:
|
||||
data = pickle.load(file(config.get('import_partial')))
|
||||
original_value = data.get(filename, 0)
|
||||
|
||||
position = 0
|
||||
while position<len(datas):
|
||||
@@ -1033,7 +1029,7 @@ class orm_template(object):
|
||||
return (-1, res, 'Line ' + str(position) +' : ' + '!\n'.join(warning), '')
|
||||
|
||||
try:
|
||||
id = ir_model_data_obj._update(cr, uid, self._name,
|
||||
ir_model_data_obj._update(cr, uid, self._name,
|
||||
current_module, res, mode=mode, xml_id=xml_id,
|
||||
noupdate=noupdate, res_id=res_id, context=context)
|
||||
except Exception, e:
|
||||
@@ -1705,7 +1701,6 @@ class orm_template(object):
|
||||
result = {'type': view_type, 'model': self._name}
|
||||
|
||||
ok = True
|
||||
model = True
|
||||
sql_res = False
|
||||
parent_view_model = None
|
||||
while ok:
|
||||
@@ -1739,7 +1734,6 @@ class orm_template(object):
|
||||
|
||||
ok = sql_res[5]
|
||||
view_id = ok or sql_res[3]
|
||||
model = False
|
||||
parent_view_model = sql_res[6]
|
||||
|
||||
# if a view was found
|
||||
@@ -3074,7 +3068,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()
|
||||
|
||||
|
||||
+1
-49
@@ -23,18 +23,15 @@
|
||||
# OSV: Objects Services
|
||||
#
|
||||
|
||||
import sys
|
||||
import inspect
|
||||
import orm
|
||||
import openerp.netsvc as netsvc
|
||||
import openerp.pooler as pooler
|
||||
import openerp.sql_db as sql_db
|
||||
import copy
|
||||
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 +200,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
|
||||
|
||||
+5
-16
@@ -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)
|
||||
RegistryManager.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):
|
||||
|
||||
@@ -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' \
|
||||
|
||||
Reference in New Issue
Block a user