|
| 1 | +Configuration providers |
| 2 | +----------------------- |
| 3 | + |
| 4 | +.. currentmodule:: dependency_injector.providers |
| 5 | + |
| 6 | +:py:class:`Configuration` provider provides configuration options to the other providers. |
| 7 | + |
| 8 | +.. literalinclude:: ../../examples/providers/configuration/configuration.py |
| 9 | + :language: python |
| 10 | + :emphasize-lines: 7,12-13,18-25 |
| 11 | + :linenos: |
| 12 | + |
| 13 | +It implements "use first, define later" principle. |
| 14 | + |
| 15 | +Loading from ``ini`` file |
| 16 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 17 | + |
| 18 | +:py:class:`Configuration` provider can load configuration from ``ini`` file using |
| 19 | +:py:meth:`Configuration.from_ini`: |
| 20 | + |
| 21 | +.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py |
| 22 | + :language: python |
| 23 | + :lines: 6- |
| 24 | + :emphasize-lines: 3 |
| 25 | + :linenos: |
| 26 | + |
| 27 | +where ``examples/providers/configuration/config.ini`` is: |
| 28 | + |
| 29 | +.. literalinclude:: ../../examples/providers/configuration/config.ini |
| 30 | + :language: ini |
| 31 | + :linenos: |
| 32 | + |
| 33 | +Loading from ``yaml`` file |
| 34 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 35 | + |
| 36 | +:py:class:`Configuration` provider can load configuration from ``yaml`` file using |
| 37 | +:py:meth:`Configuration.from_yaml`: |
| 38 | + |
| 39 | +.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py |
| 40 | + :language: python |
| 41 | + :lines: 6- |
| 42 | + :emphasize-lines: 3 |
| 43 | + :linenos: |
| 44 | + |
| 45 | +where ``examples/providers/configuration/config.yml`` is: |
| 46 | + |
| 47 | +.. literalinclude:: ../../examples/providers/configuration/config.yml |
| 48 | + :language: ini |
| 49 | + :linenos: |
| 50 | + |
| 51 | +.. note:: |
| 52 | + |
| 53 | + Loading configuration from yaml requires ``PyYAML`` package. You can install |
| 54 | + `Dependency Injector` with extras ``pip install dependency-injector[yaml]`` or install |
| 55 | + ``PyYAML`` separately ``pip install pyyaml``. |
| 56 | + |
| 57 | +Loading from environment variable |
| 58 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | +:py:class:`Configuration` provider can load configuration from environment variable using |
| 61 | +:py:meth:`Configuration.from_env`: |
| 62 | + |
| 63 | +.. literalinclude:: ../../examples/providers/configuration/configuration_env.py |
| 64 | + :language: python |
| 65 | + :lines: 13-21 |
| 66 | + :emphasize-lines: 3-5 |
| 67 | + :linenos: |
| 68 | + |
| 69 | + |
| 70 | +Loading from multiple sources |
| 71 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +:py:class:`Configuration` provider can load configuration from multiple sources. Loaded |
| 74 | +configuration is merged recursively over existing configuration. |
| 75 | + |
| 76 | +.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py |
| 77 | + :language: python |
| 78 | + :lines: 6-14 |
| 79 | + :emphasize-lines: 3-4 |
| 80 | + :linenos: |
| 81 | + |
| 82 | +where ``examples/providers/configuration/config.local.yml`` is: |
| 83 | + |
| 84 | +.. literalinclude:: ../../examples/providers/configuration/config.local.yml |
| 85 | + :language: ini |
| 86 | + :linenos: |
| 87 | + |
| 88 | +.. disqus:: |
0 commit comments