Skip to content

Commit 21e5cfd

Browse files
xcp/xmlunwrap.py:getText() follow-up for #18: Add type comments
1 parent 75520a2 commit 21e5cfd

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

xcp/xmlunwrap.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
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

2627
import six
2728

2829
class 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

4952
def 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,24 +63,27 @@ def getStrAttribute(el, attrs, default='', mandatory=False):
5963
return matching[0]
6064

6165
def getBoolAttribute(el, attrs, default = None):
66+
# type:(Element, list, bool | None) -> bool | None
6267
mandatory = (default == None)
6368
val = getStrAttribute(el, attrs, '', mandatory).lower()
6469
if val == '':
6570
return default
6671
return val in ['yes', 'true']
6772

6873
def getIntAttribute(el, attrs, default = None):
74+
# type:(Element, list, int | None) -> int | None
6975
mandatory = (default == None)
7076
val = getStrAttribute(el, attrs, '', mandatory)
7177
if val == '':
7278
return default
7379
try:
74-
int_val = int(val, 0)
80+
int_val = int(val, 0) # type: ignore # (handled by raising XmlUnwarpError below)
7581
except Exception as e:
7682
six.raise_from(XmlUnwrapError("Invalid integer value for %s" % attrs[0]), e)
7783
return int_val
7884

7985
def getMapAttribute(el, attrs, mapping, default = None):
86+
# type:(Element, list, list[list], str | None) -> str
8087
mandatory = (default == None)
8188
k, v = zip(*mapping)
8289
key = getStrAttribute(el, attrs, default, mandatory)

0 commit comments

Comments
 (0)