Skip to main content

Example that plugs into dhm_module_base

Project description

DHM Example Module

Basic module meant as a plugin for dhm_module_base. It shows basic functionality on how to "plug" in commands into the main CLI.

Plugin for https://github.com/Kortforsyningen/dhm-module-base

The CLI module is based on Click https://click.palletsprojects.com/en/7.x/

Module is packaged by PyPi: https://pypi.org/project/dhm-module-example/

Build status Linux: CircleCI

Installation

Conda Environment

git clone https://github.com/Kortforsyningen/dhm-module-example
cd dhm-module-example
conda env create -n dhm_module_example -f environment.yml
conda activate dhm_module_example
pip install .

The plugin can also be installed in developer or "editable" mode.

git clone https://github.com/Kortforsyningen/dhm-module-example
cd dhm-module-base
conda env create -n dhm_module_example -f environment-dev.yml
conda activate dhm_module_example
pip install -e .

Usage

This plugin has dhm_module_base https://github.com/Kortforsyningen/dhm-module-base as a dependency, and does not work without it.

This plugin "plugs" into dhm_module_base using a click group and a set of entrypoints in setup.py

Adding commands is done by adding an entrypoint to setup.py under the [dhm_module_base.plugins] section. Each command has to be added here for it to be exposed on the CLI.

ENTRY_POINTS = """
      [dhm_module_base.plugins]
      inout=dhm_module_example.core:inout
      pipe=dhm_module_example.core:pipe
      configuration=dhm_module_example.core:configuration

"""

Calling the base plugin will now reveal these three commands:

Usage: dhm_module_base [OPTIONS] COMMAND [ARGS]...

  dhm_module_base command line interface.

Options:
  --version                       Show the version and exit.
  -v, --verbosity [CRITICAL|ERROR|WARNING|INFO|DEBUG]
                                  Set verbosity level
  -c, --config FILENAME           Configuration file
  --help                          Show this message and exit.

Commands:
  configuration  Example command configuration.
  inout          Example command inout.
  pipe           Example of a custom options handler being used along with
                 a...

If a command is not able to be imported it is considered broken, and will display a small error on the CLI. If we deliberately break a command (we can mispell to command in the entrypoing) we get the following error:

Usage: dhm_module_base [OPTIONS] COMMAND [ARGS]...

  dhm_module_base command line interface.

Options:
  --version                       Show the version and exit.
  -v, --verbosity [CRITICAL|ERROR|WARNING|INFO|DEBUG]
                                  Set verbosity level
  -c, --config FILENAME           Configuration file
  --help                          Show this message and exit.

Commands:
  configuration  † Warning: could not load plugin. See `dhm_module_base
                 configuration --help`.

  inout          Example command inout.
  pipe           Example of a custom options handler being used along with
                 a...

To debug, we check dhm_module_base configuration --help and get the following output:

ImportError: module 'dhm_module_example.core' has no attribute 'configurationsssss'

This error is fabricated for the purpose of this example. Usually commands will return errors if package requirements are not met, commands are not linked properly in setup.py etc.

Registering a new command

Registering a new command is done by decorating a python function with the @click.command() decorator, telling click that the function is a CLI command. Descriptions are harvested by the functions docstring, so make sure it's descriptive of what the command does. Arguments and options are added by using the @click.argument() and @click.option() decorators. For more information see:

https://click.palletsprojects.com/en/7.x/quickstart/#basic-concepts-creating-a-command

Accessing the configuration object

Click commands can access the current context ctx by decorating commands with @click.pass_context and setting the first argument to ctx. The base module puts the configuration object on ctx.obj["config"], so if a command is decorated with @click.pass_context this context can be fetched. See https://click.palletsprojects.com/en/7.x/commands/#nested-handling-and-contexts.

The base module can be supplied with an .ini file using the -c | --config option. A command could look like dhm_module_base -c "config.ini" inout - "out.txt" which will call the inout plugin command with a base configuration in config.ini. If inout is decorated with pass_context, configuration specific options like database connections or paths can be retrieved.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dhm_module_example-0.0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

dhm_module_example-0.0.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file dhm_module_example-0.0.1.tar.gz.

File metadata

  • Download URL: dhm_module_example-0.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for dhm_module_example-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ffdcbd64522266642a164b1abd71b656b0e6b4ba3d1989abbad924940128d6b7
MD5 fa75d142624b9ea4257d3dbd74d177dd
BLAKE2b-256 09eaae0fe6ae5634db39c850602f54873ff3f05129e81445777a776dfcd7f2ee

See more details on using hashes here.

File details

Details for the file dhm_module_example-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dhm_module_example-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for dhm_module_example-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9822cc6ac272a8f26f79421f326949900341db68e5406c4ba22920dbe3761a9
MD5 b488ee490ba4d75fff0e146d82cd9330
BLAKE2b-256 f5b519ca04d19cb84ba4e6484bd3e9ebef88961995e2e62f40578ba1c10cba56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page