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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ be preserved:

1. Indents will not be preserved outside of multi-line values
2. Spacing around assignments will be normalized.
3. Casing of all options will be written as lowercase.

### Before

Expand All @@ -85,7 +84,7 @@ logging=true
; and many could be between things
[NEW SECTION]
# Another comment
multi-line=
multiLine=
value01
value02
value03
Expand All @@ -110,7 +109,7 @@ logging = true
; and many could be between things
[NEW SECTION]
# Another comment
multi-line =
multiLine =
value01
value02
value03
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "commented-configparser"
requires-python = ">=3.9"
description = "Custom ConfigParser class that preserves comments in file when writing"
description = "A custom ConfigParser class that preserves comments and most formatting when writing loaded config out."
readme = "README.md"
license = "MIT"
authors = [
Expand Down
10 changes: 9 additions & 1 deletion src/commentedconfigparser/commentedconfigparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ def read(

def read_file(self, f: Iterable[str], source: str | None = None) -> None:
content = self._translate_comments([line for line in f])
return super().read_file(content.splitlines(), source)
self.__is_reading = True
super().read_file(content.splitlines(), source)
self.__is_reading = False

def optionxform(self, optionstr: str) -> str:
if hasattr(self, "_CommentedConfigParser__is_reading") and self.__is_reading:
return optionstr

return super().optionxform(optionstr)

def write(
self,
Expand Down
2 changes: 1 addition & 1 deletion tests/pydocs_expected.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ values like this = 1000000
or this = 3.14159265359
are they treated as numbers? = no
integers, floats and booleans are held as = strings
can use the api to get converted values directly = true
can use the API to get converted values directly = true

[Multiline Values]
chorus = I'm a lumberjack, and I'm okay
Expand Down
1 change: 1 addition & 0 deletions tests/regression_original_expected.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ multi-line =
value02
value03
closing = 0
caseVariant = 0
# Trailing comment
1 change: 1 addition & 0 deletions tests/regression_original_input.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ multi-line=
value02
value03
closing=0
caseVariant=0
# Trailing comment
Loading