Skip to main content

python toml configuration library

Project description

version-badge coverage-badge

conficus is a python toml configuration wrapper. providing some extra type coercions (e.g. str -> Path) easier access and section inheritance.

conficus python 3.11+.

Installation

Install conficus with pip.

pip install conficus

Quick Start

Basic usage

>>>
>>> import conficus
>>>

Configurations can be loaded directly from a string variable or read via file path string or Path object:

>>> config = conficus.load('/Users/mgemmill/config.ini', toml=True)
>>>

Easier Selection

Accessing nested sections is made easier with chained selectors:

>>> # regular dictionary access:
...
>>> config['app']['debug']
True
>>>
>>> # chained selector access:
...
>>> config['app.debug']
True

Inheritance

Inheritance pushes parent values down to any child section:

# config.ini

[app]
debug = true

[email]
_inherit = 0
host = "smtp.mailhub.com"
port = 2525
sender = "emailerdude@mailhub.com"

[email.alert]
to = ["alert-handler@service.com"]
subject = "THIS IS AN ALERT"
body = "Alerting!"

It is turned on via the inheritance option:

>>> config = conficus.load("config.ini", inheritance=True)

Sub-sections will now contain parent values:

>>> alert_config = config["email.alert"]
>>> alert_config["host"]
>>> "smtp.mailhub.com"
>>> alert_config["subject"]
>>> "THIS IS AN ALERT"

Inheritence can be controled per section via the _inherit option. _inherit = 0 will block the section from inheriting parent values. _inherit = 1 would only allow inheritance from the sections immediate parent; _inherit = 2 would allow both the immediate parent and grandparent inheritance.

_inherit values are stripped from the resulting configuration dictionary.

Additional Conversion Options

In addition to toml’s standard type conversions, conficus has two builtin conversion options and also allows for adding custom conversions.

Conversions only work with string values.

Path Conversions

The pathlib option will convert any toml string value that looks like a path to a python pathlib.Path object:

>>> config = conficus.load("path = '/home/user/.dir'", pathlib=True)
>>> isinstance(config["path"], Path)
>>> True

Decimal Conversions

The decimal option will convert any toml string value that matches \d+\.\d+ to a python Decimal object:

>>> config = conficus.load("number = '12.22'", decimal=True)
>>> isinstance(config["number"], Decimal)
>>> True

Custom Conversions

A custom coercer consists of 3 valus:

  1. a name string

  2. a regular expression string

  3. a conversion function that takes a string value and returns the coerced value

This a contrived example, where we’re defining a notation (“upper::”) to identify a custom value we want to convert. You could easily take this example and do something similar that decrypts an encrypted value.

>>> def convert_to_caps(raw_value: str) -> str:
...     return raw_value.upper()
>>> config = conficus.load("address = 'upper::121 fleet street'", coercers=[("upper-case", (r"^upper::(?P<value>.*)$", convert_to_caps))])
>>> config["address"]
... "121 FLEET STREET"

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

conficus-1.0.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

conficus-1.0.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file conficus-1.0.0.tar.gz.

File metadata

  • Download URL: conficus-1.0.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.6 Darwin/24.6.0

File hashes

Hashes for conficus-1.0.0.tar.gz
Algorithm Hash digest
SHA256 11dd676ca8dca2bd80820d13048568480ebaeee7f0d117dc4a49bb2b1292eba4
MD5 475a992eb7b55a9234b7ff07d59cc212
BLAKE2b-256 dac17ac0e78672854e8e88c68a093ec4ac63e26d371c0cb0d9914d3155065d11

See more details on using hashes here.

File details

Details for the file conficus-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: conficus-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.6 Darwin/24.6.0

File hashes

Hashes for conficus-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c9d368d4bdbe744372718075a3b9942abf5730e2e3ed6ac7a47d2f3a48305f7
MD5 0d47951223014aee27feab94629e5237
BLAKE2b-256 6db46d8821d2d7286035b3df001fd962382062e66f23f44a8fad4c7003a65a49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page