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