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 the {kiwi}
6 system command space with custom task plugins, following the conventions
7 below.
8
9 Naming conventions
10 ------------------
11
12 Task plugin file name
13 The file name of a task plugin must follow the pattern
14 :file:`system_<command>.py`. This allows you to invoke the task
15 with :command:`kiwi-ng system command ...`
16
17 Task plugin option handling
18 {kiwi} uses the typer module to handle options. Each task plugin
19 must use typer to allow option handling. The typer definition
20 must be provided in a file named :file:`cli.py` and must live in the
21 toplevel of the plugin python namespace.
22
23 Task plugin class
24 The implementation of the plugin must be a class that matches the naming
25 convention :class:`System<Command>Task`. The class must inherit from the
26 :class:`CliTask` base class. On the plugin startup, {kiwi} expects an
27 implementation of the :file:`process` method.
28
29 Task plugin entry point
30 Registration of the plugin must be done in :file:`pyproject.toml`
31 using the `tool.poetry.plugins` concept.
32
33 .. code::
34
35 [tool.poetry]
36 name = "kiwi_plugin"
37
38 packages = [
39 { include = "kiwi_plugin"},
40 ]
41
42 [tool.poetry.plugins]
43 [tool.poetry.plugins."kiwi.tasks"]
44 system_<command> = "kiwi_<pluginname>_plugin.tasks.system_<command>"
45
46 Example plugin
47 --------------
48
49 .. note::
50
51 The following example assumes an existing Python project
52 which was set up using poetry and pyproject.toml.
53
54 1. Assuming the project namespace is **kiwi_relax_plugin**, create the task
55 plugin directory :file:`kiwi_relax_plugin/tasks`.
56
57 2. Create the entry point in :command:`pyproject.toml`.
58
59 Assuming we want to create the system command **justdoit**, this is
60 the following entry point definition in :file:`pyproject.toml`:
61
62 .. code:: toml
63
64 [tool.poetry]
65 name = "kiwi_relax_plugin"
66
67 packages = [
68 { include = "kiwi_relax_plugin"},
69 ]
70
71 [tool.poetry.plugins]
72 [tool.poetry.plugins."kiwi.tasks"]
73 system_justdoit = "kiwi_relax_plugin.tasks.system_justdoit"
74
75 3. Create the typer cli interface in the file
76 :file:`kiwi_relax_plugin/cli.py` with the following
77 content:
78
79 .. code:: python
80
81 import typer
82 from typing import Annotated
83
84 # typers variable must be provided for kiwi plugins
85 typers = {
86 'justdoit': typer.Typer(add_completion=False)
87 }
88
89 system = typers['justdoit']
90
91 @system.callback(
92 help='What is it good for'
93 invoke_without_command=True,
94 subcommand_metavar=''
95 )
96 def justdoit(
97 ctx: typer.Context,
98 now: Annotated[str, typer.Option(help='For --now option')]
99 ):
100 Cli=ctx.obj
101 Cli.subcommand_args['justdoit'] = {
102 '--now': now,
103 'help': False
104 }
105 Cli.global_args['command'] = 'justdoit'
106 Cli.global_args['system'] = True
107 Cli.cli_ok = True
108
109 4. Create the plugin code in the file
110 :file:`kiwi_relax_plugin/tasks/system_justdoit.py` with the following
111 content:
112
113 .. code:: python
114
115 # These imports requires kiwi to be part of your environment
116 # It can be either installed from pip into a virtual development
117 # environment or from the distribution package manager.
118 from kiwi.tasks.base import CliTask
119 from kiwi.help import Help
120
121 class SystemJustdoitTask(CliTask):
122 def process(self):
123 self.manual = Help()
124 if self.command_args.get('help') is True:
125 # The following will invoke man to show the man page
126 # for the requested command. Thus, for the call to
127 # succeed, a man page needs to be written and
128 # installed by the plugin.
129 return self.manual.show('kiwi::relax::justdoit')
130
131 if self.command_args.get('--now'):
132 print(
133 'https://genius.com/Frankie-goes-to-hollywood-relax-lyrics'
134 )
135
136 5. Test the plugin
137
138 .. code:: bash
139
140 $ poetry run kiwi-ng system justdoit --now