Skip to content

Commit ba50469

Browse files
committed
refactor: use conf-parser in TagsParser instead of splunk-appinspect
1 parent 0b00ae1 commit ba50469

File tree

6 files changed

+74
-119
lines changed

6 files changed

+74
-119
lines changed

poetry.lock

Lines changed: 14 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ filelock = "^3.0"
4545
pytest-ordering = "*"
4646
lovely-pytest-docker = { version="^0", optional = true }
4747
junitparser = "<2.0.0"
48+
addonfactory-splunk-conf-parser-lib = "^0.3.3"
4849

4950

5051
[tool.poetry.extras]

pytest_splunk_addon/standard_lib/addon_parser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def props_parser(self):
7373
@property
7474
def tags_parser(self):
7575
if not self._tags_parser:
76-
self._tags_parser = TagsParser(self.splunk_app_path, self.app)
76+
self._tags_parser = TagsParser(self.splunk_app_path)
7777
return self._tags_parser
7878

7979
@property

pytest_splunk_addon/standard_lib/addon_parser/tags_parser.py

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,43 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
# -*- coding: utf-8 -*-
1716
"""
1817
Provides tags.conf parsing mechanism
1918
"""
19+
import os
20+
from typing import Optional, Dict, Generator
2021
from urllib.parse import unquote
2122
import logging
2223

24+
import addonfactory_splunk_conf_parser_lib as conf_parser
25+
2326
LOGGER = logging.getLogger("pytest-splunk-addon")
2427

2528

26-
class TagsParser(object):
29+
class TagsParser:
2730
"""
2831
Parses tags.conf and extracts tags
2932
3033
Args:
3134
splunk_app_path (str): Path of the Splunk app
32-
app (splunk_appinspect.App): Object of Splunk app
3335
"""
3436

35-
def __init__(self, splunk_app_path, app):
36-
self.app = app
37+
def __init__(self, splunk_app_path: str):
38+
self._conf_parser = conf_parser.TABConfigParser()
3739
self.splunk_app_path = splunk_app_path
3840
self._tags = None
3941

4042
@property
41-
def tags(self):
42-
try:
43-
if not self._tags:
44-
LOGGER.info("Parsing tags.conf")
45-
self._tags = self.app.get_config("tags.conf")
43+
def tags(self) -> Optional[Dict]:
44+
if self._tags is not None:
4645
return self._tags
47-
except OSError:
48-
LOGGER.warning("tags.conf not found.")
49-
return None
46+
LOGGER.info("Parsing tags.conf")
47+
tags_conf_path = os.path.join(self.splunk_app_path, "default", "tags.conf")
48+
self._conf_parser.read(tags_conf_path)
49+
self._tags = self._conf_parser.item_dict()
50+
return self._tags if self._tags else None
5051

51-
def get_tags(self):
52+
def get_tags(self) -> Optional[Generator]:
5253
"""
5354
Parse the tags.conf of the App & yield stanzas
5455
@@ -57,27 +58,16 @@ def get_tags(self):
5758
"""
5859
if not self.tags:
5960
return
60-
for stanza in self.tags.sects:
61-
LOGGER.info(f"Parsing tags of stanza={stanza}")
62-
tag_sections = self.tags.sects[stanza]
63-
stanza = stanza.replace("=", '="') + '"'
64-
stanza = unquote(stanza)
65-
LOGGER.debug(f"Parsed tags-stanza={stanza}")
66-
for key in tag_sections.options:
67-
tags_property = tag_sections.options[key]
68-
LOGGER.info(
69-
"Parsing tag=%s enabled=%s of stanza=%s ",
70-
tags_property.name,
71-
tags_property.value,
72-
stanza,
73-
)
61+
for stanza_key, stanza_values in self.tags.items():
62+
LOGGER.info(f"Parsing tags of stanza={stanza_key}")
63+
stanza_key = stanza_key.replace("=", '="') + '"'
64+
stanza_key = unquote(stanza_key)
65+
LOGGER.debug(f"Parsed tags-stanza={stanza_key}")
66+
for key, value in stanza_values.items():
67+
LOGGER.info(f"Parsing tag={key} enabled={value} of stanza={stanza_key}")
7468
tag_container = {
75-
"stanza": stanza,
76-
"tag": tags_property.name,
77-
# "enabled": True or False
69+
"stanza": stanza_key,
70+
"tag": key,
71+
"enabled": True if value == "enabled" else False,
7872
}
79-
if tags_property.value == "enabled":
80-
tag_container["enabled"] = True
81-
else:
82-
tag_container["enabled"] = False
8373
yield tag_container

0 commit comments

Comments
 (0)