1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Common DNS Exceptions."""
17
18
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
43 supp_kwargs = set()
44 fmt = None
45
57
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
71
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
82 fmtargs[kw] = list(map(str, data))
83 if len(fmtargs[kw]) == 1:
84
85 fmtargs[kw] = fmtargs[kw].pop()
86 else:
87 fmtargs[kw] = data
88 return fmtargs
89
98
99
103
104
106
107 """Text input is malformed."""
108
109
111
112 """Text input ended unexpectedly."""
113
114
116
117 """The DNS message is too big."""
118
119
121
122 """The DNS operation timed out."""
123 supp_kwargs = set(['timeout'])
124 fmt = "The DNS operation timed out after {timeout} seconds"
125