2
0
mirror of https://github.com/openkmip/pykmip synced 2025-12-20 18:23:24 +00:00

Add OrderedEnum to enable enum value comparisons

This change adds a new Enum subclass, OrderedEnum, that provides
comparison operators allowing for comparisons between enum values.
This will be used going forward with the KMIPVersion enum enabling
version checking on supported or unsupported object types.
This commit is contained in:
Peter Hamilton
2019-02-25 10:48:50 -05:00
committed by Peter Hamilton
parent 2057a48b31
commit 24f30d46b2
2 changed files with 90 additions and 1 deletions

View File

@@ -20,6 +20,34 @@ import copy
import enum
class OrderedEnum(enum.Enum):
"""
An ordered variant of the Enum class that allows for comparisons.
Taken from: https://docs.python.org/3/library/enum.html#orderedenum
"""
def __ge__(self, other):
if self.__class__ is other.__class__:
return self.value >= other.value
return NotImplemented
def __gt__(self, other):
if self.__class__ is other.__class__:
return self.value > other.value
return NotImplemented
def __le__(self, other):
if self.__class__ is other.__class__:
return self.value <= other.value
return NotImplemented
def __lt__(self, other):
if self.__class__ is other.__class__:
return self.value < other.value
return NotImplemented
class AdjustmentType(enum.Enum):
# KMIP 2.0
INCREMENT = 0x00000001
@@ -508,7 +536,7 @@ class KeyWrapType(enum.Enum):
AS_REGISTERED = 0x00000002
class KMIPVersion(enum.Enum):
class KMIPVersion(OrderedEnum):
KMIP_1_0 = 1.0
KMIP_1_1 = 1.1
KMIP_1_2 = 1.2