Skip to main content

A format-preserving TOML parser and writer for Python.

Project description

tomlrt

PyPI Python versions License CI

A format-preserving TOML parser and writer for Python.

Usage

import tomlrt

# Files must be opened in binary mode.
with open("pyproject.toml", "rb") as f:
    doc = tomlrt.load(f)

doc["project"]["version"] = "0.2.0"
doc["project"]["dependencies"].append("requests>=2")

print(tomlrt.dumps(doc))   # comments and layout are preserved

Structural assignment

A plain dict value installs as an inline table; a plain list installs as an inline array. To pick a different shape, assign a flavoured value:

from tomlrt import AoT, Array, Table

doc["tool"] = Table.section({"version": 1})      # [tool] section
doc["xy"]   = {"x": 1, "y": 2}                   # xy = { x = 1, y = 2 }
doc["pkgs"] = AoT([{"a": 1}, {"b": 2}])          # [[pkgs]] … [[pkgs]]
doc["tags"] = Array(["a", "b"], multiline=True)  # multi-line array

Use doc.install(path, value) for dotted-path placement. Plain doc["a.b"] = … always treats "a.b" as a single literal key, so install is the way to descend through a into b. Pass a tuple when one of the segments itself contains a literal .:

doc.install("tool.poetry.version", "0.1.0")            # [tool.poetry] version = "0.1.0"
doc.install(("tool", "weird.key"), 1)                  # [tool] "weird.key" = 1

Comment API

doc = tomlrt.loads("""
[server]
host = "localhost"  # default
port = 8080
""")

server = doc.table("server")
server.comments["port"] = "override with $PORT"
server.comments["host"] = None         # clear

print(tomlrt.dumps(doc))
# [server]
# host = "localhost"
# port = 8080 # override with $PORT

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

tomlrt-0.5.0.tar.gz (154.9 kB view details)

Uploaded Source

Built Distribution

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

tomlrt-0.5.0-py3-none-any.whl (71.9 kB view details)

Uploaded Python 3

File details

Details for the file tomlrt-0.5.0.tar.gz.

File metadata

  • Download URL: tomlrt-0.5.0.tar.gz
  • Upload date:
  • Size: 154.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for tomlrt-0.5.0.tar.gz
Algorithm Hash digest
SHA256 25b892c02d5a56e90b6346be67ba7a7af0a79033f564264e58a941caa85cb3ac
MD5 85ce023389159501271a86703ad3601a
BLAKE2b-256 428d2e8023229d2b56affe88f7773e50406754c1e8264980d9d264f6f56635e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for tomlrt-0.5.0.tar.gz:

Publisher: publish.yml on dimbleby/tomlrt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tomlrt-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: tomlrt-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 71.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for tomlrt-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ab7fc87f7dde00c5e11baefecbe5ee06f0d741ed61729f07d543e37af37ace8
MD5 bca3ec3e2a7410ebc608fb6e4cce1855
BLAKE2b-256 1b3b0feb390229a0281a738c0ccb7313716e02fa2e1a2cb550b01822796cc645

See more details on using hashes here.

Provenance

The following attestation bundles were made for tomlrt-0.5.0-py3-none-any.whl:

Publisher: publish.yml on dimbleby/tomlrt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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