Skip to main content

configuration with click builder

Project description

Cock

Travis status for master branch Codecov coverage for master branch Pypi version Pypi downloads count

Cock stands for «configuration file with click». It is a configuration aggregator, which stands on shiny click library.

Reason

No module for click with flat configuration file, which will mimic actual click options. There are click-config and click-config-file, but they targets another goals.

Features

  • Aggregate configuration file and cli options into flat configuration object.
  • Respect all click checks and conversions.
  • Flat dot-accessed (addict wrapped) configuration.
  • Entrypoint builder.

License

cock is offered under MIT license.

Requirements

  • python 3.7+

Usage

example.py:

import click

from cock import build_entrypoint


def main(config):
    print(config)


options = [
    click.option("--a-b-c", default="foo"),
    click.option("--b-c-d", default="bar"),
]
entrypoint = build_entrypoint(main, options, auto_envvar_prefix="EXAMPLE", show_default=True)

if __name__ == "__main__":
    entrypoint(prog_name="example")

This is almost pure click setup

$ python example.py --help
Usage: example [OPTIONS] [CONFIGURATION_FILE]

Options:
  --a-b-c TEXT  [default: foo]
  --b-c-d TEXT  [default: bar]
  --help        Show this message and exit.  [default: False]

But there is a CONFIGURATION_FILE argument. Lets see use cases.

All deafults

$ python example.py
{'configuration_file': None, 'a_b_c': 'foo', 'b_c_d': 'bar'}

From environment variable

$ EXAMPLE_A_B_C=foo-env python example.py
{'configuration_file': None, 'a_b_c': 'foo-env', 'b_c_d': 'bar'}

From cli arguments

$ EXAMPLE_A_B_C=foo-env python example.py --a-b-c foo-cli
{'a_b_c': 'foo-cli', 'configuration_file': None, 'b_c_d': 'bar'}

From configuration

config-example.yml:

a-b-c: foo-file
$ EXAMPLE_A_B_C=foo-env python example.py --a-b-c foo-cli config-example.yml
{'a_b_c': 'foo-file', 'configuration_file': '/absolute/path/to/config-example.yml', 'b_c_d': 'bar'}

Priority is obvious: file > cli arguments > env variables

As described in features paragraph, configuration is flattened before chaining with click options. So all configuration files listed below are equal:

a-b-c: foo-file
a:
  b:
    c: foo-file
a-b:
  c: foo-file

If provided file have key crossings:

a-b-c: foo-file1
a:
  b-c: foo-file2

Then ValueError will be raised.

cock uses pyyaml library for config loading, so it supports yaml and json formats, but this can be improved later if someone will need more configuration file types.

Configuration can be defined as dictionary too

from cock import build_entrypoint, build_options_from_dict, Option


def main(config):
    print(config)


options = {
    "a": {
        "b": {
            "c": Option(default="foo"),
        },
    },
}
entrypoint = build_entrypoint(main, build_options_from_dict(options), auto_envvar_prefix="EXAMPLE", show_default=True)

if __name__ == "__main__":
    entrypoint(prog_name="example")

API

def build_entrypoint(
    main: Callable[[AdDict], Any],
    options: List[click.option],
    **context_settings
) -> Callable[..., Any]:
  • main is a user-space function of exactly one argument, a dot-accessed config wrapper.
  • options is an iterable of click.option decorators.
  • **context_settings is a dict passed through to command decorator.

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

cock-0.6.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

cock-0.6.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file cock-0.6.0.tar.gz.

File metadata

  • Download URL: cock-0.6.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.1

File hashes

Hashes for cock-0.6.0.tar.gz
Algorithm Hash digest
SHA256 42fa3842ac17d9f1306bfd3e5e997608437b2e9cc8c811aafb803bcb06f12e8a
MD5 3cb1b939b41e9bd7d140ab3894246878
BLAKE2b-256 b61c8fcd5bb793f6c2ee5148563adaaf178ba6679e756aff125993216ac10339

See more details on using hashes here.

File details

Details for the file cock-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: cock-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.1

File hashes

Hashes for cock-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1625a7fc3abcc3e2c452fe04bd86624fa93c564a869ad0acda94bd0b0267f8e2
MD5 5e659c02db51047afc2496a93003fa1d
BLAKE2b-256 9ab902a2970de67d3a74336817f89200fa91ae1ffa106b095863e38f8f42be84

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