Skip to main content

Turns TOML files and command-line arguments into dataclasses for config

Project description

xconf - Dataclasses and TOML for command configuration

Demo

An example of how to use xconf.Command, xconf.field, and xconf.config is in xconf_demo.py. Run it to see its configuration keys.

$ python xconf_demo.py demo_command -h
demo_command: Demo command
usage: xconf_demo.py demo_command [-c CONFIG_FILE] [-h] [-v] [--dump-config] [vars ...]

positional arguments:
  vars                  Config variables set with 'key.key.key=value' notation

optional arguments:
  -c CONFIG_FILE, --config-file CONFIG_FILE
                        Path to config file (default: demo_command.conf)
  -h, --help            Print usage information
  -v, --verbose         Enable debug logging
  --dump-config         Dump final configuration state as TOML and exit

configuration keys:
  collections
      dict[str, ExtendedThingie]
  collections.<str>.name
      str
     <ExtendedThingie>
  collections.<str>.extended
      bool
     <ExtendedThingie>
  either_one
      [int, str]
  should_bar
      bool
     (default: False)
  should_foo
      bool
    Whether demo should foo
  number_list
      list[int]
    List of favorite numbers
  float_list
      list[float]
    List of favorite floating-point numbers
  str_list
      list[str]
    List of favorite strings
  sequence
      list[ExtendedThingie]
     (default: [])
  sequence[#].name
      str
     <ExtendedThingie>
  sequence[#].extended
      bool
     <ExtendedThingie>

Default config file

The command name, demo_command, is generated from the class name and used to find a default configuration file (demo_command.conf) in the current directory.

Providing arguments at the command line

Any configuration key from the help output can be supplied on the command line in a dotted.name=value format.

For lists of primitive types (str, int, float), you can just use commas to separate the values on the right hand side of the =. Example: number_list=1,2,3.

To override a single entry in a list, use some_name[#] or dotted[#].name=value where # is an integer index will work. Example: number_list[0]=99

String values are bare (i.e. no quotation marks around value). Boolean values are case-insensitive true, t, or 1 for True, false, f, or 0 for False.

Structuring the command

See xconf_demo.py for an example. Note that commands must subclass xconf.Command and apply the @xconf.config decorator. Options are defined by a hierarchy of dataclasses. (For uninteresting reasons, they aren't strictly speaking import dataclass dataclasses.)

License

All code outside xconf/vendor/ is provided under the terms of the MIT License, except for xconf_demo.py and demo_command.conf, which are released into the public domain for you to build off of.

Note that code under xconf/vendor/ is used under the terms of the licenses listed there.

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

xconf-0.0.2.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

xconf-0.0.2-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file xconf-0.0.2.tar.gz.

File metadata

  • Download URL: xconf-0.0.2.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for xconf-0.0.2.tar.gz
Algorithm Hash digest
SHA256 591f9acc1690ba5b5915e5d49af00a9f5c0d200ce9b443bface0196827697d71
MD5 bf4b4fe6bfc3413f3f3a55237fc3c2c4
BLAKE2b-256 6d670aa8ba63bbf7016187b9793c505286ca082bfc95dd63fd9d14c0c57beb2f

See more details on using hashes here.

File details

Details for the file xconf-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: xconf-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for xconf-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e11c8d61b1d1aea93bb32cfaaeb599bf08506f2bb31a60606bfed05e2cfd570a
MD5 cd19f7605280357e4b1034893691bb64
BLAKE2b-256 5d26be5d6394c3756a5383f2e76584d177a9e2506b655ccfeccca00fcf9c2b81

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