Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 7 additions & 6 deletions addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,19 @@ def options(self, section):

return res

def item_dict(self):
def item_dict(self, preserve_comments: bool = False):
res = {}
sections = dict(self._sections)
for section, key_values in list(sections.items()):
kv = {}
for k, v in list(key_values.items()):
if (
not isinstance(k, str)
or k.startswith(COMMENT_KEY)
or k == "__name__"
isinstance(k, str)
and k != "__name__"
and (
preserve_comments or not k.startswith(COMMENT_KEY)
) # Include if comments are desired, OR if it's not a comment key
):
continue
kv[k] = v
kv[k] = v
res[section] = kv
return res
41 changes: 40 additions & 1 deletion test_addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def test_read(self):
parser.get("sourcetype:deprecated", "EVAL-deprecated_field"), '"old_value"'
)
self.assertEqual(parser.get("eventtype=some_eventtype", "tag"), "enabled")
self.assertEqual(
parser.top_comments, ["\n", "#\n", "# Very big copyright comment.\n", "#\n"]
)

def test_read_incorrect_conf(self):
conf = """
Expand Down Expand Up @@ -175,7 +178,7 @@ def test_options(self):
expected_options = ["__name__", "EVAL-field1", "FIELDALIAS-field2"]
self.assertEqual(expected_options, parser.options("source"))

def test_item_dict(self):
def test_item_dict_when_preserve_comment_False(self):
conf = """
#
# Very big copyright comment.
Expand Down Expand Up @@ -208,8 +211,44 @@ def test_item_dict(self):
"tag2": "enabled",
},
}
# Default value of preserve_comments is False
self.assertEqual(expected_item_dict, parser.item_dict())

def test_item_dict_when_preserve_comment_True(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test which has conf, then item_dict and then dump to a string? I would expect to see the same as in the original conf.

conf = """
#
# Very big copyright comment.
#
[source]
# This is a very simple field aliasing.
FIELDALIAS-field1 = field2 AS field1 # inline comment
EVAL-field3 = case(isnotnull(field4), "success",\
isnotnull(field5), "failure")

[sourcetype:deprecated]
EVAL-deprecated_field = "old_value"

[eventtype=some_eventtype]
tag1 = enabled
tag2 = enabled
"""
parser = conf_parser.TABConfigParser()
parser.read_string(conf)
expected_item_dict = {
"source": {
"__COMMENTS__0": "# This is a very simple field aliasing.\n",
"FIELDALIAS-field1": "field2 AS field1 # inline comment",
"EVAL-field3": 'case(isnotnull(field4), "success", isnotnull(field5), "failure")',
"__COMMENTS__1": "\n",
},
"sourcetype:deprecated": {
"EVAL-deprecated_field": '"old_value"',
"__COMMENTS__2": "\n",
},
"eventtype=some_eventtype": {"tag1": "enabled", "tag2": "enabled"},
}
self.assertEqual(expected_item_dict, parser.item_dict(preserve_comments=True))

def test_item_dict_when_there_is_empty_stanza(self):
conf = """
[stanza_with_something]
Expand Down
Loading