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

Source Code for Module dns.exception

  1  # Copyright (C) 2003-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  """Common DNS Exceptions.""" 
 17   
 18   
19 -class DNSException(Exception):
20 21 """Abstract base class shared by all dnspython exceptions. 22 23 It supports two basic modes of operation: 24 25 a) Old/compatible mode is used if __init__ was called with 26 empty **kwargs. 27 In compatible mode all *args are passed to standard Python Exception class 28 as before and all *args are printed by standard __str__ implementation. 29 Class variable msg (or doc string if msg is None) is returned from str() 30 if *args is empty. 31 32 b) New/parametrized mode is used if __init__ was called with 33 non-empty **kwargs. 34 In the new mode *args has to be empty and all kwargs has to exactly match 35 set in class variable self.supp_kwargs. All kwargs are stored inside 36 self.kwargs and used in new __str__ implementation to construct 37 formatted message based on self.fmt string. 38 39 In the simplest case it is enough to override supp_kwargs and fmt 40 class variables to get nice parametrized messages. 41 """ 42 msg = None # non-parametrized message 43 supp_kwargs = set() # accepted parameters for _fmt_kwargs (sanity check) 44 fmt = None # message parametrized with results from _fmt_kwargs 45
46 - def __init__(self, *args, **kwargs):
47 self._check_params(*args, **kwargs) 48 self._check_kwargs(**kwargs) 49 self.kwargs = kwargs 50 if self.msg is None: 51 # doc string is better implicit message than empty string 52 self.msg = self.__doc__ 53 if args: 54 super(DNSException, self).__init__(*args) 55 else: 56 super(DNSException, self).__init__(self.msg)
57
58 - def _check_params(self, *args, **kwargs):
59 """Old exceptions supported only args and not kwargs. 60 61 For sanity we do not allow to mix old and new behavior.""" 62 if args or kwargs: 63 assert bool(args) != bool(kwargs), \ 64 'keyword arguments are mutually exclusive with positional args'
65
66 - def _check_kwargs(self, **kwargs):
67 if kwargs: 68 assert set(kwargs.keys()) == self.supp_kwargs, \ 69 'following set of keyword args is required: %s' % ( 70 self.supp_kwargs)
71
72 - def _fmt_kwargs(self, **kwargs):
73 """Format kwargs before printing them. 74 75 Resulting dictionary has to have keys necessary for str.format call 76 on fmt class variable. 77 """ 78 fmtargs = {} 79 for kw, data in kwargs.items(): 80 if isinstance(data, (list, set)): 81 # convert list of <someobj> to list of str(<someobj>) 82 fmtargs[kw] = list(map(str, data)) 83 if len(fmtargs[kw]) == 1: 84 # remove list brackets [] from single-item lists 85 fmtargs[kw] = fmtargs[kw].pop() 86 else: 87 fmtargs[kw] = data 88 return fmtargs
89
90 - def __str__(self):
91 if self.kwargs and self.fmt: 92 # provide custom message constructed from keyword arguments 93 fmtargs = self._fmt_kwargs(**self.kwargs) 94 return self.fmt.format(**fmtargs) 95 else: 96 # print *args directly in the same way as old DNSException 97 return super(DNSException, self).__str__()
98 99
100 -class FormError(DNSException):
101 102 """DNS message is malformed."""
103 104
105 -class SyntaxError(DNSException):
106 107 """Text input is malformed."""
108 109
110 -class UnexpectedEnd(SyntaxError):
111 112 """Text input ended unexpectedly."""
113 114
115 -class TooBig(DNSException):
116 117 """The DNS message is too big."""
118 119
120 -class Timeout(DNSException):
121 122 """The DNS operation timed out.""" 123 supp_kwargs = set(['timeout']) 124 fmt = "The DNS operation timed out after {timeout} seconds"
125