2015-10-11 12:34:21 +00:00
|
|
|
Overriding of catalogs
|
|
|
|
----------------------
|
|
|
|
|
2015-11-29 21:30:48 +00:00
|
|
|
.. currentmodule:: dependency_injector.catalogs
|
2015-11-20 16:52:19 +00:00
|
|
|
|
2015-10-11 12:34:21 +00:00
|
|
|
Catalogs can be overridden by other catalogs. This, actually, means that
|
|
|
|
all of the providers from overriding catalog will override providers with the
|
|
|
|
same names in overridden catalog.
|
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
There are two ways to override :py:class:`DeclarativeCatalog` with another
|
|
|
|
catalog:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
- Use :py:meth:`DeclarativeCatalog.override` method.
|
|
|
|
- Use :py:func:`override` class decorator.
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
Example of overriding catalog using :py:meth:`DeclarativeCatalog.override`
|
2015-11-10 08:42:29 +00:00
|
|
|
method:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
.. literalinclude:: ../../examples/catalogs/override_declarative.py
|
2015-10-11 12:34:21 +00:00
|
|
|
:language: python
|
2016-04-11 07:43:02 +00:00
|
|
|
:linenos:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
Example of overriding catalog using :py:func:`override` decorator:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
.. literalinclude:: ../../examples/catalogs/override_declarative_decorator.py
|
2015-10-11 12:34:21 +00:00
|
|
|
:language: python
|
2016-04-11 07:43:02 +00:00
|
|
|
:linenos:
|
2015-10-11 12:34:21 +00:00
|
|
|
|
2015-11-20 16:52:19 +00:00
|
|
|
Also there are several useful :py:class:`DeclarativeCatalog` methods and
|
|
|
|
properties that help to work with catalog overridings:
|
|
|
|
|
|
|
|
- :py:attr:`DeclarativeCatalog.is_overridden` - read-only property that is set
|
|
|
|
to ``True`` if catalog is overridden.
|
|
|
|
- :py:attr:`DeclarativeCatalog.last_overriding` - read-only reference to
|
|
|
|
the last overriding catalog, if any.
|
|
|
|
- :py:attr:`DeclarativeCatalog.overridden_by` - tuple of all overriding
|
|
|
|
catalogs.
|
|
|
|
- :py:meth:`DeclarativeCatalog.reset_last_overriding()` - reset last
|
|
|
|
overriding catalog.
|
|
|
|
- :py:meth:`DeclarativeCatalog.reset_override()` - reset all overridings for
|
|
|
|
all catalog providers.
|
|
|
|
|
|
|
|
:py:class:`DynamicCatalog` has exactly the same functionality, except of
|
|
|
|
:py:func:`override` decorator. Also :py:class:`DynamicCatalog` can override
|
|
|
|
:py:class:`DeclarativeCatalog` and vise versa.
|
|
|
|
|
|
|
|
Example of overriding :py:class:`DeclarativeCatalog` by
|
|
|
|
:py:class:`DynamicCatalog`:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/catalogs/override_declarative_by_dynamic.py
|
|
|
|
:language: python
|
2016-04-11 07:43:02 +00:00
|
|
|
:linenos:
|