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
16 changes: 8 additions & 8 deletions src/commentedconfigparser/commentedconfigparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

__all__ = ["CommentedConfigParser"]

COMMENT_PATTERN = re.compile(r"^\s*[#|;]\s*(.+)$")
COMMENT_OPTION_PATTERN = re.compile(r"^(\s*)?__comment_\d+\s?[=|:]\s?(.*)$")
KEY_PATTERN = re.compile(r"^(.+?)\s?[=|:].*$")
SECTION_PATTERN = re.compile(r"^\s*\[(.+)\]\s*$")
_COMMENT_PATTERN = re.compile(r"^\s*[#|;]\s*(.+)$")
_COMMENT_OPTION_PATTERN = re.compile(r"^(\s*)?__comment_\d+\s?[=|:]\s?(.*)$")
_KEY_PATTERN = re.compile(r"^(.+?)\s?[=|:].*$")
_SECTION_PATTERN = re.compile(r"^\s*\[(.+)\]\s*$")


class CommentedConfigParser(ConfigParser):
Expand Down Expand Up @@ -84,7 +84,7 @@ def _translate_comments(self, content: list[str]) -> str:

translated_lines = []
for idx, line in enumerate(content):
if SECTION_PATTERN.match(line):
if _SECTION_PATTERN.match(line):
seen_section = True

if not seen_section:
Expand All @@ -93,12 +93,12 @@ def _translate_comments(self, content: list[str]) -> str:
# invalid config format.
self._headers.append(line)

elif COMMENT_PATTERN.match(line):
elif _COMMENT_PATTERN.match(line):
# Translate the comment into an option for the section. These
# are handled by the parent and retain order of insertion.
line = f"__comment_{self._commentprefix}{idx}={line.lstrip()}"

elif KEY_PATTERN.match(line) or SECTION_PATTERN.match(line):
elif _KEY_PATTERN.match(line) or _SECTION_PATTERN.match(line):
# Strip the left whitespace from sections and keys. This will
# leave only multiline values with leading whitespace preventing
# the saved output from incorrectly indenting after a comment
Expand All @@ -121,7 +121,7 @@ def _restore_comments(self, content: str) -> str:
rendered += self._headers

for line in content.splitlines():
comment_match = COMMENT_OPTION_PATTERN.match(line)
comment_match = _COMMENT_OPTION_PATTERN.match(line)
if comment_match:
line = comment_match.group(2)

Expand Down
8 changes: 4 additions & 4 deletions tests/commentedconfigparser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
),
)
def test_comment_pattern(line: str, expected: bool) -> None:
result = commentedconfigparser.COMMENT_PATTERN.match(line)
result = commentedconfigparser._COMMENT_PATTERN.match(line)

assert bool(result) is expected

Expand All @@ -40,7 +40,7 @@ def test_comment_pattern(line: str, expected: bool) -> None:
),
)
def test_section_pattern(line: str, expected: bool) -> None:
result = commentedconfigparser.SECTION_PATTERN.match(line)
result = commentedconfigparser._SECTION_PATTERN.match(line)

assert bool(result) is expected

Expand All @@ -61,7 +61,7 @@ def test_section_pattern(line: str, expected: bool) -> None:
)
def test_key_pattern(line: str, expected: str) -> None:

match = commentedconfigparser.KEY_PATTERN.match(line)
match = commentedconfigparser._KEY_PATTERN.match(line)

assert match
assert match.group(1) == expected
Expand All @@ -77,7 +77,7 @@ def test_key_pattern(line: str, expected: str) -> None:
)
def test_comment_option_pattern(line: str, expected: str) -> None:

match = commentedconfigparser.COMMENT_OPTION_PATTERN.match(line)
match = commentedconfigparser._COMMENT_OPTION_PATTERN.match(line)

assert match
assert match.group(2) == expected
Expand Down