Passed
Pull Request — master (#157)
by
unknown
03:23 queued 19s
created

gvm.protocols.gmpv9.types   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 267
Duplicated Lines 47.19 %

Importance

Changes 0
Metric Value
eloc 193
dl 126
loc 267
rs 10
c 0
b 0
f 0
wmc 18

3 Functions

Rating   Name   Duplication   Size   Complexity  
B get_entity_type_from_string() 29 29 7
B get_filter_type_from_string() 32 32 8
A __get_usage_type_from_string() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
from enum import Enum
19
20
from typing import Optional
21
22
from gvm.errors import InvalidArgument
23
24
from gvm.protocols.gmpv8.types import (
25
    AlertCondition,
26
    AlertEvent,
27
    AlertMethod,
28
    AliveTest,
29
    AssetType,
30
    CredentialFormat,
31
    CredentialType,
32
    FeedType,
33
    HostsOrdering,
34
    InfoType,
35
    PermissionSubjectType,
36
    PortRangeType,
37
    ScannerType,
38
    SeverityLevel,
39
    SnmpAuthAlgorithm,
40
    SnmpPrivacyAlgorithm,
41
    TicketStatus,
42
    TimeUnit,
43
    get_alert_condition_from_string,
44
    get_alert_event_from_string,
45
    get_alert_method_from_string,
46
    get_alive_test_from_string,
47
    get_asset_type_from_string,
48
    get_credential_format_from_string,
49
    get_credential_type_from_string,
50
    get_feed_type_from_string,
51
    get_hosts_ordering_from_string,
52
    get_info_type_from_string,
53
    get_permission_subject_type_from_string,
54
    get_port_range_type_from_string,
55
    get_severity_level_from_string,
56
    get_scanner_type_from_string,
57
    get_snmp_auth_algorithm_from_string,
58
    get_snmp_privacy_algorithm_from_string,
59
    get_ticket_status_from_string,
60
)
61
62
63
__all__ = [
64
    "AlertCondition",
65
    "AlertEvent",
66
    "AlertMethod",
67
    "AliveTest",
68
    "AssetType",
69
    "CredentialFormat",
70
    "CredentialType",
71
    "EntityType",
72
    "FeedType",
73
    "FilterType",
74
    "HostsOrdering",
75
    "InfoType",
76
    "PermissionSubjectType",
77
    "PortRangeType",
78
    "ScannerType",
79
    "SeverityLevel",
80
    "SnmpAuthAlgorithm",
81
    "SnmpPrivacyAlgorithm",
82
    "TicketStatus",
83
    "TimeUnit",
84
    "get_alert_condition_from_string",
85
    "get_alert_event_from_string",
86
    "get_alert_method_from_string",
87
    "get_alive_test_from_string",
88
    "get_asset_type_from_string",
89
    "get_credential_format_from_string",
90
    "get_credential_type_from_string",
91
    "get_entity_type_from_string",
92
    "get_feed_type_from_string",
93
    "get_filter_type_from_string",
94
    "get_hosts_ordering_from_string",
95
    "get_info_type_from_string",
96
    "get_permission_subject_type_from_string",
97
    "get_port_range_type_from_string",
98
    "get_scanner_type_from_string",
99
    "get_severity_level_from_string",
100
    "get_snmp_auth_algorithm_from_string",
101
    "get_snmp_privacy_algorithm_from_string",
102
    "get_ticket_status_from_string",
103
]
104
105
106 View Code Duplication
class EntityType(Enum):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
    """ Enum for entity types """
108
109
    AGENT = "note"
110
    ALERT = "alert"
111
    ASSET = "asset"
112
    CERT_BUND_ADV = "cert_bund_adv"
113
    CPE = "cpe"
114
    CREDENTIAL = "credential"
115
    CVE = "cve"
116
    DFN_CERT_ADV = "dfn_cert_adv"
117
    FILTER = "filter"
118
    GROUP = "group"
119
    HOST = "host"
120
    INFO = "info"
121
    NOTE = "note"
122
    NVT = "nvt"
123
    OPERATING_SYSTEM = "os"
124
    OVALDEF = "ovaldef"
125
    OVERRIDE = "override"
126
    PERMISSION = "permission"
127
    PORT_LIST = "port_list"
128
    REPORT = "report"
129
    REPORT_FORMAT = "report_format"
130
    RESULT = "result"
131
    ROLE = "role"
132
    SCAN_CONFIG = "config"
133
    SCANNER = "scanner"
134
    SCHEDULE = "schedule"
135
    TAG = "tag"
136
    TARGET = "target"
137
    TASK = "task"
138
    TICKET = "ticket"
139
    TLS_CERTIFICATE = "tls_certificate"
140
    USER = "user"
141
    VULNERABILITY = "vuln"
142
143
144 View Code Duplication
def get_entity_type_from_string(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
145
    entity_type: Optional[str]
146
) -> Optional[EntityType]:
147
    """ Convert a entity type string to an actual EntityType instance
148
149
    Arguments:
150
        entity_type: Entity type string to convert to a EntityType
151
    """
152
    if not entity_type:
153
        return None
154
155
    if entity_type == 'vuln':
156
        return EntityType.VULNERABILITY
157
158
    if entity_type == 'os':
159
        return EntityType.OPERATING_SYSTEM
160
161
    if entity_type == 'config':
162
        return EntityType.SCAN_CONFIG
163
164
    if entity_type == 'tls_certificate':
165
        return EntityType.TLS_CERTIFICATE
166
167
    try:
168
        return EntityType[entity_type.upper()]
169
    except KeyError:
170
        raise InvalidArgument(
171
            argument='entity_type',
172
            function=get_entity_type_from_string.__name__,
173
        )
174
175
176 View Code Duplication
class FilterType(Enum):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
177
    """ Enum for filter types """
178
179
    AGENT = "agent"
180
    ALERT = "alert"
181
    ASSET = "asset"
182
    SCAN_CONFIG = "config"
183
    CREDENTIAL = "credential"
184
    FILTER = "filter"
185
    GROUP = "group"
186
    HOST = "host"
187
    NOTE = "note"
188
    OPERATING_SYSTEM = "os"
189
    OVERRIDE = "override"
190
    PERMISSION = "permission"
191
    PORT_LIST = "port_list"
192
    REPORT = "report"
193
    REPORT_FORMAT = "report_format"
194
    RESULT = "result"
195
    ROLE = "role"
196
    SCHEDULE = "schedule"
197
    ALL_SECINFO = "secinfo"
198
    TAG = "tag"
199
    TARGET = "target"
200
    TASK = "task"
201
    TICKET = "ticket"
202
    TLS_CERTIFICATE = "tls_certificate"
203
    USER = "user"
204
    VULNERABILITY = "vuln"
205
206
207 View Code Duplication
def get_filter_type_from_string(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
208
    filter_type: Optional[str]
209
) -> Optional[FilterType]:
210
    """ Convert a filter type string to an actual FilterType instance
211
212
    Arguments:
213
        filter_type (str): Filter type string to convert to a FilterType
214
    """
215
    if not filter_type:
216
        return None
217
218
    if filter_type == 'vuln':
219
        return FilterType.VULNERABILITY
220
221
    if filter_type == 'os':
222
        return FilterType.OPERATING_SYSTEM
223
224
    if filter_type == 'config':
225
        return FilterType.SCAN_CONFIG
226
227
    if filter_type == 'secinfo':
228
        return FilterType.ALL_SECINFO
229
230
    if filter_type == 'tls_certificate':
231
        return FilterType.TLS_CERTIFICATE
232
233
    try:
234
        return FilterType[filter_type.upper()]
235
    except KeyError:
236
        raise InvalidArgument(
237
            argument='filter_type',
238
            function=get_filter_type_from_string.__name__,
239
        )
240
241
242
class _UsageType(Enum):
243
    """ Enum for usage types """
244
245
    AUDIT = "audit"
246
    POLICY = "policy"
247
    SCAN = "scan"
248
249
250
def __get_usage_type_from_string(
251
    usage_type: Optional[str]
252
) -> Optional[_UsageType]:
253
    """ Convert a usage type string to an actual _UsageType instance
254
255
    Arguments:
256
        entity_type: Usage type string to convert to a _UsageType
257
    """
258
    if not usage_type:
259
        return None
260
261
    try:
262
        return _UsageType[usage_type.upper()]
263
    except KeyError:
264
        raise InvalidArgument(
265
            argument='usage_type',
266
            function=__get_usage_type_from_string.__name__,
267
        )
268