Debian Codegraph @

doc/source/contributing/kiwi_plugin_architecture.rst

raw
1 Plugin Architecture
2 ===================
3
4 Each command provided by {kiwi} is written as a task plugin under the
5 **kiwi.tasks** namespace. As a developer, you can extend {kiwi} with custom task
6 plugins, following the conventions below.
7
8 Naming conventions
9 ------------------
10
11 Task plugin file name
12 The file name of a task plugin must follow the pattern
13 :file:`<service>_<command>.py`. This allows you to invoke the task
14 with :command:`kiwi-ng service command ...`.
15
16 Task plugin option handling
17 {kiwi} uses the `docopt` module to handle options. Each task plugin
18 must use `docopt` to allow option handling.
19
20 Task plugin class
21 The implementation of the plugin must be a class that matches the naming
22 convention `<Service><Command>Task`. The class must inherit from the
23 :class:`CliTask` base class. On plugin startup, {kiwi} expects an
24 implementation of the `process` method.
25
26 Task plugin entry point
27 Registration of the plugin must be done in :file:`pyproject.toml`
28 using the `tool.poetry.plugins` concept.
29
30 .. code:: python
31
32 [tool.poetry]
33 name = "kiwi_plugin"
34
35 packages = [
36 { include = "kiwi_plugin"},
37 ]
38
39 [tool.poetry.plugins]
40 [tool.poetry.plugins."kiwi.tasks"]
41 service_command = "kiwi_plugin.tasks.service_command"
42
43 Example plugin
44 --------------
45
46 .. note::
47
48 The following example assumes an existing Python project
49 which was set up using poetry and pyproject.toml.
50
51 1. Assuming the project namespace is **kiwi_relax_plugin**, create the task
52 plugin directory :file:`kiwi_relax_plugin/tasks`.
53
54 2. Create the entry point in :command:`pyproject.toml`.
55
56 Assuming we want to create the service named **relax** that has
57 the command **justdoit**, this is the required plugin
58 definition in :file:`pyproject.toml`:
59
60 .. code:: python
61
62 [tool.poetry]
63 name = "kiwi_relax_plugin"
64
65 packages = [
66 { include = "kiwi_relax_plugin"},
67 ]
68
69 [tool.poetry.plugins]
70 [tool.poetry.plugins."kiwi.tasks"]
71 relax_justdoit = "kiwi_relax_plugin.tasks.relax_justdoit"
72
73 3. Create the plugin code in the file
74 :file:`kiwi_relax_plugin/tasks/relax_justdoit.py` with the following
75 content:
76
77 .. code:: python
78
79 """
80 usage: kiwi-ng relax justdoit -h | --help
81 kiwi-ng relax justdoit --now
82
83 commands:
84 justdoit
85 time to relax
86
87 options:
88 --now
89 right now. For more details about docopt,
90 see: http://docopt.org
91 """
92 # These imports require kiwi to be part of your environment.
93 # It can be either installed from pip into a virtual development
94 # environment or from the distribution package manager.
95 from kiwi.tasks.base import CliTask
96 from kiwi.help import Help
97
98 class RelaxJustdoitTask(CliTask):
99 def process(self):
100 self.manual = Help()
101 if self.command_args.get('help') is True:
102 # The following will invoke man to show the man page
103 # for the requested command. Thus, for the call to
104 # succeed, a man page needs to be written and
105 # installed by the plugin.
106 return self.manual.show('kiwi::relax::justdoit')
107
108 print(
109 'https://genius.com/Frankie-goes-to-hollywood-relax-lyrics'
110 )
111
112 4. Test the plugin.
113
114 .. code:: bash
115
116 $ poetry run kiwi-ng relax justdoit --now