Skip to main content

Load toml/yaml/json config files into classes for a typed config (type hinting etc.)

Project description

Classy Configuraptor

Configuraptor

Load config files into Python classes for a typed config (for type hinting etc.). Supported file types are toml/yaml/json, and .env/.ini to a lesser degree (see Supported Config File Types).

PyPI - Version PyPI - Python Version
Code style: black License: MIT
su6 checks Coverage

Table of Contents

Installation

pip install configuraptor

Usage

Configuraptor can be used to load your config files into structured Python classes.

# examples/example_from_readme.toml
[config]
name = "Hello World!"

[config.reference]
number = 42
numbers = [41, 43]
string = "42"

Could be loaded into Python classes using the following code:

# examples/example_from_readme.py
from configuraptor import load_into, TypedConfig


######################
# with basic classes #
######################

class SomeRegularClass:
    number: int
    numbers: list[int]
    string: str


class Config:
    name: str
    reference: SomeRegularClass


if __name__ == '__main__':
    my_config = load_into(Config, "example_from_readme.toml")  # or .json, .yaml, ...

    print(my_config.name)
    # Hello World!
    print(my_config.reference.numbers)
    # [41, 43]


########################
# alternative notation #
########################

class SomeOtherRegularClass:
    number: int
    numbers: list[int]
    string: str


class OtherConfig(TypedConfig):
    name: str
    reference: SomeRegularClass


if __name__ == '__main__':
    my_config = OtherConfig.load("https://api.my-server.dev/v1/config.json?secret=token")  # or .toml, .yaml, ...

    print(my_config.name)
    # Hello World!
    print(my_config.reference.numbers)
    # [41, 43]

    # TypedConfig has an extra benefit of allowing .update:
    my_config.update(numbers=[68, 70])

The second argument of .load_into and the first argument of .load (which is "example_from_readme.toml" in the examples above), can be either a string or a Path to a file, a raw dictionary with data, a URL or empty. You can also use a list of these options to combine data sources. If it is left empty, the pyproject.toml will be used. You can supply a key='tool.mytool.myconf' to specify which section of the file should be read. For HTTP authentication, currently you can use http basic auth (https://user:pass@host or query parameters (like ?token=...)). Other authentication methods are not currently supported.

More examples can be found in examples.

Supported Config File Types

  • .toml: supports the most types (strings, numbers, booleans, datetime, lists/arrays, dicts/tables);
  • .json: supports roughly the same types as toml (except datetime);
  • .yaml: supports roughly the same types as toml, backwards compatible with JSON;
  • .env: only supports strings. You can use convert_types=True to try to convert to your annotated types;
  • .ini: only supports strings. You can use convert_types=True to try to convert to your annotated types;

For other file types, a custom Loader can be written. See examples/readme.md#Custom File Types

Binary Config

You can also parse a struct-packed bytestring into a config class. For this, you have to use BinaryConfig with BinaryFields. Annotations are not supported in this case, because the order of properties is important for this type of config.

from configuraptor import BinaryConfig, BinaryField


class MyBinaryConfig(BinaryConfig):
    # annotations not supported! (because mixing annotation and __dict__ lookup messes with the order,
    # which is important for struct.(un)pack
    number = BinaryField(int)
    string = BinaryField(str, length=5)
    decimal = BinaryField(float)
    double = BinaryField(float, format="d")
    other_string = BinaryField(str, format="10s")
    boolean = BinaryField(bool)


MyBinaryConfig.load(
    b'*\x00\x00\x00Hello\x00\x00\x00fff@\xab\xaa\xaa\xaa\xaa\xaa\n@Hi\x00\x00\x00\x00\x00\x00\x00\x00\x01')

License

configuraptor is distributed under the terms of the MIT license.

Changelog

See CHANGELOG.md

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

configuraptor-1.20.0.tar.gz (561.4 kB view details)

Uploaded Source

Built Distribution

configuraptor-1.20.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file configuraptor-1.20.0.tar.gz.

File metadata

  • Download URL: configuraptor-1.20.0.tar.gz
  • Upload date:
  • Size: 561.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for configuraptor-1.20.0.tar.gz
Algorithm Hash digest
SHA256 bf523b64030b4638f22d28169dc28d9e0c5ac0ecb2c11a695394c8a93f21245d
MD5 1cbf531e6aa20c14744c41f6d680486a
BLAKE2b-256 f10b694edcf809473f9b5ab123e9f2a665c89c764449f7d68863fa3ef8e22971

See more details on using hashes here.

File details

Details for the file configuraptor-1.20.0-py3-none-any.whl.

File metadata

File hashes

Hashes for configuraptor-1.20.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f199921b12e8dbd36cdac90c09a8fcac7bb8d636f2db002c3a8057216c423ca6
MD5 839abeb79eb28d89f5b74c9c74157b3f
BLAKE2b-256 c6a43dcaa09e08bbb460a17687856bd7253e394541dab521b9a94a968d020077

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