Skip to content

Commit b6dec3d

Browse files
authored
Change: Removing all get_entity_type_from_string() methods and replace them by EntityType.from_string() classmethods [#573]
Refactor: Replace `enum_from_string()` functions with `Enum.from_string()` functions
2 parents c3faf04 + 6db33f3 commit b6dec3d

74 files changed

Lines changed: 1387 additions & 1171 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

gvm/protocols/gmpv208/__init__.py

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,32 @@
3535
AlertEvent,
3636
AlertMethod,
3737
AlertsMixin,
38-
get_alert_condition_from_string,
39-
get_alert_event_from_string,
40-
get_alert_method_from_string,
4138
)
4239
from gvm.protocols.gmpv208.entities.audits import AuditsMixin
4340
from gvm.protocols.gmpv208.entities.credentials import (
4441
CredentialFormat,
4542
CredentialsMixin,
4643
CredentialType,
47-
get_credential_format_from_string,
48-
get_credential_type_from_string,
49-
get_snmp_auth_algorithm_from_string,
50-
get_snmp_privacy_algorithm_from_string,
5144
SnmpAuthAlgorithm,
5245
SnmpPrivacyAlgorithm,
5346
)
5447
from gvm.protocols.gmpv208.entities.entities import (
5548
EntityType,
56-
get_entity_type_from_string,
5749
)
5850
from gvm.protocols.gmpv208.entities.filter import (
5951
FiltersMixin,
6052
FilterType,
61-
get_filter_type_from_string,
6253
)
6354
from gvm.protocols.gmpv208.entities.groups import GroupsMixin
6455
from gvm.protocols.gmpv208.entities.hosts import (
6556
HostsMixin,
6657
HostsOrdering,
67-
get_hosts_ordering_from_string,
58+
)
59+
from gvm.protocols.gmpv208.entities.permissions import (
60+
PermissionsMixin,
61+
PermissionSubjectType,
6862
)
6963
from gvm.protocols.gmpv208.entities.port_lists import (
70-
get_port_range_type_from_string,
7164
PortListMixin,
7265
PortRangeType,
7366
)
@@ -77,72 +70,56 @@
7770
OperatingSystemsMixin,
7871
)
7972
from gvm.protocols.gmpv208.entities.overrides import OverridesMixin
80-
from gvm.protocols.gmpv208.entities.permissions import (
81-
PermissionsMixin,
82-
PermissionSubjectType,
83-
get_permission_subject_type_from_string,
84-
)
8573
from gvm.protocols.gmpv208.entities.policies import PoliciesMixin
8674
from gvm.protocols.gmpv208.entities.results import ResultsMixin
8775
from gvm.protocols.gmpv208.entities.report_formats import (
8876
ReportFormatType,
8977
ReportFormatsMixin,
90-
get_report_format_id_from_string,
9178
)
9279
from gvm.protocols.gmpv208.entities.roles import RolesMixin
9380
from gvm.protocols.gmpv208.entities.scan_configs import ScanConfigsMixin
9481
from gvm.protocols.gmpv208.entities.scanners import (
9582
ScannersMixin,
9683
ScannerType,
97-
get_scanner_type_from_string,
9884
)
9985
from gvm.protocols.gmpv208.entities.schedules import SchedulesMixin
10086
from gvm.protocols.gmpv208.entities.secinfo import (
101-
get_info_type_from_string,
10287
InfoType,
10388
SecInfoMixin,
10489
)
10590
from gvm.protocols.gmpv208.entities.severity import (
10691
SeverityLevel,
107-
get_severity_level_from_string,
10892
)
10993
from gvm.protocols.gmpv208.entities.tags import TagsMixin
11094
from gvm.protocols.gmpv208.entities.targets import (
11195
AliveTest,
112-
get_alive_test_from_string,
11396
TargetsMixin,
11497
)
11598
from gvm.protocols.gmpv208.entities.tasks import TasksMixin
11699
from gvm.protocols.gmpv208.entities.tickets import (
117100
TicketsMixin,
118101
TicketStatus,
119-
get_ticket_status_from_string,
120102
)
121103
from gvm.protocols.gmpv208.entities.tls_certificates import TLSCertificateMixin
122104
from gvm.protocols.gmpv208.entities.users import (
123105
UserAuthType,
124106
UsersMixin,
125-
get_user_auth_type_from_string,
126107
)
127108
from gvm.protocols.gmpv208.entities.vulnerabilities import VulnerabilitiesMixin
128109

