1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Common DNS Exceptions."""
17
18
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
42 supp_kwargs = set()
43 fmt = None
44
56
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
70
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
81 fmtargs[kw] = list(map(str, data))
82 if len(fmtargs[kw]) == 1:
83
84 fmtargs[kw] = fmtargs[kw].pop()
85 else:
86 fmtargs[kw] = data
87 return fmtargs
88
97
98
101
103 """Text input is malformed."""
104
106 """Text input ended unexpectedly."""
107
109 """The DNS message is too big."""
110
112 """The DNS operation timed out."""
113 supp_kwargs = set(['timeout'])
114 fmt = "%s after {timeout} seconds" % __doc__[:-1]
115