diff --git a/pcapkit/const/arp/hardware.py b/pcapkit/const/arp/hardware.py index 4f8b4450aa..638c3f8150 100644 --- a/pcapkit/const/arp/hardware.py +++ b/pcapkit/const/arp/hardware.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Hardware'] -class Hardware(IntEnum): +class Hardware(EnumRegistry, IntEnum): """[Hardware] Hardware Types [:rfc:`826`][:rfc:`5494`]""" #: Reserved [:rfc:`5494`] @@ -146,72 +148,6 @@ class Hardware(IntEnum): #: Reserved [:rfc:`5494`] Reserved_65535 = 65535 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Hardware': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Hardware(key) - except ValueError: - if default == -1: - raise - return Hardware(default) - try: - return Hardware[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Hardware(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Hardware': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Hardware': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Hardware': """Lookup function used when value is not found. diff --git a/pcapkit/const/arp/operation.py b/pcapkit/const/arp/operation.py index 9200c86a78..cf85c52bcd 100644 --- a/pcapkit/const/arp/operation.py +++ b/pcapkit/const/arp/operation.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Operation'] -class Operation(IntEnum): +class Operation(EnumRegistry, IntEnum): """[Operation] Operation Codes [:rfc:`826`][:rfc:`5494`]""" #: Reserved [:rfc:`5494`] @@ -99,72 +101,6 @@ class Operation(IntEnum): #: Reserved [:rfc:`5494`] Reserved_65535 = 65535 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Operation': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Operation(key) - except ValueError: - if default == -1: - raise - return Operation(default) - try: - return Operation[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Operation(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Operation': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Operation': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Operation': """Lookup function used when value is not found. diff --git a/pcapkit/const/esp/cipher.py b/pcapkit/const/esp/cipher.py index 49d0b8c940..e9071f357c 100644 --- a/pcapkit/const/esp/cipher.py +++ b/pcapkit/const/esp/cipher.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Cipher'] -class Cipher(IntEnum): +class Cipher(EnumRegistry, IntEnum): """[Cipher] Transform Type 1 - Encryption Algorithm Transform IDs""" #: Reserved [:rfc:`7296`] @@ -216,72 +218,6 @@ class Cipher(IntEnum): #: Alias of :attr:`Cipher.ENCR_MAGMA_MGM_MAC_KTREE`. MAGMA_MGM_MAC_KTREE = 35 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Cipher': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Cipher(key) - except ValueError: - if default == -1: - raise - return Cipher(default) - try: - return Cipher[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Cipher(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Cipher': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Cipher': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Cipher': """Lookup function used when value is not found. diff --git a/pcapkit/const/esp/integrity.py b/pcapkit/const/esp/integrity.py index 52abb09c66..8992c95b2e 100644 --- a/pcapkit/const/esp/integrity.py +++ b/pcapkit/const/esp/integrity.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Integrity'] -class Integrity(IntEnum): +class Integrity(EnumRegistry, IntEnum): """[Integrity] Transform Type 3 - Integrity Algorithm Transform IDs""" #: NONE [:rfc:`7296`] @@ -105,72 +107,6 @@ class Integrity(IntEnum): #: Alias of :attr:`Integrity.AUTH_HMAC_SHA2_512_256`. HMAC_SHA2_512_256 = 14 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Integrity': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Integrity(key) - except ValueError: - if default == -1: - raise - return Integrity(default) - try: - return Integrity[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Integrity(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Integrity': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Integrity': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Integrity': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/certificate.py b/pcapkit/const/hip/certificate.py index c7f09e75e0..1225b4dbda 100644 --- a/pcapkit/const/hip/certificate.py +++ b/pcapkit/const/hip/certificate.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Certificate'] -class Certificate(IntEnum): +class Certificate(EnumRegistry, IntEnum): """[Certificate] HIP Certificate Types""" #: Reserved [:rfc:`8002`] @@ -45,72 +47,6 @@ class Certificate(IntEnum): #: Obsoleted [:rfc:`8002`] Obsoleted_8 = 8 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Certificate': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Certificate(key) - except ValueError: - if default == -1: - raise - return Certificate(default) - try: - return Certificate[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Certificate(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Certificate': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Certificate': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Certificate': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/cipher.py b/pcapkit/const/hip/cipher.py index 8817e90593..0159b834fc 100644 --- a/pcapkit/const/hip/cipher.py +++ b/pcapkit/const/hip/cipher.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Cipher'] -class Cipher(IntEnum): +class Cipher(EnumRegistry, IntEnum): """[Cipher] Cipher IDs""" #: RESERVED [:rfc:`7401`] @@ -33,72 +35,6 @@ class Cipher(IntEnum): #: AES-256-CBC [:rfc:`7401`] AES_256_CBC = 4 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Cipher': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Cipher(key) - except ValueError: - if default == -1: - raise - return Cipher(default) - try: - return Cipher[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Cipher(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Cipher': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Cipher': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Cipher': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/di.py b/pcapkit/const/hip/di.py index d4c87f3198..b3bb92e2ed 100644 --- a/pcapkit/const/hip/di.py +++ b/pcapkit/const/hip/di.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['DITypes'] -class DITypes(IntEnum): +class DITypes(EnumRegistry, IntEnum): """[DITypes] DI-Types""" #: none included [:rfc:`7401`] @@ -27,72 +29,6 @@ class DITypes(IntEnum): #: NAI [:rfc:`7401`] NAI = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'DITypes': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return DITypes(key) - except ValueError: - if default == -1: - raise - return DITypes(default) - try: - return DITypes[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return DITypes(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'DITypes': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'DITypes': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'DITypes': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/ecdsa_curve.py b/pcapkit/const/hip/ecdsa_curve.py index fb248f8acf..5e79a051da 100644 --- a/pcapkit/const/hip/ecdsa_curve.py +++ b/pcapkit/const/hip/ecdsa_curve.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['ECDSACurve'] -class ECDSACurve(IntEnum): +class ECDSACurve(EnumRegistry, IntEnum): """[ECDSACurve] ECDSA Curve Label""" #: RESERVED [:rfc:`7401`] @@ -27,72 +29,6 @@ class ECDSACurve(IntEnum): #: NIST P-384 [:rfc:`7401`] NIST_P_384 = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ECDSACurve': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ECDSACurve(key) - except ValueError: - if default == -1: - raise - return ECDSACurve(default) - try: - return ECDSACurve[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ECDSACurve(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ECDSACurve': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ECDSACurve': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ECDSACurve': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/ecdsa_low_curve.py b/pcapkit/const/hip/ecdsa_low_curve.py index 47f90053f0..a132829ade 100644 --- a/pcapkit/const/hip/ecdsa_low_curve.py +++ b/pcapkit/const/hip/ecdsa_low_curve.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['ECDSALowCurve'] -class ECDSALowCurve(IntEnum): +class ECDSALowCurve(EnumRegistry, IntEnum): """[ECDSALowCurve] ECDSA_LOW Curve Label""" #: RESERVED [:rfc:`7401`] @@ -24,72 +26,6 @@ class ECDSALowCurve(IntEnum): #: SECP160R1 [:rfc:`7401`] SECP160R1 = 1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ECDSALowCurve': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ECDSALowCurve(key) - except ValueError: - if default == -1: - raise - return ECDSALowCurve(default) - try: - return ECDSALowCurve[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ECDSALowCurve(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ECDSALowCurve': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ECDSALowCurve': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ECDSALowCurve': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/eddsa_curve.py b/pcapkit/const/hip/eddsa_curve.py index 3dbd2d76a2..cab38daacd 100644 --- a/pcapkit/const/hip/eddsa_curve.py +++ b/pcapkit/const/hip/eddsa_curve.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['EdDSACurve'] -class EdDSACurve(IntEnum): +class EdDSACurve(EnumRegistry, IntEnum): """[EdDSACurve] EdDSA Curve Label""" #: RESERVED [:rfc:`9374`] @@ -33,72 +35,6 @@ class EdDSACurve(IntEnum): #: EdDSA448ph [:rfc:`8032`] EdDSA448ph = 4 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'EdDSACurve': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return EdDSACurve(key) - except ValueError: - if default == -1: - raise - return EdDSACurve(default) - try: - return EdDSACurve[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return EdDSACurve(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'EdDSACurve': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'EdDSACurve': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'EdDSACurve': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/esp_transform_suite.py b/pcapkit/const/hip/esp_transform_suite.py index 3bc26e9adf..a7f4077bdb 100644 --- a/pcapkit/const/hip/esp_transform_suite.py +++ b/pcapkit/const/hip/esp_transform_suite.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['ESPTransformSuite'] -class ESPTransformSuite(IntEnum): +class ESPTransformSuite(EnumRegistry, IntEnum): """[ESPTransformSuite] ESP Transform Suite IDs""" #: RESERVED [:rfc:`7402`] @@ -66,72 +68,6 @@ class ESPTransformSuite(IntEnum): #: AES-GMAC [:rfc:`4543`] AES_GMAC = 15 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ESPTransformSuite': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ESPTransformSuite(key) - except ValueError: - if default == -1: - raise - return ESPTransformSuite(default) - try: - return ESPTransformSuite[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ESPTransformSuite(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ESPTransformSuite': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ESPTransformSuite': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ESPTransformSuite': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/group.py b/pcapkit/const/hip/group.py index 1c7a9d5c3f..ea350ebde4 100644 --- a/pcapkit/const/hip/group.py +++ b/pcapkit/const/hip/group.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Group'] -class Group(IntEnum): +class Group(EnumRegistry, IntEnum): """[Group] Group IDs""" #: Reserved [:rfc:`7401`] @@ -54,72 +56,6 @@ class Group(IntEnum): #: 2048-bit MODP group [:rfc:`7401`] Group_2048_bit_MODP_group = 11 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Group': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Group(key) - except ValueError: - if default == -1: - raise - return Group(default) - try: - return Group[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Group(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Group': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Group': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Group': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/hi_algorithm.py b/pcapkit/const/hip/hi_algorithm.py index 822d023abf..d49bcd7748 100644 --- a/pcapkit/const/hip/hi_algorithm.py +++ b/pcapkit/const/hip/hi_algorithm.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['HIAlgorithm'] -class HIAlgorithm(IntEnum): +class HIAlgorithm(EnumRegistry, IntEnum): """[HIAlgorithm] HI Algorithm""" #: RESERVED [:rfc:`7401`] @@ -51,72 +53,6 @@ class HIAlgorithm(IntEnum): #: EdDSA [:rfc:`8032`] EdDSA = 13 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HIAlgorithm': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HIAlgorithm(key) - except ValueError: - if default == -1: - raise - return HIAlgorithm(default) - try: - return HIAlgorithm[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HIAlgorithm(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HIAlgorithm': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HIAlgorithm': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HIAlgorithm': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/hit_suite.py b/pcapkit/const/hip/hit_suite.py index 2fff029676..c9c5960453 100644 --- a/pcapkit/const/hip/hit_suite.py +++ b/pcapkit/const/hip/hit_suite.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['HITSuite'] -class HITSuite(IntEnum): +class HITSuite(EnumRegistry, IntEnum): """[HITSuite] HIT Suite ID""" #: RESERVED [:rfc:`7401`] @@ -36,72 +38,6 @@ class HITSuite(IntEnum): #: EdDSA/cSHAKE128 [:rfc:`9374`] EdDSA_cSHAKE128 = 5 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HITSuite': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HITSuite(key) - except ValueError: - if default == -1: - raise - return HITSuite(default) - try: - return HITSuite[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HITSuite(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HITSuite': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HITSuite': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HITSuite': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/nat_traversal.py b/pcapkit/const/hip/nat_traversal.py index 8f84e9905b..6fb3289aa6 100644 --- a/pcapkit/const/hip/nat_traversal.py +++ b/pcapkit/const/hip/nat_traversal.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['NATTraversal'] -class NATTraversal(IntEnum): +class NATTraversal(EnumRegistry, IntEnum): """[NATTraversal] HIP NAT Traversal Modes""" #: Reserved [:rfc:`5770`] @@ -30,72 +32,6 @@ class NATTraversal(IntEnum): #: ICE-HIP-UDP [:rfc:`9028`] ICE_HIP_UDP = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'NATTraversal': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return NATTraversal(key) - except ValueError: - if default == -1: - raise - return NATTraversal(default) - try: - return NATTraversal[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return NATTraversal(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'NATTraversal': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'NATTraversal': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'NATTraversal': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/notify_message.py b/pcapkit/const/hip/notify_message.py index bae8602132..eafba59527 100644 --- a/pcapkit/const/hip/notify_message.py +++ b/pcapkit/const/hip/notify_message.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['NotifyMessage'] -class NotifyMessage(IntEnum): +class NotifyMessage(EnumRegistry, IntEnum): """[NotifyMessage] Notify Message Types""" #: Reserved [:rfc:`7401`] @@ -132,72 +134,6 @@ class NotifyMessage(IntEnum): #: NAT_KEEPALIVE [:rfc:`9028`] NAT_KEEPALIVE = 16385 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'NotifyMessage': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return NotifyMessage(key) - except ValueError: - if default == -1: - raise - return NotifyMessage(default) - try: - return NotifyMessage[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return NotifyMessage(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'NotifyMessage': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'NotifyMessage': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'NotifyMessage': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/packet.py b/pcapkit/const/hip/packet.py index b0a544cb33..469cfa9dd6 100644 --- a/pcapkit/const/hip/packet.py +++ b/pcapkit/const/hip/packet.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Packet'] -class Packet(IntEnum): +class Packet(EnumRegistry, IntEnum): """[Packet] HIP Packet Types""" #: Reserved [:rfc:`7401`] @@ -51,72 +53,6 @@ class Packet(IntEnum): #: HIP_DATA [:rfc:`6078`] HIP_DATA = 32 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Packet': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Packet(key) - except ValueError: - if default == -1: - raise - return Packet(default) - try: - return Packet[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Packet(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Packet': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Packet': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/parameter.py b/pcapkit/const/hip/parameter.py index 96f966ef23..7dbf8c26c2 100644 --- a/pcapkit/const/hip/parameter.py +++ b/pcapkit/const/hip/parameter.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Parameter'] -class Parameter(IntEnum): +class Parameter(EnumRegistry, IntEnum): """[Parameter] HIP Parameter Types""" #: ESP_INFO [:rfc:`7402`] (Length: 12) @@ -204,72 +206,6 @@ class Parameter(IntEnum): #: RELAY_HMAC [:rfc:`5770`] RELAY_HMAC = 65520 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Parameter': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Parameter(key) - except ValueError: - if default == -1: - raise - return Parameter(default) - try: - return Parameter[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Parameter(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Parameter': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Parameter': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Parameter': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/registration.py b/pcapkit/const/hip/registration.py index b6a17cfa35..d9c3f3695d 100644 --- a/pcapkit/const/hip/registration.py +++ b/pcapkit/const/hip/registration.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Registration'] -class Registration(IntEnum): +class Registration(EnumRegistry, IntEnum): """[Registration] Registration Types""" #: Unassigned @@ -33,72 +35,6 @@ class Registration(IntEnum): #: CANDIDATE_DISCOVERY [:rfc:`9028`] CANDIDATE_DISCOVERY = 4 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Registration': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Registration(key) - except ValueError: - if default == -1: - raise - return Registration(default) - try: - return Registration[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Registration(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Registration': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Registration': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Registration': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/registration_failure.py b/pcapkit/const/hip/registration_failure.py index dd0b402955..d5e148c814 100644 --- a/pcapkit/const/hip/registration_failure.py +++ b/pcapkit/const/hip/registration_failure.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['RegistrationFailure'] -class RegistrationFailure(IntEnum): +class RegistrationFailure(EnumRegistry, IntEnum): """[RegistrationFailure] Registration Failure Types""" #: Registration requires additional credentials [:rfc:`8003`] @@ -49,72 +51,6 @@ class RegistrationFailure(IntEnum): #: [:rfc:`9028`] Simultaneous_Rendezvous_and_Control_Relay_Service_usage_prohibited = 9 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'RegistrationFailure': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return RegistrationFailure(key) - except ValueError: - if default == -1: - raise - return RegistrationFailure(default) - try: - return RegistrationFailure[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return RegistrationFailure(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'RegistrationFailure': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'RegistrationFailure': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'RegistrationFailure': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/suite.py b/pcapkit/const/hip/suite.py index d1a3c0711b..33463de8d9 100644 --- a/pcapkit/const/hip/suite.py +++ b/pcapkit/const/hip/suite.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Suite'] -class Suite(IntEnum): +class Suite(EnumRegistry, IntEnum): """[Suite] Suite IDs""" #: Reserved [:rfc:`5201`] @@ -39,72 +41,6 @@ class Suite(IntEnum): #: NULL-ENCRYPT with HMAC-MD5 [:rfc:`5201`] NULL_ENCRYPT_with_HMAC_MD5 = 6 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Suite': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Suite(key) - except ValueError: - if default == -1: - raise - return Suite(default) - try: - return Suite[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Suite(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Suite': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Suite': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Suite': """Lookup function used when value is not found. diff --git a/pcapkit/const/hip/transport.py b/pcapkit/const/hip/transport.py index 4465d235f6..6ced8d0327 100644 --- a/pcapkit/const/hip/transport.py +++ b/pcapkit/const/hip/transport.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Transport'] -class Transport(IntEnum): +class Transport(EnumRegistry, IntEnum): """[Transport] HIP Transport Modes""" #: RESERVED [:rfc:`6261`] @@ -30,72 +32,6 @@ class Transport(IntEnum): #: ESP-TCP [:rfc:`6261`] ESP_TCP = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Transport': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Transport(key) - except ValueError: - if default == -1: - raise - return Transport(default) - try: - return Transport[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Transport(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Transport': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Transport': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Transport': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/error_code.py b/pcapkit/const/http/error_code.py index fcce61c00e..368d1ff2e7 100644 --- a/pcapkit/const/http/error_code.py +++ b/pcapkit/const/http/error_code.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ErrorCode'] -class ErrorCode(IntEnum): +class ErrorCode(EnumRegistry, IntEnum): """[ErrorCode] HTTP/2 Error Code""" #: NO_ERROR, Graceful shutdown [:rfc:`9113#section-7`] @@ -62,72 +64,6 @@ class ErrorCode(IntEnum): #: HTTP_1_1_REQUIRED, Use HTTP/1.1 for the request [:rfc:`9113#section-7`] HTTP_1_1_REQUIRED = 0x0000000D - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ErrorCode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ErrorCode(key) - except ValueError: - if default == -1: - raise - return ErrorCode(default) - try: - return ErrorCode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ErrorCode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ErrorCode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ErrorCode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ErrorCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/frame.py b/pcapkit/const/http/frame.py index 1b269de63c..f6bc562360 100644 --- a/pcapkit/const/http/frame.py +++ b/pcapkit/const/http/frame.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Frame'] -class Frame(IntEnum): +class Frame(EnumRegistry, IntEnum): """[Frame] HTTP/2 Frame Type""" #: ``DATA`` [:rfc:`9113#section-6.1`] @@ -60,72 +62,6 @@ class Frame(IntEnum): #: ``PRIORITY_UPDATE`` [:rfc:`9218`] PRIORITY_UPDATE = 0x10 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Frame': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Frame(key) - except ValueError: - if default == -1: - raise - return Frame(default) - try: - return Frame[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Frame(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Frame': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Frame': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Frame': """Lookup function used when value is not found. diff --git a/pcapkit/const/http/setting.py b/pcapkit/const/http/setting.py index 08a755f088..7550b4d343 100644 --- a/pcapkit/const/http/setting.py +++ b/pcapkit/const/http/setting.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Setting'] -class Setting(IntEnum): +class Setting(EnumRegistry, IntEnum): """[Setting] HTTP/2 Settings""" #: ``Reserved`` [:rfc:`9113`] @@ -58,72 +60,6 @@ class Setting(IntEnum): #: Value: 0) SETTINGS_ENABLE_METADATA = 0x4D44 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Setting': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Setting(key) - except ValueError: - if default == -1: - raise - return Setting(default) - try: - return Setting[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Setting(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Setting': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Setting': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Setting': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/classification_level.py b/pcapkit/const/ipv4/classification_level.py index 2603b3374c..75f213d077 100644 --- a/pcapkit/const/ipv4/classification_level.py +++ b/pcapkit/const/ipv4/classification_level.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ClassificationLevel'] -class ClassificationLevel(IntEnum): +class ClassificationLevel(EnumRegistry, IntEnum): """[ClassificationLevel] Classification Level Encodings""" Reserved_4 = 0b00000001 @@ -34,72 +36,6 @@ class ClassificationLevel(IntEnum): Reserved_1 = 0b11110001 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ClassificationLevel': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ClassificationLevel(key) - except ValueError: - if default == -1: - raise - return ClassificationLevel(default) - try: - return ClassificationLevel[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ClassificationLevel(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ClassificationLevel': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ClassificationLevel': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ClassificationLevel': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/option_class.py b/pcapkit/const/ipv4/option_class.py index cef626cb31..d8385a6164 100644 --- a/pcapkit/const/ipv4/option_class.py +++ b/pcapkit/const/ipv4/option_class.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['OptionClass'] -class OptionClass(IntEnum): +class OptionClass(EnumRegistry, IntEnum): """[OptionClass] Option Classes""" control = 0 @@ -26,72 +28,6 @@ class OptionClass(IntEnum): reserved_for_future_use_3 = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'OptionClass': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return OptionClass(key) - except ValueError: - if default == -1: - raise - return OptionClass(default) - try: - return OptionClass[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return OptionClass(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'OptionClass': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'OptionClass': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'OptionClass': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/option_number.py b/pcapkit/const/ipv4/option_number.py index 8f03a90266..754345b3a4 100644 --- a/pcapkit/const/ipv4/option_number.py +++ b/pcapkit/const/ipv4/option_number.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['OptionNumber'] -class OptionNumber(IntEnum): +class OptionNumber(EnumRegistry, IntEnum): """[OptionNumber] IP Option Numbers""" #: ``EOOL``, End of Options List [:rfc:`791`][Jon Postel] @@ -108,72 +110,6 @@ class OptionNumber(IntEnum): #: ``EXP``, RFC3692-style Experiment [:rfc:`4727`] EXP_222 = 222 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'OptionNumber': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return OptionNumber(key) - except ValueError: - if default == -1: - raise - return OptionNumber(default) - try: - return OptionNumber[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return OptionNumber(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'OptionNumber': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'OptionNumber': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'OptionNumber': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/protection_authority.py b/pcapkit/const/ipv4/protection_authority.py index 7593af5b8d..8f0d31fc6f 100644 --- a/pcapkit/const/ipv4/protection_authority.py +++ b/pcapkit/const/ipv4/protection_authority.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ProtectionAuthority'] -class ProtectionAuthority(IntEnum): +class ProtectionAuthority(EnumRegistry, IntEnum): """[ProtectionAuthority] Protection Authority Bit Assignments""" GENSER = 0 @@ -34,72 +36,6 @@ class ProtectionAuthority(IntEnum): Field_Termination_Indicator = 7 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ProtectionAuthority': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ProtectionAuthority(key) - except ValueError: - if default == -1: - raise - return ProtectionAuthority(default) - try: - return ProtectionAuthority[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ProtectionAuthority(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ProtectionAuthority': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ProtectionAuthority': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ProtectionAuthority': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/qs_function.py b/pcapkit/const/ipv4/qs_function.py index 8f5fcd8587..08881a5c25 100644 --- a/pcapkit/const/ipv4/qs_function.py +++ b/pcapkit/const/ipv4/qs_function.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['QSFunction'] -class QSFunction(IntEnum): +class QSFunction(EnumRegistry, IntEnum): """[QSFunction] QS Functions""" Quick_Start_Request = 0 Report_of_Approved_Rate = 8 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'QSFunction': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return QSFunction(key) - except ValueError: - if default == -1: - raise - return QSFunction(default) - try: - return QSFunction[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return QSFunction(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'QSFunction': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'QSFunction': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'QSFunction': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/router_alert.py b/pcapkit/const/ipv4/router_alert.py index 551d2145e3..231f4508fd 100644 --- a/pcapkit/const/ipv4/router_alert.py +++ b/pcapkit/const/ipv4/router_alert.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['RouterAlert'] -class RouterAlert(IntEnum): +class RouterAlert(EnumRegistry, IntEnum): """[RouterAlert] IPv4 Router Alert Option Values""" #: Router shall examine packet [:rfc:`2113`] @@ -219,72 +221,6 @@ class RouterAlert(IntEnum): #: Reserved [:rfc:`5350`] Reserved_65535 = 65535 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'RouterAlert': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return RouterAlert(key) - except ValueError: - if default == -1: - raise - return RouterAlert(default) - try: - return RouterAlert[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return RouterAlert(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'RouterAlert': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'RouterAlert': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'RouterAlert': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_del.py b/pcapkit/const/ipv4/tos_del.py index e252286484..14e32ebdd4 100644 --- a/pcapkit/const/ipv4/tos_del.py +++ b/pcapkit/const/ipv4/tos_del.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ToSDelay'] -class ToSDelay(IntEnum): +class ToSDelay(EnumRegistry, IntEnum): """[ToSDelay] ToS (DS Field) Delay""" NORMAL = 0 LOW = 1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ToSDelay': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ToSDelay(key) - except ValueError: - if default == -1: - raise - return ToSDelay(default) - try: - return ToSDelay[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ToSDelay(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ToSDelay': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ToSDelay': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ToSDelay': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_ecn.py b/pcapkit/const/ipv4/tos_ecn.py index aeaaad5655..f033350604 100644 --- a/pcapkit/const/ipv4/tos_ecn.py +++ b/pcapkit/const/ipv4/tos_ecn.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ToSECN'] -class ToSECN(IntEnum): +class ToSECN(EnumRegistry, IntEnum): """[ToSECN] ToS ECN Field""" Not_ECT = 0b00 @@ -26,72 +28,6 @@ class ToSECN(IntEnum): CE = 0b11 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ToSECN': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ToSECN(key) - except ValueError: - if default == -1: - raise - return ToSECN(default) - try: - return ToSECN[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ToSECN(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ToSECN': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ToSECN': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ToSECN': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_pre.py b/pcapkit/const/ipv4/tos_pre.py index 6fc7f1762a..182e259a2b 100644 --- a/pcapkit/const/ipv4/tos_pre.py +++ b/pcapkit/const/ipv4/tos_pre.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ToSPrecedence'] -class ToSPrecedence(IntEnum): +class ToSPrecedence(EnumRegistry, IntEnum): """[ToSPrecedence] ToS (DS Field) Precedence""" Network_Control = 7 @@ -34,72 +36,6 @@ class ToSPrecedence(IntEnum): Routine = 0 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ToSPrecedence': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ToSPrecedence(key) - except ValueError: - if default == -1: - raise - return ToSPrecedence(default) - try: - return ToSPrecedence[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ToSPrecedence(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ToSPrecedence': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ToSPrecedence': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ToSPrecedence': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_rel.py b/pcapkit/const/ipv4/tos_rel.py index 1b69affb18..c59677b189 100644 --- a/pcapkit/const/ipv4/tos_rel.py +++ b/pcapkit/const/ipv4/tos_rel.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ToSReliability'] -class ToSReliability(IntEnum): +class ToSReliability(EnumRegistry, IntEnum): """[ToSReliability] ToS (DS Field) Reliability""" NORMAL = 0 HIGH = 1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ToSReliability': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ToSReliability(key) - except ValueError: - if default == -1: - raise - return ToSReliability(default) - try: - return ToSReliability[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ToSReliability(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ToSReliability': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ToSReliability': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ToSReliability': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/tos_thr.py b/pcapkit/const/ipv4/tos_thr.py index 15ba5ada34..da7a64578f 100644 --- a/pcapkit/const/ipv4/tos_thr.py +++ b/pcapkit/const/ipv4/tos_thr.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ToSThroughput'] -class ToSThroughput(IntEnum): +class ToSThroughput(EnumRegistry, IntEnum): """[ToSThroughput] ToS (DS Field) Throughput""" NORMAL = 0 HIGH = 1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ToSThroughput': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ToSThroughput(key) - except ValueError: - if default == -1: - raise - return ToSThroughput(default) - try: - return ToSThroughput[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ToSThroughput(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ToSThroughput': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ToSThroughput': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ToSThroughput': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv4/ts_flag.py b/pcapkit/const/ipv4/ts_flag.py index ff6c3a013d..3f932dc4d5 100644 --- a/pcapkit/const/ipv4/ts_flag.py +++ b/pcapkit/const/ipv4/ts_flag.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['TSFlag'] -class TSFlag(IntEnum): +class TSFlag(EnumRegistry, IntEnum): """[TSFlag] TS Flag""" Timestamp_Only = 0 @@ -24,72 +26,6 @@ class TSFlag(IntEnum): Prespecified_IP_with_Timestamp = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'TSFlag': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return TSFlag(key) - except ValueError: - if default == -1: - raise - return TSFlag(default) - try: - return TSFlag[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return TSFlag(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'TSFlag': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'TSFlag': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'TSFlag': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/extension_header.py b/pcapkit/const/ipv6/extension_header.py index a6b9d9cd2b..5d24d1b9dd 100644 --- a/pcapkit/const/ipv6/extension_header.py +++ b/pcapkit/const/ipv6/extension_header.py @@ -12,7 +12,7 @@ from aenum import IntEnum -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['ExtensionHeader'] diff --git a/pcapkit/const/ipv6/option.py b/pcapkit/const/ipv6/option.py index d58d1b8a6e..6f1200f49e 100644 --- a/pcapkit/const/ipv6/option.py +++ b/pcapkit/const/ipv6/option.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Option'] -class Option(IntEnum): +class Option(EnumRegistry, IntEnum): """[Option] Destination Options and Hop-by-Hop Options""" #: Pad1 [:rfc:`2460`] @@ -108,72 +110,6 @@ class Option(IntEnum): #: RFC3692-style Experiment [:rfc:`4727`] RFC3692_style_Experiment_0xFE = 0xFE - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Option': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Option(key) - except ValueError: - if default == -1: - raise - return Option(default) - try: - return Option[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Option(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Option': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Option': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Option': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/option_action.py b/pcapkit/const/ipv6/option_action.py index 14c798a56a..5a54cf41f2 100644 --- a/pcapkit/const/ipv6/option_action.py +++ b/pcapkit/const/ipv6/option_action.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['OptionAction'] -class OptionAction(IntEnum): +class OptionAction(EnumRegistry, IntEnum): """[OptionAction] Option Actions""" skip = 0 @@ -26,72 +28,6 @@ class OptionAction(IntEnum): discard_icmp_unicast = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'OptionAction': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return OptionAction(key) - except ValueError: - if default == -1: - raise - return OptionAction(default) - try: - return OptionAction[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return OptionAction(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'OptionAction': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'OptionAction': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'OptionAction': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/qs_function.py b/pcapkit/const/ipv6/qs_function.py index 10e05beb87..ee02d867b6 100644 --- a/pcapkit/const/ipv6/qs_function.py +++ b/pcapkit/const/ipv6/qs_function.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['QSFunction'] -class QSFunction(IntEnum): +class QSFunction(EnumRegistry, IntEnum): """[QSFunction] QS Functions""" Quick_Start_Request = 0 Report_of_Approved_Rate = 8 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'QSFunction': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return QSFunction(key) - except ValueError: - if default == -1: - raise - return QSFunction(default) - try: - return QSFunction[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return QSFunction(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'QSFunction': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'QSFunction': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'QSFunction': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/router_alert.py b/pcapkit/const/ipv6/router_alert.py index 0881ed5f4e..ef4f11addd 100644 --- a/pcapkit/const/ipv6/router_alert.py +++ b/pcapkit/const/ipv6/router_alert.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['RouterAlert'] -class RouterAlert(IntEnum): +class RouterAlert(EnumRegistry, IntEnum): """[RouterAlert] IPv6 Router Alert Option Values""" #: Datagram contains a Multicast Listener Discovery message [:rfc:`2710`] @@ -231,72 +233,6 @@ class RouterAlert(IntEnum): #: Reserved [The Internet Assigned Numbers Authority] Reserved_65535 = 65535 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'RouterAlert': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return RouterAlert(key) - except ValueError: - if default == -1: - raise - return RouterAlert(default) - try: - return RouterAlert[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return RouterAlert(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'RouterAlert': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'RouterAlert': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'RouterAlert': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/routing.py b/pcapkit/const/ipv6/routing.py index a292c1fe48..96ba60c2ee 100644 --- a/pcapkit/const/ipv6/routing.py +++ b/pcapkit/const/ipv6/routing.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Routing'] -class Routing(IntEnum): +class Routing(EnumRegistry, IntEnum): """[Routing] IPv6 Routing Types""" #: Source Route (DEPRECATED) [:rfc:`2460`][:rfc:`5095`] @@ -48,72 +50,6 @@ class Routing(IntEnum): #: Reserved Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Routing': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Routing(key) - except ValueError: - if default == -1: - raise - return Routing(default) - try: - return Routing[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Routing(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Routing': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Routing': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Routing': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/seed_id.py b/pcapkit/const/ipv6/seed_id.py index 4649ff93c9..ba43ef4e2c 100644 --- a/pcapkit/const/ipv6/seed_id.py +++ b/pcapkit/const/ipv6/seed_id.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['SeedID'] -class SeedID(IntEnum): +class SeedID(EnumRegistry, IntEnum): """[SeedID] Seed-ID Types""" IPV6_SOURCE_ADDRESS = 0b00 @@ -26,72 +28,6 @@ class SeedID(IntEnum): SEEDID_128_BIT_UNSIGNED_INTEGER = 0b11 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'SeedID': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return SeedID(key) - except ValueError: - if default == -1: - raise - return SeedID(default) - try: - return SeedID[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return SeedID(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'SeedID': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'SeedID': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'SeedID': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/smf_dpd_mode.py b/pcapkit/const/ipv6/smf_dpd_mode.py index f5f525e59f..96e085ae01 100644 --- a/pcapkit/const/ipv6/smf_dpd_mode.py +++ b/pcapkit/const/ipv6/smf_dpd_mode.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['SMFDPDMode'] -class SMFDPDMode(IntEnum): +class SMFDPDMode(EnumRegistry, IntEnum): """[SMFDPDMode] Simplified Multicast Forwarding Duplicate Packet Detection (``SMF_DPD``) Options""" I_DPD = 0 H_DPD = 1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'SMFDPDMode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return SMFDPDMode(key) - except ValueError: - if default == -1: - raise - return SMFDPDMode(default) - try: - return SMFDPDMode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return SMFDPDMode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'SMFDPDMode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'SMFDPDMode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'SMFDPDMode': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipv6/tagger_id.py b/pcapkit/const/ipv6/tagger_id.py index dd227c5fe0..1527b829e4 100644 --- a/pcapkit/const/ipv6/tagger_id.py +++ b/pcapkit/const/ipv6/tagger_id.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['TaggerID'] -class TaggerID(IntEnum): +class TaggerID(EnumRegistry, IntEnum): """[TaggerID] TaggerID Types""" #: NULL [:rfc:`6621`] @@ -30,72 +32,6 @@ class TaggerID(IntEnum): #: IPv6 [:rfc:`6621`] IPv6 = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'TaggerID': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return TaggerID(key) - except ValueError: - if default == -1: - raise - return TaggerID(default) - try: - return TaggerID[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return TaggerID(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'TaggerID': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'TaggerID': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'TaggerID': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipx/packet.py b/pcapkit/const/ipx/packet.py index c8a7c9b81a..8336a0bad2 100644 --- a/pcapkit/const/ipx/packet.py +++ b/pcapkit/const/ipx/packet.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Packet'] -class Packet(IntEnum): +class Packet(EnumRegistry, IntEnum): """[Packet] IPX Packet Types""" #: Unknown @@ -43,72 +45,6 @@ class Packet(IntEnum): #: Broadcast[4] Broadcast_4 = 20 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Packet': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Packet(key) - except ValueError: - if default == -1: - raise - return Packet(default) - try: - return Packet[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Packet(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Packet': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Packet': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/ipx/socket.py b/pcapkit/const/ipx/socket.py index e7005e61f6..ea692e7ccc 100644 --- a/pcapkit/const/ipx/socket.py +++ b/pcapkit/const/ipx/socket.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Socket'] -class Socket(IntEnum): +class Socket(EnumRegistry, IntEnum): """[Socket] Socket Types""" #: Unspecified socket; this is IPX's own default for the dst/src socket field. @@ -63,72 +65,6 @@ class Socket(IntEnum): #: IPXF, IPX Fragmentation Protocol IPXF = 0x9093 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Socket': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Socket(key) - except ValueError: - if default == -1: - raise - return Socket(default) - try: - return Socket[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Socket(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Socket': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Socket': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Socket': """Lookup function used when value is not found. diff --git a/pcapkit/const/l2tp/type.py b/pcapkit/const/l2tp/type.py index 4448bcfb8b..a60a1b84e4 100644 --- a/pcapkit/const/l2tp/type.py +++ b/pcapkit/const/l2tp/type.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Type'] -class Type(IntEnum): +class Type(EnumRegistry, IntEnum): """[Type] L2TP Type""" Control = 0 Data = 1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Type': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Type(key) - except ValueError: - if default == -1: - raise - return Type(default) - try: - return Type[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Type(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Type': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Type': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Type': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/access_type.py b/pcapkit/const/mh/access_type.py index b213bd4d96..87043d167c 100644 --- a/pcapkit/const/mh/access_type.py +++ b/pcapkit/const/mh/access_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['AccessType'] -class AccessType(IntEnum): +class AccessType(EnumRegistry, IntEnum): """[AccessType] Access Technology Type Option Type Values""" #: Reserved [:rfc:`5213`] @@ -60,72 +62,6 @@ class AccessType(IntEnum): #: 3GPP NB-IOT [3GPP TS 29.275][Kimmo Kymalainen] AccessType_3GPP_NB_IOT = 13 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'AccessType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return AccessType(key) - except ValueError: - if default == -1: - raise - return AccessType(default) - try: - return AccessType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return AccessType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'AccessType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'AccessType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'AccessType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/ack_status_code.py b/pcapkit/const/mh/ack_status_code.py index 53b39454e8..cbca43d486 100644 --- a/pcapkit/const/mh/ack_status_code.py +++ b/pcapkit/const/mh/ack_status_code.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ACKStatusCode'] -class ACKStatusCode(IntEnum): +class ACKStatusCode(EnumRegistry, IntEnum): """[ACKStatusCode] Pseudo Home Address Acknowledgement Status Codes""" #: Success [:rfc:`5726`] @@ -36,72 +38,6 @@ class ACKStatusCode(IntEnum): #: Dynamic pseudo home address assignment not available [:rfc:`5726`] Dynamic_pseudo_home_address_assignment_not_available = 132 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ACKStatusCode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ACKStatusCode(key) - except ValueError: - if default == -1: - raise - return ACKStatusCode(default) - try: - return ACKStatusCode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ACKStatusCode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ACKStatusCode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ACKStatusCode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ACKStatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/ani_suboption.py b/pcapkit/const/mh/ani_suboption.py index 94616d684c..5eaa20e2c1 100644 --- a/pcapkit/const/mh/ani_suboption.py +++ b/pcapkit/const/mh/ani_suboption.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['ANISuboption'] -class ANISuboption(IntEnum): +class ANISuboption(EnumRegistry, IntEnum): """[ANISuboption] Access Network Information (ANI) Sub-Option Type Values""" #: Reserved [:rfc:`6757`] @@ -42,72 +44,6 @@ class ANISuboption(IntEnum): #: Reserved [:rfc:`6757`] Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'ANISuboption': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return ANISuboption(key) - except ValueError: - if default == -1: - raise - return ANISuboption(default) - try: - return ANISuboption[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return ANISuboption(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'ANISuboption': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'ANISuboption': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'ANISuboption': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/auth_subtype.py b/pcapkit/const/mh/auth_subtype.py index c7b55859cd..664750ac98 100644 --- a/pcapkit/const/mh/auth_subtype.py +++ b/pcapkit/const/mh/auth_subtype.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['AuthSubtype'] -class AuthSubtype(IntEnum): +class AuthSubtype(EnumRegistry, IntEnum): """[AuthSubtype] Subtype Field of the MN-HA and MN-AAA Authentication Mobility Options""" #: MN-HA authentication mobility option [:rfc:`4285`] @@ -24,72 +26,6 @@ class AuthSubtype(IntEnum): #: MN-AAA authentication mobility option [:rfc:`4285`] MN_AAA = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'AuthSubtype': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return AuthSubtype(key) - except ValueError: - if default == -1: - raise - return AuthSubtype(default) - try: - return AuthSubtype[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return AuthSubtype(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'AuthSubtype': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'AuthSubtype': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'AuthSubtype': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_ack_flag.py b/pcapkit/const/mh/binding_ack_flag.py index 84582b0263..bb34ce85cc 100644 --- a/pcapkit/const/mh/binding_ack_flag.py +++ b/pcapkit/const/mh/binding_ack_flag.py @@ -12,7 +12,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['BindingACKFlag'] diff --git a/pcapkit/const/mh/binding_error.py b/pcapkit/const/mh/binding_error.py index e97daa391a..17890e49ab 100644 --- a/pcapkit/const/mh/binding_error.py +++ b/pcapkit/const/mh/binding_error.py @@ -12,82 +12,18 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['BindingError'] -class BindingError(IntEnum): +class BindingError(EnumRegistry, IntEnum): """[BindingError] Binding Error Status Code""" Unknown_binding_for_Home_Address_destination_option = 1 Unrecognized_MH_Type_value = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'BindingError': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return BindingError(key) - except ValueError: - if default == -1: - raise - return BindingError(default) - try: - return BindingError[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return BindingError(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'BindingError': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'BindingError': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'BindingError': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_revocation.py b/pcapkit/const/mh/binding_revocation.py index a43f47c75c..13da8f6b9e 100644 --- a/pcapkit/const/mh/binding_revocation.py +++ b/pcapkit/const/mh/binding_revocation.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['BindingRevocation'] -class BindingRevocation(IntEnum): +class BindingRevocation(EnumRegistry, IntEnum): """[BindingRevocation] Binding Revocation Type""" #: Reserved [:rfc:`5846`] @@ -27,72 +29,6 @@ class BindingRevocation(IntEnum): #: Binding Revocation Acknowledgement [:rfc:`5846`] Binding_Revocation_Acknowledgement = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'BindingRevocation': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return BindingRevocation(key) - except ValueError: - if default == -1: - raise - return BindingRevocation(default) - try: - return BindingRevocation[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return BindingRevocation(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'BindingRevocation': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'BindingRevocation': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'BindingRevocation': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/binding_update_flag.py b/pcapkit/const/mh/binding_update_flag.py index 5b835fb866..4535b87d8d 100644 --- a/pcapkit/const/mh/binding_update_flag.py +++ b/pcapkit/const/mh/binding_update_flag.py @@ -12,7 +12,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['BindingUpdateFlag'] diff --git a/pcapkit/const/mh/cga_extension.py b/pcapkit/const/mh/cga_extension.py index 89d8ad80ae..2f8e197a36 100644 --- a/pcapkit/const/mh/cga_extension.py +++ b/pcapkit/const/mh/cga_extension.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['CGAExtension'] -class CGAExtension(IntEnum): +class CGAExtension(EnumRegistry, IntEnum): """[CGAExtension] CGA Extension Type Values""" #: Multi-Prefix [:rfc:`5535`] @@ -30,72 +32,6 @@ class CGAExtension(IntEnum): #: Exp_FFFF (experimental) [:rfc:`4581`] Exp_FFFF = 0xFFFF - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'CGAExtension': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return CGAExtension(key) - except ValueError: - if default == -1: - raise - return CGAExtension(default) - try: - return CGAExtension[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return CGAExtension(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'CGAExtension': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'CGAExtension': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'CGAExtension': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/cga_sec.py b/pcapkit/const/mh/cga_sec.py index f30bb933d0..edd3c5ce2e 100644 --- a/pcapkit/const/mh/cga_sec.py +++ b/pcapkit/const/mh/cga_sec.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['CGASec'] -class CGASec(IntEnum): +class CGASec(EnumRegistry, IntEnum): """[CGASec] CGA SEC""" #: SHA-1_0hash2bits [:rfc:`4982`] @@ -27,72 +29,6 @@ class CGASec(IntEnum): #: SHA-1_32hash2bits [:rfc:`4982`] SHA_1_32hash2bits = 0b010 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'CGASec': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return CGASec(key) - except ValueError: - if default == -1: - raise - return CGASec(default) - try: - return CGASec[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return CGASec(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'CGASec': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'CGASec': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'CGASec': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/cga_type.py b/pcapkit/const/mh/cga_type.py index 144beedce4..03510ee152 100644 --- a/pcapkit/const/mh/cga_type.py +++ b/pcapkit/const/mh/cga_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['CGAType'] -class CGAType(IntEnum): +class CGAType(EnumRegistry, IntEnum): """[CGAType] CGA Extension Type Tags""" #: 0x086F CA5E 10B2 00C9 9C8C E001 6427 7C08 [:rfc:`3971`] @@ -39,72 +41,6 @@ class CGAType(IntEnum): #: 0x00B5 A69C 795D F5D5 F008 7F56 843F 2C40 [:rfc:`9374`] Tag_00B5_A69C_795D_F5D5_F008_7F56_843F_2C40 = 0x00B5_A69C_795D_F5D5_F008_7F56_843F_2C40 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'CGAType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return CGAType(key) - except ValueError: - if default == -1: - raise - return CGAType(default) - try: - return CGAType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return CGAType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'CGAType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'CGAType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'CGAType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dhcp_support_mode.py b/pcapkit/const/mh/dhcp_support_mode.py index b93f736251..eb0301edf5 100644 --- a/pcapkit/const/mh/dhcp_support_mode.py +++ b/pcapkit/const/mh/dhcp_support_mode.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['DHCPSupportMode'] -class DHCPSupportMode(IntEnum): +class DHCPSupportMode(EnumRegistry, IntEnum): """[DHCPSupportMode] IPv4 DHCP Support Mode Flags""" #: Unassigned @@ -24,72 +26,6 @@ class DHCPSupportMode(IntEnum): #: (S) flag [:rfc:`5844`] S_flag = 0x1 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'DHCPSupportMode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return DHCPSupportMode(key) - except ValueError: - if default == -1: - raise - return DHCPSupportMode(default) - try: - return DHCPSupportMode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return DHCPSupportMode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'DHCPSupportMode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'DHCPSupportMode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'DHCPSupportMode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dns_status_code.py b/pcapkit/const/mh/dns_status_code.py index 07bf21901f..5b708a079c 100644 --- a/pcapkit/const/mh/dns_status_code.py +++ b/pcapkit/const/mh/dns_status_code.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['DNSStatusCode'] -class DNSStatusCode(IntEnum): +class DNSStatusCode(EnumRegistry, IntEnum): """[DNSStatusCode] Status Codes (DNS Update Mobility Option)""" #: DNS update performed [:rfc:`5026`] @@ -30,72 +32,6 @@ class DNSStatusCode(IntEnum): #: DNS Update Failed [:rfc:`5026`] DNS_Update_Failed = 130 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'DNSStatusCode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return DNSStatusCode(key) - except ValueError: - if default == -1: - raise - return DNSStatusCode(default) - try: - return DNSStatusCode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return DNSStatusCode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'DNSStatusCode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'DNSStatusCode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'DNSStatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dsmip6_tls_packet.py b/pcapkit/const/mh/dsmip6_tls_packet.py index 52217f58b1..28ff3f2209 100644 --- a/pcapkit/const/mh/dsmip6_tls_packet.py +++ b/pcapkit/const/mh/dsmip6_tls_packet.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['DSMIP6TLSPacket'] -class DSMIP6TLSPacket(IntEnum): +class DSMIP6TLSPacket(EnumRegistry, IntEnum): """[DSMIP6TLSPacket] DSMIP6-TLS Packet Types Registry""" #: non-encrypted IP packet [:rfc:`6618`] @@ -27,72 +29,6 @@ class DSMIP6TLSPacket(IntEnum): #: mobility header [:rfc:`6618`] mobility_header = 8 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'DSMIP6TLSPacket': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return DSMIP6TLSPacket(key) - except ValueError: - if default == -1: - raise - return DSMIP6TLSPacket(default) - try: - return DSMIP6TLSPacket[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return DSMIP6TLSPacket(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'DSMIP6TLSPacket': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'DSMIP6TLSPacket': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'DSMIP6TLSPacket': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/dsmipv6_home_address.py b/pcapkit/const/mh/dsmipv6_home_address.py index 1446a529c8..1c4fd0161a 100644 --- a/pcapkit/const/mh/dsmipv6_home_address.py +++ b/pcapkit/const/mh/dsmipv6_home_address.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['DSMIPv6HomeAddress'] -class DSMIPv6HomeAddress(IntEnum): +class DSMIPv6HomeAddress(EnumRegistry, IntEnum): """[DSMIPv6HomeAddress] Dual Stack MIPv6 (DSMIPv6) IPv4 Home Address Option Status Codes""" #: Success [:rfc:`5555`] @@ -39,72 +41,6 @@ class DSMIPv6HomeAddress(IntEnum): #: Prefix allocation unauthorized [:rfc:`5555`] Prefix_allocation_unauthorized = 133 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'DSMIPv6HomeAddress': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return DSMIPv6HomeAddress(key) - except ValueError: - if default == -1: - raise - return DSMIPv6HomeAddress(default) - try: - return DSMIPv6HomeAddress[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return DSMIPv6HomeAddress(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'DSMIPv6HomeAddress': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'DSMIPv6HomeAddress': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'DSMIPv6HomeAddress': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/enumerating_algorithm.py b/pcapkit/const/mh/enumerating_algorithm.py index 34aa8a32c7..1d8071ee39 100644 --- a/pcapkit/const/mh/enumerating_algorithm.py +++ b/pcapkit/const/mh/enumerating_algorithm.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['EnumeratingAlgorithm'] -class EnumeratingAlgorithm(IntEnum): +class EnumeratingAlgorithm(EnumRegistry, IntEnum): """[EnumeratingAlgorithm] Enumerating Algorithms""" #: Reserved (not available for assignment) [:rfc:`4285`] @@ -27,72 +29,6 @@ class EnumeratingAlgorithm(IntEnum): #: Reserved for use by 3GPP2 [:rfc:`4285`] Reserved_for_use_by_3GPP2 = 5 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'EnumeratingAlgorithm': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return EnumeratingAlgorithm(key) - except ValueError: - if default == -1: - raise - return EnumeratingAlgorithm(default) - try: - return EnumeratingAlgorithm[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return EnumeratingAlgorithm(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'EnumeratingAlgorithm': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'EnumeratingAlgorithm': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'EnumeratingAlgorithm': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_ack_status.py b/pcapkit/const/mh/fb_ack_status.py index 478fa71bad..2b15d706e4 100644 --- a/pcapkit/const/mh/fb_ack_status.py +++ b/pcapkit/const/mh/fb_ack_status.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FlowBindingACKStatus'] -class FlowBindingACKStatus(IntEnum): +class FlowBindingACKStatus(EnumRegistry, IntEnum): """[FlowBindingACKStatus] Flow Binding Acknowledgement Status Codes""" #: Success [:rfc:`7109`] @@ -27,72 +29,6 @@ class FlowBindingACKStatus(IntEnum): #: Action NOT Authorized [:rfc:`7109`] Action_NOT_Authorized = 129 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FlowBindingACKStatus': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FlowBindingACKStatus(key) - except ValueError: - if default == -1: - raise - return FlowBindingACKStatus(default) - try: - return FlowBindingACKStatus[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FlowBindingACKStatus(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FlowBindingACKStatus': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FlowBindingACKStatus': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingACKStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_action.py b/pcapkit/const/mh/fb_action.py index a1d0d32bf6..9256f93693 100644 --- a/pcapkit/const/mh/fb_action.py +++ b/pcapkit/const/mh/fb_action.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FlowBindingAction'] -class FlowBindingAction(IntEnum): +class FlowBindingAction(EnumRegistry, IntEnum): """[FlowBindingAction] Flow Binding Action Values""" #: Add a flow binding [:rfc:`7109`] @@ -36,72 +38,6 @@ class FlowBindingAction(IntEnum): #: Revoke a flow binding [:rfc:`7109`] Revoke = 16 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FlowBindingAction': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FlowBindingAction(key) - except ValueError: - if default == -1: - raise - return FlowBindingAction(default) - try: - return FlowBindingAction[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FlowBindingAction(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FlowBindingAction': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FlowBindingAction': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingAction': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_indication_trigger.py b/pcapkit/const/mh/fb_indication_trigger.py index d44f585283..09d4a98205 100644 --- a/pcapkit/const/mh/fb_indication_trigger.py +++ b/pcapkit/const/mh/fb_indication_trigger.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FlowBindingIndicationTrigger'] -class FlowBindingIndicationTrigger(IntEnum): +class FlowBindingIndicationTrigger(EnumRegistry, IntEnum): """[FlowBindingIndicationTrigger] Flow Binding Indication Triggers""" #: Reserved [:rfc:`7109`] @@ -30,72 +32,6 @@ class FlowBindingIndicationTrigger(IntEnum): #: Possible Out-of-Sync BCE State [:rfc:`7109`] Possible_Out_of_Sync_BCE_State = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FlowBindingIndicationTrigger': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FlowBindingIndicationTrigger(key) - except ValueError: - if default == -1: - raise - return FlowBindingIndicationTrigger(default) - try: - return FlowBindingIndicationTrigger[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FlowBindingIndicationTrigger(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FlowBindingIndicationTrigger': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FlowBindingIndicationTrigger': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingIndicationTrigger': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/fb_type.py b/pcapkit/const/mh/fb_type.py index 9c688377ad..4b4abef473 100644 --- a/pcapkit/const/mh/fb_type.py +++ b/pcapkit/const/mh/fb_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FlowBindingType'] -class FlowBindingType(IntEnum): +class FlowBindingType(EnumRegistry, IntEnum): """[FlowBindingType] Flow Binding Type""" #: Unassigned @@ -27,72 +29,6 @@ class FlowBindingType(IntEnum): #: Flow Binding Acknowledgement [:rfc:`7109`] Acknowledgement = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FlowBindingType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FlowBindingType(key) - except ValueError: - if default == -1: - raise - return FlowBindingType(default) - try: - return FlowBindingType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FlowBindingType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FlowBindingType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FlowBindingType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FlowBindingType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/flow_id_status.py b/pcapkit/const/mh/flow_id_status.py index 492e812af4..8825f986c6 100644 --- a/pcapkit/const/mh/flow_id_status.py +++ b/pcapkit/const/mh/flow_id_status.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FlowIDStatus'] -class FlowIDStatus(IntEnum): +class FlowIDStatus(EnumRegistry, IntEnum): """[FlowIDStatus] Flow Identification Mobility Option Status Codes""" #: Flow binding successful [:rfc:`6089`] @@ -39,72 +41,6 @@ class FlowIDStatus(IntEnum): #: Traffic selector format not supported [:rfc:`6089`] Traffic_selector_format_not_supported = 133 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FlowIDStatus': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FlowIDStatus(key) - except ValueError: - if default == -1: - raise - return FlowIDStatus(default) - try: - return FlowIDStatus[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FlowIDStatus(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FlowIDStatus': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FlowIDStatus': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FlowIDStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/flow_id_suboption.py b/pcapkit/const/mh/flow_id_suboption.py index 54b3cc975e..ed6eb62210 100644 --- a/pcapkit/const/mh/flow_id_suboption.py +++ b/pcapkit/const/mh/flow_id_suboption.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FlowIDSuboption'] -class FlowIDSuboption(IntEnum): +class FlowIDSuboption(EnumRegistry, IntEnum): """[FlowIDSuboption] Flow Identification Sub-Options""" #: Pad [:rfc:`6089`] @@ -36,72 +38,6 @@ class FlowIDSuboption(IntEnum): #: Target Care-of Address [:rfc:`7109`] Target_Care_of_Address = 5 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FlowIDSuboption': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FlowIDSuboption(key) - except ValueError: - if default == -1: - raise - return FlowIDSuboption(default) - try: - return FlowIDSuboption[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FlowIDSuboption(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FlowIDSuboption': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FlowIDSuboption': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FlowIDSuboption': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handoff_type.py b/pcapkit/const/mh/handoff_type.py index 58467ce06c..43bbcbdf28 100644 --- a/pcapkit/const/mh/handoff_type.py +++ b/pcapkit/const/mh/handoff_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['HandoffType'] -class HandoffType(IntEnum): +class HandoffType(EnumRegistry, IntEnum): """[HandoffType] Handoff Indicator Option Type Values""" #: Reserved [:rfc:`5213`] @@ -39,72 +41,6 @@ class HandoffType(IntEnum): #: Attachment over a new interface sharing prefixes [:rfc:`7864`] Attachment_over_a_new_interface_sharing_prefixes = 6 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HandoffType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HandoffType(key) - except ValueError: - if default == -1: - raise - return HandoffType(default) - try: - return HandoffType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HandoffType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HandoffType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HandoffType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HandoffType': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handover_ack_flag.py b/pcapkit/const/mh/handover_ack_flag.py index 8c23f13ea5..0ea3834e1a 100644 --- a/pcapkit/const/mh/handover_ack_flag.py +++ b/pcapkit/const/mh/handover_ack_flag.py @@ -12,7 +12,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['HandoverACKFlag'] diff --git a/pcapkit/const/mh/handover_ack_status.py b/pcapkit/const/mh/handover_ack_status.py index db852148b8..f5d7e53c25 100644 --- a/pcapkit/const/mh/handover_ack_status.py +++ b/pcapkit/const/mh/handover_ack_status.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['HandoverACKStatus'] -class HandoverACKStatus(IntEnum): +class HandoverACKStatus(EnumRegistry, IntEnum): """[HandoverACKStatus] Handover Acknowledge Status Codes""" #: Handover Accepted or Successful (when 'P' flag is set) [:rfc:`5949`] @@ -57,72 +59,6 @@ class HandoverACKStatus(IntEnum): #: Forwarding Not Available [:rfc:`5949`] Forwarding_Not_Available = 132 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HandoverACKStatus': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HandoverACKStatus(key) - except ValueError: - if default == -1: - raise - return HandoverACKStatus(default) - try: - return HandoverACKStatus[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HandoverACKStatus(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HandoverACKStatus': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HandoverACKStatus': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HandoverACKStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/handover_initiate_flag.py b/pcapkit/const/mh/handover_initiate_flag.py index aa6d63c16d..d4ecc19972 100644 --- a/pcapkit/const/mh/handover_initiate_flag.py +++ b/pcapkit/const/mh/handover_initiate_flag.py @@ -12,7 +12,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['HandoverInitiateFlag'] diff --git a/pcapkit/const/mh/handover_initiate_status.py b/pcapkit/const/mh/handover_initiate_status.py index 49e7ffb68b..0f25bc5c7c 100644 --- a/pcapkit/const/mh/handover_initiate_status.py +++ b/pcapkit/const/mh/handover_initiate_status.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['HandoverInitiateStatus'] -class HandoverInitiateStatus(IntEnum): +class HandoverInitiateStatus(EnumRegistry, IntEnum): """[HandoverInitiateStatus] Handover Initiate Status Codes""" #: FBU with the PCoA as source IP address [:rfc:`5568`] @@ -30,72 +32,6 @@ class HandoverInitiateStatus(IntEnum): #: All available context transferred [:rfc:`5949`] All_available_context_transferred = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HandoverInitiateStatus': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HandoverInitiateStatus(key) - except ValueError: - if default == -1: - raise - return HandoverInitiateStatus(default) - try: - return HandoverInitiateStatus[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HandoverInitiateStatus(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HandoverInitiateStatus': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HandoverInitiateStatus': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HandoverInitiateStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/home_address_reply.py b/pcapkit/const/mh/home_address_reply.py index 90df233259..265e09e4fa 100644 --- a/pcapkit/const/mh/home_address_reply.py +++ b/pcapkit/const/mh/home_address_reply.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['HomeAddressReply'] -class HomeAddressReply(IntEnum): +class HomeAddressReply(EnumRegistry, IntEnum): """[HomeAddressReply] IPv4 Home Address Reply Status Codes""" #: Success [:rfc:`5844`] @@ -36,72 +38,6 @@ class HomeAddressReply(IntEnum): #: Dynamic IPv4 home address assignment not available [:rfc:`5844`] Dynamic_IPv4_home_address_assignment_not_available = 132 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HomeAddressReply': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HomeAddressReply(key) - except ValueError: - if default == -1: - raise - return HomeAddressReply(default) - try: - return HomeAddressReply[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HomeAddressReply(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HomeAddressReply': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HomeAddressReply': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HomeAddressReply': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/lla_code.py b/pcapkit/const/mh/lla_code.py index b6b938c3e2..8abdc81a9b 100644 --- a/pcapkit/const/mh/lla_code.py +++ b/pcapkit/const/mh/lla_code.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['LLACode'] -class LLACode(IntEnum): +class LLACode(EnumRegistry, IntEnum): """[LLACode] Link-Layer Address (LLA) Option Code""" Wilcard = 0 @@ -34,72 +36,6 @@ class LLACode(IntEnum): no_fast_handover_support = 7 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'LLACode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return LLACode(key) - except ValueError: - if default == -1: - raise - return LLACode(default) - try: - return LLACode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return LLACode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'LLACode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'LLACode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'LLACode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/lma_mag_suboption.py b/pcapkit/const/mh/lma_mag_suboption.py index c21825f342..2988c39b26 100644 --- a/pcapkit/const/mh/lma_mag_suboption.py +++ b/pcapkit/const/mh/lma_mag_suboption.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['LMAControlledMAGSuboption'] -class LMAControlledMAGSuboption(IntEnum): +class LMAControlledMAGSuboption(EnumRegistry, IntEnum): """[LMAControlledMAGSuboption] LMA-Controlled MAG Parameters Sub-Option Type Values""" #: Reserved [:rfc:`8127`] @@ -27,72 +29,6 @@ class LMAControlledMAGSuboption(IntEnum): #: Heartbeat Control Sub-Option [:rfc:`8127`] Heartbeat_Control = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'LMAControlledMAGSuboption': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return LMAControlledMAGSuboption(key) - except ValueError: - if default == -1: - raise - return LMAControlledMAGSuboption(default) - try: - return LMAControlledMAGSuboption[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return LMAControlledMAGSuboption(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'LMAControlledMAGSuboption': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'LMAControlledMAGSuboption': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'LMAControlledMAGSuboption': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/mn_group_id.py b/pcapkit/const/mh/mn_group_id.py index 471692a80f..724ac00b53 100644 --- a/pcapkit/const/mh/mn_group_id.py +++ b/pcapkit/const/mh/mn_group_id.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['MNGroupID'] -class MNGroupID(IntEnum): +class MNGroupID(EnumRegistry, IntEnum): """[MNGroupID] Mobile Node Group Identifier Type Registry""" #: Reserved [:rfc:`6602`] @@ -27,72 +29,6 @@ class MNGroupID(IntEnum): #: Reserved [:rfc:`6602`] Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'MNGroupID': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return MNGroupID(key) - except ValueError: - if default == -1: - raise - return MNGroupID(default) - try: - return MNGroupID[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return MNGroupID(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'MNGroupID': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'MNGroupID': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'MNGroupID': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/mn_id_subtype.py b/pcapkit/const/mh/mn_id_subtype.py index 50f09d06ff..36507e5959 100644 --- a/pcapkit/const/mh/mn_id_subtype.py +++ b/pcapkit/const/mh/mn_id_subtype.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['MNIDSubtype'] -class MNIDSubtype(IntEnum): +class MNIDSubtype(EnumRegistry, IntEnum): """[MNIDSubtype] Mobile Node Identifier Option Subtypes""" #: NAI [:rfc:`4283`] @@ -42,72 +44,6 @@ class MNIDSubtype(IntEnum): #: DUID [:rfc:`8371`] DUID = 8 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'MNIDSubtype': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return MNIDSubtype(key) - except ValueError: - if default == -1: - raise - return MNIDSubtype(default) - try: - return MNIDSubtype[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return MNIDSubtype(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'MNIDSubtype': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'MNIDSubtype': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'MNIDSubtype': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/operator_id.py b/pcapkit/const/mh/operator_id.py index f8d81169f6..afa9213720 100644 --- a/pcapkit/const/mh/operator_id.py +++ b/pcapkit/const/mh/operator_id.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['OperatorID'] -class OperatorID(IntEnum): +class OperatorID(EnumRegistry, IntEnum): """[OperatorID] Operator-Identifier Type Registry""" #: Reserved [:rfc:`6757`] @@ -31,72 +33,6 @@ class OperatorID(IntEnum): #: Reserved [:rfc:`6757`] Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'OperatorID': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return OperatorID(key) - except ValueError: - if default == -1: - raise - return OperatorID(default) - try: - return OperatorID[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return OperatorID(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'OperatorID': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'OperatorID': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'OperatorID': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/option.py b/pcapkit/const/mh/option.py index 7603389689..c8e2a9c133 100644 --- a/pcapkit/const/mh/option.py +++ b/pcapkit/const/mh/option.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Option'] -class Option(IntEnum): +class Option(EnumRegistry, IntEnum): """[Option] Mobility Options""" #: Pad1 [:rfc:`6275`] @@ -231,72 +233,6 @@ class Option(IntEnum): #: DLIF Link-Layer Address [:rfc:`8885`] DLIF_Link_Layer_Address = 70 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Option': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Option(key) - except ValueError: - if default == -1: - raise - return Option(default) - try: - return Option[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Option(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Option': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Option': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Option': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/packet.py b/pcapkit/const/mh/packet.py index 05b3b1e3f9..33774b00db 100644 --- a/pcapkit/const/mh/packet.py +++ b/pcapkit/const/mh/packet.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Packet'] -class Packet(IntEnum): +class Packet(EnumRegistry, IntEnum): """[Packet] Mobility Header Types - for the MH Type field in the Mobility Header""" #: Binding Refresh Request [:rfc:`6275`] @@ -90,72 +92,6 @@ class Packet(IntEnum): #: Subscription Response [:rfc:`7161`] Subscription_Response = 23 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Packet': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Packet(key) - except ValueError: - if default == -1: - raise - return Packet(default) - try: - return Packet[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Packet(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Packet': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Packet': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/qos_attribute.py b/pcapkit/const/mh/qos_attribute.py index 8fdea57b75..13ed521a8d 100644 --- a/pcapkit/const/mh/qos_attribute.py +++ b/pcapkit/const/mh/qos_attribute.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['QoSAttribute'] -class QoSAttribute(IntEnum): +class QoSAttribute(EnumRegistry, IntEnum): """[QoSAttribute] Quality-of-Service Attribute Registry""" #: Reserved [:rfc:`7222`] @@ -57,72 +59,6 @@ class QoSAttribute(IntEnum): #: Reserved [:rfc:`7222`] Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'QoSAttribute': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return QoSAttribute(key) - except ValueError: - if default == -1: - raise - return QoSAttribute(default) - try: - return QoSAttribute[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return QoSAttribute(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'QoSAttribute': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'QoSAttribute': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'QoSAttribute': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/revocation_status_code.py b/pcapkit/const/mh/revocation_status_code.py index 5d38760943..9d42d31fe6 100644 --- a/pcapkit/const/mh/revocation_status_code.py +++ b/pcapkit/const/mh/revocation_status_code.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['RevocationStatusCode'] -class RevocationStatusCode(IntEnum): +class RevocationStatusCode(EnumRegistry, IntEnum): """[RevocationStatusCode] Binding Revocation Acknowledgement Status Codes""" #: success [:rfc:`5846`] @@ -48,72 +50,6 @@ class RevocationStatusCode(IntEnum): #: Proxy Binding Revocation NOT Supported [:rfc:`5846`] Proxy_Binding_Revocation_NOT_Supported = 135 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'RevocationStatusCode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return RevocationStatusCode(key) - except ValueError: - if default == -1: - raise - return RevocationStatusCode(default) - try: - return RevocationStatusCode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return RevocationStatusCode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'RevocationStatusCode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'RevocationStatusCode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'RevocationStatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/revocation_trigger.py b/pcapkit/const/mh/revocation_trigger.py index f308753c54..71fab858d8 100644 --- a/pcapkit/const/mh/revocation_trigger.py +++ b/pcapkit/const/mh/revocation_trigger.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['RevocationTrigger'] -class RevocationTrigger(IntEnum): +class RevocationTrigger(EnumRegistry, IntEnum): """[RevocationTrigger] Revocation Trigger Values""" #: Unspecified [:rfc:`5846`] @@ -48,72 +50,6 @@ class RevocationTrigger(IntEnum): #: Revoking Mobility Node Local Policy [:rfc:`5846`] Revoking_Mobility_Node_Local_Policy = 129 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'RevocationTrigger': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return RevocationTrigger(key) - except ValueError: - if default == -1: - raise - return RevocationTrigger(default) - try: - return RevocationTrigger[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return RevocationTrigger(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'RevocationTrigger': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'RevocationTrigger': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'RevocationTrigger': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/status_code.py b/pcapkit/const/mh/status_code.py index c9971a84da..560df024b6 100644 --- a/pcapkit/const/mh/status_code.py +++ b/pcapkit/const/mh/status_code.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['StatusCode'] -class StatusCode(IntEnum): +class StatusCode(EnumRegistry, IntEnum): """[StatusCode] Status Codes""" #: Binding Update accepted/Proxy Binding Update accepted @@ -199,72 +201,6 @@ class StatusCode(IntEnum): #: CANNOT_SUPPORT_MULTIPATH_BINDING [:rfc:`8278`] CANNOT_SUPPORT_MULTIPATH_BINDING = 180 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'StatusCode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return StatusCode(key) - except ValueError: - if default == -1: - raise - return StatusCode(default) - try: - return StatusCode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return StatusCode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'StatusCode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'StatusCode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'StatusCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/traffic_selector.py b/pcapkit/const/mh/traffic_selector.py index 400e352c10..b44353ddb3 100644 --- a/pcapkit/const/mh/traffic_selector.py +++ b/pcapkit/const/mh/traffic_selector.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['TrafficSelector'] -class TrafficSelector(IntEnum): +class TrafficSelector(EnumRegistry, IntEnum): """[TrafficSelector] Traffic Selector Format""" #: Reserved [:rfc:`6089`] @@ -27,72 +29,6 @@ class TrafficSelector(IntEnum): #: IPv6 Binary Traffic Selector [:rfc:`6088`] IPv6_Binary_Traffic_Selector = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'TrafficSelector': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return TrafficSelector(key) - except ValueError: - if default == -1: - raise - return TrafficSelector(default) - try: - return TrafficSelector[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return TrafficSelector(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'TrafficSelector': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'TrafficSelector': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'TrafficSelector': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/upa_status.py b/pcapkit/const/mh/upa_status.py index d273c209af..fec56e0f80 100644 --- a/pcapkit/const/mh/upa_status.py +++ b/pcapkit/const/mh/upa_status.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['UpdateNotificationACKStatus'] -class UpdateNotificationACKStatus(IntEnum): +class UpdateNotificationACKStatus(EnumRegistry, IntEnum): """[UpdateNotificationACKStatus] Update Notification Acknowledgement Status Registry""" #: SUCCESS [:rfc:`7077`] @@ -36,72 +38,6 @@ class UpdateNotificationACKStatus(IntEnum): #: MN not attached. [:rfc:`7864`] MN_not_attached = 132 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'UpdateNotificationACKStatus': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return UpdateNotificationACKStatus(key) - except ValueError: - if default == -1: - raise - return UpdateNotificationACKStatus(default) - try: - return UpdateNotificationACKStatus[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return UpdateNotificationACKStatus(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'UpdateNotificationACKStatus': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'UpdateNotificationACKStatus': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'UpdateNotificationACKStatus': """Lookup function used when value is not found. diff --git a/pcapkit/const/mh/upn_reason.py b/pcapkit/const/mh/upn_reason.py index 9e3879fc81..b53c2acaef 100644 --- a/pcapkit/const/mh/upn_reason.py +++ b/pcapkit/const/mh/upn_reason.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['UpdateNotificationReason'] -class UpdateNotificationReason(IntEnum): +class UpdateNotificationReason(EnumRegistry, IntEnum): """[UpdateNotificationReason] Update Notification Reasons Registry""" #: Reserved [:rfc:`7077`] @@ -48,72 +50,6 @@ class UpdateNotificationReason(IntEnum): #: Reserved [:rfc:`7077`] Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'UpdateNotificationReason': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return UpdateNotificationReason(key) - except ValueError: - if default == -1: - raise - return UpdateNotificationReason(default) - try: - return UpdateNotificationReason[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return UpdateNotificationReason(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'UpdateNotificationReason': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'UpdateNotificationReason': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'UpdateNotificationReason': """Lookup function used when value is not found. diff --git a/pcapkit/const/ospf/authentication.py b/pcapkit/const/ospf/authentication.py index bf27856436..b55c3d5745 100644 --- a/pcapkit/const/ospf/authentication.py +++ b/pcapkit/const/ospf/authentication.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Authentication'] -class Authentication(IntEnum): +class Authentication(EnumRegistry, IntEnum): """[Authentication] Authentication Types""" #: No Authentication [:rfc:`1583`] @@ -30,72 +32,6 @@ class Authentication(IntEnum): #: Cryptographic Authentication with Extended Sequence Numbers [:rfc:`7474`] Cryptographic_Authentication_with_Extended_Sequence_Numbers = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Authentication': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Authentication(key) - except ValueError: - if default == -1: - raise - return Authentication(default) - try: - return Authentication[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Authentication(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Authentication': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Authentication': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Authentication': """Lookup function used when value is not found. diff --git a/pcapkit/const/ospf/packet.py b/pcapkit/const/ospf/packet.py index fc4cac226f..e6683e4b58 100644 --- a/pcapkit/const/ospf/packet.py +++ b/pcapkit/const/ospf/packet.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Packet'] -class Packet(IntEnum): +class Packet(EnumRegistry, IntEnum): """[Packet] OSPF Packet Types""" #: Reserved @@ -36,72 +38,6 @@ class Packet(IntEnum): #: Link State Ack [:rfc:`2328`] Link_State_Ack = 5 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Packet': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Packet(key) - except ValueError: - if default == -1: - raise - return Packet(default) - try: - return Packet[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Packet(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Packet': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Packet': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Packet': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/block_type.py b/pcapkit/const/pcapng/block_type.py index ae4b542de2..f922f70547 100644 --- a/pcapkit/const/pcapng/block_type.py +++ b/pcapkit/const/pcapng/block_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['BlockType'] -class BlockType(IntEnum): +class BlockType(EnumRegistry, IntEnum): """[BlockType] Block Types""" #: Reserved ??? @@ -108,72 +110,6 @@ class BlockType(IntEnum): #: Section Header Block ( Section 4.1 ) Section_Header_Block = 0x0a0d0d0a - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'BlockType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return BlockType(key) - except ValueError: - if default == -1: - raise - return BlockType(default) - try: - return BlockType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return BlockType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'BlockType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'BlockType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'BlockType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/filter_type.py b/pcapkit/const/pcapng/filter_type.py index 47aaa29bf4..9fc0fe9872 100644 --- a/pcapkit/const/pcapng/filter_type.py +++ b/pcapkit/const/pcapng/filter_type.py @@ -12,79 +12,15 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['FilterType'] -class FilterType(IntEnum): +class FilterType(EnumRegistry, IntEnum): """[FilterType] Filter Types""" - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'FilterType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return FilterType(key) - except ValueError: - if default == -1: - raise - return FilterType(default) - try: - return FilterType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return FilterType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'FilterType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'FilterType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'FilterType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/hash_algorithm.py b/pcapkit/const/pcapng/hash_algorithm.py index 2f45e2d0e5..670152ac9b 100644 --- a/pcapkit/const/pcapng/hash_algorithm.py +++ b/pcapkit/const/pcapng/hash_algorithm.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['HashAlgorithm'] -class HashAlgorithm(IntEnum): +class HashAlgorithm(EnumRegistry, IntEnum): """[HashAlgorithm] Hash Algorithms""" two_s_complement = 0 @@ -30,72 +32,6 @@ class HashAlgorithm(IntEnum): Toeplitz = 5 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'HashAlgorithm': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return HashAlgorithm(key) - except ValueError: - if default == -1: - raise - return HashAlgorithm(default) - try: - return HashAlgorithm[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return HashAlgorithm(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'HashAlgorithm': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'HashAlgorithm': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'HashAlgorithm': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/record_type.py b/pcapkit/const/pcapng/record_type.py index bb49c7091f..ab055cebaf 100644 --- a/pcapkit/const/pcapng/record_type.py +++ b/pcapkit/const/pcapng/record_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['RecordType'] -class RecordType(IntEnum): +class RecordType(EnumRegistry, IntEnum): """[RecordType] Record Types""" #: nrb_record_end @@ -27,72 +29,6 @@ class RecordType(IntEnum): #: nrb_record_ipv6 nrb_record_ipv6 = 0x0002 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'RecordType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return RecordType(key) - except ValueError: - if default == -1: - raise - return RecordType(default) - try: - return RecordType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return RecordType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'RecordType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'RecordType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'RecordType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/secrets_type.py b/pcapkit/const/pcapng/secrets_type.py index 70d72a674f..c9cb64248b 100644 --- a/pcapkit/const/pcapng/secrets_type.py +++ b/pcapkit/const/pcapng/secrets_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['SecretsType'] -class SecretsType(IntEnum): +class SecretsType(EnumRegistry, IntEnum): """[SecretsType] Secrets Types""" TLS_Key_Log = 0x544c534b @@ -26,72 +28,6 @@ class SecretsType(IntEnum): ZigBee_APS_Key = 0x5a415053 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'SecretsType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return SecretsType(key) - except ValueError: - if default == -1: - raise - return SecretsType(default) - try: - return SecretsType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return SecretsType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'SecretsType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'SecretsType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'SecretsType': """Lookup function used when value is not found. diff --git a/pcapkit/const/pcapng/verdict_type.py b/pcapkit/const/pcapng/verdict_type.py index 1bb9464f78..86b4578336 100644 --- a/pcapkit/const/pcapng/verdict_type.py +++ b/pcapkit/const/pcapng/verdict_type.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['VerdictType'] -class VerdictType(IntEnum): +class VerdictType(EnumRegistry, IntEnum): """[VerdictType] Verdict Types""" Hardware = 0 @@ -24,72 +26,6 @@ class VerdictType(IntEnum): Linux_eBPF_XDP = 2 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'VerdictType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return VerdictType(key) - except ValueError: - if default == -1: - raise - return VerdictType(default) - try: - return VerdictType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return VerdictType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'VerdictType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'VerdictType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'VerdictType': """Lookup function used when value is not found. diff --git a/pcapkit/const/reg/ethertype.py b/pcapkit/const/reg/ethertype.py index f78630427a..1e81d4e706 100644 --- a/pcapkit/const/reg/ethertype.py +++ b/pcapkit/const/reg/ethertype.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['EtherType'] -class EtherType(IntEnum): +class EtherType(EnumRegistry, IntEnum): """[EtherType] Ethertype IEEE 802 Numbers""" #: Formerly XEROX PUP. Invalid as an Ethertype since 1983. Use 0x0A00. [Boggs, @@ -516,72 +518,6 @@ class EtherType(IntEnum): #: Reserved [:rfc:`1701`] Reserved = 0xFFFF - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'EtherType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return EtherType(key) - except ValueError: - if default == -1: - raise - return EtherType(default) - try: - return EtherType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return EtherType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'EtherType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'EtherType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'EtherType': """Lookup function used when value is not found. diff --git a/pcapkit/const/reg/linktype.py b/pcapkit/const/reg/linktype.py index 52d7fc0565..833460bf4c 100644 --- a/pcapkit/const/reg/linktype.py +++ b/pcapkit/const/reg/linktype.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['LinkType'] -class LinkType(IntEnum): +class LinkType(EnumRegistry, IntEnum): """[LinkType] Link-Layer Header Type Values""" #: [``DLT_NULL``] BSD loopback encapsulation. @@ -750,72 +752,6 @@ class LinkType(IntEnum): #: [``DLT_IPMB_LINUX``] Legacy names (do not use) for Linux I2C below. IPMB_LINUX = 209 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'LinkType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return LinkType(key) - except ValueError: - if default == -1: - raise - return LinkType(default) - try: - return LinkType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return LinkType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'LinkType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'LinkType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'LinkType': """Lookup function used when value is not found. diff --git a/pcapkit/const/reg/transtype.py b/pcapkit/const/reg/transtype.py index cb8af75827..bf1bacd9ca 100644 --- a/pcapkit/const/reg/transtype.py +++ b/pcapkit/const/reg/transtype.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['TransType'] -class TransType(IntEnum): +class TransType(EnumRegistry, IntEnum): """[TransType] Transport Layer Protocol Numbers""" #: IPv6 Hop-by-Hop Option [:rfc:`8200`] @@ -494,72 +496,6 @@ class TransType(IntEnum): #: [Internet Assigned Numbers Authority] Reserved_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'TransType': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return TransType(key) - except ValueError: - if default == -1: - raise - return TransType(default) - try: - return TransType[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return TransType(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'TransType': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'TransType': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'TransType': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/cause_code.py b/pcapkit/const/sctp/cause_code.py index 458de847c8..dc6f09356c 100644 --- a/pcapkit/const/sctp/cause_code.py +++ b/pcapkit/const/sctp/cause_code.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['CauseCode'] -class CauseCode(IntEnum): +class CauseCode(EnumRegistry, IntEnum): """[CauseCode] SCTP Error Cause Codes""" #: Invalid Stream Identifier [:rfc:`9260`] @@ -91,72 +93,6 @@ class CauseCode(IntEnum): #: Unsupported HMAC Identifier [:rfc:`4895`] Unsupported_HMAC_Identifier = 261 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'CauseCode': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return CauseCode(key) - except ValueError: - if default == -1: - raise - return CauseCode(default) - try: - return CauseCode[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return CauseCode(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'CauseCode': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'CauseCode': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'CauseCode': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/chunk.py b/pcapkit/const/sctp/chunk.py index 4ca60d2226..10f964f3a9 100644 --- a/pcapkit/const/sctp/chunk.py +++ b/pcapkit/const/sctp/chunk.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Chunk'] -class Chunk(IntEnum): +class Chunk(EnumRegistry, IntEnum): """[Chunk] SCTP Chunk Types""" #: Payload Data (DATA) [:rfc:`9260`] @@ -109,72 +111,6 @@ class Chunk(IntEnum): #: Reserved for IETF-defined Chunk Extensions [:rfc:`9260`] Reserved_for_IETF_defined_Chunk_Extensions_255 = 255 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Chunk': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Chunk(key) - except ValueError: - if default == -1: - raise - return Chunk(default) - try: - return Chunk[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Chunk(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Chunk': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Chunk': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Chunk': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/parameter.py b/pcapkit/const/sctp/parameter.py index 5a6b2bfcc2..f78d63f57f 100644 --- a/pcapkit/const/sctp/parameter.py +++ b/pcapkit/const/sctp/parameter.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Parameter'] -class Parameter(IntEnum): +class Parameter(EnumRegistry, IntEnum): """[Parameter] SCTP Chunk Parameter Types""" #: Heartbeat Info [:rfc:`9260`] @@ -115,72 +117,6 @@ class Parameter(IntEnum): #: Reserved for IETF-defined Chunk Extensions [:rfc:`9260`] Reserved_for_IETF_defined_Chunk_Extensions = 65535 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Parameter': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Parameter(key) - except ValueError: - if default == -1: - raise - return Parameter(default) - try: - return Parameter[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Parameter(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Parameter': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Parameter': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Parameter': """Lookup function used when value is not found. diff --git a/pcapkit/const/sctp/payload_protocol_identifier.py b/pcapkit/const/sctp/payload_protocol_identifier.py index 69a7853025..016a326ad4 100644 --- a/pcapkit/const/sctp/payload_protocol_identifier.py +++ b/pcapkit/const/sctp/payload_protocol_identifier.py @@ -10,12 +10,14 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['PayloadProtocolIdentifier'] -class PayloadProtocolIdentifier(IntEnum): +class PayloadProtocolIdentifier(EnumRegistry, IntEnum): """[PayloadProtocolIdentifier] SCTP Payload Protocol Identifiers""" #: Reserved by SCTP [:rfc:`9260`] @@ -264,72 +266,6 @@ class PayloadProtocolIdentifier(IntEnum): #: chunk-01] DTLS_Chunk_Key_Management_Messages = 4242 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'PayloadProtocolIdentifier': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return PayloadProtocolIdentifier(key) - except ValueError: - if default == -1: - raise - return PayloadProtocolIdentifier(default) - try: - return PayloadProtocolIdentifier[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return PayloadProtocolIdentifier(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'PayloadProtocolIdentifier': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'PayloadProtocolIdentifier': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'PayloadProtocolIdentifier': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/checksum.py b/pcapkit/const/tcp/checksum.py index d3ae2c3400..9050fdbd62 100644 --- a/pcapkit/const/tcp/checksum.py +++ b/pcapkit/const/tcp/checksum.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Checksum'] -class Checksum(IntEnum): +class Checksum(EnumRegistry, IntEnum): """[Checksum] TCP Checksum [:rfc:`1146`]""" TCP_checksum = 0 @@ -26,72 +28,6 @@ class Checksum(IntEnum): Redundant_Checksum_Avoidance = 3 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Checksum': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Checksum(key) - except ValueError: - if default == -1: - raise - return Checksum(default) - try: - return Checksum[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Checksum(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Checksum': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Checksum': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Checksum': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/flags.py b/pcapkit/const/tcp/flags.py index 7419ceea97..80a0957539 100644 --- a/pcapkit/const/tcp/flags.py +++ b/pcapkit/const/tcp/flags.py @@ -12,7 +12,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['Flags'] diff --git a/pcapkit/const/tcp/mp_tcp_option.py b/pcapkit/const/tcp/mp_tcp_option.py index 43748e7a4d..038a5364d3 100644 --- a/pcapkit/const/tcp/mp_tcp_option.py +++ b/pcapkit/const/tcp/mp_tcp_option.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['MPTCPOption'] -class MPTCPOption(IntEnum): +class MPTCPOption(EnumRegistry, IntEnum): """[MPTCPOption] Multipath TCP options [:rfc:`6824`]""" #: Multipath Capable [:rfc:`8684#3.1`] @@ -48,72 +50,6 @@ class MPTCPOption(IntEnum): #: [:rfc:`8684`] Reserved_for_Private_Use = 0xf - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'MPTCPOption': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return MPTCPOption(key) - except ValueError: - if default == -1: - raise - return MPTCPOption(default) - try: - return MPTCPOption[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return MPTCPOption(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'MPTCPOption': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'MPTCPOption': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'MPTCPOption': """Lookup function used when value is not found. diff --git a/pcapkit/const/tcp/option.py b/pcapkit/const/tcp/option.py index 14927a7672..3c76b1fa7d 100644 --- a/pcapkit/const/tcp/option.py +++ b/pcapkit/const/tcp/option.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['Option'] -class Option(IntEnum): +class Option(EnumRegistry, IntEnum): """[Option] TCP Option Kind Numbers""" #: End of Option List [:rfc:`9293`] @@ -155,72 +157,6 @@ class Option(IntEnum): #: products) [:rfc:`4727`] RFC3692_style_Experiment_2 = 254 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'Option': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return Option(key) - except ValueError: - if default == -1: - raise - return Option(default) - try: - return Option[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return Option(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'Option': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'Option': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'Option': """Lookup function used when value is not found. diff --git a/pcapkit/const/vlan/priority_level.py b/pcapkit/const/vlan/priority_level.py index a13ada4025..3e151c41f4 100644 --- a/pcapkit/const/vlan/priority_level.py +++ b/pcapkit/const/vlan/priority_level.py @@ -12,10 +12,12 @@ from aenum import IntEnum, extend_enum +from pcapkit.corekit.enum import EnumRegistry + __all__ = ['PriorityLevel'] -class PriorityLevel(IntEnum): +class PriorityLevel(EnumRegistry, IntEnum): """[PriorityLevel] Priority levels defined in IEEE 802.1p""" #: Background (lowest) @@ -42,72 +44,6 @@ class PriorityLevel(IntEnum): #: Network control (highest) NC = 0b111 - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> 'PriorityLevel': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return PriorityLevel(key) - except ValueError: - if default == -1: - raise - return PriorityLevel(default) - try: - return PriorityLevel[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return PriorityLevel(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> 'PriorityLevel': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> 'PriorityLevel': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> 'PriorityLevel': """Lookup function used when value is not found. diff --git a/pcapkit/corekit/__init__.py b/pcapkit/corekit/__init__.py index e8c4bb177f..e05bed5f39 100644 --- a/pcapkit/corekit/__init__.py +++ b/pcapkit/corekit/__init__.py @@ -17,7 +17,7 @@ class :class:`~pcapkit.corekit.infoclass.Info`, """ from pcapkit.corekit.io import SeekableReader -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry from pcapkit.corekit.fields import * from pcapkit.corekit.infoclass import Info, info_final from pcapkit.corekit.module import ModuleDescriptor diff --git a/pcapkit/corekit/enums.py b/pcapkit/corekit/enum.py similarity index 99% rename from pcapkit/corekit/enums.py rename to pcapkit/corekit/enum.py index ceb500e92b..b1c68ebacf 100644 --- a/pcapkit/corekit/enums.py +++ b/pcapkit/corekit/enum.py @@ -2,9 +2,9 @@ """Constant Enumeration Base ============================== -.. module:: pcapkit.corekit.enums +.. module:: pcapkit.corekit.enum -:mod:`pcapkit.corekit.enums` contains :class:`~pcapkit.corekit.enums.EnumRegistry` +:mod:`pcapkit.corekit.enum` contains :class:`~pcapkit.corekit.enum.EnumRegistry` only, the base class every constant enumeration under :mod:`pcapkit.const` is to inherit the registry protocol from. diff --git a/pcapkit/vendor/default.py b/pcapkit/vendor/default.py index b7eecaa9b1..c908d058da 100644 --- a/pcapkit/vendor/default.py +++ b/pcapkit/vendor/default.py @@ -63,82 +63,18 @@ """ -from aenum import IntEnum, extend_enum +from aenum import IntEnum{', extend_enum' if 'extend_enum(' in MISS else ''} + +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] -class {NAME}(IntEnum): +class {NAME}(EnumRegistry, IntEnum): """[{NAME}] {DOCS}""" {ENUM} - @staticmethod - def get(key: 'int | str', default: 'int' = -1) -> '{NAME}': - """Backport support for original codes. - - Args: - key: Key to get enum item. - default: Default value if not found. The placeholder ``-1`` stands - for *no default*, in which case an unresolvable key propagates - the lookup error instead of falling back. - - :meta private: - """ - if isinstance(key, int): - try: - return {NAME}(key) - except ValueError: - if default == -1: - raise - return {NAME}(default) - try: - return {NAME}[key] # type: ignore[misc] - except KeyError: - if default == -1: - raise - return {NAME}(default) - - @classmethod - def register(cls, value: 'int', name: 'str') -> '{NAME}': - """Explicitly register a new member. - - Unlike :meth:`get` and :meth:`_missing_`, which resolve a key or a - value without minting anything new, this is the caller-named entry - point that still grows the registry, via :func:`aenum.extend_enum`. - - Args: - value: Value of the new member. - name: Name of the new member. - - Returns: - The newly registered member. - - """ - return extend_enum(cls, name, value) - - @classmethod - def _unregistered_member(cls, value: 'int', name: 'str') -> '{NAME}': - """Build a member absent from this registry's own lookup tables. - - Used by :meth:`_missing_` for a bounded-but-unassigned value it - resolves without anyone asking for a name, so that such a lookup no - longer grows the registry -- contrast :meth:`register`, the explicit - path that still does. - - Args: - value: The member's value. - name: The member's name. - - Returns: - The unregistered member. - - """ - obj = int.__new__(cls, value) - obj._name_ = name # pylint: disable=protected-access - obj._value_ = value # pylint: disable=protected-access - return obj - @classmethod def _missing_(cls, value: 'int') -> '{NAME}': """Lookup function used when value is not found. diff --git a/pcapkit/vendor/ipv6/extension_header.py b/pcapkit/vendor/ipv6/extension_header.py index b93cd9e0cf..fe4246fb54 100644 --- a/pcapkit/vendor/ipv6/extension_header.py +++ b/pcapkit/vendor/ipv6/extension_header.py @@ -38,7 +38,7 @@ from aenum import IntEnum -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] diff --git a/pcapkit/vendor/mh/binding_ack_flag.py b/pcapkit/vendor/mh/binding_ack_flag.py index 3010b38e22..d1df9c50f3 100644 --- a/pcapkit/vendor/mh/binding_ack_flag.py +++ b/pcapkit/vendor/mh/binding_ack_flag.py @@ -37,7 +37,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] diff --git a/pcapkit/vendor/mh/binding_update_flag.py b/pcapkit/vendor/mh/binding_update_flag.py index 790a222305..55ceec0894 100644 --- a/pcapkit/vendor/mh/binding_update_flag.py +++ b/pcapkit/vendor/mh/binding_update_flag.py @@ -36,7 +36,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] diff --git a/pcapkit/vendor/mh/handover_ack_flag.py b/pcapkit/vendor/mh/handover_ack_flag.py index 7e84c00832..a64ddee533 100644 --- a/pcapkit/vendor/mh/handover_ack_flag.py +++ b/pcapkit/vendor/mh/handover_ack_flag.py @@ -37,7 +37,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] diff --git a/pcapkit/vendor/mh/handover_initiate_flag.py b/pcapkit/vendor/mh/handover_initiate_flag.py index b7fecc6d6d..30659b6973 100644 --- a/pcapkit/vendor/mh/handover_initiate_flag.py +++ b/pcapkit/vendor/mh/handover_initiate_flag.py @@ -37,7 +37,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] diff --git a/pcapkit/vendor/tcp/flags.py b/pcapkit/vendor/tcp/flags.py index 5228207711..78ee305e6c 100644 --- a/pcapkit/vendor/tcp/flags.py +++ b/pcapkit/vendor/tcp/flags.py @@ -49,7 +49,7 @@ from aenum import IntFlag -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry __all__ = ['{NAME}'] diff --git a/tests/const/test_const_enum_builtin_parity.py b/tests/const/test_const_enum_builtin_parity.py index acf0dd03d8..ccce746d7b 100644 --- a/tests/const/test_const_enum_builtin_parity.py +++ b/tests/const/test_const_enum_builtin_parity.py @@ -1050,7 +1050,7 @@ def test_the_tcp_flags_template_renders_the_committed_module(self) -> None: after the enumeration block is a ``classmethod``, because GitHub issue #775's tier 2 found ``tcp/flags``' own exclusion rationale did not hold -- see ``tests.const.test_const_registry_protocol`` -- and converted it - onto :class:`~pcapkit.corekit.enums.EnumRegistry` alongside the four + onto :class:`~pcapkit.corekit.enum.EnumRegistry` alongside the four ``mh/*_flag`` templates. The generated ``@staticmethod get`` that used to close the block is gone, leaving ``_missing_`` next, the same rewrite ``test_const_enum_lookup.py`` made for those four. diff --git a/tests/const/test_const_enum_get.py b/tests/const/test_const_enum_get.py index e3be99c403..99db232b20 100644 --- a/tests/const/test_const_enum_get.py +++ b/tests/const/test_const_enum_get.py @@ -54,7 +54,6 @@ import importlib.util import inspect import pkgutil -import re import unittest from typing import TYPE_CHECKING @@ -287,10 +286,22 @@ def test_the_always_resolving_registries_have_nothing_to_fall_back_to(self) -> N def test_the_vendor_template_still_emits_the_fix(self) -> None: """A regeneration must not undo the fix. - :mod:`pcapkit.const` is generated from :mod:`pcapkit.vendor`, so the - committed tree passing is not evidence that the template agrees with it. - Renders the shared template and compares its ``get()`` block, character - for character, against the module generated from it. + GitHub issue #775's tier 3 moved #584's fix off the per-file generated + ``get()`` entirely: it now lives permanently in + :meth:`~pcapkit.corekit.enum.EnumRegistry.get`, which every one of the + 105 default-template registries *inherits* rather than copies, so + there is no longer a ``get()`` block here for a regeneration to drop + the fix from -- the block this test used to extract and compare no + longer exists in either the template's output or the generated + module, in either case for the same reason. + + What a regeneration could still undo is the inheritance itself, or + bring back a local copy of ``get``, ``register`` or + ``_unregistered_member`` that would shadow the base and silently + resurrect the pre-#775 two-contract bug -- this pins their absence, + and the presence of the mix-in, on both the freshly rendered template + (with synthetic content, as the pre-#775 version of this test did for + ``get()``) and the real, committed module. """ import pathlib @@ -304,14 +315,19 @@ def test_the_vendor_template_still_emits_the_fix(self) -> None: import pcapkit.const.arp.hardware as generated_module generated = pathlib.Path(generated_module.__file__).read_text(encoding='utf-8') - block = re.compile(r' @staticmethod\n def get\(.*?\n(?= @)', re.S) - from_template = block.search(rendered) - from_generated = block.search(generated) - - self.assertIsNotNone(from_template, 'the template rendered no get() block') - self.assertIsNotNone(from_generated, 'the generated module has no get() block') - self.assertIn('except ValueError:', from_template.group(0)) - self.assertEqual(from_template.group(0), from_generated.group(0)) + removed_signatures = ( + "def get(key: 'int | str'", + "def register(cls, value: 'int', name: 'str')", + 'def _unregistered_member(', + ) + for label, rendering in (('template', rendered), ('generated module', generated)): + with self.subTest(rendering=label): + self.assertIn('from pcapkit.corekit.enum import EnumRegistry', rendering) + self.assertIn('class Hardware(EnumRegistry, IntEnum):', rendering) + for signature in removed_signatures: + self.assertNotIn(signature, rendering, + f'{label} still carries a local {signature!r}, ' + f'shadowing EnumRegistry') if __name__ == '__main__': diff --git a/tests/const/test_const_enum_lookup.py b/tests/const/test_const_enum_lookup.py index d533f751f0..5068b6fd2f 100644 --- a/tests/const/test_const_enum_lookup.py +++ b/tests/const/test_const_enum_lookup.py @@ -403,7 +403,7 @@ def test_the_vendor_templates_still_emit_the_fix(self) -> None: # NOTE: the class statement names EnumRegistry and the first method after # the enumeration block is a classmethod, because #775's tier 2 moved # get/get_all/register/register_alias off these templates and onto - # pcapkit.corekit.enums.EnumRegistry -- the generated `@staticmethod get` + # pcapkit.corekit.enum.EnumRegistry -- the generated `@staticmethod get` # that used to close the block is gone, leaving `_missing_` next. block = re.compile( r'class \w+\(EnumRegistry, IntFlag\):\n """.*?"""\n\n (.*?)\n\n @classmethod', diff --git a/tests/const/test_const_registry_protocol.py b/tests/const/test_const_registry_protocol.py index 71711112e5..f02090d22b 100644 --- a/tests/const/test_const_registry_protocol.py +++ b/tests/const/test_const_registry_protocol.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -"""Tests for :class:`pcapkit.corekit.enums.EnumRegistry`, tier 2 of issue #775. +"""Tests for :class:`pcapkit.corekit.enum.EnumRegistry`, tier 2 of issue #775. Tier 1 (#838) removed the mint from the two sites in :data:`pcapkit.vendor.default.LINE` that the 105 default-template registries @@ -13,7 +13,7 @@ necessary overrides and dispatching logic; AppType subclasses will have their necessary overrides again pertaining their different contracts."* -:class:`~pcapkit.corekit.enums.EnumRegistry` is tier one of that hierarchy. This +:class:`~pcapkit.corekit.enum.EnumRegistry` is tier one of that hierarchy. This module pins both halves of the claim: that the generated registries in this batch really do inherit the protocol rather than carry a copy of it, and that each of the four methods honours the contract the maintainer wrote for it. @@ -28,7 +28,7 @@ from aenum import IntEnum, IntFlag, StrEnum -from pcapkit.corekit.enums import EnumRegistry +from pcapkit.corekit.enum import EnumRegistry from tests._support import ISOLATED_PREFIXES, purge_modules, restore_modules, snapshot_modules if TYPE_CHECKING: @@ -37,7 +37,7 @@ #: Repository root, for reading generated sources as text. REPO_ROOT = pathlib.Path(__file__).resolve().parents[2] -#: The batch converted onto :class:`~pcapkit.corekit.enums.EnumRegistry`: the +#: The batch converted onto :class:`~pcapkit.corekit.enum.EnumRegistry`: the #: six bespoke-template registries whose members carry no extra attributes, so #: the shared ``_unregistered_member`` -- which sets only ``_name_`` and #: ``_value_`` -- builds a complete member for them. ``tcp/flags`` joined the @@ -50,7 +50,7 @@ #: ``_missing_`` still does its own 16-bit range check ending in #: ``super()._missing_(value)``, unchanged by the conversion: that call chain #: resolves through :mod:`aenum`'s own :class:`~aenum.Flag` machinery either -#: way, since :class:`~pcapkit.corekit.enums.EnumRegistry` never defines +#: way, since :class:`~pcapkit.corekit.enum.EnumRegistry` never defines #: ``_missing_`` itself. The remaining five bespoke templates (``ftp/command``, #: ``ftp/return_code``, ``http/method``, ``http/status_code``, #: ``pcapng/option_type``) do each define a custom ``__new__`` attaching further @@ -122,7 +122,7 @@ def test_converted_const_modules_declare_the_base(self) -> None: for _, name, relpath in CONVERTED: with self.subTest(registry=name): source = (REPO_ROOT / relpath).read_text() - self.assertIn('from pcapkit.corekit.enums import EnumRegistry', source) + self.assertIn('from pcapkit.corekit.enum import EnumRegistry', source) self.assertIn(f'class {name}(EnumRegistry, ', source) def test_converted_const_modules_carry_no_copy_of_the_protocol(self) -> None: @@ -138,7 +138,7 @@ def test_converted_crawlers_no_longer_spell_their_own_get(self) -> None: for relpath in CONVERTED_VENDORS: with self.subTest(vendor=relpath): source = (REPO_ROOT / relpath).read_text() - self.assertIn('from pcapkit.corekit.enums import EnumRegistry', source) + self.assertIn('from pcapkit.corekit.enum import EnumRegistry', source) self.assertNotIn("def get(key: 'int | str'", source) @@ -162,7 +162,7 @@ def setUpClass(cls) -> None: # entry from ``sys.modules``, so the freshly imported registries inherit # a *new* EnumRegistry class object -- identity against the outer one # would fail for a reason that says nothing about the product. - cls.base = importlib.import_module('pcapkit.corekit.enums').EnumRegistry + cls.base = importlib.import_module('pcapkit.corekit.enum').EnumRegistry cls.addClassCleanup(restore_modules, snapshot, ISOLATED_PREFIXES) def test_base_class_is_in_the_mro(self) -> None: @@ -260,7 +260,7 @@ def test_tcp_flags_now_inherits_the_base(self) -> None: """Fails on ``02296b5dd``, where ``Flags`` is a plain ``IntFlag``. Resolves ``EnumRegistry`` freshly from the just-reimported - ``pcapkit.corekit.enums`` rather than the module-scope import above -- + ``pcapkit.corekit.enum`` rather than the module-scope import above -- ``setUp`` purged ``pcapkit`` from ``sys.modules``, so ``Flags`` now inherits a *new* ``EnumRegistry`` class object, and identity against the stale outer one would fail for a reason that says nothing about @@ -269,12 +269,12 @@ def test_tcp_flags_now_inherits_the_base(self) -> None: """ from pcapkit.const.tcp.flags import Flags - base = importlib.import_module('pcapkit.corekit.enums').EnumRegistry + base = importlib.import_module('pcapkit.corekit.enum').EnumRegistry self.assertIn(base, Flags.__mro__) def test_missing_resolves_identically_to_the_pre_conversion_shape(self) -> None: """The conversion changes *what the class inherits*, not - ``_missing_``'s own logic -- :class:`~pcapkit.corekit.enums.EnumRegistry` + ``_missing_``'s own logic -- :class:`~pcapkit.corekit.enum.EnumRegistry` never defines ``_missing_`` itself, so ``super()._missing_(value)`` inside ``Flags._missing_`` resolves through :mod:`aenum`'s :class:`~aenum.Flag` machinery exactly as it did when ``Flags`` @@ -498,7 +498,7 @@ def test_register_over_a_taken_value_raises_value_error_and_does_not_alias(self) docstring contract that ``register`` is what mints and nothing else does so silently. Reproduced against this exact shape on ``pcapkit.const.reg.apptype.apptype.TransportProtocol`` (a different, - non-:class:`~pcapkit.corekit.enums.EnumRegistry` registry, since + non-:class:`~pcapkit.corekit.enum.EnumRegistry` registry, since that one is not gated the same way) before this guard existed: ``TransportProtocol.register(6, 'TOTALLY_NEW_NAME')`` returned ``TransportProtocol.tcp`` unchanged and minted nothing. This pins the @@ -529,7 +529,7 @@ def test_register_over_a_taken_value_raises_value_error_and_does_not_alias(self) def test_register_over_a_taken_value_on_a_flag_registry_also_refuses(self) -> None: """The same guard, on an :class:`~aenum.IntFlag` registry: value collision is checked the same way regardless of member type, since - both share :meth:`~pcapkit.corekit.enums.EnumRegistry._extend`.""" + both share :meth:`~pcapkit.corekit.enum.EnumRegistry._extend`.""" from pcapkit.const.mh.binding_ack_flag import BindingACKFlag target = list(BindingACKFlag)[0] @@ -544,14 +544,14 @@ def test_register_over_a_taken_value_on_a_flag_registry_also_refuses(self) -> No def test_register_alias_still_aliases_after_the_value_guard(self) -> None: """:meth:`register_alias` depends on :meth:`register` accepting an already-registered value -- that dependency moved to the shared, - ungated :meth:`~pcapkit.corekit.enums.EnumRegistry._extend` when this + ungated :meth:`~pcapkit.corekit.enum.EnumRegistry._extend` when this guard was added, so this pins that the move did not also gate the path :meth:`register_alias` needs. A naive guard placed directly in the body :meth:`register` calls would make every alias registration raise the exact error this test's sibling above checks for, rather than aliasing. Deliberately on ``ExtensionHeader`` rather than ``Flags``: this registry already inherited - :class:`~pcapkit.corekit.enums.EnumRegistry` before this change, so + :class:`~pcapkit.corekit.enum.EnumRegistry` before this change, so this is a regression pin on the internal refactor and holds on both the prior head and this one -- unlike this class's ``Flags``-based siblings above, which pin the guard itself and so only hold once @@ -674,5 +674,382 @@ class _Str(EnumRegistry, StrEnum): self.assertNotIn('unit_test_absent', _Str.__members__) +#: Sample of the 105 default-template registries GitHub issue #775's tier 3 +#: moved onto :class:`~pcapkit.corekit.enum.EnumRegistry`, chosen to cover +#: the two shapes the census turned up in ``pcapkit/vendor/default.py``'s +#: ``LINE`` template: a registry whose own ``_missing_`` mints directly via +#: :func:`~aenum.extend_enum` for an unassigned value (``TransType``, +#: ``LinkType``, ``RecordType``, ``Parameter`` -- the last two from crawlers +#: that override ``process()`` to supply that ``_missing_`` body themselves), +#: and one that resolves a bounded-but-unassigned value through the +#: inherited ``_unregistered_member`` instead (``Hardware``). The template's +#: own ``from aenum import IntEnum{, extend_enum}`` import line depends on +#: which shape a given registry's crawler produces, so the sample deliberately +#: covers both. +GENERATED_SAMPLE = ( + ('pcapkit.const.reg.transtype', 'TransType', 'pcapkit/const/reg/transtype.py'), + ('pcapkit.const.reg.linktype', 'LinkType', 'pcapkit/const/reg/linktype.py'), + ('pcapkit.const.arp.hardware', 'Hardware', 'pcapkit/const/arp/hardware.py'), + ('pcapkit.const.pcapng.record_type', 'RecordType', 'pcapkit/const/pcapng/record_type.py'), + ('pcapkit.const.hip.parameter', 'Parameter', 'pcapkit/const/hip/parameter.py'), +) + +#: Const modules tier 3 deliberately leaves alone. The first six override +#: their crawler's own ``process()``/``context()`` with a bespoke, +#: mint-on-lookup ``get()``/``_missing_`` pair that does not share +#: :mod:`pcapkit.vendor.default`'s template at all -- converting any of them +#: onto :class:`~pcapkit.corekit.enum.EnumRegistry` would silently change +#: behaviour rather than just move it, per the base ``get()``'s own +#: str-key-never-tries-the-value-path limitation measured in +#: :class:`StrEnumValuePathLimitationTests` below. The remaining four +#: (``reg/apptype``'s transport subclasses) plus ``AppType``/ +#: ``TransportProtocol`` themselves are excluded for the separate reason +#: :mod:`pcapkit.corekit.enum`'s own module docstring gives: they are tier 2 +#: of GitHub issue #842's three-tier hierarchy, not tier 3, and stay as they +#: are until tier 2 lands. +EXCLUDED_STILL_BESPOKE = frozenset({ + 'pcapkit/const/ftp/command.py', + 'pcapkit/const/ftp/return_code.py', + 'pcapkit/const/http/method.py', + 'pcapkit/const/http/status_code.py', + 'pcapkit/const/pcapng/option_type.py', + 'pcapkit/const/reg/apptype/apptype.py', + 'pcapkit/const/reg/apptype/dccp.py', + 'pcapkit/const/reg/apptype/sctp.py', + 'pcapkit/const/reg/apptype/tcp.py', + 'pcapkit/const/reg/apptype/udp.py', +}) + + +class GeneratedTemplateSourceInheritsTests(unittest.TestCase): + """The 105 default-template registries must inherit the protocol too, + not just the six bespoke ones :class:`GeneratedSourceInheritsTests` + above (tier 2) already covers.""" + + def test_generated_sample_declares_the_base(self) -> None: + for _, name, relpath in GENERATED_SAMPLE: + with self.subTest(registry=name): + source = (REPO_ROOT / relpath).read_text() + self.assertIn('from pcapkit.corekit.enum import EnumRegistry', source) + self.assertIn(f'class {name}(EnumRegistry, IntEnum):', source) + + def test_generated_sample_carries_no_copy_of_the_protocol(self) -> None: + """A ``def get``/``def register``/``def _unregistered_member`` left + behind would silently shadow the base -- the exact two-contract bug + this tier exists to remove. ``_missing_`` must survive, since it + carries each registry's own bounded ranges and was never meant to + move.""" + for _, name, relpath in GENERATED_SAMPLE: + source = (REPO_ROOT / relpath).read_text() + for method in PROTOCOL: + with self.subTest(registry=name, method=method): + self.assertNotIn(f'def {method}(', source) + with self.subTest(registry=name, method='_missing_'): + self.assertIn("def _missing_(cls, value: 'int')", source) + + def test_every_generated_const_module_is_accounted_for(self) -> None: + """The full census, measured on this batch rather than assumed: 121 + modules under :mod:`pcapkit.const`, splitting exactly three ways -- + the 6 :data:`CONVERTED` bespoke ones tier 2 already handled, the 10 + :data:`EXCLUDED_STILL_BESPOKE` deliberately left alone, and the + remaining 105 this tier converts. Measured by me on the prior head + (commit ``05468a06b``, this batch's own base): of those 121, 6 carried + :class:`~pcapkit.corekit.enum.EnumRegistry` and 111 carried the + literal "Backport support for original codes." docstring (including + the 6 bespoke StrEnum/AppType files, since a hand-copied docstring is + not proof of a shared template) -- 105 is what is left once the 6 + converted and the 10 excluded are both taken out. + """ + const_root = REPO_ROOT / 'pcapkit' / 'const' + all_files = sorted( + path.relative_to(REPO_ROOT).as_posix() + for path in const_root.rglob('*.py') + if path.name != '__init__.py' + ) + converted_relpaths = {relpath for _, _, relpath in CONVERTED} + generated = [path for path in all_files + if path not in converted_relpaths and path not in EXCLUDED_STILL_BESPOKE] + + self.assertEqual(len(all_files), 121) + self.assertEqual(len(generated), 105) + + for relpath in generated: + with self.subTest(module=relpath): + source = (REPO_ROOT / relpath).read_text() + self.assertIn('from pcapkit.corekit.enum import EnumRegistry', source) + self.assertRegex(source, r'class \w+\(EnumRegistry, IntEnum\):') + for method in PROTOCOL: + self.assertNotIn(f'def {method}(', source) + + +class GeneratedProtocolIsInheritedTests(unittest.TestCase): + """The generated sample's protocol methods must resolve to the base + class, exactly as :class:`ProtocolIsInheritedTests` above pins for the + six bespoke ones.""" + + if TYPE_CHECKING: + registries: 'list[Any]' + base: 'Any' + + @classmethod + def setUpClass(cls) -> None: + snapshot = snapshot_modules(ISOLATED_PREFIXES) + purge_modules(['pcapkit']) + cls.registries = [ + getattr(importlib.import_module(module_name), class_name) + for module_name, class_name, _ in GENERATED_SAMPLE + ] + cls.base = importlib.import_module('pcapkit.corekit.enum').EnumRegistry + cls.addClassCleanup(restore_modules, snapshot, ISOLATED_PREFIXES) + + def test_base_class_is_in_the_mro(self) -> None: + for registry in self.registries: + with self.subTest(registry=registry.__qualname__): + self.assertIn(self.base, registry.__mro__) + + def test_every_method_resolves_to_the_base_implementation(self) -> None: + for registry in self.registries: + for method in PROTOCOL: + with self.subTest(registry=registry.__qualname__, method=method): + self.assertEqual(getattr(registry, method).__func__, + getattr(self.base, method).__func__) + + def test_member_type_stays_int_not_enumregistry(self) -> None: + """The base-class ordering rule #855 established: + ``class {NAME}(EnumRegistry, IntEnum)`` with the non-``Enum`` mixin + first is what keeps :mod:`aenum` resolving ``_member_type_`` from + ``IntEnum`` rather than from the mix-in -- verified rather than + assumed, per this tier's own instructions.""" + for registry in self.registries: + with self.subTest(registry=registry.__qualname__): + self.assertIs(registry._member_type_, int) + + +class GeneratedMissingRangeParityTests(unittest.TestCase): + """``_missing_``'s own bounded ranges must behave identically after the + conversion -- it was deliberately left untouched, so this is a + regression pin on that claim rather than a test of new behaviour.""" + + def setUp(self) -> None: + snapshot = snapshot_modules(ISOLATED_PREFIXES) + purge_modules(['pcapkit']) + self.addCleanup(restore_modules, snapshot, ISOLATED_PREFIXES) + + def test_hardware_still_bounds_and_resolves_its_unassigned_ranges(self) -> None: + """:class:`~pcapkit.const.arp.hardware.Hardware` declares ``0..38`` + and ``39..255``/``258..65534`` as unassigned-but-in-range, ``65535`` + as its own reserved member, and rejects anything else -- unchanged by + this tier, which only removed the duplicate ``get``/``register``/ + ``_unregistered_member`` sitting next to it.""" + from pcapkit.const.arp.hardware import Hardware + + # In range, unassigned: resolves without minting a permanent member + # (tier 1's fix), reachable via the now-inherited get() too. + for value in (39, 255, 258, 65534): + with self.subTest(value=value): + self.assertEqual(Hardware(value).value, value) + self.assertEqual(Hardware(value).name, 'Unassigned') + self.assertNotIn(value, Hardware._value2member_map_) + self.assertEqual(Hardware.get(value).value, value) + + # Out of the declared 0..65535 range: still raises, with or without + # a default -- the default is only consulted once _missing_ itself + # has already failed to resolve the key. + with self.assertRaises(ValueError): + Hardware(65536) + with self.assertRaises(ValueError): + Hardware.get(65536) + self.assertIs(Hardware.get(65536, 1), Hardware.Ethernet) + + def test_transtype_still_mints_its_declared_unassigned_range(self) -> None: + """:class:`~pcapkit.const.reg.transtype.TransType` is the other + shape: its own ``_missing_`` mints permanently via + :func:`~aenum.extend_enum` for ``148..252`` rather than going through + ``_unregistered_member`` -- also unchanged by this tier.""" + from pcapkit.const.reg.transtype import TransType + + before = len(TransType._member_names_) + member = TransType(200) + + self.assertEqual(member.value, 200) + self.assertEqual(member.name, 'Unassigned_200') + self.assertIn(200, TransType._value2member_map_) + self.assertEqual(before + 1, len(TransType._member_names_)) + + with self.assertRaises(ValueError): + TransType(9999) + + +class RegisterAlreadyRegisteredNowRaisesOnAGeneratedRegistryTests(unittest.TestCase): + """The residue #855 disclosed and this tier's own instructions name: + ``register()`` on an already-registered value used to silently alias + rather than mint or raise, on every one of the 105 generated registries + (they had no guard of their own -- only the six bespoke ones tier 2 + already fixed did). Measured directly, before and after, in the + docstring below rather than only asserted. + + Before (commit ``05468a06b``, this batch's own base):: + + >>> TransType.register(6, 'TOTALLY_NEW_NAME') + # returned the *existing* member + # 'TOTALLY_NEW_NAME' in TransType.__members__ == True + # _member_names_ grew by 0 -- nothing minted, nothing raised + + After (this commit):: + + >>> TransType.register(6, 'TOTALLY_NEW_NAME') + ValueError: 6 is already registered on TransType as 'TCP'; use + TransType.register_alias() to add a further name for it + # 'TOTALLY_NEW_NAME' in TransType.__members__ == False + + """ + + def setUp(self) -> None: + snapshot = snapshot_modules(ISOLATED_PREFIXES) + purge_modules(['pcapkit']) + self.addCleanup(restore_modules, snapshot, ISOLATED_PREFIXES) + + def test_register_over_a_taken_value_now_raises(self) -> None: + from pcapkit.const.reg.transtype import TransType + + names_before = list(TransType._member_names_) + members_before = dict(TransType.__members__) + + with self.assertRaises(ValueError) as caught: + TransType.register(6, 'TOTALLY_NEW_NAME') + + self.assertIn('6', str(caught.exception)) + self.assertIn('TCP', str(caught.exception)) + self.assertIn('register_alias', str(caught.exception)) + self.assertEqual(names_before, list(TransType._member_names_)) + self.assertEqual(members_before, dict(TransType.__members__)) + self.assertNotIn('TOTALLY_NEW_NAME', TransType.__members__) + self.assertIs(TransType(6), TransType.TCP) + + def test_register_still_mints_for_a_genuinely_new_value(self) -> None: + from pcapkit.const.reg.transtype import TransType + + value = _unused_value(TransType) + self.addCleanup(_purge_member, TransType, 'unit_test_generated_minted') + + member = TransType.register(value, 'unit_test_generated_minted') + + self.assertEqual(member.value, value) + self.assertIn(value, TransType._value2member_map_) + + +class GetDispatchMatrixTests(unittest.TestCase): + """The dispatch flip this tier's own instructions name: the generated + ``get()`` checked ``isinstance(key, int)`` first and treated anything + else as a name; the inherited base checks ``isinstance(key, str)`` first + and treats anything else as a value. For a key that actually is an ``int`` + or a ``str``, both orderings agree, so every probe below except the + "neither" one is a parity pin rather than a change; the "neither" probe + is the one documented, deliberate difference, measured before and after + on ``TransType`` (a plain generated ``IntEnum`` registry): + + Before (commit ``05468a06b``):: + + >>> TransType.get(3.5) + KeyError: 3.5 # not int -> treated as a name -> dict miss + + After (this commit):: + + >>> TransType.get(3.5) + ValueError: 3.5 is not a valid TransType # not str -> treated as a + # value -> TransType(3.5) -> _missing_(3.5) -> not int -> ValueError + + Neither raised exception mints anything, and both are still exactly the + kind of error :meth:`~pcapkit.corekit.enum.EnumRegistry.get`'s own + docstring promises for an unresolvable key with no default. + """ + + def setUp(self) -> None: + snapshot = snapshot_modules(ISOLATED_PREFIXES) + purge_modules(['pcapkit']) + self.addCleanup(restore_modules, snapshot, ISOLATED_PREFIXES) + + def test_valid_int_value(self) -> None: + from pcapkit.const.reg.transtype import TransType + + self.assertIs(TransType.get(6), TransType.TCP) + + def test_valid_name(self) -> None: + from pcapkit.const.reg.transtype import TransType + + self.assertIs(TransType.get('TCP'), TransType.TCP) + + def test_missing_name_without_default_raises_key_error(self) -> None: + from pcapkit.const.reg.transtype import TransType + + with self.assertRaises(KeyError): + TransType.get('Definitely_Not_A_Member') + + def test_missing_name_with_default_falls_back(self) -> None: + from pcapkit.const.reg.transtype import TransType + + self.assertIs(TransType.get('Definitely_Not_A_Member', 6), TransType.TCP) + + def test_missing_int_without_default_raises_value_error(self) -> None: + """``9999`` is outside TransType's declared ``0..255`` domain + entirely, so this is a genuine miss rather than an unassigned-but + in-range value that ``_missing_`` would otherwise mint.""" + from pcapkit.const.reg.transtype import TransType + + with self.assertRaises(ValueError): + TransType.get(9999) + + def test_missing_int_with_default_falls_back(self) -> None: + from pcapkit.const.reg.transtype import TransType + + self.assertIs(TransType.get(9999, 6), TransType.TCP) + + def test_a_key_that_is_neither_int_nor_str_flips_exception_type(self) -> None: + """The one deliberate, documented behaviour change this tier makes, + pinned to the *new* (post-migration) shape: a key that is neither + ``int`` nor ``str`` now raises :exc:`ValueError` (treated as a value, + rejected by ``_missing_``'s own ``isinstance(value, int)`` guard) + rather than the old :exc:`KeyError` (treated as a name, rejected by + a plain dict lookup) -- measured before/after in this class's own + docstring above.""" + from pcapkit.const.reg.transtype import TransType + + for key in (3.5, None, b'x'): + with self.subTest(key=key): + with self.assertRaises(ValueError) as caught: + TransType.get(key) + self.assertIn('is not a valid TransType', str(caught.exception)) + + +class StrEnumValuePathLimitationTests(unittest.TestCase): + """Justifies excluding every :class:`~aenum.StrEnum` const module from + this tier (:data:`EXCLUDED_STILL_BESPOKE`'s ``ftp``/``http``/``pcapng``/ + ``reg.apptype`` entries): the base ``get()``'s ``isinstance(key, str)`` + branch only ever tries ``cls._member_map_[key]`` -- a *name* lookup -- and + never falls back to treating a ``str`` key as a *value*, unlike the + integer path. That is pre-existing behaviour of + :class:`~pcapkit.corekit.enum.EnumRegistry` itself (added by tier 1, + #855, which never converted a :class:`~aenum.StrEnum` registry either), + not something this tier introduces -- reproduced here on a synthetic + registry so the exclusion decision is measured rather than assumed, and + so a future tier that does take on the ``StrEnum`` registries knows + exactly what it would need to fix first. + """ + + def test_a_str_key_that_is_a_value_but_not_a_name_is_not_resolved(self) -> None: + class _Str(EnumRegistry, StrEnum): + KNOWN = 'known-value' + + # The name resolves, as it always would. + self.assertIs(_Str.get('KNOWN'), _Str.KNOWN) + + # The *value* -- a different string from the name -- does not, even + # though ``_Str('known-value')`` (the constructor) resolves it fine. + # This is the gap: get() never reaches the constructor for a str key. + self.assertIs(_Str('known-value'), _Str.KNOWN) + with self.assertRaises(KeyError): + _Str.get('known-value') + + if __name__ == '__main__': unittest.main()