Skip to content

Commit b9245b4

Browse files
committed
feat: added logic to add comment in .conf files
1 parent 9d710ac commit b9245b4

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

addonfactory_splunk_conf_parser_lib.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,19 @@ def options(self, section):
228228

229229
return res
230230

231-
def item_dict(self):
231+
def item_dict(self, preserve_comments: bool = False):
232232
res = {}
233233
sections = dict(self._sections)
234234
for section, key_values in list(sections.items()):
235235
kv = {}
236236
for k, v in list(key_values.items()):
237237
if (
238-
not isinstance(k, str)
239-
or k.startswith(COMMENT_KEY)
240-
or k == "__name__"
238+
isinstance(k, str)
239+
and k != "__name__"
240+
and (
241+
preserve_comments or not k.startswith(COMMENT_KEY)
242+
) # Include if comments are desired, OR if it's not a comment key
241243
):
242-
continue
243-
kv[k] = v
244+
kv[k] = v
244245
res[section] = kv
245246
return res

test_addonfactory_splunk_conf_parser_lib.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def test_read(self):
4949
parser.get("sourcetype:deprecated", "EVAL-deprecated_field"), '"old_value"'
5050
)
5151
self.assertEqual(parser.get("eventtype=some_eventtype", "tag"), "enabled")
52+
self.assertEqual(
53+
parser.top_comments, ["\n", "#\n", "# Very big copyright comment.\n", "#\n"]
54+
)
5255

5356
def test_read_incorrect_conf(self):
5457
conf = """
@@ -175,7 +178,7 @@ def test_options(self):
175178
expected_options = ["__name__", "EVAL-field1", "FIELDALIAS-field2"]
176179
self.assertEqual(expected_options, parser.options("source"))
177180

178-
def test_item_dict(self):
181+
def test_item_dict_when_preserve_comment_False(self):
179182
conf = """
180183
#
181184
# Very big copyright comment.
@@ -208,8 +211,44 @@ def test_item_dict(self):
208211
"tag2": "enabled",
209212
},
210213
}
214+
# Default value of preserve_comments is False
211215
self.assertEqual(expected_item_dict, parser.item_dict())
212216

217+
def test_item_dict_when_preserve_comment_True(self):
218+
conf = """
219+
#
220+
# Very big copyright comment.
221+
#
222+
[source]
223+
# This is a very simple field aliasing.
224+
FIELDALIAS-field1 = field2 AS field1 # inline comment
225+
EVAL-field3 = case(isnotnull(field4), "success",\
226+
isnotnull(field5), "failure")
227+
228+
[sourcetype:deprecated]
229+
EVAL-deprecated_field = "old_value"
230+
231+
[eventtype=some_eventtype]
232+
tag1 = enabled
233+
tag2 = enabled
234+
"""
235+
parser = conf_parser.TABConfigParser()
236+
parser.read_string(conf)
237+
expected_item_dict = {
238+
"source": {
239+
"__COMMENTS__0": "# This is a very simple field aliasing.\n",
240+
"FIELDALIAS-field1": "field2 AS field1 # inline comment",
241+
"EVAL-field3": 'case(isnotnull(field4), "success", isnotnull(field5), "failure")',
242+
"__COMMENTS__1": "\n",
243+
},
244+
"sourcetype:deprecated": {
245+
"EVAL-deprecated_field": '"old_value"',
246+
"__COMMENTS__2": "\n",
247+
},
248+
"eventtype=some_eventtype": {"tag1": "enabled", "tag2": "enabled"},
249+
}
250+
self.assertEqual(expected_item_dict, parser.item_dict(preserve_comments=True))
251+
213252
def test_item_dict_when_there_is_empty_stanza(self):
214253
conf = """
215254
[stanza_with_something]

0 commit comments

Comments
 (0)