1 # Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
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.
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.
16 """TXT-like base class."""
22 class TXTBase(dns
.rdata
.Rdata
):
23 """Base class for rdata that is like a TXT record
25 @ivar strings: the text strings
26 @type strings: list of string
29 __slots__
= ['strings']
31 def __init__(self
, rdclass
, rdtype
, strings
):
32 super(TXTBase
, self
).__init
__(rdclass
, rdtype
)
33 if isinstance(strings
, str):
35 self
.strings
= strings
[:]
37 def to_text(self
, origin
=None, relativize
=True, **kw
):
40 for s
in self
.strings
:
41 txt
+= '%s"%s"' % (prefix
, dns
.rdata
._escapify
(s
))
45 def from_text(cls
, rdclass
, rdtype
, tok
, origin
= None, relativize
= True):
48 token
= tok
.get().unescape()
49 if token
.is_eol_or_eof():
51 if not (token
.is_quoted_string() or token
.is_identifier()):
52 raise dns
.exception
.SyntaxError("expected a string")
53 if len(token
.value
) > 255:
54 raise dns
.exception
.SyntaxError("string too long")
55 strings
.append(token
.value
)
57 raise dns
.exception
.UnexpectedEnd
58 return cls(rdclass
, rdtype
, strings
)
60 from_text
= classmethod(from_text
)
62 def to_wire(self
, file, compress
= None, origin
= None):
63 for s
in self
.strings
:
70 def from_wire(cls
, rdclass
, rdtype
, wire
, current
, rdlen
, origin
= None):
73 l
= ord(wire
[current
])
77 raise dns
.exception
.FormError
78 s
= wire
[current
: current
+ l
].unwrap()
82 return cls(rdclass
, rdtype
, strings
)
84 from_wire
= classmethod(from_wire
)
86 def _cmp(self
, other
):
87 return cmp(self
.strings
, other
.strings
)