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