From 368835bdf581467b0d7324eda0effc6ca09e618e Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 6 Sep 2018 14:18:29 +0300 Subject: Upgrade Python syntax with pyupgrade https://github.com/asottile/pyupgrade --- dns/dnssec.py | 2 +- dns/edns.py | 4 ++-- dns/exception.py | 2 +- dns/flags.py | 4 ++-- dns/ipv6.py | 5 +++-- dns/name.py | 2 +- dns/opcode.py | 2 +- dns/rcode.py | 2 +- dns/rdata.py | 2 +- dns/rdataclass.py | 2 +- dns/rdataset.py | 6 +++--- dns/rdatatype.py | 2 +- dns/rdtypes/ANY/GPOS.py | 2 +- dns/rdtypes/ANY/HINFO.py | 4 ++-- dns/rdtypes/ANY/ISDN.py | 2 +- dns/rdtypes/ANY/LOC.py | 2 +- dns/rdtypes/ANY/NSEC.py | 2 +- dns/rdtypes/ANY/RP.py | 2 +- dns/rdtypes/dnskeybase.py | 2 +- dns/rdtypes/txtbase.py | 2 +- dns/resolver.py | 10 +++++----- dns/zone.py | 2 +- examples/zonediff.py | 20 ++++++++++---------- setup.py | 2 +- tests/test_exceptions.py | 2 +- tests/test_rdtypeanydnskey.py | 18 +++++++++--------- tests/test_rdtypeanyloc.py | 8 ++++---- 27 files changed, 58 insertions(+), 57 deletions(-) diff --git a/dns/dnssec.py b/dns/dnssec.py index 1bd422e..5426373 100644 --- a/dns/dnssec.py +++ b/dns/dnssec.py @@ -87,7 +87,7 @@ _algorithm_by_text = { # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mapping not to be true inverse. -_algorithm_by_value = dict((y, x) for x, y in _algorithm_by_text.items()) +_algorithm_by_value = {y: x for x, y in _algorithm_by_text.items()} def algorithm_from_text(text): diff --git a/dns/edns.py b/dns/edns.py index 22104d8..487e67e 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -197,8 +197,8 @@ class ECSOption(Option): self.addrdata = self.addrdata[:-1] + last def to_text(self): - return "ECS %s/%s scope/%s" % (self.address, self.srclen, - self.scopelen) + return "ECS {}/{} scope/{}".format(self.address, self.srclen, + self.scopelen) def to_wire(self, file): file.write(struct.pack('!H', self.family)) diff --git a/dns/exception.py b/dns/exception.py index 89aee9c..57f2bc1 100644 --- a/dns/exception.py +++ b/dns/exception.py @@ -122,5 +122,5 @@ class TooBig(DNSException): class Timeout(DNSException): """The DNS operation timed out.""" - supp_kwargs = set(['timeout']) + supp_kwargs = {'timeout'} fmt = "The DNS operation timed out after {timeout} seconds" diff --git a/dns/flags.py b/dns/flags.py index f9a62b3..f6e30e2 100644 --- a/dns/flags.py +++ b/dns/flags.py @@ -56,9 +56,9 @@ _edns_by_text = { # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mappings not to be true inverses. -_by_value = dict((y, x) for x, y in _by_text.items()) +_by_value = {y: x for x, y in _by_text.items()} -_edns_by_value = dict((y, x) for x, y in _edns_by_text.items()) +_edns_by_value = {y: x for x, y in _edns_by_text.items()} def _order_flags(table): diff --git a/dns/ipv6.py b/dns/ipv6.py index 49ef556..db1e9c9 100644 --- a/dns/ipv6.py +++ b/dns/ipv6.py @@ -117,8 +117,9 @@ def inet_aton(text): m = _v4_ending.match(text) if not m is None: b = bytearray(dns.ipv4.inet_aton(m.group(2))) - text = (u"%s:%02x%02x:%02x%02x" % (m.group(1).decode(), b[0], b[1], - b[2], b[3])).encode() + text = (u"{}:{:02x}{:02x}:{:02x}{:02x}".format(m.group(1).decode(), + b[0], b[1], b[2], + b[3])).encode() # # Try to turn '::' into ':'; if no match try to # turn '::' into ':' diff --git a/dns/name.py b/dns/name.py index 9764de5..2b159b4 100644 --- a/dns/name.py +++ b/dns/name.py @@ -98,7 +98,7 @@ class NoIDNA2008(dns.exception.DNSException): class IDNAException(dns.exception.DNSException): """IDNA processing raised an exception.""" - supp_kwargs = set(['idna_exception']) + supp_kwargs = {'idna_exception'} fmt = "IDNA processing exception: {idna_exception}" diff --git a/dns/opcode.py b/dns/opcode.py index 5dcd2ab..249e359 100644 --- a/dns/opcode.py +++ b/dns/opcode.py @@ -40,7 +40,7 @@ _by_text = { # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mapping not to be true inverse. -_by_value = dict((y, x) for x, y in _by_text.items()) +_by_value = {y: x for x, y in _by_text.items()} class UnknownOpcode(dns.exception.DNSException): diff --git a/dns/rcode.py b/dns/rcode.py index eb4d1d6..4d564c7 100644 --- a/dns/rcode.py +++ b/dns/rcode.py @@ -62,7 +62,7 @@ _by_text = { # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mapping not to be a true inverse. -_by_value = dict((y, x) for x, y in _by_text.items()) +_by_value = {y: x for x, y in _by_text.items()} class UnknownRcode(dns.exception.DNSException): diff --git a/dns/rdata.py b/dns/rdata.py index 5123a24..06c4722 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -427,7 +427,7 @@ def from_wire(rdclass, rdtype, wire, current, rdlen, origin=None): class RdatatypeExists(dns.exception.DNSException): """DNS rdatatype already exists.""" - supp_kwargs = set(['rdclass', 'rdtype']) + supp_kwargs = {'rdclass', 'rdtype'} fmt = "The rdata type with class {rdclass} and rdtype {rdtype} " + \ "already exists." diff --git a/dns/rdataclass.py b/dns/rdataclass.py index 916dc61..d3d883d 100644 --- a/dns/rdataclass.py +++ b/dns/rdataclass.py @@ -39,7 +39,7 @@ _by_text = { # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mapping not to be true inverse. -_by_value = dict((y, x) for x, y in _by_text.items()) +_by_value = {y: x for x, y in _by_text.items()} # Now that we've built the inverse map, we can add class aliases to # the _by_text mapping. diff --git a/dns/rdataset.py b/dns/rdataset.py index bd8cfab..96f0661 100644 --- a/dns/rdataset.py +++ b/dns/rdataset.py @@ -204,9 +204,9 @@ class Rdataset(dns.set.Set): # some dynamic updates, so we don't need to print out the TTL # (which is meaningless anyway). # - s.write(u'%s%s%s %s\n' % (ntext, pad, - dns.rdataclass.to_text(rdclass), - dns.rdatatype.to_text(self.rdtype))) + s.write(u'{}{}{} {}\n'.format(ntext, pad, + dns.rdataclass.to_text(rdclass), + dns.rdatatype.to_text(self.rdtype))) else: for rd in self: s.write(u'%s%s%d %s %s %s\n' % diff --git a/dns/rdatatype.py b/dns/rdatatype.py index c9bf599..94cbb73 100644 --- a/dns/rdatatype.py +++ b/dns/rdatatype.py @@ -167,7 +167,7 @@ _by_text = { # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mapping not to be true inverse. -_by_value = dict((y, x) for x, y in _by_text.items()) +_by_value = {y: x for x, y in _by_text.items()} _metatypes = { OPT: True diff --git a/dns/rdtypes/ANY/GPOS.py b/dns/rdtypes/ANY/GPOS.py index a359a77..4f22818 100644 --- a/dns/rdtypes/ANY/GPOS.py +++ b/dns/rdtypes/ANY/GPOS.py @@ -80,7 +80,7 @@ class GPOS(dns.rdata.Rdata): self.altitude = altitude def to_text(self, origin=None, relativize=True, **kw): - return '%s %s %s' % (self.latitude.decode(), + return '{} {} {}'.format(self.latitude.decode(), self.longitude.decode(), self.altitude.decode()) diff --git a/dns/rdtypes/ANY/HINFO.py b/dns/rdtypes/ANY/HINFO.py index e5a1bea..1e0620a 100644 --- a/dns/rdtypes/ANY/HINFO.py +++ b/dns/rdtypes/ANY/HINFO.py @@ -45,8 +45,8 @@ class HINFO(dns.rdata.Rdata): self.os = os def to_text(self, origin=None, relativize=True, **kw): - return '"%s" "%s"' % (dns.rdata._escapify(self.cpu), - dns.rdata._escapify(self.os)) + return '"{}" "{}"'.format(dns.rdata._escapify(self.cpu), + dns.rdata._escapify(self.os)) @classmethod def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): diff --git a/dns/rdtypes/ANY/ISDN.py b/dns/rdtypes/ANY/ISDN.py index da2ae3a..e264814 100644 --- a/dns/rdtypes/ANY/ISDN.py +++ b/dns/rdtypes/ANY/ISDN.py @@ -46,7 +46,7 @@ class ISDN(dns.rdata.Rdata): def to_text(self, origin=None, relativize=True, **kw): if self.subaddress: - return '"%s" "%s"' % (dns.rdata._escapify(self.address), + return '"{}" "{}"'.format(dns.rdata._escapify(self.address), dns.rdata._escapify(self.subaddress)) else: return '"%s"' % dns.rdata._escapify(self.address) diff --git a/dns/rdtypes/ANY/LOC.py b/dns/rdtypes/ANY/LOC.py index b433da9..cd5e3fb 100644 --- a/dns/rdtypes/ANY/LOC.py +++ b/dns/rdtypes/ANY/LOC.py @@ -156,7 +156,7 @@ class LOC(dns.rdata.Rdata): if self.size != _default_size or \ self.horizontal_precision != _default_hprec or \ self.vertical_precision != _default_vprec: - text += " %0.2fm %0.2fm %0.2fm" % ( + text += " {:0.2f}m {:0.2f}m {:0.2f}m".format( self.size / 100.0, self.horizontal_precision / 100.0, self.vertical_precision / 100.0 ) diff --git a/dns/rdtypes/ANY/NSEC.py b/dns/rdtypes/ANY/NSEC.py index dfe9685..470a49d 100644 --- a/dns/rdtypes/ANY/NSEC.py +++ b/dns/rdtypes/ANY/NSEC.py @@ -50,7 +50,7 @@ class NSEC(dns.rdata.Rdata): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) text += (' ' + ' '.join(bits)) - return '%s%s' % (next, text) + return '{}{}'.format(next, text) @classmethod def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): diff --git a/dns/rdtypes/ANY/RP.py b/dns/rdtypes/ANY/RP.py index e9071c7..5b701d9 100644 --- a/dns/rdtypes/ANY/RP.py +++ b/dns/rdtypes/ANY/RP.py @@ -39,7 +39,7 @@ class RP(dns.rdata.Rdata): def to_text(self, origin=None, relativize=True, **kw): mbox = self.mbox.choose_relativity(origin, relativize) txt = self.txt.choose_relativity(origin, relativize) - return "%s %s" % (str(mbox), str(txt)) + return "{} {}".format(str(mbox), str(txt)) @classmethod def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): diff --git a/dns/rdtypes/dnskeybase.py b/dns/rdtypes/dnskeybase.py index 85c4b23..4f730ce 100644 --- a/dns/rdtypes/dnskeybase.py +++ b/dns/rdtypes/dnskeybase.py @@ -38,7 +38,7 @@ _flag_by_text = { # We construct the inverse mapping programmatically to ensure that we # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that # would cause the mapping not to be true inverse. -_flag_by_value = dict((y, x) for x, y in _flag_by_text.items()) +_flag_by_value = {y: x for x, y in _flag_by_text.items()} def flags_to_text_set(flags): diff --git a/dns/rdtypes/txtbase.py b/dns/rdtypes/txtbase.py index aa341f1..bb77609 100644 --- a/dns/rdtypes/txtbase.py +++ b/dns/rdtypes/txtbase.py @@ -48,7 +48,7 @@ class TXTBase(dns.rdata.Rdata): txt = '' prefix = '' for s in self.strings: - txt += '%s"%s"' % (prefix, dns.rdata._escapify(s)) + txt += '{}"{}"'.format(prefix, dns.rdata._escapify(s)) prefix = ' ' return txt diff --git a/dns/resolver.py b/dns/resolver.py index b44431c..b794d31 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -47,7 +47,7 @@ if sys.platform == 'win32': class NXDOMAIN(dns.exception.DNSException): """The DNS query name does not exist.""" - supp_kwargs = set(['qnames', 'responses']) + supp_kwargs = {'qnames', 'responses'} fmt = None # we have our own __str__ implementation def _check_kwargs(self, qnames, responses=None): @@ -71,7 +71,7 @@ class NXDOMAIN(dns.exception.DNSException): else: msg = 'The DNS query name does not exist' qnames = ', '.join(map(str, qnames)) - return "%s: %s" % (msg, qnames) + return "{}: {}".format(msg, qnames) def canonical_name(self): if not 'qnames' in self.kwargs: @@ -140,7 +140,7 @@ class NoAnswer(dns.exception.DNSException): """The DNS response does not contain an answer to the question.""" fmt = 'The DNS response does not contain an answer ' + \ 'to the question: {query}' - supp_kwargs = set(['response']) + supp_kwargs = {'response'} def _fmt_kwargs(self, **kwargs): return super(NoAnswer, self)._fmt_kwargs( @@ -158,12 +158,12 @@ class NoNameservers(dns.exception.DNSException): msg = "All nameservers failed to answer the query." fmt = "%s {query}: {errors}" % msg[:-1] - supp_kwargs = set(['request', 'errors']) + supp_kwargs = {'request', 'errors'} def _fmt_kwargs(self, **kwargs): srv_msgs = [] for err in kwargs['errors']: - srv_msgs.append('Server %s %s port %s answered %s' % (err[0], + srv_msgs.append('Server {} {} port {} answered {}'.format(err[0], 'TCP' if err[1] else 'UDP', err[2], err[3])) return super(NoNameservers, self)._fmt_kwargs( query=kwargs['request'].question, errors='; '.join(srv_msgs)) diff --git a/dns/zone.py b/dns/zone.py index 2c7ea69..65a1ea1 100644 --- a/dns/zone.py +++ b/dns/zone.py @@ -715,7 +715,7 @@ class _MasterReader(object): # helpful filename:line info. (ty, va) = sys.exc_info()[:2] raise dns.exception.SyntaxError( - "caught exception %s: %s" % (str(ty), str(va))) + "caught exception {}: {}".format(str(ty), str(va))) if not self.default_ttl_known and isinstance(rd, dns.rdtypes.ANY.SOA.SOA): # The pre-RFC2308 and pre-BIND9 behavior inherits the zone default diff --git a/examples/zonediff.py b/examples/zonediff.py index bfe4ee3..b65f828 100755 --- a/examples/zonediff.py +++ b/examples/zonediff.py @@ -101,7 +101,7 @@ def format_changes_plain(oldf, # type: str Given 2 filenames and a list of changes from diff_zones, produce diff-like output. If ignore_ttl is True, TTL-only changes are not displayed""" - ret = "--- %s\n+++ %s\n" % (oldf, newf) + ret = "--- {}\n+++ {}\n".format(oldf, newf) for name, old, new in changes: ret += "@ %s\n" % name if not old: @@ -244,34 +244,34 @@ The differences shown will be logical differences, not textual differences. else: if len(args) == 3: filename, oldr, newr = args - oldn = "%s:%s" % (oldr, filename) - newn = "%s:%s" % (newr, filename) + oldn = "{}:{}".format(oldr, filename) + newn = "{}:{}".format(newr, filename) else: filename, oldr = args newr = None - oldn = "%s:%s" % (oldr, filename) + oldn = "{}:{}".format(oldr, filename) newn = filename old, new = None, None oldz, newz = None, None if opts.use_bzr: old = _open(["bzr", "cat", "-r" + oldr, filename], - "Unable to retrieve revision %s of %s" % (oldr, filename)) + "Unable to retrieve revision {} of {}".format(oldr, filename)) if newr is not None: new = _open(["bzr", "cat", "-r" + newr, filename], - "Unable to retrieve revision %s of %s" % (newr, filename)) + "Unable to retrieve revision {} of {}".format(newr, filename)) elif opts.use_git: old = _open(["git", "show", oldn], - "Unable to retrieve revision %s of %s" % (oldr, filename)) + "Unable to retrieve revision {} of {}".format(oldr, filename)) if newr is not None: new = _open(["git", "show", newn], - "Unable to retrieve revision %s of %s" % (newr, filename)) + "Unable to retrieve revision {} of {}".format(newr, filename)) elif opts.use_rcs: old = _open(["co", "-q", "-p", "-r" + oldr, filename], - "Unable to retrieve revision %s of %s" % (oldr, filename)) + "Unable to retrieve revision {} of {}".format(oldr, filename)) if newr is not None: new = _open(["co", "-q", "-p", "-r" + newr, filename], - "Unable to retrieve revision %s of %s" % (newr, filename)) + "Unable to retrieve revision {} of {}".format(newr, filename)) if not opts.use_vc: old = _open(oldn, "Unable to open %s" % oldn) if not opts.use_vc or newr is None: diff --git a/setup.py b/setup.py index 9a83a87..dbcedd1 100755 --- a/setup.py +++ b/setup.py @@ -49,7 +49,7 @@ direct manipulation of DNS zones, messages, names, and records.""", 'packages' : ['dns', 'dns.rdtypes', 'dns.rdtypes.IN', 'dns.rdtypes.ANY'], 'package_data' : {'dns': ['py.typed']}, 'download_url' : \ - 'http://www.dnspython.org/kits/%s/dnspython-%s.tar.gz' % (version, version), + 'http://www.dnspython.org/kits/{}/dnspython-{}.tar.gz'.format(version, version), 'classifiers' : [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index a684ca1..6c4d016 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -20,7 +20,7 @@ from dns.exception import DNSException class FormatedError(DNSException): fmt = "Custom format: {parameter}" - supp_kwargs = set(['parameter']) + supp_kwargs = {'parameter'} class ExceptionTestCase(unittest.TestCase): diff --git a/tests/test_rdtypeanydnskey.py b/tests/test_rdtypeanydnskey.py index 4fea7af..48f8dd1 100644 --- a/tests/test_rdtypeanydnskey.py +++ b/tests/test_rdtypeanydnskey.py @@ -29,27 +29,27 @@ class RdtypeAnyDnskeyTestCase(unittest.TestCase): good_f = 0 from_flags = dns.rdtypes.ANY.DNSKEY.flags_to_text_set(good_f) self.failUnless(from_flags == good_s, - '"%s" != "%s"' % (from_flags, good_s)) + '"{}" != "{}"'.format(from_flags, good_s)) from_set = dns.rdtypes.ANY.DNSKEY.flags_from_text_set(good_s) self.failUnless(from_set == good_f, - '"0x%x" != "0x%x"' % (from_set, good_f)) + '"0x{:x}" != "0x{:x}"'.format(from_set, good_f)) def testFlagsAll(self): # type: () -> None '''Test that all defined flags are recognized.''' - good_s = set(['SEP', 'REVOKE', 'ZONE']) + good_s = {'SEP', 'REVOKE', 'ZONE'} good_f = 0x181 from_flags = dns.rdtypes.ANY.DNSKEY.flags_to_text_set(good_f) self.failUnless(from_flags == good_s, - '"%s" != "%s"' % (from_flags, good_s)) + '"{}" != "{}"'.format(from_flags, good_s)) from_text = dns.rdtypes.ANY.DNSKEY.flags_from_text_set(good_s) self.failUnless(from_text == good_f, - '"0x%x" != "0x%x"' % (from_text, good_f)) + '"0x{:x}" != "0x{:x}"'.format(from_text, good_f)) def testFlagsUnknownToText(self): # type: () -> None '''Test that undefined flags are returned in hexadecimal notation.''' - unk_s = set(['0x8000']) + unk_s = {'0x8000'} flags_s = dns.rdtypes.ANY.DNSKEY.flags_to_text_set(0x8000) - self.failUnless(flags_s == unk_s, '"%s" != "%s"' % (flags_s, unk_s)) + self.failUnless(flags_s == unk_s, '"{}" != "{}"'.format(flags_s, unk_s)) def testFlagsUnknownToFlags(self): # type: () -> None '''Test that conversion from undefined mnemonic raises error.''' @@ -60,9 +60,9 @@ class RdtypeAnyDnskeyTestCase(unittest.TestCase): def testFlagsRRToText(self): # type: () -> None '''Test that RR method returns correct flags.''' rr = dns.rrset.from_text('foo', 300, 'IN', 'DNSKEY', '257 3 8 KEY=')[0] - rr_s = set(['ZONE', 'SEP']) + rr_s = {'ZONE', 'SEP'} flags_s = rr.flags_to_text_set() - self.failUnless(flags_s == rr_s, '"%s" != "%s"' % (flags_s, rr_s)) + self.failUnless(flags_s == rr_s, '"{}" != "{}"'.format(flags_s, rr_s)) if __name__ == '__main__': diff --git a/tests/test_rdtypeanyloc.py b/tests/test_rdtypeanyloc.py index e6936c3..7f5990e 100644 --- a/tests/test_rdtypeanyloc.py +++ b/tests/test_rdtypeanyloc.py @@ -28,7 +28,7 @@ class RdtypeAnyLocTestCase(unittest.TestCase): r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', '49 11 42.400 N 16 36 29.600 E 227.64m ' '1.00m 10000.00m 10.00m') - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + self.failUnless(r1 == r2, '"{}" != "{}"'.format(r1, r2)) def testEqual2(self): '''Test default values for size, horizontal and vertical precision.''' @@ -39,7 +39,7 @@ class RdtypeAnyLocTestCase(unittest.TestCase): (16, 36, 29, 600, 1), 22764.0, # centimeters 100.0, 1000000.00, 1000.0) # centimeters - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + self.failUnless(r1 == r2, '"{}" != "{}"'.format(r1, r2)) def testEqual3(self): '''Test size, horizontal and vertical precision parsers: 100 cm == 1 m. @@ -51,7 +51,7 @@ class RdtypeAnyLocTestCase(unittest.TestCase): r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', '49 11 42.400 N 16 36 29.600 E 227.64m ' '2.00m 10.00m 2.00m')[0] - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + self.failUnless(r1 == r2, '"{}" != "{}"'.format(r1, r2)) def testEqual4(self): '''Test size, horizontal and vertical precision parsers without unit. @@ -64,7 +64,7 @@ class RdtypeAnyLocTestCase(unittest.TestCase): r2 = dns.rrset.from_text('FOO', 600, 'in', 'loc', '49 11 42.400 N 16 36 29.600 E 227.64 ' '2 10 2')[0] # meters without explicit unit - self.failUnless(r1 == r2, '"%s" != "%s"' % (r1, r2)) + self.failUnless(r1 == r2, '"{}" != "{}"'.format(r1, r2)) if __name__ == '__main__': unittest.main() -- cgit v1.2.1