-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Add a base class for Enums in python-gvm
This will simplify all our enums and reduce duplicate code.
- Loading branch information
1 parent
6db4b1e
commit 5e74178
Showing
3 changed files
with
41 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# SPDX-FileCopyrightText: 2024 Greenbone AG | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
from enum import Enum as PythonEnum | ||
from typing import Optional | ||
|
||
from typing_extensions import Self | ||
|
||
from gvm.errors import InvalidArgument | ||
|
||
|
||
class Enum(PythonEnum): | ||
""" | ||
Base class for Enums in python-gvm | ||
""" | ||
|
||
@classmethod | ||
def from_string( | ||
cls, | ||
value: Optional[str], | ||
) -> Optional[Self]: | ||
""" | ||
Convert a string value into an Enum instance | ||
If value is None or empty None is returned | ||
""" | ||
if not value: | ||
return None | ||
|
||
try: | ||
return cls[value.replace(" ", "_").upper()] | ||
except KeyError: | ||
raise InvalidArgument( | ||
f"Invalid argument {value} for {cls.__name__}.from_string. " | ||
f"Allowed values are {','.join(e.name for e in cls)}." | ||
) from None |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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