Completed
Push — master ( 6834c1...18d750 )
by Andrii
12:59
created

heppy.Module.render_check_command()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 5
rs 9.4285
1
2
from pprint import pprint
3
4
class Module:
5
    opmap = {}
6
7
    def __init__(self, xmlns):
8
        self.name   = self.__class__.__name__
9
        self.xmlns  = xmlns
10
11
### RESPONSE parsing
12
13
    def parse_nothing(self, response, tag):
14
        pass
15
16
    def parse_set(self, response, tag):
17
        response.set(tag.tag.split('}')[1], tag.text)
18
19
    def parse_addpair(self, response, tag):
20
        response.addpair(tag.tag.split('}')[1] + 's', tag.text)
21
22
    def parse_descend(self, response, tag):
23
        for child in tag:
24
            response.parse(child)
25
26
    def parse_status(self, response, tag):
27
        response.addpair('statuses', tag.attrib['s'])
28
29
    def parse_cd_tag(self, response, tag):
30
        name = tag[0]
31
        response.addto('avails', {name.text.lower(): name.attrib['avail']})
32
        if len(tag)>1:
33
            response.addto('reasons', {name.text.lower(): tag[1].text})
34
35
### REQUEST rendering
36
37
    def render_epp(self, request):
38
        if request.epp is None:
39
            request.epp = request.element('epp', {'xmlns': request.get_module('epp').xmlns})
40
        return request.epp
41
42
    def render_clTRID(self, request):
43
        clTRID = request.get('clTRID', 'AA-00')
44
        if clTRID != 'NONE' and request.command is not None:
45
            request.sub(request.command, 'clTRID', {}, clTRID)
46
47
    def render_root_command(self, request, command, attrs = {}):
48
        if request.command is None:
49
            epp = self.render_epp(request)
50
            request.command = request.sub(epp, 'command')
51
        return request.sub(request.command, command, attrs)
52
53
    def render_header(self, request, source, action):
54
        return request.sub(source, self.name + ':' + action, {'xmlns:' + self.name: self.xmlns})
55
56
    def render_command(self, request, action):
57
        command = self.render_root_command(request, action)
58
        return self.render_header(request, command, action)
59
60
    def render_command_fields(self, request, command, fields = {'name': {}}):
61
        command = self.render_command(request, command)
62
        request.subfields(command, fields)
63
        return command
64
65
    def render_check_command(self, request, mod, field):
66
        command = self.render_command(request, 'check')
67
        for name in request.get(field + 's').itervalues():
68
            request.sub(command, mod+':'+field, {}, name)
69
        return command
70
71
    def render_root_extension(self, request):
72
        if request.extension is None:
73
            request.extension = request.sub(request.command, 'extension')
74
        return request.extension
75
76
    def render_extension(self, request, action):
77
        extension = self.render_root_extension(request)
78
        return self.render_header(request, extension, action)
79
80
    def render_extension_fields(self, request, action, fields):
81
        extension = self.render_extension(request, action)
82
        request.subfields(extension, fields)
83
        return extension
84
85