Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: write content as-is without trailing new lines #104

Merged
merged 3 commits into from
Aug 5, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ __pycache__
.pytest_cache
dist
actionlint
*.log
events.pickle
14 changes: 12 additions & 2 deletions addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#
import configparser
from os import SEEK_SET
from typing import Any, Dict

COMMENT_PREFIX = ";#*"
COMMENT_KEY = "__COMMENTS__"
Expand All @@ -28,6 +30,10 @@ class TABConfigParser(configparser.RawConfigParser):
3. Support multiline end with \
"""

_defaults: Dict[Any, Any]
_sections: Dict[Any, Any]
_optcre: Dict[Any, Any]

def _read(self, fp, fpname):
"""
Override the built-in _read() method to read comments
Expand Down Expand Up @@ -149,7 +155,8 @@ def _read(self, fp, fpname):
if isinstance(val, list):
options[name] = "\n".join(val)

def write(self, fp):
# As the type of fp is not defined in RawConfigParser which is the parent class so we have to go with Any.
def write(self, fp: Any, *args) -> None:
"""
Override the write() method to write comments
"""
Expand All @@ -162,7 +169,6 @@ def write(self, fp):
if hasattr(self, "fields_outside_stanza"):
for field in self.fields_outside_stanza:
fp.write(field)

if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in list(self._defaults.items()):
Expand All @@ -187,6 +193,10 @@ def write(self, fp):
# write the separator line for stanza
fp.write("\n")

# remove the trailing lines in a file, as the content should be written as-is
fp.seek(fp.tell() - 1, SEEK_SET)
fp.truncate()

def optionxform(self, optionstr):
return optionstr

Expand Down
166 changes: 166 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ keywords = ["splunk"]

[tool.poetry.dependencies]
python = "^3.7"
configparser = "5.3.0"

[tool.poetry.dev-dependencies]
pytest = ">=7"
Expand Down
3 changes: 1 addition & 2 deletions test_addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def test_write(self):
parser.read_string(conf)
output = io.StringIO()
parser.write(output)
expected_output = conf + "\n"
self.assertEqual(expected_output, output.getvalue())
self.assertEqual(conf, output.getvalue())

def test_items(self):
conf = """
Expand Down
Loading