Skip to content

Commit 7d2aff9

Browse files
fix: write content as-is without trailing new lines (#104)
**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.
1 parent ddd3afa commit 7d2aff9

File tree

5 files changed

+182
-4
lines changed

5 files changed

+182
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ __pycache__
55
.pytest_cache
66
dist
77
actionlint
8+
*.log
9+
events.pickle

addonfactory_splunk_conf_parser_lib.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
import configparser
17+
from os import SEEK_SET
18+
from typing import Any, Dict
1719

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

33+
_defaults: Dict[Any, Any]
34+
_sections: Dict[Any, Any]
35+
_optcre: Dict[Any, Any]
36+
3137
def _read(self, fp, fpname):
3238
"""
3339
Override the built-in _read() method to read comments
@@ -149,7 +155,8 @@ def _read(self, fp, fpname):
149155
if isinstance(val, list):
150156
options[name] = "\n".join(val)
151157

152-
def write(self, fp):
158+
# As the type of fp is not defined in RawConfigParser which is the parent class so we have to go with Any.
159+
def write(self, fp: Any, *args) -> None:
153160
"""
154161
Override the write() method to write comments
155162
"""
@@ -162,7 +169,6 @@ def write(self, fp):
162169
if hasattr(self, "fields_outside_stanza"):
163170
for field in self.fields_outside_stanza:
164171
fp.write(field)
165-
166172
if self._defaults:
167173
fp.write("[%s]\n" % DEFAULTSECT)
168174
for (key, value) in list(self._defaults.items()):
@@ -187,6 +193,10 @@ def write(self, fp):
187193
# write the separator line for stanza
188194
fp.write("\n")
189195

196+
# remove the trailing lines in a file, as the content should be written as-is
197+
fp.seek(fp.tell() - 1, SEEK_SET)
198+
fp.truncate()
199+
190200
def optionxform(self, optionstr):
191201
return optionstr
192202

poetry.lock

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ keywords = ["splunk"]
4444

4545
[tool.poetry.dependencies]
4646
python = "^3.7"
47+
configparser = "5.3.0"
4748

4849
[tool.poetry.dev-dependencies]
4950
pytest = ">=7"

test_addonfactory_splunk_conf_parser_lib.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def test_write(self):
8383
parser.read_string(conf)
8484
output = io.StringIO()
8585
parser.write(output)
86-
expected_output = conf + "\n"
87-
self.assertEqual(expected_output, output.getvalue())
86+
self.assertEqual(conf, output.getvalue())
8887

8988
def test_items(self):
9089
conf = """

0 commit comments

Comments
 (0)