1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """TXT-like base class."""
17
18 import dns.exception
19 import dns.rdata
20 import dns.tokenizer
21 import dns.util
24 """Base class for rdata that is like a TXT record
25
26 @ivar strings: the text strings
27 @type strings: list of string
28 @see: RFC 1035"""
29
30 __slots__ = ['strings']
31
32 - def __init__(self, rdclass, rdtype, strings):
37
38 - def to_text(self, origin=None, relativize=True, **kw):
39 txt = ''
40 prefix = ''
41 for s in self.strings:
42 txt += '%s"%s"' % (prefix, dns.rdata._escapify(s))
43 prefix = ' '
44 return txt
45
46 @classmethod
47 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
48 strings = []
49 while 1:
50 token = tok.get().unescape()
51 if token.is_eol_or_eof():
52 break
53 if not (token.is_quoted_string() or token.is_identifier()):
54 raise dns.exception.SyntaxError("expected a string")
55 if len(token.value) > 255:
56 raise dns.exception.SyntaxError("string too long")
57 strings.append(token.value)
58 if len(strings) == 0:
59 raise dns.exception.UnexpectedEnd
60 return cls(rdclass, rdtype, strings)
61
62 - def to_wire(self, file, compress = None, origin = None):
63 for s in self.strings:
64 l = len(s)
65 assert l < 256
66 dns.util.write_uint8(file, l)
67 file.write(s.encode('latin_1'))
68
69 @classmethod
70 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
71 strings = []
72 while rdlen > 0:
73 l = wire[current]
74 current += 1
75 rdlen -= 1
76 if l > rdlen:
77 raise dns.exception.FormError
78 s = wire[current : current + l].decode('latin_1')
79 current += l
80 rdlen -= l
81 strings.append(s)
82 return cls(rdclass, rdtype, strings)
83
84 - def _cmp(self, other):
86