Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,102 @@
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/Preocts/commented-configparser/main.svg)](https://results.pre-commit.ci/latest/github/Preocts/commented-configparser/main)
[![Python tests](https://github.com/Preocts/commented-configparser/actions/workflows/python-tests.yml/badge.svg?branch=main)](https://github.com/Preocts/commented-configparser/actions/workflows/python-tests.yml)

# commented-configparser (WIP)
# commented-configparser

Custom ConfigParser class that preserves comments in file when writing
A custom ConfigParser class that preserves comments when writing loaded config out.

This library gives you a custom class of the standard library's `configparser.ConfigParger` which will preserve the comments of a loaded config file when writing that file back out.

---

## Install via pip

From pypi: TBD

From github:

```bash
python -m pip install commented-configparser@git+https://github.com/Preocts/commented-configparser@main
```

**Note:** Replace `main` with the desired tag or branch. This can be placed in a `requirements.txt` file as well.

---

## Example use

```py
from commentedconfigparser import CommentedConfigParser

# Load the config like normal
config = CommentedConfigParser()
config.read("myconfig.ini")

# Use the config like normal
...

# Update the config like normal
...

# Save the config back to the file
with open("myconfig.ini", "w") as savefile:
config.write(savefile)
```

## Results

We favor the line spacing choices of the `ConfigParser` class so the input format may not be preserved completely. However, the comments will be preserved.

### Before

```ini
# Welcome to our config
[DEFAULT]
# This value has some meaning to someone
foo=bar
# Make sure to add this when you need it
trace=false
logging=true
; This is a comment as well

# so we need to track all of them

; and many could be between things
[NEW SECTION]
# Another comment
multi-line=
value01
value02
value03
closing=0
# Trailing comment

```

### After

```ini
# Welcome to our config
[DEFAULT]
# This value has some meaning to someone
foo=bar
# Make sure to add this when you need it
trace=false
logging=true
; This is a comment as well
# so we need to track all of them
; and many could be between things

[NEW SECTION]
# Another comment
multi-line=
value01
value02
value03
closing=0
# Trailing comment

```

---

Expand Down
5 changes: 5 additions & 0 deletions src/commentedconfigparser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations

from commentedconfigparser.commentedconfigparser import CommentedConfigParser

__all__ = ["CommentedConfigParser"]
7 changes: 3 additions & 4 deletions src/commentedconfigparser/commentedconfigparser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""
Custom ConfigParser class that preserves comments when writing a loaded config out.

"""
"""Custom ConfigParser class that preserves comments when writing loaded config out."""
from __future__ import annotations

import os
Expand All @@ -16,6 +13,8 @@
from _typeshed import StrOrBytesPath
from _typeshed import SupportsWrite

__all__ = ["CommentedConfigParser"]

COMMENT_PTN = re.compile(r"\s*[#|;]")
KEY_PTN = re.compile("(.+?)[=|:]")

Expand Down