From: Brian Wellington Date: Mon, 18 May 2020 20:40:08 +0000 (-0700) Subject: Convert opcode, rcode, dnssec constants to enums. X-Git-Tag: v2.0.0rc1~198^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=934130f670fe2d520bb8bb0f9a0c4b4cff2664ed;p=thirdparty%2Fdnspython.git Convert opcode, rcode, dnssec constants to enums. --- diff --git a/dns/dnssec.py b/dns/dnssec.py index 1f5cf0ed..c0050d2c 100644 --- a/dns/dnssec.py +++ b/dns/dnssec.py @@ -41,66 +41,26 @@ class ValidationFailure(dns.exception.DNSException): """The DNSSEC signature is invalid.""" -#: RSAMD5 -RSAMD5 = 1 -#: DH -DH = 2 -#: DSA -DSA = 3 -#: ECC -ECC = 4 -#: RSASHA1 -RSASHA1 = 5 -#: DSANSEC3SHA1 -DSANSEC3SHA1 = 6 -#: RSASHA1NSEC3SHA1 -RSASHA1NSEC3SHA1 = 7 -#: RSASHA256 -RSASHA256 = 8 -#: RSASHA512 -RSASHA512 = 10 -#: ECC-GOST -ECCGOST = 12 -#: ECDSAP256SHA256 -ECDSAP256SHA256 = 13 -#: ECDSAP384SHA384 -ECDSAP384SHA384 = 14 -#: ED25519 -ED25519 = 15 -#: ED448 -ED448 = 16 -#: INDIRECT -INDIRECT = 252 -#: PRIVATEDNS -PRIVATEDNS = 253 -#: PRIVATEOID -PRIVATEOID = 254 - -_algorithm_by_text = { - 'RSAMD5': RSAMD5, - 'DH': DH, - 'DSA': DSA, - 'ECC': ECC, - 'RSASHA1': RSASHA1, - 'DSANSEC3SHA1': DSANSEC3SHA1, - 'RSASHA1NSEC3SHA1': RSASHA1NSEC3SHA1, - 'RSASHA256': RSASHA256, - 'RSASHA512': RSASHA512, - 'ECCGOST': ECCGOST, - 'ECDSAP256SHA256': ECDSAP256SHA256, - 'ECDSAP384SHA384': ECDSAP384SHA384, - 'ED25519': ED25519, - 'ED448': ED448, - 'INDIRECT': INDIRECT, - 'PRIVATEDNS': PRIVATEDNS, - 'PRIVATEOID': PRIVATEOID, -} - -# 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. - -_algorithm_by_value = {y: x for x, y in _algorithm_by_text.items()} +class Algorithm(enum.IntEnum): + RSAMD5 = 1 + DH = 2 + DSA = 3 + ECC = 4 + RSASHA1 = 5 + DSANSEC3SHA1 = 6 + RSASHA1NSEC3SHA1 = 7 + RSASHA256 = 8 + RSASHA512 = 10 + ECCGOST = 12 + ECDSAP256SHA256 = 13 + ECDSAP384SHA384 = 14 + ED25519 = 15 + ED448 = 16 + INDIRECT = 252 + PRIVATEDNS = 253 + PRIVATEOID = 254 + +globals().update(Algorithm.__members__) def algorithm_from_text(text): @@ -111,10 +71,10 @@ def algorithm_from_text(text): Returns an ``int``. """ - value = _algorithm_by_text.get(text.upper()) - if value is None: - value = int(text) - return value + try: + return Algorithm[text.upper()] + except KeyError: + return int(text) def algorithm_to_text(value): @@ -125,10 +85,10 @@ def algorithm_to_text(value): Returns a ``str``, the name of a DNSSEC algorithm. """ - text = _algorithm_by_value.get(value) - if text is None: - text = str(value) - return text + try: + return Algorithm(value).name + except ValueError: + return str(value) def _to_rdata(record, origin): diff --git a/dns/opcode.py b/dns/opcode.py index dd7b61b1..17f55897 100644 --- a/dns/opcode.py +++ b/dns/opcode.py @@ -17,32 +17,23 @@ """DNS Opcodes.""" -import dns.exception - -#: Query -QUERY = 0 -#: Inverse Query (historical) -IQUERY = 1 -#: Server Status (unspecified and unimplemented anywhere) -STATUS = 2 -#: Notify -NOTIFY = 4 -#: Dynamic Update -UPDATE = 5 +import enum -_by_text = { - 'QUERY': QUERY, - 'IQUERY': IQUERY, - 'STATUS': STATUS, - 'NOTIFY': NOTIFY, - 'UPDATE': UPDATE -} +import dns.exception -# 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. +class Opcode(enum.IntEnum): + #: Query + QUERY = 0 + #: Inverse Query (historical) + IQUERY = 1 + #: Server Status (unspecified and unimplemented anywhere) + STATUS = 2 + #: Notify + NOTIFY = 4 + #: Dynamic Update + UPDATE = 5 -_by_value = {y: x for x, y in _by_text.items()} +globals().update(Opcode.__members__) class UnknownOpcode(dns.exception.DNSException): @@ -62,11 +53,14 @@ def from_text(text): if text.isdigit(): value = int(text) if value >= 0 and value <= 15: - return value - value = _by_text.get(text.upper()) - if value is None: + try: + return Opcode(value) + except ValueError: + return value + try: + return Opcode[text.upper()] + except: raise UnknownOpcode - return value def from_flags(flags): @@ -102,10 +96,10 @@ def to_text(value): Returns a ``str``. """ - text = _by_value.get(value) - if text is None: - text = str(value) - return text + try: + return Opcode(value).name + except ValueError: + return str(value) def is_update(flags): diff --git a/dns/rcode.py b/dns/rcode.py index e7c1f282..05a8f54a 100644 --- a/dns/rcode.py +++ b/dns/rcode.py @@ -17,54 +17,37 @@ """DNS Result Codes.""" -import dns.exception +import enum -#: No error -NOERROR = 0 -#: Format error -FORMERR = 1 -#: Server failure -SERVFAIL = 2 -#: Name does not exist ("Name Error" in RFC 1025 terminology). -NXDOMAIN = 3 -#: Not implemented -NOTIMP = 4 -#: Refused -REFUSED = 5 -#: Name exists. -YXDOMAIN = 6 -#: RRset exists. -YXRRSET = 7 -#: RRset does not exist. -NXRRSET = 8 -#: Not authoritative. -NOTAUTH = 9 -#: Name not in zone. -NOTZONE = 10 -#: Bad EDNS version. -BADVERS = 16 - -_by_text = { - 'NOERROR': NOERROR, - 'FORMERR': FORMERR, - 'SERVFAIL': SERVFAIL, - 'NXDOMAIN': NXDOMAIN, - 'NOTIMP': NOTIMP, - 'REFUSED': REFUSED, - 'YXDOMAIN': YXDOMAIN, - 'YXRRSET': YXRRSET, - 'NXRRSET': NXRRSET, - 'NOTAUTH': NOTAUTH, - 'NOTZONE': NOTZONE, - 'BADVERS': BADVERS -} - -# 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 a true inverse. - -_by_value = {y: x for x, y in _by_text.items()} +import dns.exception +class Rcode(enum.IntEnum): + #: No error + NOERROR = 0 + #: Format error + FORMERR = 1 + #: Server failure + SERVFAIL = 2 + #: Name does not exist ("Name Error" in RFC 1025 terminology). + NXDOMAIN = 3 + #: Not implemented + NOTIMP = 4 + #: Refused + REFUSED = 5 + #: Name exists. + YXDOMAIN = 6 + #: RRset exists. + YXRRSET = 7 + #: RRset does not exist. + NXRRSET = 8 + #: Not authoritative. + NOTAUTH = 9 + #: Name not in zone. + NOTZONE = 10 + #: Bad EDNS version. + BADVERS = 16 + +globals().update(Rcode.__members__) class UnknownRcode(dns.exception.DNSException): """A DNS rcode is unknown.""" @@ -83,11 +66,14 @@ def from_text(text): if text.isdigit(): v = int(text) if v >= 0 and v <= 4095: - return v - v = _by_text.get(text.upper()) - if v is None: + try: + return Rcode(v) + except ValueError: + return v + try: + return Rcode[text.upper()] + except KeyError: raise UnknownRcode - return v def from_flags(flags, ednsflags): @@ -137,7 +123,7 @@ def to_text(value): if value < 0 or value > 4095: raise ValueError('rcode must be >= 0 and <= 4095') - text = _by_value.get(value) - if text is None: - text = str(value) - return text + try: + return Rcode(value).name + except ValueError: + return str(value)