Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle default stanza and newlines in conf files #105

Closed
wants to merge 3 commits into from
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
21 changes: 12 additions & 9 deletions addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import configparser
from os import SEEK_SET
from typing import Any, Dict
from re import compile, VERBOSE

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


Expand All @@ -33,6 +34,8 @@ class TABConfigParser(configparser.RawConfigParser):
_defaults: Dict[Any, Any]
_sections: Dict[Any, Any]
_optcre: Dict[Any, Any]
# overriding the section regex for Splunk as blank '[]' stanza is valid in case for default.meta
SECTCRE = compile(pattern=r"\[(?P<header>.*)\]", flags=VERBOSE)

def _read(self, fp, fpname):
"""
Expand All @@ -43,7 +46,8 @@ def _read(self, fp, fpname):
cursect = None # None, or a dictionary
optname = None
lineno = 0
e = None # None, or an exception
line: str
exc = None # None, or an exception

comment_index = 0
self.top_comments = []
Expand Down Expand Up @@ -109,7 +113,7 @@ def _read(self, fp, fpname):
# no section header in the file?

elif cursect is None:
# disable the exception since splunk allows the field outside stanzas
# disable the exception since Splunk allows the field outside stanzas
# raise MissingSectionHeaderError(fpname, lineno, line)
self.fields_outside_stanza.append(line)
# an option line?
Expand Down Expand Up @@ -140,13 +144,12 @@ def _read(self, fp, fpname):
# exception but keep going. the exception will be
# raised at the end of the file and will contain a
# list of all bogus lines
if not e:
e = ParsingError(fpname)
e.append(lineno, repr(line))
if not exc:
exc = ParsingError(fpname)
exc.append(lineno, repr(line))
# if any parsing errors occurred, raise an exception
if e:
raise e

if exc:
raise exc
# join the multi-line values collected while reading
all_sections = [self._defaults]
all_sections.extend(list(self._sections.values()))
Expand Down
62 changes: 62 additions & 0 deletions test_addonfactory_splunk_conf_parser_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,68 @@ def test_write(self):
parser.write(output)
self.assertEqual(conf, output.getvalue())

def test_write_default_meta(self):
"""
Parsing of default stanza as '[]' as it is valid for default.meta in Splunk
"""
conf = """
# Application-level permissions

[]
owner = admin
access = read : [ * ], write : [ admin, sc_admin ]
export = system

[props]
owner = admin
access = read : [ * ], write : [ admin, sc_admin ]
export = system
"""
parser = conf_parser.TABConfigParser()
parser.read_string(conf)
output = io.StringIO()
parser.write(output)
self.assertEqual(conf, output.getvalue())

def test_write_extra_lines_between_stanza(self):
"""
In case of a conf file with extra 'n' new lines between stanzas,
there would one empty new line between the end and start
"""
conf = """
[stanza_0]
k = v


[stanza_1]
key = value
"""
parser = conf_parser.TABConfigParser()
parser.read_string(conf)
output = io.StringIO()
parser.write(output)
self.assertEqual(conf.replace("\n\n\n", "\n\n"), output.getvalue())

def test_write_extra_lines_at_end(self):
"""
In case of a conf file with extra 'n' new lines at the end,
we remove all and keep just one.
"""
conf = """
[stanza_0]
k = v

[stanza_1]
key = value


"""
parser = conf_parser.TABConfigParser()
parser.read_string(conf)
output = io.StringIO()
parser.write(output)
self.assertEqual(conf.rstrip() + "\n", output.getvalue())

def test_items(self):
conf = """
#
Expand Down
Loading