1- # -*- coding: utf-8 -*-
21"""
32Provides tags.conf parsing mechanism
43"""
4+ from typing import Dict
5+ from typing import Generator
6+ from typing import Optional
7+ import os
58from urllib .parse import unquote
69import logging
710
11+ import conf_parser
12+
813LOGGER = logging .getLogger ("pytest-splunk-addon" )
914
1015
11- class TagsParser ( object ) :
16+ class TagsParser :
1217 """
1318 Parses tags.conf and extracts tags
1419
1520 Args:
1621 splunk_app_path (str): Path of the Splunk app
17- app (splunk_appinspect.App): Object of Splunk app
1822 """
1923
20- def __init__ (self , splunk_app_path , app ):
21- self .app = app
24+ def __init__ (self , splunk_app_path : str ):
25+ self ._conf_parser = conf_parser . TABConfigParser ()
2226 self .splunk_app_path = splunk_app_path
2327 self ._tags = None
2428
2529 @property
26- def tags (self ):
27- try :
28- if not self ._tags :
29- LOGGER .info ("Parsing tags.conf" )
30- self ._tags = self .app .get_config ("tags.conf" )
30+ def tags (self ) -> Optional [Dict ]:
31+ if self ._tags is not None :
3132 return self ._tags
32- except OSError :
33- LOGGER .warning ("tags.conf not found." )
34- return None
33+ LOGGER .info ("Parsing tags.conf" )
34+ tags_conf_path = os .path .join (self .splunk_app_path , "default" , "tags.conf" )
35+ self ._conf_parser .read (tags_conf_path )
36+ self ._tags = self ._conf_parser .item_dict ()
37+ return self ._tags if self ._tags else None
3538
36- def get_tags (self ):
39+ def get_tags (self ) -> Optional [ Generator ] :
3740 """
3841 Parse the tags.conf of the App & yield stanzas
3942
@@ -42,27 +45,16 @@ def get_tags(self):
4245 """
4346 if not self .tags :
4447 return
45- for stanza in self .tags .sects :
46- LOGGER .info (f"Parsing tags of stanza={ stanza } " )
47- tag_sections = self .tags .sects [stanza ]
48- stanza = stanza .replace ("=" , '="' ) + '"'
49- stanza = unquote (stanza )
50- LOGGER .debug (f"Parsed tags-stanza={ stanza } " )
51- for key in tag_sections .options :
52- tags_property = tag_sections .options [key ]
53- LOGGER .info (
54- "Parsing tag=%s enabled=%s of stanza=%s " ,
55- tags_property .name ,
56- tags_property .value ,
57- stanza ,
58- )
48+ for stanza_key , stanza_values in self .tags .items ():
49+ LOGGER .info (f"Parsing tags of stanza={ stanza_key } " )
50+ stanza_key = stanza_key .replace ("=" , '="' ) + '"'
51+ stanza_key = unquote (stanza_key )
52+ LOGGER .debug (f"Parsed tags-stanza={ stanza_key } " )
53+ for key , value in stanza_values .items ():
54+ LOGGER .info (f"Parsing tag={ key } enabled={ value } of stanza={ stanza_key } " )
5955 tag_container = {
60- "stanza" : stanza ,
61- "tag" : tags_property . name ,
62- # "enabled": True or False
56+ "stanza" : stanza_key ,
57+ "tag" : key ,
58+ "enabled" : True if value == "enabled" else False ,
6359 }
64- if tags_property .value == "enabled" :
65- tag_container ["enabled" ] = True
66- else :
67- tag_container ["enabled" ] = False
6860 yield tag_container
0 commit comments