[REF] osv_pool cleaning:

- removed unused variables (module_list, module_object_list, created)
  - removed unecessary call to del on a dictionary
  (as its element is replaced anyway)
  - added some comments

bzr revid: vmt@openerp.com-20110519085035-p9ohve0nfkudgrx5
This commit is contained in:
Vo Minh Thu 2011-05-19 10:50:35 +02:00
parent 67aa6c461a
commit 4b3357652d
2 changed files with 51 additions and 20 deletions

View File

@ -33,7 +33,7 @@ from psycopg2 import IntegrityError, errorcodes
from openerp.tools.func import wraps from openerp.tools.func import wraps
from openerp.tools.translate import translate from openerp.tools.translate import translate
module_list = [] # Mapping between openerp module names and their osv classes.
module_class_list = {} module_class_list = {}
class except_osv(Exception): class except_osv(Exception):
@ -205,16 +205,20 @@ class object_proxy(netsvc.Service):
object_proxy() object_proxy()
class osv_pool(object): 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): def __init__(self):
self._ready = False self._ready = False
self.obj_pool = {} self.obj_pool = {} # model name/model instance mapping
self.module_object_list = {}
self.created = []
self._sql_error = {} self._sql_error = {}
self._store_function = {} self._store_function = {}
self._init = True self._init = True
self._init_parent = {} self._init_parent = {}
self.logger = logging.getLogger("pool")
def init_set(self, cr, mode): def init_set(self, cr, mode):
different = mode != self._init different = mode != self._init
@ -229,23 +233,17 @@ class osv_pool(object):
self._ready = True self._ready = True
return different return different
def obj_list(self): def obj_list(self):
""" Return the list of model names in this registry."""
return self.obj_pool.keys() return self.obj_pool.keys()
# adds a new object instance to the object pool. def add(self, model_name, model):
# if it already existed, the instance is replaced """ Add or replace a model in the registry."""
def add(self, name, obj_inst): self.obj_pool[model_name] = model
if name in self.obj_pool:
del self.obj_pool[name]
self.obj_pool[name] = obj_inst
module = obj_inst.__class__.__module__.split('.')[0]
self.module_object_list.setdefault(module, []).append(obj_inst)
# Return None if object does not exist
def get(self, name): def get(self, name):
obj = self.obj_pool.get(name, None) """ Return a model for a given name or None if it doesn't exist."""
return obj return self.obj_pool.get(name)
#TODO: pass a list of modules to load #TODO: pass a list of modules to load
def instanciate(self, module, cr): def instanciate(self, module, cr):
@ -256,18 +254,49 @@ class osv_pool(object):
return res return res
class osv_base(object): class osv_base(object):
""" Base class for openerp models.
OpenERP models are created by inheriting from this class (although
not directly; more specifically by inheriting from osv or
osv_memory). The constructor is called once, usually directly
after the class definition, e.g.:
class user(osv):
...
user()
The system will later instanciate the class once per database (on
which the class' module is installed).
"""
def __init__(self, pool, cr): def __init__(self, pool, cr):
""" Initialize a model and make it part of the given registry."""
pool.add(self._name, self) pool.add(self._name, self)
self.pool = pool self.pool = pool
super(osv_base, self).__init__(cr) super(osv_base, self).__init__(cr)
def __new__(cls): def __new__(cls):
""" Register this model.
This doesn't create an instance but simply register the model
as being part of the module where it is defined.
TODO make it possible to not even have to call the constructor
to be registered.
"""
# Set the module name (e.g. base, sale, accounting, ...) on the class.
module = cls.__module__.split('.')[0] module = cls.__module__.split('.')[0]
if not hasattr(cls, '_module'): if not hasattr(cls, '_module'):
cls._module = module cls._module = module
# Remember which models to instanciate for this module.
module_class_list.setdefault(cls._module, []).append(cls) module_class_list.setdefault(cls._module, []).append(cls)
if module not in module_list:
module_list.append(cls._module) # Since we don't return an instance here, the __init__
# method won't be called.
return None return None
class osv_memory(osv_base, orm.orm_memory): class osv_memory(osv_base, orm.orm_memory):

View File

@ -21,6 +21,8 @@
import sql_db import sql_db
# Mapping between db name and osv_pool.
# Accessed through the functions below.
pool_dic = {} pool_dic = {}
def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False, pooljobs=True): def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False, pooljobs=True):
@ -38,7 +40,7 @@ def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False,
pool = osv_osv.osv_pool() pool = osv_osv.osv_pool()
# Initializing an osv_pool will call general code which will in turn # Initializing an osv_pool will call general code which will in turn
# call get_db_and_pool (this function) to obtain the osv_pool begin # 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 # initialized. Make it available in the pool_dic then remove it if
# an exception is raised. # an exception is raised.
pool_dic[db_name] = pool pool_dic[db_name] = pool