Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.venv
.venv*
.idea
.mypy_cache
__pycache__
Expand Down
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ header:
- "*.lock"
- ".*"
- "renovate.json"
- testdata/**
32 changes: 18 additions & 14 deletions addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from os import SEEK_SET
from typing import Any, Dict

COMMENT_PREFIX = ";#*"
COMMENT_PREFIX = [";", "#", "*"]
COMMENT_KEY = "__COMMENTS__"


Expand Down Expand Up @@ -71,7 +71,6 @@ def _read(self, fp, fpname):
if line.split(None, 1)[0].lower() == "rem" and line[0] in "rR":
# no leading whitespace
continue
# continuation line?

# support multiline with \
if add_space_to_next_line:
Expand Down Expand Up @@ -121,12 +120,6 @@ def _read(self, fp, fpname):
# This check is fine because the OPTCRE cannot
# match if it would set optval to None
if optval is not None:
if vi in ("=", ":") and ";" in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
pos = optval.find(";")
if pos != -1 and optval[pos - 1].isspace():
optval = optval[:pos]
optval = optval.strip()
# allow empty values
if optval == '""':
Expand Down Expand Up @@ -228,18 +221,29 @@ 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
# Remove inline comments if preserve_comments is False
if not preserve_comments:
for comment_char in COMMENT_PREFIX:
if comment_char in v:
pos = v.find(comment_char)
if v[pos - 1].isspace():
v = v[: pos - 1]
else:
v = v[:pos]
break
kv[k] = v
res[section] = kv
return res
55 changes: 33 additions & 22 deletions test_addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
import configparser
import io
import unittest
import os

import addonfactory_splunk_conf_parser_lib as conf_parser

CONF_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "testdata", "test.conf"
)


class TABConfigParserTest(unittest.TestCase):
def test_read(self):
Expand Down Expand Up @@ -49,6 +54,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,40 +183,43 @@ def test_options(self):
expected_options = ["__name__", "EVAL-field1", "FIELDALIAS-field2"]
self.assertEqual(expected_options, parser.options("source"))

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

[sourcetype:deprecated]
EVAL-deprecated_field = "old_value"
def test_item_dict_when_preserve_comment_False(self):
parser = conf_parser.TABConfigParser()
parser.read(CONF_PATH)
Copy link
Member

Choose a reason for hiding this comment

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

nit: you could inline the conf and parametrize the test to stay consistent with other tests but totally up to you

Copy link
Author

Choose a reason for hiding this comment

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

I did try it earlier with @pytest.mark.parametrize but for some reason test were failing.

expected_item_dict = {
"source": {
"FIELDALIAS-field1": "field2 AS field1",
"EVAL-field3": 'case(isnotnull(field4), "success",\\\nisnotnull(field5), "failure")',
},
"sourcetype:deprecated": {"EVAL-deprecated_field": '"old_value"'},
"eventtype=some_eventtype": {
"tag1": "enabled",
"tag2": "enabled",
},
}
# Default value of preserve_comments is False
self.assertEqual(expected_item_dict, parser.item_dict())

[eventtype=some_eventtype]
tag1 = enabled
tag2 = enabled
"""
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.

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

def test_item_dict_when_there_is_empty_stanza(self):
conf = """
Expand Down
19 changes: 19 additions & 0 deletions testdata/test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Very big copyright comment.
#




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

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

[eventtype=some_eventtype]
tag1 = enabled ; Inline comment example using semicolon
tag2 = enabled # Inline comment using hash
Loading