s4-python: Format to PEP8, simplify tests.
[kai/samba-autobuild/.git] / lib / dnspython / dns / rdtypes / nsbase.py
1 # Copyright (C) 2003-2007, 2009, 2010 Nominum, Inc.
2 #
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16 """NS-like base classes."""
17
18 import cStringIO
19
20 import dns.exception
21 import dns.rdata
22 import dns.name
23
24 class NSBase(dns.rdata.Rdata):
25     """Base class for rdata that is like an NS record.
26
27     @ivar target: the target name of the rdata
28     @type target: dns.name.Name object"""
29
30     __slots__ = ['target']
31
32     def __init__(self, rdclass, rdtype, target):
33         super(NSBase, self).__init__(rdclass, rdtype)
34         self.target = target
35
36     def to_text(self, origin=None, relativize=True, **kw):
37         target = self.target.choose_relativity(origin, relativize)
38         return str(target)
39
40     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
41         target = tok.get_name()
42         target = target.choose_relativity(origin, relativize)
43         tok.get_eol()
44         return cls(rdclass, rdtype, target)
45
46     from_text = classmethod(from_text)
47
48     def to_wire(self, file, compress = None, origin = None):
49         self.target.to_wire(file, compress, origin)
50
51     def to_digestable(self, origin = None):
52         return self.target.to_digestable(origin)
53
54     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
55         (target, cused) = dns.name.from_wire(wire[: current + rdlen],
56                                              current)
57         if cused != rdlen:
58             raise dns.exception.FormError
59         if not origin is None:
60             target = target.relativize(origin)
61         return cls(rdclass, rdtype, target)
62
63     from_wire = classmethod(from_wire)
64
65     def choose_relativity(self, origin = None, relativize = True):
66         self.target = self.target.choose_relativity(origin, relativize)
67
68     def _cmp(self, other):
69         return cmp(self.target, other.target)
70
71 class UncompressedNS(NSBase):
72     """Base class for rdata that is like an NS record, but whose name
73     is not compressed when convert to DNS wire format, and whose
74     digestable form is not downcased."""
75
76     def to_wire(self, file, compress = None, origin = None):
77         super(UncompressedNS, self).to_wire(file, None, origin)
78
79     def to_digestable(self, origin = None):
80         f = cStringIO.StringIO()
81         self.to_wire(f, None, origin)
82         return f.getvalue()