Package dns :: Module wiredata |
[hide private]
[frames] | no frames]

# Source Code for Module dns.wiredata

     1  # Copyright (C) 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  """DNS Wire Data Helper"""
    17
    18  import sys
    19
    20  import dns.exception
    21
    22  # Figure out what constant python passes for an unspecified slice bound.
    23  # It's supposed to be sys.maxint, yet on 64-bit windows sys.maxint is 2^31 - 1
    24  # but Python uses 2^63 - 1 as the constant.  Rather than making pointless
    25  # extra comparisons, duplicating code, or weakening WireData, we just figure
    26  # out what constant Python will use.
    27
    28 -class _SliceUnspecifiedBound(str):
    29 -    def __getslice__(self, i, j):
    30          return j
    31
    32  _unspecified_bound = _SliceUnspecifiedBound('')[1:]
    33
    34 -class WireData(str):
    35      # WireData is a string with stricter slicing
    36 -    def __getitem__(self, key):
    37          try:
    38              return WireData(super(WireData, self).__getitem__(key))
    39          except IndexError:
    40              raise dns.exception.FormError
    41 -    def __getslice__(self, i, j):
    42          try:
    43              if j == _unspecified_bound:
    44                  # handle the case where the right bound is unspecified
    45                  j = len(self)
    46              if i < 0 or j < 0:
    47                  raise dns.exception.FormError
    48              # If it's not an empty slice, access left and right bounds
    49              # to make sure they're valid
    50              if i != j:
    51                  super(WireData, self).__getitem__(i)
    52                  super(WireData, self).__getitem__(j - 1)
    53              return WireData(super(WireData, self).__getslice__(i, j))
    54          except IndexError:
    55              raise dns.exception.FormError
    56 -    def __iter__(self):
    57          i = 0
    58          while 1:
    59              try:
    60                  yield self[i]
    61                  i += 1
    62              except dns.exception.FormError:
    63                  raise StopIteration
    64 -    def unwrap(self):
    65          return str(self)
    66
    67 -def maybe_wrap(wire):
    68      if not isinstance(wire, WireData):
    69          return WireData(wire)
    70      else:
    71          return wire
    72