Completed
Push — master ( 699989...d91a4b )
by Andrii
14:03
created

heppy.Response.find_text()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 4
rs 10
1
import xml.etree.ElementTree as ET
2
3
from pprint import pprint
4
5
from Doc import Doc
6
7
class Response(Doc):
8
    def __init__(self, root):
9
        self.data = {}
10
        self.root = root
11
        self.parse(self.root[0])
12
13
    def find(self, tag, name):
14
        return tag.find(name, namespaces=self.nsmap)
15
16
    def find_text(self, parent, name):
17
        tag = self.find(parent, name)
18
        if tag is not None:
19
            return tag.text
20
21
    def findall(self, tag, name):
22
        return tag.findall(name, self.nsmap)
23
24
    def parse(self, tag):
25
        ns = tag.tag.split('}')[0][1:]
26
        name = tag.tag.split('}')[1]
27
        module = self.get_module(ns)
28
        if name in module.opmap:
29
            name = module.opmap[name]
30
        method = 'parse_' + name
31
        if not hasattr(module, method):
32
            raise Exception('unknown tag', ns + ':' + name)
33
        getattr(module, method)(self, tag)
34
35
    @staticmethod
36
    def parsexml(xml):
37
        root = ET.fromstring(xml)
38
        return Response(root)
39
40
    @staticmethod
41
    def build(name, start):
42
        type = globals()[name]
43
        return type(start)
44
45