Skip to main content

A round-trip parser for ini files

Project description

Round Trip INI Parser

For reading and respectfully modifying INI files when dealing with software that still uses this format.

This library aims to be as flexible as possible when it comes to interpreting ini files.

Why roundtripini?

There are already a number of existing ini libraries, including python's builtin configparser, but not only do most of these libraries not support round-trip-parsing, most of them also do not support duplicate sections and keys.

roundtripini simultaneously supports round trip parsing, handling duplicate keys by treating them as lists, and allowing sections to be defined multiple times (with each section being queried when reading values).

Why INI?

Lots of software still uses this poorly-specified format. roundtripini is designed to help interface with such software.

If you want a library to read configuration files for the software itself, I would recommend instead using a file format which has a specification and consistent implementations in multiple languages and for multiple platforms, like TOML or YAML.

Usage

from roundtripini import INI

with open("file") as file:
    ini = INI(file)

# Unlike configparser, ini takes a tuple, rather than returning a section when accessed with []
# This is necessary as multiple sections may exist in the file.
ini["section", "key"] = "value"
# Multiple values can be included as a list. Each one will be added with a separate key
ini["section", "key"] = ["value 1", "value 2"]
ini["section", "other key"] = "other value"
# When assigning values, single-element lists are equivalent to strings
ini["section", "other key"] = ["other value"]

assert ini.dump() == """[section]
key = value 1
key = value 2
other key = other value
"""

assert isinstance(ini["section", "key"], list)
assert isinstance(ini["section", "other key"], str)

with open("file", "w") as file:
    file.write(ini.dump())

Restrictions

  • key/value pairs must be separated by =
  • keys may not begin or end with whitespace
  • values will have beginning or ending whitespace stripped when returned.
  • Comments will only be ignored if they are on one line, but not if they are on the same line as a key/value pair, where they will be treated as part of the value

Implementation Notes

  • Validation of key/value pairs occurs when data is used, not when the file is read.
  • When replacing keys with duplicates, all old keys will be removed from all sections (in the case of duplicate sections), and the new elements will be inserted in a single block at the location of the first old key.
  • Lists returned by the [] operator should not be modified, as the underlying data will not change.

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

roundtripini-0.4.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

roundtripini-0.4.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file roundtripini-0.4.0.tar.gz.

File metadata

  • Download URL: roundtripini-0.4.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for roundtripini-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ec060e1a1723d8e797d838304dc5bb4f8c9322c2c8eac06ba009466ae5f26c1e
MD5 2d8e15ee04f5113a2c0b0b8ad5eaf847
BLAKE2b-256 052e952a543d82d0c45ef4dcc3c7898e1dbaaa3a13a1cf11ac95c6cd5ce3ce30

See more details on using hashes here.

File details

Details for the file roundtripini-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: roundtripini-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for roundtripini-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7c9a1beb5cff2b2f6e16504a26a5484a142be6f327c18057b5645549049050d
MD5 58b49bb987f9c26c980bc9aeb6d5a783
BLAKE2b-256 4a9d8c7f74da95bf575a42f10bea78bd352d5589b0a79c4e48151ab7ed831659

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