]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Convert opcode, rcode, dnssec constants to enums.
authorBrian Wellington <bwelling@xbill.org>
Mon, 18 May 2020 20:40:08 +0000 (13:40 -0700)
committerBrian Wellington <bwelling@xbill.org>
Mon, 18 May 2020 20:40:08 +0000 (13:40 -0700)
dns/dnssec.py
dns/opcode.py
dns/rcode.py

index 1f5cf0edd3fda3ef831acbd10484925ffacd7e9d..c0050d2c5cff525ecc329d51adda0986ed1efc8f 100644 (file)
@@ -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):
index dd7b61b1d7e3a3ca27b37d8526fbc387a9f3d1fe..17f558977ea4ca5d560f51d6977ca69b01491e4c 100644 (file)
 
 """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):
index e7c1f282018f376d64219cce031c799b2cd55d3a..05a8f54a9887a7798ea5e1bb393308de9ce150ca 100644 (file)
 
 """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)