Debian Codegraph @

doc/extending.rst

raw
1 .. meta::
2 :description: How to modify to core doit components, extend and create CLI programs
3 :keywords: python, doit, documentation, guide, extending, framework, CLI
4
5 .. title:: Using doit as a framework for CLI power-tools - pydoit guide
6
7
8 =========================
9 Extending `doit`
10 =========================
11
12 .. _extending:
13
14 `doit` is built to be extended and this can be done in several levels.
15 So far we have seen:
16
17 1) User's can create new ways to define when a task is up-to-date using
18 the `uptodate` task parameter (:ref:`more <uptodate_api>`)
19 2) You can customize how tasks are executed by creating new Action types
20 (:ref:`more <custom-actions>`)
21 3) Tasks can be created in different styles by creating custom
22 task creators (:ref:`more <create-doit-tasks>`)
23 4) The output can be configured by creating custom
24 reports (:ref:`more <reporter>`)
25
26
27 Apart from those, `doit` also provides a plugin system and
28 expose it's internal API so you can create new applications on top of `doit`.
29
30
31 .. _custom_loader:
32
33 task loader customization
34 ===========================
35
36 The task loader controls the source/creation of tasks.
37 Normally `doit` tasks are defined in a `dodo.py` file.
38 This file is loaded, and the list of tasks is created from
39 the dict containing task meta-data from the *task-creator* functions.
40
41 Subclass ``TaskLoader2`` to create a custom loader:
42
43 .. autoclass:: doit.cmd_base.TaskLoader2
44 :members:
45
46 Before the introduction of ``TaskLoader2`` a now deprecated loader interface ``TaskLoader`` was
47 used, which did not separate the setup, configuration loading and task loading phases explicit.
48
49 The main program is implemented in the `DoitMain`. It's constructor
50 takes an instance of the task loader to be used.
51
52 Example: pre-defined task
53 ----------------------------
54
55 In the full example below a application is created where the only
56 task available is defined using a dict (so no `dodo.py` will be used).
57
58 .. literalinclude:: samples/custom_loader.py
59
60
61 .. _ModuleTaskLoader:
62
63 Example: load tasks from a module
64 -------------------------------------
65
66 The `ModuleTaskLoader` can be used to load tasks from a specified module,
67 where this module specifies tasks in the same way as in `dodo.py`.
68 `ModuleTaskLoader` is included in `doit` source.
69
70 .. literalinclude:: samples/module_loader.py
71
72 `ModuleTaskLoader` can take also take a `dict` where its items are functions
73 or methods of an object.
74
75
76 .. _custom_command:
77
78 command customization
79 =====================
80
81 In `doit` a command usually perform some kind of operations on tasks.
82 `run` to execute tasks, `list` to display available tasks, etc.
83
84 Most of the time you should really be creating tasks but
85 when developing a custom application on top of `doit` it may make sense
86 to provide some extra commands...
87
88 To create a new command, subclass `doit.cmd_base.Command`
89 set some class variables and implement the `execute` method.
90
91
92 .. autoclass:: doit.cmd_base.Command
93 :members: execute
94
95
96 ``cmd_options`` uses the same format as
97 :ref:`task parameters <parameters-attributes>`.
98
99
100 If the command needs to access tasks it should
101 sub-class `doit.cmd_base.DoitCmdBase`.
102
103
104 Example: scaffolding
105 ----------------------
106
107 A common example is applications that provide some kind of scaffolding when
108 creating new projects.
109
110 .. literalinclude:: samples/custom_cmd.py
111
112
113
114 .. _plugins:
115
116 plugins
117 =======
118
119 `doit` plugin system is based on the use of *entry points*, the plugin
120 does not need to implement any kind of "plugin interface".
121 It needs only to implement the API of the component it is extending.
122
123 Plugins can be enabled in 2 different ways:
124
125 - *local plugins* are enabled through the `doit.cfg` file.
126 - plugins installed with *setuptools* (that provide an entry point),
127 are automatically enabled on installation.
128
129 Check this `sample plugin <https://github.com/pydoit/doit-plugin-sample>`_
130 for details on how to create a plugin.
131
132
133 config plugin
134 -------------
135
136 To enable a plugin create a section named after the plugin category.
137 The value is an entry point to the python class/function/object
138 that implements the plugin. The format is `<module-name>:<attribute-name>`.
139
140 Example of command plugin configured in `pyproject.toml`, implemented in
141 the *class* `FooCmd`, located at the module `my_plugins.py`:
142
143 .. code-block:: toml
144
145 [tool.doit.plugins.command]
146 foo = "my_plugins:FooCmd"
147
148 Similarly, in `doit.cfg`:
149
150 .. code-block:: ini
151
152 [COMMAND]
153 foo = my_plugins:FooCmd
154
155 .. note::
156
157 The python module containing the plugin must be in the *PYTHONPATH*.
158
159
160 category COMMAND
161 ----------------
162
163 Creates a new sub-command. Check :ref:`command <custom_command>` section
164 for details on how to create a new command.
165
166
167 category BACKEND
168 ----------------
169
170 Implements the internal `doit` DB storage system.
171 Check the module `doit/dependency.py` to see the existing implementation / API.
172
173
174 .. _plugin_reporter:
175
176 category REPORTER
177 -----------------
178
179 Register a custom reporter as introduced in the
180 :ref:`custom reporter<custom_reporter>` section.
181
182
183 category LOADER
184 ----------------
185
186 Creates a custom task loader. Check :ref:`loader <custom_loader>` section
187 for details on how to create a new command.
188
189 Apart from getting the plugin you also need to indicate which loader will be
190 used. In the `tool.doit` section in `pyproject.toml`:
191
192 .. code-block:: toml
193
194 [tool.doit]
195 loader = "my_loader"
196
197 [tool.doit.plugins.loader]
198 my_loader = "my_plugins:Loader"
199
200
201 In the `GLOBAL` section of `doit.cfg`:
202
203 .. code-block:: ini
204
205 [GLOBAL]
206 loader = my_loader
207
208 [LOADER]
209 my_loader = my_plugins:MyLoader