Package dns :: Package rdtypes :: Module txtbase |
[hide private]
[frames] | no frames]

# Source Code for Module dns.rdtypes.txtbase

     1  # Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
     2  #
     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.
     7  #
     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.
    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):
    37          super(TXTBase, self).__init__(rdclass, rdtype)
    38          if isinstance(strings, str):
    39              strings = [strings]
    40          self.strings = 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):
    79          strings = []
    80          while rdlen > 0:
    81              l = wire[current]
    82              current += 1
    83              rdlen -= 1
    84              if l > rdlen:
    85                  raise dns.exception.FormError
    86              s = wire[current: current + l].unwrap()
    87              current += l
    88              rdlen -= l
    89              strings.append(s)
    90          return cls(rdclass, rdtype, strings)
    91