configuration with click builder
Project description
Cock
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.
dict
-like, flat, sorted, dot-accessed configuration object.- Entrypoint builder.
License
cock
is offered under MIT license.
Requirements
- python 3.7+
Usage
example.py
:
import click
from cock import build_entrypoint, Config
def main(config: 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, Config
def main(config: 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")
Config
is an extended (with dot-access) version of sortedcontainers.SortedDict
>>> from cock import Config
>>> c = Config(b=1, a=2)
Config({'a': 2, 'b': 1})
>>> c["a"], c.b
(2, 1)
>>> c.z
...
KeyError: 'z'
>>> c.items()
SortedItemsView(Config({'a': 2, 'b': 1}))
>>> c["0"] = 0
>>> c
Config({'0': 0, 'a': 2, 'b': 1})
API
def build_entrypoint(
main: Callable[[Config], 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 ofclick.option
decorators.**context_settings
is a dict passed through tocommand
decorator.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file cock-0.8.0.tar.gz
.
File metadata
- Download URL: cock-0.8.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3aa87db6b67cadb521a20ffdfb55efe568ced82b5c10398f8707bca379d8abf |
|
MD5 | 6027f71512959e2f97012df180d5b2ec |
|
BLAKE2b-256 | 9b99f80db77e0821319507dd6a1f4c8f4ae3d42dcecb29f699b3d7ca4fdc6d4d |
File details
Details for the file cock-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: cock-0.8.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80615b5a18ffd67abde5644c79d02933a4feae616e7e3b655f20e3e365c603d9 |
|
MD5 | 55fd3adda3e0d1926df2100c7f38143d |
|
BLAKE2b-256 | 40c3598e647c35588260fcc655ba7814acb0c69ba17dd4efa2c840e39021b096 |