-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from bwelling/enum-cleanup
Enum refactoring.
- Loading branch information
Showing
6 changed files
with
172 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license | ||
|
||
# Copyright (C) 2003-2017 Nominum, Inc. | ||
# | ||
# Permission to use, copy, modify, and distribute this software and its | ||
# documentation for any purpose with or without fee is hereby granted, | ||
# provided that the above copyright notice and this permission notice | ||
# appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES | ||
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR | ||
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | ||
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
import enum | ||
|
||
class IntEnum(enum.IntEnum): | ||
@classmethod | ||
def _check_value(cls, value): | ||
max = cls._maximum() | ||
if value < 0 or value > max: | ||
name = cls._short_name() | ||
raise ValueError(f"{name} must be between >= 0 and <= {max}") | ||
|
||
@classmethod | ||
def from_text(cls, text): | ||
text = text.upper() | ||
try: | ||
return cls[text] | ||
except KeyError: | ||
pass | ||
prefix = cls._prefix() | ||
if text.startswith(prefix) and text[len(prefix):].isdigit(): | ||
value = int(text[len(prefix):]) | ||
cls._check_value(value) | ||
try: | ||
return cls(value) | ||
except ValueError: | ||
return value | ||
raise cls._unknown_exception_class() | ||
|
||
@classmethod | ||
def to_text(cls, value): | ||
cls._check_value(value) | ||
try: | ||
return cls(value).name | ||
except ValueError: | ||
return f"{cls._prefix()}{value}" | ||
|
||
@classmethod | ||
def to_enum(cls, value): | ||
if isinstance(value, str): | ||
return cls.from_text(value) | ||
cls._check_value(value) | ||
try: | ||
return cls(value) | ||
except ValueError: | ||
return value | ||
|
||
@classmethod | ||
def _maximum(cls): | ||
raise NotImplemented | ||
|
||
@classmethod | ||
def _short_name(cls): | ||
return cls.__name__.lower() | ||
|
||
@classmethod | ||
def _prefix(cls): | ||
return '' | ||
|
||
@classmethod | ||
def _unknown_exception_class(cls): | ||
return ValueError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.