s3:lib/events: s/EVENT_FD/TEVENT_FD
[Samba/gebeck_regimport.git] / lib / dnspython / dns / wiredata.py
blob86d954a90a6af1d26e3ed43100f393898a0a17e9
1 # Copyright (C) 2011 Nominum, Inc.
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.
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.
16 """DNS Wire Data Helper"""
18 import sys
20 import dns.exception
22 class WireData(str):
23 # WireData is a string with stricter slicing
24 def __getitem__(self, key):
25 try:
26 return WireData(super(WireData, self).__getitem__(key))
27 except IndexError:
28 raise dns.exception.FormError
29 def __getslice__(self, i, j):
30 try:
31 if j == sys.maxint:
32 # handle the case where the right bound is unspecified
33 j = len(self)
34 if i < 0 or j < 0:
35 raise dns.exception.FormError
36 # If it's not an empty slice, access left and right bounds
37 # to make sure they're valid
38 if i != j:
39 super(WireData, self).__getitem__(i)
40 super(WireData, self).__getitem__(j - 1)
41 return WireData(super(WireData, self).__getslice__(i, j))
42 except IndexError:
43 raise dns.exception.FormError
44 def __iter__(self):
45 i = 0
46 while 1:
47 try:
48 yield self[i]
49 i += 1
50 except dns.exception.FormError:
51 raise StopIteration
52 def unwrap(self):
53 return str(self)
55 def maybe_wrap(wire):
56 if not isinstance(wire, WireData):
57 return WireData(wire)
58 else:
59 return wire