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 -class WireData(bytes):
23 # WireData is a bytes with stricter slicing
24 - def __getitem__(self, key):
25 try: 26 if isinstance(key, slice): 27 start = key.start 28 if start is None: 29 start = 0 30 elif start < 0: 31 start += len(self) 32 stop = key.stop 33 if stop is None: 34 stop = len(self) 35 elif stop < 0: 36 stop += len(self) 37 if start < 0 or stop < 0: 38 raise dns.exception.FormError 39 # If it's not an empty slice, access left and right bounds 40 # to make sure they're valid 41 if start != stop: 42 super(WireData, self).__getitem__(start) 43 super(WireData, self).__getitem__(stop - 1) 44 return WireData(super(WireData, self).__getitem__(key)) 45 else: 46 return super(WireData, self).__getitem__(key) 47 except IndexError: 48 raise dns.exception.FormError
49 - def __iter__(self):
50 i = 0 51 while 1: 52 try: 53 yield self[i] 54 i += 1 55 except dns.exception.FormError: 56 raise StopIteration
57 - def unwrap(self):
58 return bytes(self)
59
60 -def maybe_wrap(wire):
61 if not isinstance(wire, WireData): 62 return WireData(wire) 63 else: 64 return wire
65