1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515#
16- # -*- coding: utf-8 -*-
1716"""
1817Provides tags.conf parsing mechanism
1918"""
19+ import os
20+ from typing import Optional , Dict , Generator
2021from urllib .parse import unquote
2122import logging
2223
24+ import addonfactory_splunk_conf_parser_lib as conf_parser
25+
2326LOGGER = 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