129110
from gvm.protocols.gmpv208.system.aggregates import (
130111
AggregatesMixin,
131112
AggregateStatistic,
132-
get_aggregate_statistic_from_string,
133113
SortOrder,
134-
get_sort_order_from_string,
135114
)
136115
from gvm.protocols.gmpv208.system.authentication import AuthenticationMixin
137116
from gvm.protocols.gmpv208.system.feed import (
138117
FeedType,
139118
FeedMixin,
140-
get_feed_type_from_string,
141119
)
142120
from gvm.protocols.gmpv208.system.help import (
143121
HelpFormat,
144122
HelpMixin,
145-
get_help_format_from_string,
146123
)
147124
from gvm.protocols.gmpv208.system.system_reports import SystemReportsMixin
148125
from gvm.protocols.gmpv208.system.trashcan import TrashcanMixin

gvm/protocols/gmpv208/entities/alerts.py

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@ class AlertEvent(Enum):
3737
ASSIGNED_TICKET_CHANGED = 'Assigned ticket changed'
3838
OWNED_TICKET_CHANGED = 'Owned ticket changed'
3939

40-
41-
def get_alert_event_from_string(
42-
alert_event: Optional[str],
43-
) -> Optional[AlertEvent]:
44-
"""Convert an alert event string into a AlertEvent instance"""
45-
if not alert_event:
46-
return None
47-
48-
try:
49-
return AlertEvent[alert_event.replace(' ', '_').upper()]
50-
except KeyError:
51-
raise InvalidArgument(
52-
argument='alert_event',
53-
function=get_alert_event_from_string.__name__,
54-
) from None
40+
@classmethod
41+
def from_string(
42+
cls,
43+
alert_event: Optional[str],
44+
) -> Optional["AlertEvent"]:
45+
"""Convert an alert event string into a AlertEvent instance"""
46+
if not alert_event:
47+
return None
48+
49+
try:
50+
return cls[alert_event.replace(' ', '_').upper()]
51+
except KeyError:
52+
raise InvalidArgument(
53+
argument='alert_event',
54+
function=cls.from_string.__name__,
55+
) from None
5556

5657

5758
class AlertCondition(Enum):
@@ -64,21 +65,21 @@ class AlertCondition(Enum):
6465
FILTER_COUNT_CHANGED = 'Filter count changed'
6566
FILTER_COUNT_AT_LEAST = 'Filter count at least'
6667

67-
68-
def get_alert_condition_from_string(
69-
alert_condition: Optional[str],
70-
) -> Optional[AlertCondition]:
71-
"""Convert an alert condition string into a AlertCondition instance"""
72-
if not alert_condition:
73-
return None
74-
75-
try:
76-
return AlertCondition[alert_condition.replace(' ', '_').upper()]
77-
except KeyError:
78-
raise InvalidArgument(
79-
argument='alert_condition',
80-
function=get_alert_condition_from_string.__name__,
81-
) from None
68+
@classmethod
69+
def from_string(
70+
cls, alert_condition: Optional[str]
71+
) -> Optional["AlertCondition"]:
72+
"""Convert an alert condition string into a AlertCondition instance"""
73+
if not alert_condition:
74+
return None
75+
76+
try:
77+
return cls[alert_condition.replace(' ', '_').upper()]
78+
except KeyError:
79+
raise InvalidArgument(
80+
argument='alert_condition',
81+
function=cls.from_string.__name__,
82+
) from None
8283

8384

8485
class AlertMethod(Enum):
@@ -97,21 +98,22 @@ class AlertMethod(Enum):
9798
TIPPINGPOINT_SMS = "TippingPoint SMS"
9899
ALEMBA_VFIRE = "Alemba vFire"
99100

100-
101-
def get_alert_method_from_string(
102-
alert_method: Optional[str],
103-
) -> Optional[AlertMethod]:
104-
"""Convert an alert method string into a AlertCondition instance"""
105-
if not alert_method:
106-
return None
107-
108-
try:
109-
return AlertMethod[alert_method.replace(' ', '_').upper()]
110-
except KeyError:
111-
raise InvalidArgument(
112-
argument='alert_method',
113-
function=get_alert_method_from_string.__name__,
114-
) from None
101+
@classmethod
102+
def from_string(
103+
cls,
104+
alert_method: Optional[str],
105+
) -> Optional["AlertMethod"]:
106+
"""Convert an alert method string into a AlertCondition instance"""
107+
if not alert_method:
108+
return None
109+
110+
try:
111+
return cls[alert_method.replace(' ', '_').upper()]
112+
except KeyError:
113+
raise InvalidArgument(
114+
argument='alert_method',
115+
function=cls.from_string.__name__,
116+
) from None
115117

116118

