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