Skip to content

Commit

Permalink
fix(write): truncate last line written to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
hetangmodi-crest committed Aug 1, 2024
1 parent ddd3afa commit 9096f0d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion 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 io import TextIOBase
from os import SEEK_SET

COMMENT_PREFIX = ";#*"
COMMENT_KEY = "__COMMENTS__"
Expand Down Expand Up @@ -149,7 +151,7 @@ def _read(self, fp, fpname):
if isinstance(val, list):
options[name] = "\n".join(val)

def write(self, fp):
def write(self, fp: TextIOBase):
"""
Override the write() method to write comments
"""
Expand Down Expand Up @@ -187,6 +189,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

0 comments on commit 9096f0d

Please sign in to comment.