From 044c8afd92070648f3e76ed054be69d264f114c2 Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Tue, 19 May 2020 13:18:05 -0700 Subject: Enum refactoring. Consolidate the common methods related to enum classes. --- dns/opcode.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'dns/opcode.py') diff --git a/dns/opcode.py b/dns/opcode.py index d81d255..509916e 100644 --- a/dns/opcode.py +++ b/dns/opcode.py @@ -17,11 +17,10 @@ """DNS Opcodes.""" -import enum - +import dns.enum import dns.exception -class Opcode(enum.IntEnum): +class Opcode(dns.enum.IntEnum): #: Query QUERY = 0 #: Inverse Query (historical) @@ -33,6 +32,14 @@ class Opcode(enum.IntEnum): #: Dynamic Update UPDATE = 5 + @classmethod + def _maximum(cls): + return 15 + + @classmethod + def _unknown_exception_class(cls): + return UnknownOpcode + globals().update(Opcode.__members__) @@ -50,17 +57,7 @@ def from_text(text): Returns an ``int``. """ - if text.isdigit(): - value = int(text) - if value >= 0 and value <= 15: - try: - return Opcode(value) - except ValueError: - return value - try: - return Opcode[text.upper()] - except KeyError: - raise UnknownOpcode + return Opcode.from_text(text) def from_flags(flags): @@ -96,10 +93,7 @@ def to_text(value): Returns a ``str``. """ - try: - return Opcode(value).name - except ValueError: - return str(value) + return Opcode.to_text(value) def is_update(flags): -- cgit v1.2.1