Skip to main content

This package defines a Config class which allows you to employ an extended syntax in your TOML files.

Syntax

Defaults

Consider this scrap of TOML:

[A]

[B]
flavour = "strawberry"

Confusion lets you add a DEFAULT section just like the traditional Python configparser module:

[DEFAULT]
flavour = "vanilla"

[A]

[B]
flavour = "strawberry"

Substitution

By applying the extended substitution syntax, you can define a value in only one location and reference it elsewhere (DRY principle):

[A]
flavour = "strawberry"

[B]
flavour = ${A:flavour}

Cross-compatible strings

Multiline strings have to be formatted to conform to both configparser and TOML syntaxes:

[caveat]
text = """
    Multiline strings
    are possible but
    indentation of these closing quotes
    is required.
    """

Usage

Let’s use this scrap of CFN for demonstration:

text = """

[DEFAULT]
flavour = "vanilla"
flake = false

[A]
flavour = "strawberry"

[B]
flavour = ${A:flavour}
flake = true

"""

The simplest way to construct a parser is via the from_string class method:

from confusion import Config

conf = Config.from_string(text)

Or, to read a file, just use the from_path method:

conf = Config.from_path("my_config.cfn")

A Config object has all the methods of Python’s standard ConfigParser class. There is one difference; the sections attribute is a property which returns a dictionary:

print(conf.sections)
>>> {'A': <Section: A>, 'B': <Section: B>}

The hierarchical TOML data is available via the object’s tables property:

print(conf.tables)
>>> {'A': {'flavour': 'strawberry', 'flake': False}, 'B': {'flavour': 'strawberry', 'flake': True}}

Environment variables

You can override settings in the Config object from a flat dictionary. Keys in the dictionary are parsed for a separator (‘_’ by default) which denotes the section in which the setting belongs.

Suppose you want to avoid storing a password in this file:

[DB]
USER = "postgres"

You can define an environment variable DB_PASS=postgres and merge it in from there:

import os

conf = Config.from_path("my_config.cfn")
conf.merge(os.environ)

Only those variables with a corresponding section in the file will be merged in. You can pass an alternative separator to the merge method:

conf.merge({"window.width": 800, "window.height": 600}, sep=".")

Logging

The Config file has a convenient method to set up your logs according to the logging configuration schema.

This means you can achieve complete configuration of your application in this single line of code:

conf = Config.from_path("my_config.cfn").merge(os.environ).configure_logging(table_name="logging")

Utilities

cfn2dot is a command line utility which generates a Grapviz .dot file from a data graph in confusion format:

$ python -m confusion.utils.cfn2dot --help

This utility translates a graph defined in a CFN file to an equivalent .dot

Usage:

    python -m confusion.utils.cfn2dot --label-graph Taxonomy --digraph taxonomy.cfn > taxonomy.dot

    dot -Tsvg taxonomy.dot > taxonomy.svg

       [-h] [--label-graph LABEL_GRAPH] [--label-inherits LABEL_INHERITS] [--cluster] [--digraph] input [input ...]

positional arguments:
  input                 Set input file.

options:
  -h, --help            show this help message and exit
  --label-graph LABEL_GRAPH
                        Set a label for the graph.
  --label-inherits LABEL_INHERITS
                        Set the label for an arc signifying an 'inherits' relationship.
  --cluster             Generate a clustered graph.
  --digraph, --directed
                        Make arcs directional.

Metadata

Release files for confusion 0.4.0

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

Source distribution (sdist)

Source distribution for confusion 0.4.0
File Size Uploaded Provenance
confusion-0.4.0.tar.gz 32.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for confusion 0.4.0
File Interpreter ABI Platform
confusion-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 64.0 kB

Release files / confusion-0.4.0.tar.gz

Download URL confusion-0.4.0.tar.gz
Size 32.1 kB
Tags Source
SHA-256 checksum
How to use checksums
365a149455963cdb7f4a68e5e1c3ac89c941462e659fee163099fb0a1d14a356
BLAKE2b-256 checksum
How to use checksums
016e7ac3d9276d5cfb94f6e29a29844bc7fa86449e0ffd49a8efe1491e891239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5

Release files / confusion-0.4.0-py3-none-any.whl

Download URL confusion-0.4.0-py3-none-any.whl
Size 31.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
41cfbdd72cb70ee6b06690846440fb30c46ce82e37b741050ee2c0a6e50ea155
BLAKE2b-256 checksum
How to use checksums
886ef35b91882b7c18a3c1265e828dada1d41e90b82d90ba936ef090d8bb633b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5

Security for confusion 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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