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