117119
def _check_event(

gvm/protocols/gmpv208/entities/credentials.py

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ class CredentialFormat(Enum):
3434
EXE = 'exe'
3535
PEM = 'pem'
3636

37-
38-
def get_credential_format_from_string(
39-
credential_format: Optional[str],
40-
) -> Optional[CredentialFormat]:
41-
if not credential_format:
42-
return None
43-
44-
try:
45-
return CredentialFormat[credential_format.upper()]
46-
except KeyError:
47-
raise InvalidArgument(
48-
argument='credential_format',
49-
function=get_credential_format_from_string.__name__,
50-
) from None
37+
@classmethod
38+
def from_string(
39+
cls,
40+
credential_format: Optional[str],
41+
) -> Optional["CredentialFormat"]:
42+
if not credential_format:
43+
return None
44+
45+
try:
46+
return cls[credential_format.upper()]
47+
except KeyError:
48+
raise InvalidArgument(
49+
argument='credential_format',
50+
function=cls.from_string.__name__,
51+
) from None
5152

5253

5354
class CredentialType(Enum):
@@ -61,21 +62,22 @@ class CredentialType(Enum):
6162
PGP_ENCRYPTION_KEY = 'pgp'
6263
PASSWORD_ONLY = 'pw'
6364

64-
65-
def get_credential_type_from_string(
66-
credential_type: Optional[str],
67-
) -> Optional[CredentialType]:
68-
"""Convert a credential type string into a CredentialType instance"""
69-
if not credential_type:
70-
return None
71-
72-
try:
73-
return CredentialType[credential_type.upper()]
74-
except KeyError:
75-
raise InvalidArgument(
76-
argument='credential_type',
77-
function=get_credential_type_from_string.__name__,
78-
) from None
65+
@classmethod
66+
def from_string(
67+
cls,
68+
credential_type: Optional[str],
69+
) -> Optional["CredentialType"]:
70+
"""Convert a credential type string into a CredentialType instance"""
71+
if not credential_type:
72+
return None
73+
74+
try:
75+
return cls[credential_type.upper()]
76+
except KeyError:
77+
raise InvalidArgument(
78+
argument='credential_type',
79+
function=cls.from_string.__name__,
80+
) from None
7981

8082

8183
class SnmpAuthAlgorithm(Enum):
@@ -84,21 +86,23 @@ class SnmpAuthAlgorithm(Enum):
8486
SHA1 = 'sha1'
8587
MD5 = 'md5'
8688

87-
88-
def get_snmp_auth_algorithm_from_string(
89-
algorithm: Optional[str],
90-
) -> Optional[SnmpAuthAlgorithm]:
91-
"""Convert a SNMP auth algorithm string into a SnmpAuthAlgorithm instance"""
92-
if not algorithm:
93-
return None
94-
95-
try:
96-
return SnmpAuthAlgorithm[algorithm.upper()]
97-
except KeyError:
98-
raise InvalidArgument(
99-
argument='algorithm',
100-
function=get_snmp_auth_algorithm_from_string.__name__,
101-
) from None
89+
@classmethod
90+
def from_string(
91+
cls,
92+
algorithm: Optional[str],
93+
) -> Optional["SnmpAuthAlgorithm"]:
94+
"""Convert a SNMP auth algorithm string into a
95+
SnmpAuthAlgorithm instance"""
96+
if not algorithm:
97+
return None
98+
99+
try:
100+
return cls[algorithm.upper()]
101+
except KeyError:
102+
raise InvalidArgument(
103+
argument='algorithm',
104+
function=cls.from_string.__name__,
105+
) from None
102106

103107

104108
class SnmpPrivacyAlgorithm(Enum):
@@ -107,23 +111,24 @@ class SnmpPrivacyAlgorithm(Enum):
107111
AES = 'aes'
108112
DES = 'des'
109113

110-
111-
def get_snmp_privacy_algorithm_from_string(
112-
algorithm: Optional[str],
113-
) -> Optional[SnmpPrivacyAlgorithm]:
114-
"""Convert a SNMP privacy algorithm string into a SnmpPrivacyAlgorithm
115-
instance
116-
"""
117-
if not algorithm:
118-
return None
119-
120-
try:
121-
return SnmpPrivacyAlgorithm[algorithm.upper()]
122-
except KeyError:
123-
raise InvalidArgument(
124-
argument='algorithm',
125-
function=get_snmp_privacy_algorithm_from_string.__name__,
126-
) from None
114+
@classmethod
115+
def from_string(
116+
cls,
117+
algorithm: Optional[str],
118+
) -> Optional["SnmpPrivacyAlgorithm"]:
119+
"""Convert a SNMP privacy algorithm string into a SnmpPrivacyAlgorithm
120+
instance
121+
"""
122+
if not algorithm:
123+
return None
124+
125+
try:
126+
return cls[algorithm.upper()]
127+
except KeyError:
128+
raise InvalidArgument(
129+
argument='algorithm',
130+
function=cls.from_string.__name__,
131+
) from None
127132

128133

129134
class CredentialsMixin:

0 commit comments

Comments
 (0)