diff options
| author | Petr Spacek <pspacek@redhat.com> | 2015-03-20 15:55:03 +0100 |
|---|---|---|
| committer | Petr Viktorin <pviktori@redhat.com> | 2015-05-21 14:25:12 +0200 |
| commit | 0bda8c7d89079945c5873f87b8c45e9469a87d38 (patch) | |
| tree | e9f562c778f9efe32fd7a2edbd171446ea0f64d3 | |
| parent | e6196f1e161348bc3a17b065f490afa6139b71c9 (diff) | |
| download | dnspython-0bda8c7d89079945c5873f87b8c45e9469a87d38.tar.gz | |
Add list pretty-printing for parametrized exceptions.
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>].
| -rw-r--r-- | dns/exception.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/dns/exception.py b/dns/exception.py index 9c57826..0790aa8 100644 --- a/dns/exception.py +++ b/dns/exception.py @@ -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__() |
