| 1 |
.. meta:: |
| 2 |
:description: Configuring pydoit with TOML and INI files |
| 3 |
:keywords: python, doit, documentation, guide, configuration, ini, toml |
| 4 |
|
| 5 |
.. title:: Configuration with TOML and INI files - pydoit guide |
| 6 |
|
| 7 |
|
| 8 |
Configuration |
| 9 |
============= |
| 10 |
|
| 11 |
|
| 12 |
config param names |
| 13 |
------------------ |
| 14 |
|
| 15 |
Note that the configuration option's name is not always the same as the |
| 16 |
*long* argument name used in the command line. |
| 17 |
|
| 18 |
I.e. To specify dodo file other than `dodo.py` from the command line |
| 19 |
you specify the option as ``-f`` or ``--file``, but from a config file |
| 20 |
it is called ``dodoFile``. |
| 21 |
|
| 22 |
The name can be seem from ``doit help`` output:: |
| 23 |
|
| 24 |
-f ARG, --file=ARG load task from dodo FILE [default: dodo.py] (config: dodoFile) |
| 25 |
|
| 26 |
|
| 27 |
pyproject.toml |
| 28 |
-------------- |
| 29 |
|
| 30 |
`doit` configuration can be read from `pyproject.toml <https://www.python.org/dev/peps/pep-0518/>`_ |
| 31 |
under the `tool.doit` namespace. |
| 32 |
This is the preferred configuration source, and may gain features not available in the legacy `doit.cfg`. |
| 33 |
|
| 34 |
.. note:: |
| 35 |
|
| 36 |
A TOML parser (`tomllib <https://docs.python.org/3.11/library/tomllib.html>`_) |
| 37 |
is part of the standard library since Python 3.11. |
| 38 |
For earlier Python versions, a third-party package is required, one of: |
| 39 |
|
| 40 |
- `tomli <https://pypi.org/project/tomli/>`_ |
| 41 |
- `tomlkit <https://pypi.org/project/tomlkit/>`_ |
| 42 |
|
| 43 |
|
| 44 |
TOML vs INI |
| 45 |
^^^^^^^^^^^ |
| 46 |
|
| 47 |
While mostly similar, `TOML <https://toml.io>`_ differs from the INI format |
| 48 |
in a few ways: |
| 49 |
|
| 50 |
- all strings must be quoted with `'` or `"` |
| 51 |
- triple-quoted strings may contain new line characters (`\n`) and quotes |
| 52 |
- must be saved as UTF-8 |
| 53 |
- integers and floating point numbers can be written without quotes |
| 54 |
- boolean values can be written unquoted and lower-cased, as `true` and `false` |
| 55 |
|
| 56 |
Unlike "plain" TOML, `doit` will parse pythonic strings into their correct types, |
| 57 |
e.g. `"True"`, `"False"`, `"3"`, but using "native" TOML types may be preferable. |
| 58 |
|
| 59 |
|
| 60 |
tool.doit |
| 61 |
^^^^^^^^^ |
| 62 |
|
| 63 |
The `tool.doit` section may contain command line options that will be used |
| 64 |
(if applicable) by any commands. |
| 65 |
|
| 66 |
Example setting the DB backend type: |
| 67 |
|
| 68 |
.. code-block:: toml |
| 69 |
|
| 70 |
[tool.doit] |
| 71 |
backend = "json" |
| 72 |
|
| 73 |
All commands that have a `backend` option (*run*, *clean*, *forget*, etc), |
| 74 |
will use this option without the need for this option in the command line. |
| 75 |
|
| 76 |
|
| 77 |
tool.doit.commands |
| 78 |
^^^^^^^^^^^^^^^^^^ |
| 79 |
|
| 80 |
To configure options for a specific command, use a section with |
| 81 |
the command name under `tool.doit.commands`: |
| 82 |
|
| 83 |
.. code-block:: toml |
| 84 |
|
| 85 |
[tools.doit.commands.list] |
| 86 |
status = true |
| 87 |
subtasks = true |
| 88 |
|
| 89 |
|
| 90 |
tool.doit.plugins |
| 91 |
^^^^^^^^^^^^^^^^^ |
| 92 |
|
| 93 |
Check the :ref:`plugins <plugins>` section for an introduction |
| 94 |
on available plugin categories. |
| 95 |
|
| 96 |
|
| 97 |
tool.doit.tasks |
| 98 |
^^^^^^^^^^^^^^^ |
| 99 |
|
| 100 |
To configure options for a specific task, use a section with |
| 101 |
the task name under `tool.doit.tasks`: |
| 102 |
|
| 103 |
.. code-block:: toml |
| 104 |
|
| 105 |
[tool.doit.tasks.make_cookies] |
| 106 |
cookie_type = "chocolate" |
| 107 |
temp = "375F" |
| 108 |
duration = 12 |
| 109 |
|
| 110 |
|
| 111 |
doit.cfg |
| 112 |
-------- |
| 113 |
|
| 114 |
`doit` also supports an INI style configuration file |
| 115 |
(see `configparser <https://docs.python.org/3/library/configparser.html>`_). |
| 116 |
Note: key/value entries can be separated only by the equal sign `=`. |
| 117 |
|
| 118 |
If a file name `doit.cfg` is present in the current working directory, |
| 119 |
it is processed. It supports 4 kind of sections: |
| 120 |
|
| 121 |
- a `GLOBAL` section |
| 122 |
- a section for each plugin category |
| 123 |
- a section for each command |
| 124 |
- a section for each task |
| 125 |
|
| 126 |
|
| 127 |
GLOBAL section |
| 128 |
^^^^^^^^^^^^^^ |
| 129 |
|
| 130 |
The `GLOBAL` section may contain command line options that will |
| 131 |
be used (if applicable) by any commands. |
| 132 |
|
| 133 |
Example setting the DB backend type: |
| 134 |
|
| 135 |
.. code-block:: ini |
| 136 |
|
| 137 |
[GLOBAL] |
| 138 |
backend = json |
| 139 |
|
| 140 |
All commands that have a `backend` option (*run*, *clean*, *forget*, etc), |
| 141 |
will use this option without the need for this option in the command line. |
| 142 |
|
| 143 |
|
| 144 |
commands section |
| 145 |
^^^^^^^^^^^^^^^^ |
| 146 |
|
| 147 |
To configure options for a specific command, use a section with |
| 148 |
the command name: |
| 149 |
|
| 150 |
.. code-block:: ini |
| 151 |
|
| 152 |
[list] |
| 153 |
status = True |
| 154 |
subtasks = True |
| 155 |
|
| 156 |
|
| 157 |
plugins sections |
| 158 |
^^^^^^^^^^^^^^^^ |
| 159 |
|
| 160 |
Check the :ref:`plugins <plugins>` section for an introduction |
| 161 |
on available plugin categories. |
| 162 |
|
| 163 |
|
| 164 |
per-task sections |
| 165 |
^^^^^^^^^^^^^^^^^ |
| 166 |
|
| 167 |
To configure options for a specific task, use a section with |
| 168 |
the task name prefixed with "task:": |
| 169 |
|
| 170 |
.. code-block:: ini |
| 171 |
|
| 172 |
[task:make_cookies] |
| 173 |
cookie_type = chocolate |
| 174 |
temp = 375F |
| 175 |
duration = 12 |
| 176 |
|
| 177 |
|
| 178 |
configuration at *dodo.py* |
| 179 |
-------------------------- |
| 180 |
|
| 181 |
As a convenience you can also set `GLOBAL` options directly into a `dodo.py`. |
| 182 |
Just put the option in the `DOIT_CONFIG` dict. |
| 183 |
This example below sets the default tasks to be run, the ``continue`` option, |
| 184 |
and a different reporter. |
| 185 |
|
| 186 |
.. literalinclude:: samples/doit_config.py |
| 187 |
|
| 188 |
So if you just execute |
| 189 |
|
| 190 |
.. code-block:: console |
| 191 |
|
| 192 |
$ doit |
| 193 |
|
| 194 |
it will have the same effect as executing |
| 195 |
|
| 196 |
.. code-block:: console |
| 197 |
|
| 198 |
$ doit --continue --reporter json my_task_1 my_task_2 |
| 199 |
|
| 200 |
|
| 201 |
.. note:: |
| 202 |
|
| 203 |
Not all options can be set on `dodo.py` file. |
| 204 |
The parameters ``--file`` and ``--dir`` can not be used on config because |
| 205 |
they control how the *dodo* file itself is loaded. |
| 206 |
|
| 207 |
Also if the command does not read the `dodo.py` file it obviously will |
| 208 |
not be used. |