Skip to main content

A lil' TOML parser, but with span

Project description

Build Status codecov.io PyPI version

Spanned-Toml

A lil' TOML parser, but with span

This project is an extension of @hukkin's Tomli libray, but with span.

A span is simply a Python slice that helps to retrieve where a given object was parsed from.

Motivation

TOML has become a popular format for configuration files, and many tools now rely on parsing such files. However, most parsers error on invalid TOML syntax, not configuration specific errors. E.g., what happens with the following file?

age = -45  # age should be a positive integer

First, you parse the TOML file, which is valid, then you invalidate the age value because it is negative. But how to pinpoint the location of where age was defined to the user?

There is where Spanned-Toml comes into play. For every key / value, you can obtain the span information about where it was define. The span is simple a Python slice, that can be used to index the original TOML string.

Table of Contents generated with mdformat-toc

Intro

Spanned-Toml is a Python library for parsing TOML, with the only addition of span information for every object (both keys and values). It is fully compatible with TOML v1.0.0, and its goal is to provide span information with minimal overhead over Tomli.

As such, Spanned-Toml provides the same features and API as Tomli, with the only difference is that it returns a Spanned[dict], instead of dict.

If you whish to get the same output as with Tomli, you can always call .unspan() on a Spanned object.

Installation

pip install spanned-toml

Usage

Toml-Spanned has the exact same interface as Tomli. Therefore, I recommend you checking Tomli's usage.

The only addition is that, instead of returning an object T, it returns Spanned[T], and nested objects are also Spanned, i.e., array and dictionnary values are also spanned.

From Spanned[T], you can always obtain the inner value T with .inner():

import spanned_toml as toml

toml_dict = toml.loads("age = 10")

assert toml_dict["age"].inner() == 10

NOTE: for convenience, Spanned[T] inherits most attributes from T.

If you have nested Spanned objects, then you can call .unspan() to remove all span information, and obtain the same object as if you used Tomli.

toml_str = """
[[players]]
name = "Lehtinen"
number = 26

[[players]]
name = "Numminen"
number = 27
"""

toml_dict = toml.loads(toml_str).unspan()
assert toml_dict == {
    "players": [{"name": "Lehtinen", "number": 26}, {"name": "Numminen", "number": 27}]
}

Last, but not least, you can retrieve the exact part of the string that was used to parse a given key or value.

player_span = toml_dict["players"][0]["name"].span()

assert toml_str[player_span] == '"Lehtinen"'  # Quotes are included in span

NOTE: arrays of tables have an empty span, since then can be defined in multiple parts of a given file.

Why choosing Spanned-Toml over others?

Spanned-Toml was mainly built for another project I am working on.

You should use this package whenever you care about where specific parts in a TOML config file are coming from. This might be useful, e.g., if you want to have a validation layer, on top of the default TOML, and that you want to exactly pinpoint where an error originated.

Otherwise, if you just care about parsing TOML file or speed, then directly use Tomli (or other faster alternatives).

Versions

Toml-Spanned follows the same versions as Tomli, and tries to be in sync with it.

Therefore, Tomli-Spanned's version X.Y.Z.P matches Tomli's version X.Y.Z. The P number of for patches, and is only intended to fix issues related to span.

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

spanned-toml-2.0.1.1.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

spanned_toml-2.0.1.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file spanned-toml-2.0.1.1.tar.gz.

File metadata

  • Download URL: spanned-toml-2.0.1.1.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.15

File hashes

Hashes for spanned-toml-2.0.1.1.tar.gz
Algorithm Hash digest
SHA256 4de893e1ec7a01cb023392affbab040b3da9afb7e4a886fc478ac52ff2158753
MD5 501d905e1f45b09ca1b099c24859df97
BLAKE2b-256 23e312eb1f49ef10755251cb6037c9da0964feb90cd4f75a3b9021e592e84456

See more details on using hashes here.

File details

Details for the file spanned_toml-2.0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for spanned_toml-2.0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cf962412aad749c840bc65f5d0ee7d720f00d15c2ee720c776af3236a6b0d0b2
MD5 fe7155aa0cddf2d8958f925e6a338d63
BLAKE2b-256 1426988a772d03fa5385edc1e3481c69b4e471309fef99928f9a1031ecbe4728

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