Skip to main content

click-schema-config

click-schema-config allows you to add settings from a config file. Those will be automatically pulled into your program description without having to repeat them. Comments will be used as helper text for click.

Is this project necessary?

After having built and used this, I am no longer convinced it is the best approach. I think yt-dlp and parallel way of doing it might be more pertinent: Having profile files that specifies what options to enable.
That said, codegen-ini solves the problem of packaging click's parameters and well-tying them, which has been a constant pain point for me.
I will continue developping and using this tool for now, but it might not be the best way to go about this problem.

Installation

poetry add click-schema-config

or, using pip

pip install click-schema-config

Usage

Decorate your function with

@schema_from_inis(files=[...])

This will automatically infer the structure of your ini files and its documentation and add it to click.

Example of a config.default.ini:

initialized_to_none =

[test1]
; Wow, multilines
; Talk about eye candy
multiline_commented="value1"
typed: int = 2
inferred = True

[test2]
inline_commented = "value1" # This comment does not appear in the documentation

; This is a comment
after_paragraph = None

Note that you can type values directly. If a parameter appears without = succeding it, it becomes a required parameter.

main.py

import pprint
import click
from click_schema_config import schema_from_inis


@click.command()
@schema_from_inis(files=["config.default.ini"])
def main(**kwargs):
    pprint.pprint(kwargs)

if __name__ == "__main__":
    main()

This will result in:

python __main__.py --help

Usage: __main__.py [OPTIONS]

Options:
  --initialized_to_none TEXT
  --test1.multiline_commented TEXT
                                  Wow, multilines
                                  Talk about eye candy  [default: value1]
  --test1.typed INTEGER            [default: 2]
  --test1.inferred / --no-test1.inferred
                                   [default: test1.inferred]
  --test2.inline_commented TEXT    [default: value1]
  --test2.after_paragraph TEXT    This is a comment
  --help                          Show this message and exit.

and

python __main__.py

{'initialized_to_none': None,
 'required_parameter_overriden': 'not required',
 'test1__inferred': True,
 'test1__multiline_commented': 'value1',
 'test1__typed': 2,
 'test2__after_paragraph': None,
 'test2__inline_commented': 'value1'}

You can of course override using the options:

python main.py --test2.inline_commented "HEY"

{'initialized_to_none': None,
 'required_parameter_overriden': 'not required',
 'test1__inferred': True,
 'test1__multiline_commented': 'value1',
 'test1__typed': 2,
 'test2__after_paragraph': None,
 'test2__inline_commented': 'HEY'}

Rationale

Having setting files allow for separation of concerns and for users to know what they are expected to tweak and modify. This library is here to provide schema specifications of settings.

Codegen-ini

This library also comes with the codegen-ini tool that allows you to have well-typed click dictionaries. For instance, in the following code:

@click.command()
@schema_from_inis(files=["config.default.ini"])
def main(**kwargs):
    pprint.pprint(kwargs)

It is unclear what keywords are present in kwargs, and what can be accessed. This makes for uncoupled code where changing the configuration leads to bugs and undefined variables. To solve this, we can use codegen-ini to generate a pydantic class for us

codegen.yaml

schemas:
  MyClass: "./config.default.ini"

and run it

codegen-ini

The following code will be generated in ./generated/MyClass.py:

from pydantic import BaseModel


class MyClass(BaseModel):
    initialized_to_none: None = None
    required_parameter_overriden: str
    required_parameter: int

    """
    Wow, multilines
    Talk about eye candy
    """
    test1__multiline_commented: str = "value1"

    test1__typed: int = 2
    test1__inferred: bool = True
    test2__inline_commented: str = "value1"

    """
    This is a comment
    """
    test2__after_paragraph: None = None


class MyClassPartial(BaseModel):
    initialized_to_none: None | None = None
    required_parameter_overriden: str | None = None
    required_parameter: int | None = None

    """
    Wow, multilines
    Talk about eye candy
    """
    test1__multiline_commented: str | None = None

    test1__typed: int | None = None
    test1__inferred: bool | None = None
    test2__inline_commented: str | None = None

    """
    This is a comment
    """
    test2__after_paragraph: None | None = None

Which you can use to type your passed config:

from generated.codegen_ini import MyClass
def main(**kwargs):
    config = MyClass(**kwargs)
    print(config.test1__multiline_commented) # Well typed

TODO

  • Integration with click types, like click.choices and click.intrange
  • Test suite
  • Preserve section structure in codegen-ini's generated classes?

Metadata

Release files for click-schema-config 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for click-schema-config 0.2.5
File Size Uploaded
click_schema_config-0.2.5.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for click-schema-config 0.2.5
File Interpreter ABI Platform
click_schema_config-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / click_schema_config-0.2.5.tar.gz

Download URL click_schema_config-0.2.5.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
920b5d84aff327fffd65aaa91164cd28a9e8802de18aad1061fa15e6ee49c38b
BLAKE2b-256 checksum
How to use checksums
bda6c9f9499c5660006eb3fa4d57c5414a90b13472dc201bd727115c71bc2de4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.8.10 Linux/5.15.0-76-generic

Release files / click_schema_config-0.2.5-py3-none-any.whl

Download URL click_schema_config-0.2.5-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7082b929afc77aae1f92a4704d4556c16c47d3c77771a532103c0572a3ea16a2
BLAKE2b-256 checksum
How to use checksums
f83a6a22f888771e9ca627dadc3b0a19fefb6b1fd2ade41b583cb0c88be528e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.8.10 Linux/5.15.0-76-generic

Release history Release notifications | RSS feed

This release

0.2.5 This release

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page