Completed
Push — master ( f0b0de...698cc1 )
by Juan José
17s queued 11s
created

gvm.xml._GmpCommandFactory.resume_task_command()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 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
import defusedxml.lxml as secET
20
21
from lxml import etree
22
23
class XmlCommandElement:
24
25
    def __init__(self, element):
26
        self._element = element
27
28
    def add_element(self, name, text=None, attrs=None):
29
        node = etree.SubElement(self._element, name, attrib=attrs)
30
        node.text = text
31
        return XmlCommandElement(node)
32
33
    def set_attribute(self, name, value):
34
        self._element.set(name, value)
35
36
    def set_attributes(self, attrs):
37
        """Set several attributes at once.
38
39
        Arguments:
40
            attrs (dict): Attributes to be set on the element
41
        """
42
        for key, value in attrs.items():
43
            self._element.set(key, value)
44
45
    def append_xml_str(self, xml_text):
46
        """Append a xml element in string format."""
47
        node = secET.fromstring(xml_text)
48
        self._element.append(node)
49
50
    def to_string(self):
51
        return etree.tostring(self._element).decode('utf-8')
52
53
    def __str__(self):
54
        return self.to_string()
55
56
57
class XmlCommand(XmlCommandElement):
58
59
    def __init__(self, name):
60
        super().__init__(etree.Element(name))
61
62
63
class _GmpCommandFactory:
64
65
    """Factory to create gmp - Greenbone Management Protocol - commands
66
    """
67
68
    def delete_agent_command(self, kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
69
        """Generates xml string for delete agent on gvmd"""
70
        cmd = XmlCommand('delete_agent')
71
        for key, value in kwargs.items():
72
            cmd.set_attribute(key, value)
73
74
        return cmd.to_string()
75
76
    def delete_alert_command(self, kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
77
        """Generates xml string for delete alert on gvmd"""
78
        cmd = XmlCommand('delete_alert')
79
        for key, value in kwargs.items():
80
            cmd.set_attribute(key, value)
81
82
        return cmd.to_string()
83
84
    def delete_asset_command(self, asset_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
85
        """Generates xml string for delete asset on gvmd"""
86
        cmd = XmlCommand('delete_asset')
87
        cmd.set_attribute('asset_id', asset_id)
88
        cmd.set_attribute('ultimate', ultimate)
89
90
        return cmd.to_string()
91
92
    def delete_config_command(self, config_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
93
        """Generates xml string for delete config on gvmd"""
94
        cmd = XmlCommand('delete_config')
95
        cmd.set_attribute('config_id', config_id)
96
        cmd.set_attribute('ultimate', ultimate)
97
98
        return cmd.to_string()
99
100
    def delete_credential_command(self, credential_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
101
        """Generates xml string for delete credential on gvmd"""
102
        cmd = XmlCommand('delete_credential')
103
        cmd.set_attribute('credential_id', credential_id)
104
        cmd.set_attribute('ultimate', ultimate)
105
        return cmd.to_string()
106
107
    def delete_filter_command(self, filter_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
108
        """Generates xml string for delete filter on gvmd"""
109
        cmd = XmlCommand('delete_filter')
110
        cmd.set_attribute('filter_id', filter_id)
111
        cmd.set_attribute('ultimate', ultimate)
112
113
        return cmd.to_string()
114
115
    def delete_group_command(self, group_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
116
        """Generates xml string for delete group on gvmd"""
117
        cmd = XmlCommand('delete_group')
118
        cmd.set_attribute('group_id', group_id)
119
        cmd.set_attribute('ultimate', ultimate)
120
121
        return cmd.to_string()
122
123
    def delete_note_command(self, note_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
124
        """Generates xml string for delete note on gvmd"""
125
        cmd = XmlCommand('delete_note')
126
        cmd.set_attribute('note_id', note_id)
127
        cmd.set_attribute('ultimate', ultimate)
128
129
        return cmd.to_string()
130
131
    def delete_override_command(self, override_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
132
        """Generates xml string for delete override on gvmd"""
133
        cmd = XmlCommand('delete_override')
134
        cmd.set_attribute('override_id', override_id)
135
        cmd.set_attribute('ultimate', ultimate)
136
137
        return cmd.to_string()
138
139
    def delete_permission_command(self, permission_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
140
        """Generates xml string for delete permission on gvmd"""
141
        cmd = XmlCommand('delete_permission')
142
        cmd.set_attribute('permission_id', permission_id)
143
        cmd.set_attribute('ultimate', ultimate)
144
145
        return cmd.to_string()
146
147
    def delete_port_list_command(self, port_list_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
148
        """Generates xml string for delete port on gvmd"""
149
        cmd = XmlCommand('delete_port_list')
150
        cmd.set_attribute('port_list_id', port_list_id)
151
        cmd.set_attribute('ultimate', ultimate)
152
153
        return cmd.to_string()
154
155
    def delete_port_range_command(self, port_range_id):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
156
        """Generates xml string for delete port on gvmd"""
157
        cmd = XmlCommand('delete_port_range')
158
        cmd.set_attribute('port_range_id', port_range_id)
159
160
        return cmd.to_string()
161
162
    def delete_report_command(self, report_id):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
163
        """Generates xml string for delete report on gvmd"""
164
        cmd = XmlCommand('delete_report')
165
        cmd.set_attribute('report_id', report_id)
166
167
        return cmd.to_string()
168
169
    def delete_report_format_command(self, report_format_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
170
        """Generates xml string for delete report on gvmd"""
171
        cmd = XmlCommand('delete_report_format')
172
        cmd.set_attribute('report_format_id', report_format_id)
173
        cmd.set_attribute('ultimate', ultimate)
174
175
        return cmd.to_string()
176
177
    def delete_role_command(self, role_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
178
        """Generates xml string for delete role on gvmd"""
179
        cmd = XmlCommand('delete_role')
180
        cmd.set_attribute('role_id', role_id)
181
        cmd.set_attribute('ultimate', ultimate)
182
183
        return cmd.to_string()
184
185
    def delete_scanner_command(self, scanner_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
186
        """Generates xml string for delete scanner on gvmd"""
187
        cmd = XmlCommand('delete_scanner')
188
        cmd.set_attribute('scanner_id', scanner_id)
189
        cmd.set_attribute('ultimate', ultimate)
190
191
        return cmd.to_string()
192
193
    def delete_schedule_command(self, schedule_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
194
        """Generates xml string for delete schedule on gvmd"""
195
        # if self.ask_yes_or_no('Are you sure to delete this schedule? '):
196
        cmd = XmlCommand('delete_schedule')
197
        cmd.set_attribute('schedule_id', schedule_id)
198
        cmd.set_attribute('ultimate', ultimate)
199
200
        return cmd.to_string()
201
202
    def delete_tag_command(self, tag_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
203
        """Generates xml string for delete tag on gvmd"""
204
        cmd = XmlCommand('delete_tag')
205
        cmd.set_attribute('tag_id', tag_id)
206
        cmd.set_attribute('ultimate', ultimate)
207
208
        return cmd.to_string()
209
210
    def delete_target_command(self, target_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
211
        """Generates xml string for delete target on gvmd"""
212
        cmd = XmlCommand('delete_target')
213
        cmd.set_attribute('target_id', target_id)
214
        cmd.set_attribute('ultimate', ultimate)
215
216
        return cmd.to_string()
217
218
    def delete_task_command(self, task_id, ultimate=0):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
219
        """Generates xml string for delete task on gvmd"""
220
        cmd = XmlCommand('delete_task')
221
        cmd.set_attribute('task_id', task_id)
222
        cmd.set_attribute('ultimate', ultimate)
223
224
        return cmd.to_string()
225
226
    def delete_user_command(self, kwargs):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
227
        """Generates xml string for delete user on gvmd"""
228
        cmd = XmlCommand('delete_user')
229
230
        user_id = kwargs.get('user_id', '')
231
        if user_id:
232
            cmd.set_attribute('user_id', user_id)
233
234
        name = kwargs.get('name', '')
235
        if name:
236
            cmd.set_attribute('name', name)
237
238
        inheritor_id = kwargs.get('inheritor_id', '')
239
        if inheritor_id:
240
            cmd.set_attribute('inheritor_id', inheritor_id)
241
242
        inheritor_name = kwargs.get('inheritor_name', '')
243
        if inheritor_name:
244
            cmd.set_attribute('inheritor_name', inheritor_name)
245
246
        return cmd.to_string()
247
248
249
def pretty_print(xml):
250
    """Prints beautiful XML-Code
251
252
    This function gets an object of list<lxml.etree._Element>
253
    or directly a lxml element.
254
    Print it with good readable format.
255
256
    Arguments:
257
        xml: List<lxml.etree.Element> or directly a lxml element
258
    """
259
    if isinstance(xml, list):
260
        for item in xml:
261
            if etree.iselement(item):
262
                print(etree.tostring(item, pretty_print=True).decode('utf-8'))
263
            else:
264
                print(item)
265
    elif etree.iselement(xml):
266
        print(etree.tostring(xml, pretty_print=True).decode('utf-8'))
267