99"""
1010import re
1111from typing import List , Tuple , Union
12- from hsd .common import np , ATTRIB_SUFFIX , HSD_ATTRIB_SUFFIX , HsdError ,\
12+ from hsd .common import HSD_ATTRIB_NAME , np , ATTRIB_SUFFIX , HSD_ATTRIB_SUFFIX , HsdError ,\
1313 QUOTING_CHARS , SPECIAL_CHARS
1414from hsd .eventhandler import HsdEventHandler , HsdEventPrinter
1515
@@ -42,14 +42,16 @@ class HsdDictBuilder(HsdEventHandler):
4242
4343 Args:
4444 flatten_data: Whether multiline data in the HSD input should be
45- flattened into a single list. Othewise a list of lists is created,
46- with one list for every line (default).
47- include_hsd_attribs: Whether the HSD-attributes (processing related
48- attributes, like original tag name, line information, etc.) should
49- be stored.
45+ flattened into a single list. Othewise a list of lists is created, with one list for
46+ every line (default).
47+ lower_tag_names: Whether tag names should be all converted to lower case (to ease case
48+ insensitive processing). Default: False. If set and include_hsd_attribs is also set,
49+ the original tag names can be retrieved from the "name" hsd attributes.
50+ include_hsd_attribs: Whether the HSD-attributes (processing related attributes, like
51+ original tag name, line information, etc.) should be stored (default: False).
5052 """
5153
52- def __init__ (self , flatten_data : bool = False ,
54+ def __init__ (self , flatten_data : bool = False , lower_tag_names : bool = False ,
5355 include_hsd_attribs : bool = False ):
5456 super ().__init__ ()
5557 self ._hsddict : dict = {}
@@ -58,6 +60,7 @@ def __init__(self, flatten_data: bool = False,
5860 self ._data : Union [None , _DataType ] = None
5961 self ._attribs : List [Tuple [str , dict ]] = []
6062 self ._flatten_data : bool = flatten_data
63+ self ._lower_tag_names : bool = lower_tag_names
6164 self ._include_hsd_attribs : bool = include_hsd_attribs
6265
6366
@@ -79,46 +82,49 @@ def open_tag(self, tagname, attrib, hsdattrib):
7982 def close_tag (self , tagname ):
8083 attrib , hsdattrib = self ._attribs .pop (- 1 )
8184 parentblock = self ._parentblocks .pop (- 1 )
85+ key = tagname .lower () if self ._lower_tag_names else tagname
8286 prevcont = parentblock .get (tagname )
87+
8388 if self ._data is not None :
8489 if prevcont is None :
85- parentblock [tagname ] = self ._data
90+ parentblock [key ] = self ._data
8691 elif isinstance (prevcont , list ) and len (prevcont ) > 0 and isinstance (prevcont [0 ], dict ):
8792 prevcont .append ({None : self ._data })
8893 elif isinstance (prevcont , dict ):
89- parentblock [tagname ] = [prevcont , {None : self ._data }]
94+ parentblock [key ] = [prevcont , {None : self ._data }]
9095 else :
91- parentblock [tagname ] = [{None : prevcont }, {None : self ._data }]
96+ parentblock [key ] = [{None : prevcont }, {None : self ._data }]
9297 else :
9398 if prevcont is None :
94- parentblock [tagname ] = self ._curblock
99+ parentblock [key ] = self ._curblock
95100 elif isinstance (prevcont , list ) and len (prevcont ) > 0 and isinstance (prevcont [0 ], dict ):
96101 prevcont .append (self ._curblock )
97102 elif isinstance (prevcont , dict ):
98- parentblock [tagname ] = [prevcont , self ._curblock ]
103+ parentblock [key ] = [prevcont , self ._curblock ]
99104 else :
100- parentblock [tagname ] = [{None : prevcont }, self ._curblock ]
105+ parentblock [key ] = [{None : prevcont }, self ._curblock ]
101106
102- if prevcont is None :
103- if attrib :
104- parentblock [tagname + ATTRIB_SUFFIX ] = attrib
105- if self ._include_hsd_attribs :
106- parentblock [tagname + HSD_ATTRIB_SUFFIX ] = hsdattrib
107- else :
108- prevattrib = parentblock .get (tagname + ATTRIB_SUFFIX )
107+ if attrib and prevcont is None :
108+ parentblock [key + ATTRIB_SUFFIX ] = attrib
109+ elif prevcont is not None :
110+ prevattrib = parentblock .get (key + ATTRIB_SUFFIX )
109111 if isinstance (prevattrib , list ):
110112 prevattrib .append (attrib )
111113 else :
112- parentblock [tagname + ATTRIB_SUFFIX ] = [prevattrib , attrib ]
113- print (f"parentblock[{ tagname } + { ATTRIB_SUFFIX } ] = [{ prevattrib } , { attrib } ]" )
114+ parentblock [key + ATTRIB_SUFFIX ] = [prevattrib , attrib ]
114115
115- if self ._include_hsd_attribs :
116- prevhsdattrib = parentblock .get (tagname + HSD_ATTRIB_SUFFIX )
116+ if self ._include_hsd_attribs :
117+ if self ._lower_tag_names :
118+ hsdattrib = {} if hsdattrib is None else hsdattrib
119+ hsdattrib [HSD_ATTRIB_NAME ] = tagname
120+ if prevcont is None :
121+ parentblock [key + HSD_ATTRIB_SUFFIX ] = hsdattrib
122+ else :
123+ prevhsdattrib = parentblock .get (key + HSD_ATTRIB_SUFFIX )
117124 if isinstance (prevhsdattrib , list ):
118125 prevhsdattrib .append (hsdattrib )
119126 else :
120- parentblock [tagname + HSD_ATTRIB_SUFFIX ] = [prevhsdattrib ,
121- hsdattrib ]
127+ parentblock [key + HSD_ATTRIB_SUFFIX ] = [prevhsdattrib , hsdattrib ]
122128 self ._curblock = parentblock
123129 self ._data = None
124130
0 commit comments