""" Models registries.
"""
+from collections import Mapping
from contextlib import contextmanager
import logging
import threading
_logger = logging.getLogger(__name__)
-class Registry(object):
+class Registry(Mapping):
""" Model registry for a particular database.
The registry is essentially a mapping between model names and model
"""
def __init__(self, db_name):
+ super(Registry, self).__init__()
self.models = {} # model name/model instance mapping
self._sql_error = {}
self._store_function = {}
self.has_unaccent = openerp.tools.config['unaccent'] and has_unaccent
cr.close()
+ #
+ # Mapping abstract methods implementation
+ # => mixin provides methods keys, items, values, get, __eq__, and __ne__
+ #
+ def __len__(self):
+ """ Return the size of the registry. """
+ return len(self.models)
+
+ def __iter__(self):
+ """ Return an iterator over all model names. """
+ return iter(self.models)
+
+ def __contains__(self, model_name):
+ """ Test whether the model with the given name exists. """
+ return model_name in self.models
+
+ def __getitem__(self, model_name):
+ """ Return the model with the given name or raise KeyError if it doesn't exist."""
+ return self.models[model_name]
+
def do_parent_store(self, cr):
for o in self._init_parent:
self.get(o)._parent_store_compute(cr)
def obj_list(self):
""" Return the list of model names in this registry."""
- return self.models.keys()
+ return self.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 the model with the given name or None if it doesn't exist."""
- return self.models.get(model_name)
-
- def __getitem__(self, model_name):
- """ Return the model with the given name or raise KeyError if it doesn't exist."""
- return self.models[model_name]
-
- def __contains__(self, model_name):
- """ Test whether the model with the given name exists. """
- return model_name in self.models
-
- def __iter__(self):
- """ Return an iterator over all model names. """
- return iter(self.models)
-
def load(self, cr, module):
""" Load a given module in the registry.