Completed
Push — master ( cb7596...290817 )
by Jasper
01:07
created

niprov.getElementsByTagNameWithoutPrefix()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 9
rs 9.2
1
from xml.dom.minidom import parse
2
3
def getNonTextChildNodes(node):
4
    return [n for n in node.childNodes if n.nodeType != n.TEXT_NODE]
5
6
def getElementsByTagNameWithoutPrefix(node, partialTagName):
7
    childNodes = getNonTextChildNodes(node)
8
    tagLength = len(partialTagName)
9
    matchingNodes = []
10
    for childNode in childNodes:
11
        if len(childNode.tagName) >= tagLength:
12
            if childNode.tagName[-tagLength:] == partialTagName:
13
                matchingNodes.append(childNode)
14
    return matchingNodes
15
16
def getTextContent(node):
17
    textChildNodes = [n for n in node.childNodes if n.nodeType == n.TEXT_NODE]
18
    return ''.join([n.data for n in textChildNodes])
19
20
def getTextContentForFirstElementByTag(node, tagname):
21
    firstChildByTag = node.getElementsByTagName(tagname)[0]
22
    return getTextContent(firstChildByTag)
23
24
def getRefForFirstElementByTag(node, tagname):
25
    firstChildByTag = node.getElementsByTagName(tagname)[0]
26
    href = firstChildByTag.getAttribute('href')
27
    return href[1:]
28
29
def getElementById(node, targetId):
30
    for childNode in node.childNodes:
31
        if childNode.nodeType != childNode.TEXT_NODE:
32
            if childNode.hasAttribute('id'):
33
                idval = childNode.getAttribute('id')
34
                if idval == targetId:
35
                    return childNode
36
    raise ValueError('Cannot find element with id')
37