Skip to content

Commit

Permalink
fix: write content as-is without trailing new lines (#104)
Browse files Browse the repository at this point in the history
**Issue:** while writing content into files, new lines are added after
every stanza and new property of a given stanza.
**Fix:** After all the things are written, there are 2 extra lines that
are added as a part of logic which moves the file seeker 2 places ahead,
hence we move back one character and remove the last character.
**Test:** updated the test case to write the content as is, as it should
be written to a file.
  • Loading branch information
hetangmodi-crest authored Aug 5, 2024
1 parent ddd3afa commit 7d2aff9
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 4 deletions.
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

0 comments on commit 7d2aff9

Please sign in to comment.