Skip to content

Commit

Permalink
Change: Removing all get_entity_type_from_string() methods and repl…
Browse files Browse the repository at this point in the history
…ace them by `EntityType.from_string()` classmethods [#573]

Refactor: Replace `enum_from_string()` functions with `Enum.from_string()` functions
  • Loading branch information
y0urself authored Oct 11, 2021
2 parents c3faf04 + 6db33f3 commit b6dec3d
Show file tree
Hide file tree
Showing 74 changed files with 1,387 additions and 1,171 deletions.
31 changes: 4 additions & 27 deletions gvm/protocols/gmpv208/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,32 @@
AlertEvent,
AlertMethod,
AlertsMixin,
get_alert_condition_from_string,
get_alert_event_from_string,
get_alert_method_from_string,
)
from gvm.protocols.gmpv208.entities.audits import AuditsMixin
from gvm.protocols.gmpv208.entities.credentials import (
CredentialFormat,
CredentialsMixin,
CredentialType,
get_credential_format_from_string,
get_credential_type_from_string,
get_snmp_auth_algorithm_from_string,
get_snmp_privacy_algorithm_from_string,
SnmpAuthAlgorithm,
SnmpPrivacyAlgorithm,
)
from gvm.protocols.gmpv208.entities.entities import (
EntityType,
get_entity_type_from_string,
)
from gvm.protocols.gmpv208.entities.filter import (
FiltersMixin,
FilterType,
get_filter_type_from_string,
)
from gvm.protocols.gmpv208.entities.groups import GroupsMixin
from gvm.protocols.gmpv208.entities.hosts import (
HostsMixin,
HostsOrdering,
get_hosts_ordering_from_string,
)
from gvm.protocols.gmpv208.entities.permissions import (
PermissionsMixin,
PermissionSubjectType,
)
from gvm.protocols.gmpv208.entities.port_lists import (
get_port_range_type_from_string,
PortListMixin,
PortRangeType,
)
Expand All @@ -77,72 +70,56 @@
OperatingSystemsMixin,
)
from gvm.protocols.gmpv208.entities.overrides import OverridesMixin
from gvm.protocols.gmpv208.entities.permissions import (
PermissionsMixin,
PermissionSubjectType,
get_permission_subject_type_from_string,
)
from gvm.protocols.gmpv208.entities.policies import PoliciesMixin
from gvm.protocols.gmpv208.entities.results import ResultsMixin
from gvm.protocols.gmpv208.entities.report_formats import (
ReportFormatType,
ReportFormatsMixin,
get_report_format_id_from_string,
)
from gvm.protocols.gmpv208.entities.roles import RolesMixin
from gvm.protocols.gmpv208.entities.scan_configs import ScanConfigsMixin
from gvm.protocols.gmpv208.entities.scanners import (
ScannersMixin,
ScannerType,
get_scanner_type_from_string,
)
from gvm.protocols.gmpv208.entities.schedules import SchedulesMixin
from gvm.protocols.gmpv208.entities.secinfo import (
get_info_type_from_string,
InfoType,
SecInfoMixin,
)
from gvm.protocols.gmpv208.entities.severity import (
SeverityLevel,
get_severity_level_from_string,
)
from gvm.protocols.gmpv208.entities.tags import TagsMixin
from gvm.protocols.gmpv208.entities.targets import (
AliveTest,
get_alive_test_from_string,
TargetsMixin,
)
from gvm.protocols.gmpv208.entities.tasks import TasksMixin
from gvm.protocols.gmpv208.entities.tickets import (
TicketsMixin,
TicketStatus,
get_ticket_status_from_string,
)
from gvm.protocols.gmpv208.entities.tls_certificates import TLSCertificateMixin
from gvm.protocols.gmpv208.entities.users import (
UserAuthType,
UsersMixin,
get_user_auth_type_from_string,
)
from gvm.protocols.gmpv208.entities.vulnerabilities import VulnerabilitiesMixin

