Generates CWL documents and associated parameters from a Click CLI definition
Project description
Click2CWL - from a Click context to a CWL document
Rational and context
EO application developers use Click to create command line tools to process EO data.
Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with sensible defaults out of the box. It aims to make the process of writing command line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API.
To deploy these applications on an Exploitation Plaform on the Cloud, the EO application developers generate a CWL document that becomes an Application Package.
The repo contains a helper Python module that exploits the information contained in a Click context object to generate the CWL document and eventually the parameters.
This helper Python module can thus be used to add "CWL dump" behaviour to a Python CLI that uses Click by adding an import and call to the dump(ctx) function provided in the helper Python module.
Installation
Install this Python module in your conda environment using conda or mamba (see this article to learn about mamba):
pip install -c terradue click2cwl
Note: Add click2cwl dependency and terradue channel in your environment.yml file, e.g.:
name: my_env
channels:
- terradue
- conda-forge
dependencies:
- python
pip:
- click2cwl
Usage
First import the click2cwl.dump function with:
from click2cwl import dump
Then, in the Python application entry point function, update and enrich the Click function decorator with the information for generating the CWL document.
The information defined at the Click function decorator is explained below:
@click.command
The @click.command decorator must:
- set the
allow_extra_argsflag toTrue. This allows using the command line interface to pass the runtime information to generate the CWL. - set the
short_helpto define the CWL Workflow class label - set the
helpto define the CWL Workflow class doc
@click.command(
short_help="This is Workflow class label",
help="This is Workflow class doc",
context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
),
)
@click.option
The @click.option decorator must:
- define the option type that can be
click.Pathfor aDirectory, aclick.Filefor aFileor the default, aStringto map asstring - set the
helpto define the CWL Workflow class doc and label - set the
requiredflag toFalseif the parameter is optional
@click.option(
"--input_path",
"-i",
"input_path",
type=click.Path(),
help="this input path",
multiple=False,
required=True,
)
def cli(): ...
@click.argument
The @click.argument decorator must:
- define the argument type (same types as for
@click.option) - must OMIT the
help(this is a limitation ofclick, see https://github.com/pallets/click/issues/587) - it will be automatically marked as
required=True
@click.argument(
"input_path",
type=click.Path(),
)
def cli(): ...
The advantage of using @click.argument over @click.option is that it will correspond to a positional argument,
for cases where to option flag applies (i.e.: --<some-option>). However, it comes with limitations in terms of
additional parameters offered by click.
@click2cwl.dump
Finally, invoke the dump function in your Click decorated function with:
@click.command ...
@click.option ...
@click.pass_context
def main(ctx, **kwargs):
click2cwl.dump(ctx)
print("business as usual")
print(kwargs)
if __name__ == '__main__':
main()
Note: See the examples folder to discover typical use cases.
CWL Exporters
There are multiple exporters to deal with the various contents:
click2cwl.clt.CLTExport: CWL CommandLineTool directlyclick2cwl.cwlexport.CWLExport: CWL Workflows and its underlying CommandLineToolclick2cwl.paramexport.ParamExport: CWL Job Parameters
These can be invoked directly instead of the @click2cwl.dump
operation to extract the contents separately from the CLI invocation.
Building the application
Build your application with:
hatch build
When the app help is invoked, it will only show the arguments defined by the click.decorators:
myapp --help
but it now supports additional args to drive the generation of the CWL document and associated parameters:
The additional args are:
--dumpcwl|params|clt. Example--dump cwl --dump paramswill dump the CWL document and the CWL parameters template in YAML.cltwill dump the CWlCommandLineToolclass only (no Workflow)--requirementwithrequirement=valuewhere requirement here is one of"coresMin","coresMax","ramMin","ramMax". Example:--requirement ramMax=1 --requirement ramMin=2--docker <docker image>if set, theDockerRequirementhint is set to pull the<docker image>--envsets environment variables in the CWL withenv_var=env_var_value. Example--env A=1 --env B=2where A and B are the environment variables to set in the CWLEnvVarRequirementrequirement--wall-time <wall-time in seconds>if set, theToolTimeLimitcWL requirement is set and the CWL document version is set to v1.1 (instead of default 1.0)--cwl-version, if set, thecwlVersionuses that value
Try me on Binder
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file click2cwl-0.4.0.tar.gz.
File metadata
- Download URL: click2cwl-0.4.0.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c03d7593cfc4bcacd688e942cb235285d8ea3d939ef524de3f8985ec692fbda
|
|
| MD5 |
eb2f1582b5d07ae86d2af7d52d06e5bf
|
|
| BLAKE2b-256 |
b811f94461faf212808a4d2409101b156c3fd04bf43d38104692fd6a5784c59a
|
Provenance
The following attestation bundles were made for click2cwl-0.4.0.tar.gz:
Publisher:
package.yaml on Terradue/click2cwl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click2cwl-0.4.0.tar.gz -
Subject digest:
7c03d7593cfc4bcacd688e942cb235285d8ea3d939ef524de3f8985ec692fbda - Sigstore transparency entry: 422062593
- Sigstore integration time:
-
Permalink:
Terradue/click2cwl@f13937d0e1598e29016d5014007694e2b1ec6b5f -
Branch / Tag:
refs/heads/master - Owner: https://github.com/Terradue
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package.yaml@f13937d0e1598e29016d5014007694e2b1ec6b5f -
Trigger Event:
push
-
Statement type:
File details
Details for the file click2cwl-0.4.0-py3-none-any.whl.
File metadata
- Download URL: click2cwl-0.4.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e93c57c11c26935e86222a7fecef4105b01ad114745bf7696c60f871f1ea2c2c
|
|
| MD5 |
5a671ba81d4a03363449c161cf77e026
|
|
| BLAKE2b-256 |
ad16e5e5c4138f3aa7c4df24838d9c86a7cfffab7a2d08334d8e4a660e05fd5a
|
Provenance
The following attestation bundles were made for click2cwl-0.4.0-py3-none-any.whl:
Publisher:
package.yaml on Terradue/click2cwl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
click2cwl-0.4.0-py3-none-any.whl -
Subject digest:
e93c57c11c26935e86222a7fecef4105b01ad114745bf7696c60f871f1ea2c2c - Sigstore transparency entry: 422062617
- Sigstore integration time:
-
Permalink:
Terradue/click2cwl@f13937d0e1598e29016d5014007694e2b1ec6b5f -
Branch / Tag:
refs/heads/master - Owner: https://github.com/Terradue
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package.yaml@f13937d0e1598e29016d5014007694e2b1ec6b5f -
Trigger Event:
push
-
Statement type: