Completed
Push — master ( d15ae4...af96e1 )
by Björn
31s queued 22s
created

gvm.protocols.gmpv208.entities.schedules   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 250
Duplicated Lines 12 %

Importance

Changes 0
Metric Value
eloc 91
dl 30
loc 250
rs 10
c 0
b 0
f 0
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A SchedulesMixin.get_schedule() 0 25 3
A SchedulesMixin.clone_schedule() 0 17 2
B SchedulesMixin.modify_schedule() 0 49 6
A SchedulesMixin.get_schedules() 30 30 3
A SchedulesMixin.delete_schedule() 0 19 2
B SchedulesMixin.create_schedule() 0 76 5

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) 2021 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
19
# pylint:  disable=redefined-builtin
20
21
from typing import Any, Optional
22
23
from gvm.errors import RequiredArgument
24
from gvm.utils import add_filter, to_bool
25
from gvm.xml import XmlCommand
26
27
28
class SchedulesMixin:
29
    def clone_schedule(self, schedule_id: str) -> Any:
30
        """Clone an existing schedule
31
32
        Arguments:
33
            schedule_id: UUID of an existing schedule to clone from
34
35
        Returns:
36
            The response. See :py:meth:`send_command` for details.
37
        """
38
        if not schedule_id:
39
            raise RequiredArgument(
40
                function=self.clone_schedule.__name__, argument='schedule_id'
41
            )
42
43
        cmd = XmlCommand("create_schedule")
44
        cmd.add_element("copy", schedule_id)
45
        return self._send_xml_command(cmd)
46
47
    def create_schedule(
48
        self,
49
        name: str,
50
        icalendar: str,
51
        timezone: str,
52
        *,
53
        comment: Optional[str] = None,
54
    ) -> Any:
55
        """Create a new schedule based in `iCalendar`_ data.
56
57
        Example:
58
            Requires https://pypi.org/project/icalendar/
59
60
            .. code-block:: python
61
62
                import pytz
63
64
                from datetime import datetime
65
66
                from icalendar import Calendar, Event
67
68
                cal = Calendar()
69
70
                cal.add('prodid', '-//Foo Bar//')
71
                cal.add('version', '2.0')
72
73
                event = Event()
74
                event.add('dtstamp', datetime.now(tz=pytz.UTC))
75
                event.add('dtstart', datetime(2020, 1, 1, tzinfo=pytz.utc))
76
77
                cal.add_component(event)
78
79
                gmp.create_schedule(
80
                    name="My Schedule",
81
                    icalendar=cal.to_ical(),
82
                    timezone='UTC'
83
                )
84
        Arguments:
85
            name: Name of the new schedule
86
            icalendar: `iCalendar`_ (RFC 5545) based data.
87
            timezone: Timezone to use for the icalender events e.g
88
                Europe/Berlin. If the datetime values in the icalendar data are
89
                missing timezone information this timezone gets applied.
90
                Otherwise the datetime values from the icalendar data are
91
                displayed in this timezone
92
            comment: Comment on schedule.
93
94
        Returns:
95
            The response. See :py:meth:`send_command` for details.
96
97
        .. _iCalendar:
98
            https://tools.ietf.org/html/rfc5545
99
        """
100
        if not name:
101
            raise RequiredArgument(
102
                function=self.create_schedule.__name__, argument='name'
103
            )
104
        if not icalendar:
105
            raise RequiredArgument(
106
                function=self.create_schedule.__name__, argument='icalendar'
107
            )
108
        if not timezone:
109
            raise RequiredArgument(
110
                function=self.create_schedule.__name__, argument='timezone'
111
            )
112
113
        cmd = XmlCommand("create_schedule")
114
115
        cmd.add_element("name", name)
116
        cmd.add_element("icalendar", icalendar)
117
        cmd.add_element("timezone", timezone)
118
119
        if comment:
120
            cmd.add_element("comment", comment)
121
122
        return self._send_xml_command(cmd)
123
124
    def delete_schedule(
125
        self, schedule_id: str, *, ultimate: Optional[bool] = False
126
    ) -> Any:
127
        """Deletes an existing schedule
128
129
        Arguments:
130
            schedule_id: UUID of the schedule to be deleted.
131
            ultimate: Whether to remove entirely, or to the trashcan.
132
        """
133
        if not schedule_id:
134
            raise RequiredArgument(
135
                function=self.delete_schedule.__name__, argument='schedule_id'
136
            )
137
138
        cmd = XmlCommand("delete_schedule")
139
        cmd.set_attribute("schedule_id", schedule_id)
140
        cmd.set_attribute("ultimate", to_bool(ultimate))
141
142
        return self._send_xml_command(cmd)
143
144 View Code Duplication
    def get_schedules(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
145
        self,
146
        *,
147
        filter: Optional[str] = None,
148
        filter_id: Optional[str] = None,
149
        trash: Optional[bool] = None,
150
        tasks: Optional[bool] = None,
151
    ) -> Any:
152
        """Request a list of schedules
153
154
        Arguments:
155
            filter: Filter term to use for the query
156
            filter_id: UUID of an existing filter to use for the query
157
            trash: Whether to get the trashcan schedules instead
158
            tasks: Whether to include tasks using the schedules
159
160
        Returns:
161
            The response. See :py:meth:`send_command` for details.
162
        """
163
        cmd = XmlCommand("get_schedules")
164
165
        add_filter(cmd, filter, filter_id)
166
167
        if trash is not None:
168
            cmd.set_attribute("trash", to_bool(trash))
169
170
        if tasks is not None:
171
            cmd.set_attribute("tasks", to_bool(tasks))
172
173
        return self._send_xml_command(cmd)
174
175
    def get_schedule(
176
        self, schedule_id: str, *, tasks: Optional[bool] = None
177
    ) -> Any:
178
        """Request a single schedule
179
180
        Arguments:
181
            schedule_id: UUID of an existing schedule
182
            tasks: Whether to include tasks using the schedules
183
184
        Returns:
185
            The response. See :py:meth:`send_command` for details.
186
        """
187
        cmd = XmlCommand("get_schedules")
188
189
        if not schedule_id:
190
            raise RequiredArgument(
191
                function=self.get_schedule.__name__, argument='schedule_id'
192
            )
193
194
        cmd.set_attribute("schedule_id", schedule_id)
195
196
        if tasks is not None:
197
            cmd.set_attribute("tasks", to_bool(tasks))
198
199
        return self._send_xml_command(cmd)
200
201
    def modify_schedule(
202
        self,
203
        schedule_id: str,
204
        *,
205
        name: Optional[str] = None,
206
        icalendar: Optional[str] = None,
207
        timezone: Optional[str] = None,
208
        comment: Optional[str] = None,
209
    ) -> Any:
210
        """Modifies an existing schedule
211
212
        Arguments:
213
            schedule_id: UUID of the schedule to be modified
214
            name: Name of the schedule
215
            icalendar: `iCalendar`_ (RFC 5545) based data.
216
            timezone: Timezone to use for the icalender events e.g
217
                Europe/Berlin. If the datetime values in the icalendar data are
218
                missing timezone information this timezone gets applied.
219
                Otherwise the datetime values from the icalendar data are
220
                displayed in this timezone
221
            commenhedule.
222
223
        Returns:
224
            The response. See :py:meth:`send_command` for details.
225
226
        .. _iCalendar:
227
            https://tools.ietf.org/html/rfc5545
228
        """
229
        if not schedule_id:
230
            raise RequiredArgument(
231
                function=self.modify_schedule.__name__, argument='schedule_id'
232
            )
233
234
        cmd = XmlCommand("modify_schedule")
235
        cmd.set_attribute("schedule_id", schedule_id)
236
237
        if name:
238
            cmd.add_element("name", name)
239
240
        if icalendar:
241
            cmd.add_element("icalendar", icalendar)
242
243
        if timezone:
244
            cmd.add_element("timezone", timezone)
245
246
        if comment:
247
            cmd.add_element("comment", comment)
248
249
        return self._send_xml_command(cmd)
250