from gvm.protocols.gmpv208.system.aggregates import (
AggregatesMixin,
AggregateStatistic,
get_aggregate_statistic_from_string,
SortOrder,
get_sort_order_from_string,
)
from gvm.protocols.gmpv208.system.authentication import AuthenticationMixin
from gvm.protocols.gmpv208.system.feed import (
FeedType,
FeedMixin,
get_feed_type_from_string,
)
from gvm.protocols.gmpv208.system.help import (
HelpFormat,
HelpMixin,
get_help_format_from_string,
)
from gvm.protocols.gmpv208.system.system_reports import SystemReportsMixin
from gvm.protocols.gmpv208.system.trashcan import TrashcanMixin
Expand Down
92 changes: 47 additions & 45 deletions gvm/protocols/gmpv208/entities/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ class AlertEvent(Enum):
ASSIGNED_TICKET_CHANGED = 'Assigned ticket changed'
OWNED_TICKET_CHANGED = 'Owned ticket changed'


def get_alert_event_from_string(
alert_event: Optional[str],
) -> Optional[AlertEvent]:
"""Convert an alert event string into a AlertEvent instance"""
if not alert_event:
return None

try:
return AlertEvent[alert_event.replace(' ', '_').upper()]
except KeyError:
raise InvalidArgument(
argument='alert_event',
function=get_alert_event_from_string.__name__,
) from None
@classmethod
def from_string(
cls,
alert_event: Optional[str],
) -> Optional["AlertEvent"]:
"""Convert an alert event string into a AlertEvent instance"""
if not alert_event:
return None

try:
return cls[alert_event.replace(' ', '_').upper()]
except KeyError:
raise InvalidArgument(
argument='alert_event',
function=cls.from_string.__name__,
) from None


class AlertCondition(Enum):
Expand All @@ -64,21 +65,21 @@ class AlertCondition(Enum):
FILTER_COUNT_CHANGED = 'Filter count changed'
FILTER_COUNT_AT_LEAST = 'Filter count at least'


def get_alert_condition_from_string(
alert_condition: Optional[str],
) -> Optional[AlertCondition]:
"""Convert an alert condition string into a AlertCondition instance"""
if not alert_condition:
return None

try:
return AlertCondition[alert_condition.replace(' ', '_').upper()]
except KeyError:
raise InvalidArgument(
argument='alert_condition',
function=get_alert_condition_from_string.__name__,
) from None
@classmethod
def from_string(
cls, alert_condition: Optional[str]
) -> Optional["AlertCondition"]:
"""Convert an alert condition string into a AlertCondition instance"""
if not alert_condition:
return None

try:
return cls[alert_condition.replace(' ', '_').upper()]
except KeyError:
raise InvalidArgument(
argument='alert_condition',
function=cls.from_string.__name__,
) from None


class AlertMethod(Enum):
Expand All @@ -97,21 +98,22 @@ class AlertMethod(Enum):
TIPPINGPOINT_SMS = "TippingPoint SMS"
ALEMBA_VFIRE = "Alemba vFire"


def get_alert_method_from_string(
alert_method: Optional[str],
) -> Optional[AlertMethod]:
"""Convert an alert method string into a AlertCondition instance"""
if not alert_method:
return None

try:
return AlertMethod[alert_method.replace(' ', '_').upper()]
except KeyError:
raise InvalidArgument(
argument='alert_method',
function=get_alert_method_from_string.__name__,
) from None
@classmethod
def from_string(
cls,
alert_method: Optional[str],
) -> Optional["AlertMethod"]:
"""Convert an alert method string into a AlertCondition instance"""
if not alert_method:
return None

try:
return cls[alert_method.replace(' ', '_').upper()]
except KeyError:
raise InvalidArgument(
argument='alert_method',
function=cls.from_string.__name__,
) from None


