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

heppy.Doc.has()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
from importlib import import_module
2
3
class Doc:
4
    nsmap = {
5
        'builtin':      'builtin',
6
        'epp':          'urn:ietf:params:xml:ns:epp-1.0',
7
        'host':         'urn:ietf:params:xml:ns:host-1.0',
8
        'domain':       'urn:ietf:params:xml:ns:domain-1.0',
9
        'contact':      'urn:ietf:params:xml:ns:contact-1.0',
10
        'secDNS':       'urn:ietf:params:xml:ns:secDNS-1.1',
11
        'fee':          'urn:ietf:params:xml:ns:fee-0.7',
12
        'rgp':          'urn:ietf:params:xml:ns:rgp-1.0',
13
        'oxrs':         'urn:afilias:params:xml:ns:oxrs-1.1',
14
        'namestoreExt': 'http://www.verisign-grs.com/epp/namestoreExt-1.1',
15
    }
16
17
    okcodes = {
18
        '1000': 'completed',
19
        '1001': 'pending',
20
        '1300': 'no messages',
21
        '1301': 'ack to dequeue',
22
        '1500': 'ending session',
23
    }
24
25
    modules = {}
26
27
    def get_module(self, ns):
28
        if ns in self.nsmap:
29
            ns = self.nsmap[ns]
30
        if self.modules == {}:
31
            for name,nsi in self.nsmap.iteritems():
32
                self.modules[nsi] = name
33
        module = self.modules.get(ns)
34
        if isinstance(module, basestring):
35
            module = self.build_module(ns, module)
36
            self.modules[ns] = module
37
        return module
38
39
    def build_module(self, ns, name):
40
        lib = import_module('heppy.modules.' + name)
41
        type = getattr(lib, name)
42
        return type(ns)
43
44
    def get(self, name, default = None):
45
        return self.data.get(name, default)
46
47
    def has(self, name):
48
        return name in self.data
49
50
    @staticmethod
51
    def mget(data, map):
52
        return {k:data.get(v or k) for k,v in map.iteritems()}
53
54
    def set(self, name, value):
55
        self.data[name] = value
56
57
    def addto(self, name, values):
58
        if not name in self.data:
59
            self.data[name] = {}
60
        for k,v in values.iteritems():
61
            self.data[name][k] = v
62
63
    def addpair(self, name, value):
64
        self.addto(name, {value: value})
65
66