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.1.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for roundtripini-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ea9a67fc1d59518765e819a17ce1a77c0dbe00aae94d3680c13829a670218562
MD5 255cf559bc8698a9c120f9b9186ad907
BLAKE2b-256 595e10849b802b8702973ad89e27a518e32db06d61a7e7a88f17a3d192833d77

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for roundtripini-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14f145f273b676ef365a0c93715a3cdd7abc46d6883a7f4fcc48a8896a1ecd88
MD5 16246e0de1f7ef794d0ccbed7154406f
BLAKE2b-256 28290fefed6869a8302f5b112635ffc814ccb23af28aa8ae5fc6a7c4a2247cee

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