def _check_event(
Expand Down
127 changes: 66 additions & 61 deletions gvm/protocols/gmpv208/entities/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ class CredentialFormat(Enum):
EXE = 'exe'
PEM = 'pem'


def get_credential_format_from_string(
credential_format: Optional[str],
) -> Optional[CredentialFormat]:
if not credential_format:
return None

try:
return CredentialFormat[credential_format.upper()]
except KeyError:
raise InvalidArgument(
argument='credential_format',
function=get_credential_format_from_string.__name__,
) from None
@classmethod
def from_string(
cls,
credential_format: Optional[str],
) -> Optional["CredentialFormat"]:
if not credential_format:
return None

try:
return cls[credential_format.upper()]
except KeyError:
raise InvalidArgument(
argument='credential_format',
function=cls.from_string.__name__,
) from None


class CredentialType(Enum):
Expand All @@ -61,21 +62,22 @@ class CredentialType(Enum):
PGP_ENCRYPTION_KEY = 'pgp'
PASSWORD_ONLY = 'pw'


def get_credential_type_from_string(
credential_type: Optional[str],
) -> Optional[CredentialType]:
"""Convert a credential type string into a CredentialType instance"""
if not credential_type:
return None

try:
return CredentialType[credential_type.upper()]
except KeyError:
raise InvalidArgument(
argument='credential_type',
function=get_credential_type_from_string.__name__,
) from None
@classmethod
def from_string(
cls,
credential_type: Optional[str],
) -> Optional["CredentialType"]:
"""Convert a credential type string into a CredentialType instance"""
if not credential_type:
return None

try:
return cls[credential_type.upper()]
except KeyError:
raise InvalidArgument(
argument='credential_type',
function=cls.from_string.__name__,
) from None


class SnmpAuthAlgorithm(Enum):
Expand All @@ -84,21 +86,23 @@ class SnmpAuthAlgorithm(Enum):
SHA1 = 'sha1'
MD5 = 'md5'


def get_snmp_auth_algorithm_from_string(
algorithm: Optional[str],
) -> Optional[SnmpAuthAlgorithm]:
"""Convert a SNMP auth algorithm string into a SnmpAuthAlgorithm instance"""
if not algorithm:
return None

try:
return SnmpAuthAlgorithm[algorithm.upper()]
except KeyError:
raise InvalidArgument(
argument='algorithm',
function=get_snmp_auth_algorithm_from_string.__name__,
) from None
@classmethod
def from_string(
cls,
algorithm: Optional[str],
) -> Optional["SnmpAuthAlgorithm"]:
"""Convert a SNMP auth algorithm string into a
SnmpAuthAlgorithm instance"""
if not algorithm:
return None

try:
return cls[algorithm.upper()]
except KeyError:
raise InvalidArgument(
argument='algorithm',
function=cls.from_string.__name__,
) from None


class SnmpPrivacyAlgorithm(Enum):
Expand All @@ -107,23 +111,24 @@ class SnmpPrivacyAlgorithm(Enum):
AES = 'aes'
DES = 'des'


def get_snmp_privacy_algorithm_from_string(
algorithm: Optional[str],
) -> Optional[SnmpPrivacyAlgorithm]:
"""Convert a SNMP privacy algorithm string into a SnmpPrivacyAlgorithm
instance
"""
if not algorithm:
return None

try:
return SnmpPrivacyAlgorithm[algorithm.upper()]
except KeyError:
raise InvalidArgument(
argument='algorithm',
function=get_snmp_privacy_algorithm_from_string.__name__,
) from None
@classmethod
def from_string(
cls,
algorithm: Optional[str],
) -> Optional["SnmpPrivacyAlgorithm"]:
"""Convert a SNMP privacy algorithm string into a SnmpPrivacyAlgorithm
instance
"""
if not algorithm:
return None

try:
return cls[algorithm.upper()]
except KeyError:
raise InvalidArgument(
argument='algorithm',
function=cls.from_string.__name__,
) from None


class CredentialsMixin:
Expand Down
Loading

0 comments on commit b6dec3d

Please sign in to comment.