2222# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2323
2424"""xmlunwrap - general methods to unwrap XML elements & attributes"""
25+ from xml .dom .minidom import Element
2526
2627import six
2728
2829class XmlUnwrapError (Exception ):
2930 pass
3031
31- def getText (nodelist ):
32+ def getText (element ):
33+ # type:(Element) -> str
34+ """Return the text of the element as stripped string"""
3235 rc = ""
3336
34- for node in nodelist .childNodes :
37+ for node in element .childNodes :
3538 if node .nodeType == node .TEXT_NODE :
3639 rc = rc + node .data
3740 if not isinstance (rc , str ): # Python 2 only, otherwise it would return unicode
@@ -47,6 +50,7 @@ def getElementsByTagName(el, tags, mandatory = False):
4750 return matching
4851
4952def getStrAttribute (el , attrs , default = '' , mandatory = False ):
53+ # type:(Element, list, str | None, bool) -> str | None
5054 matching = []
5155 for attr in attrs :
5256 val = el .getAttribute (attr )
@@ -59,30 +63,34 @@ def getStrAttribute(el, attrs, default='', mandatory=False):
5963 return matching [0 ]
6064
6165def getBoolAttribute (el , attrs , default = None ):
66+ # type:(Element, list, bool | None) -> bool | None
6267 mandatory = (default == None )
63- val = getStrAttribute (el , attrs , '' , mandatory ).lower ()
68+ # Will raise an exception if attribute is not found and default is None:
69+ val = getStrAttribute (el , attrs , '' , mandatory ).lower () # type: ignore
6470 if val == '' :
6571 return default
6672 return val in ['yes' , 'true' ]
6773
6874def getIntAttribute (el , attrs , default = None ):
75+ # type:(Element, list, int | None) -> int | None
6976 mandatory = (default == None )
7077 val = getStrAttribute (el , attrs , '' , mandatory )
7178 if val == '' :
7279 return default
7380 try :
74- int_val = int (val , 0 )
81+ int_val = int (val , 0 ) # type: ignore # (handled by raising XmlUnwarpError below)
7582 except Exception as e :
7683 six .raise_from (XmlUnwrapError ("Invalid integer value for %s" % attrs [0 ]), e )
7784 return int_val
7885
7986def getMapAttribute (el , attrs , mapping , default = None ):
87+ # type:(Element, list, list[list], str | None) -> str
8088 mandatory = (default == None )
8189 k , v = zip (* mapping )
8290 key = getStrAttribute (el , attrs , default , mandatory )
8391
8492 if key not in k :
85- raise XmlUnwrapError ("Unexpected key %s for attribute" % key )
93+ raise XmlUnwrapError ("Unexpected key " + str ( key ) + " for attribute" )
8694
8795 k_list = list (k )
8896 return v [k_list .index (key )]
0 commit comments