diff --git a/component/core.py b/component/core.py index 45ee64069..aa4cb5250 100644 --- a/component/core.py +++ b/component/core.py @@ -23,7 +23,7 @@ from odoo import models from odoo.tools import LastOrderedSet, OrderedSet -from .exception import NoComponentError, SeveralComponentError +from .exception import NoComponentError, RegistryNotReadyError, SeveralComponentError _logger = logging.getLogger(__name__) @@ -252,14 +252,17 @@ def __init__( dbname = self.env.cr.dbname try: self.components_registry = _component_databases[dbname] - except KeyError: - _logger.error( + except KeyError as exc: + msg = ( "No component registry for database %s. " "Probably because the Odoo registry has not been built " - "yet.", + "yet." + ) + _logger.error( + msg, dbname, ) - raise + raise RegistryNotReadyError(msg) from exc self._propagate_kwargs = ["collection", "model_name", "components_registry"] for attr_name, value in kwargs.items(): setattr(self, attr_name, value) diff --git a/component/exception.py b/component/exception.py index 3883282e8..cb4c67853 100644 --- a/component/exception.py +++ b/component/exception.py @@ -12,3 +12,7 @@ class NoComponentError(ComponentException): class SeveralComponentError(ComponentException): """More than one component have been found""" + + +class RegistryNotReadyError(ComponentException): + """Component registry not ready yet for given DB.""" diff --git a/component/readme/CONTRIBUTORS.rst b/component/readme/CONTRIBUTORS.rst index 60a076dc1..60d861306 100644 --- a/component/readme/CONTRIBUTORS.rst +++ b/component/readme/CONTRIBUTORS.rst @@ -1,2 +1,3 @@ * Guewen Baconnier * Laurent Mignon +* Simone Orsi diff --git a/component/tests/__init__.py b/component/tests/__init__.py index de5a88a91..29c286e26 100644 --- a/component/tests/__init__.py +++ b/component/tests/__init__.py @@ -2,3 +2,4 @@ from . import test_component from . import test_lookup from . import test_work_on +from . import test_utils diff --git a/component/tests/test_utils.py b/component/tests/test_utils.py new file mode 100644 index 000000000..f630a83e6 --- /dev/null +++ b/component/tests/test_utils.py @@ -0,0 +1,20 @@ +# Copyright 2023 Camptocamp SA +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from unittest import mock + +from odoo.addons.component.utils import is_component_registry_ready + +from .common import TransactionComponentRegistryCase + + +class TestUtils(TransactionComponentRegistryCase): + def test_registry_ready(self): + path = "odoo.addons.component.utils.get_component_registry" + with mock.patch(path) as mocked: + mocked.return_value = None + self.assertFalse(is_component_registry_ready(self.env.cr.dbname)) + self._setup_registry(self) + mocked.return_value = self.comp_registry + self.assertTrue(is_component_registry_ready(self.env.cr.dbname)) + self._teardown_registry(self) diff --git a/component/utils.py b/component/utils.py new file mode 100644 index 000000000..66e955125 --- /dev/null +++ b/component/utils.py @@ -0,0 +1,14 @@ +# Copyright 2023 Camptocamp SA +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from .core import _component_databases + + +def get_component_registry(dbname): + return _component_databases.get(dbname) + + +def is_component_registry_ready(dbname): + """Return True if the registry is ready to be used.""" + comp_registry = get_component_registry(dbname) + return comp_registry.ready if comp_registry else False