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

# Source Code for Module dns.rdtypes.txtbase

     1  # Copyright (C) 2006, 2007, 2009, 2010 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 dns.exception
    19  import dns.rdata
    20  import dns.tokenizer
    21
    22 -class TXTBase(dns.rdata.Rdata):
    23      """Base class for rdata that is like a TXT record
    24
    25      @ivar strings: the text strings
    26      @type strings: list of string
    27      @see: RFC 1035"""
    28
    29      __slots__ = ['strings']
    30
    31 -    def __init__(self, rdclass, rdtype, strings):
    32          super(TXTBase, self).__init__(rdclass, rdtype)
    33          if isinstance(strings, str):
    34              strings = [ strings ]
    35          self.strings = strings[:]
    36
    37 -    def to_text(self, origin=None, relativize=True, **kw):
    38          txt = ''
    39          prefix = ''
    40          for s in self.strings:
    41              txt += '%s"%s"' % (prefix, dns.rdata._escapify(s))
    42              prefix = ' '
    43          return txt
    44
    45 -    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
    46          strings = []
    47          while 1:
    48              token = tok.get().unescape()
    49              if token.is_eol_or_eof():
    50                  break
    51              if not (token.is_quoted_string() or token.is_identifier()):
    52                  raise dns.exception.SyntaxError("expected a string")
    53              if len(token.value) > 255:
    54                  raise dns.exception.SyntaxError("string too long")
    55              strings.append(token.value)
    56          if len(strings) == 0:
    57              raise dns.exception.UnexpectedEnd
    58          return cls(rdclass, rdtype, strings)
    59
    60      from_text = classmethod(from_text)
    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              byte = chr(l)
    67              file.write(byte)
    68              file.write(s)
    69
    70 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
    71          strings = []
    72          while rdlen > 0:
    73              l = ord(wire[current])
    74              current += 1
    75              rdlen -= 1
    76              if l > rdlen:
    77                  raise dns.exception.FormError
    78              s = wire[current : current + l]
    79              current += l
    80              rdlen -= l
    81              strings.append(s)
    82          return cls(rdclass, rdtype, strings)
    83
    84      from_wire = classmethod(from_wire)
    85
    86 -    def _cmp(self, other):
    87          return cmp(self.strings, other.strings)
    88