Template for having plugin architecture in Python
- In project directory type:
poetry build - This creates package in
dist/directory - Install it with
pip install dist/menu*.tar.gz - Optionally do the same in demo plugin project
- Each plugin must provide package
menu_plugin_$pluginnamein order to be detected by menu (use poetry to have that one out of the box) - In this default package there must be at least one module called
plugin.py - This module must provide register function with one argument - hook
- This hook provides certain capabilities described separately
- Plugins can get access to logging facility of main menu by importing
menu.logger.Logger, then getting its own child logger with:Logger.get(__name__), where__name__part is crucial to stay this way
Each plugin can get access to subparsers of main argparse object. It can be used to register its own command. Proper way of interaction is also to register a function that will be executed if user chooses to execute the command.
Example:
def execute(args):
print(args)
parser = hook.subparsers.add_parser('command', help='Some command')
parser.set_defaults(func=execute)
In this example, once user executes the following: m command, he will be
presented with all the arguments he passed as internal argparse object.
Menu has integration with argcomplete. This allows effortless completion to be installed in user's shell of choice. It does not require any additional code to provide pretty good completion.
It must be however installed with:
- Executing
activate-global-python-argcomplete --useronce after installing argcomplete - Adding
eval "$(register-python-argcomplete m)"to shell's rc file, like.bashrc.