dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

txtbase.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2006-2017 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 
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):
39  super(TXTBase, self).__init__(rdclass, rdtype)
40  if isinstance(strings, binary_type) or \
41  isinstance(strings, string_types):
42  strings = [strings]
43  self.strings = []
44  for string in strings:
45  if isinstance(string, string_types):
46  string = string.encode()
47  self.strings.append(string)
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:
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):
86  strings = []
87  while rdlen > 0:
88  l = wire[current]
89  current += 1
90  rdlen -= 1
91  if l > rdlen:
93  s = wire[current: current + l].unwrap()
94  current += l
95  rdlen -= l
96  strings.append(s)
97  return cls(rdclass, rdtype, strings)
dns.exception.FormError
Definition: exception.py:109
dns.exception.SyntaxError
Definition: exception.py:113
dns.rdtypes.txtbase.TXTBase.strings
strings
Definition: txtbase.py:43
dns.rdtypes.txtbase.TXTBase.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: txtbase.py:85
dns.exception.UnexpectedEnd
Definition: exception.py:117
dns.rdtypes.txtbase.TXTBase
Definition: txtbase.py:28
dns.rdtypes.txtbase.TXTBase.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: txtbase.py:49
dns.rdtypes.txtbase.TXTBase.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: txtbase.py:58
dns.rdata._escapify
def _escapify(qstring)
Definition: rdata.py:65
dns.rdtypes.txtbase.TXTBase.__init__
def __init__(self, rdclass, rdtype, strings)
Definition: txtbase.py:38
dns.rdtypes.txtbase.TXTBase.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: txtbase.py:77
dns.rdata
Definition: rdata.py:1
dns._compat
Definition: _compat.py:1
dns.tokenizer
Definition: tokenizer.py:1
dns.exception
Definition: exception.py:1
dns.rdata.Rdata
Definition: rdata.py:95