]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Add list pretty-printing for parametrized exceptions.
authorPetr Spacek <pspacek@redhat.com>
Fri, 20 Mar 2015 14:55:03 +0000 (15:55 +0100)
committerPetr Viktorin <pviktori@redhat.com>
Thu, 21 May 2015 12:25:12 +0000 (14:25 +0200)
All list entries are converted to strings before the list is printed.
I.e. one-item list is printed as 'vm-123.idm.lab.eng.brq.redhat.com. IN NS'
instead [<DNS vm-123.idm.lab.eng.brq.redhat.com. IN NS RRset>].

dns/exception.py

index 9c5782654f6c2439d7aedce4d5f07d7467de0e78..0790aa8bfba17d919defd3599154a890fd11a370 100644 (file)
@@ -63,10 +63,29 @@ class DNSException(Exception):
                 'following set of keyword args is required: %s' % (
                     self.supp_kwargs)
 
+    def _fmt_kwargs(self, **kwargs):
+        """Format kwargs before printing them.
+
+        Resulting dictionary has to have keys necessary for str.format call
+        on fmt class variable.
+        """
+        fmtargs = {}
+        for kw, data in kwargs.items():
+            if isinstance(data, (list, set)):
+                # convert list of <someobj> to list of str(<someobj>)
+                fmtargs[kw] = list(map(str, data))
+                if len(fmtargs[kw]) == 1:
+                    # remove list brackets [] from single-item lists
+                    fmtargs[kw] = fmtargs[kw].pop()
+            else:
+                fmtargs[kw] = data
+        return fmtargs
+
     def __str__(self):
         if self.kwargs and self.fmt:
             # provide custom message constructed from keyword arguments
-            return self.fmt.format(**self.kwargs)
+            fmtargs = self._fmt_kwargs(**self.kwargs)
+            return self.fmt.format(**fmtargs)
         else:
             # print *args directly in the same way as old DNSException
             return super(DNSException, self).__str__()