Passed
Pull Request — master (#119)
by
unknown
02:34
created

gmp.transforms.EtreeTransform.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
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
Module for transforming responses
20
"""
21
from lxml import etree
22
23
from gmp.errors import GmpError
24
25
26
class EtreeTransform:
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
27
    """
28
    Transform a response into a lxml.etree root element
29
    """
30
31
    def __init__(self):
32
        self._parser = etree.XMLParser(encoding='utf-8', recover=True)
0 ignored issues
show
Bug introduced by
The Module lxml.etree does not seem to have a member named XMLParser.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
33
34
    def _convert_response(self, response):
35
        return etree.XML(response, parser=self._parser)
0 ignored issues
show
Bug introduced by
The Module lxml.etree does not seem to have a member named XML.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
36
37
    def __call__(self, response):
38
        return self._convert_response(response)
39
40
41
def _check_command_status(root):
42
    status = root.attrib['status']
43
    status_text = root.attrib['status_text']
44
45
    if status is None or status[0] != '2':
46
        raise GmpError('Error in response. {0}'.format(status_text))
47
48
49
class CheckCommandTransform(EtreeTransform):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
50
    """
51
    Check the response code of a response and raise GmpError if
52
    response was an error response
53
    """
54
    def __call__(self, response):
55
        root = self._convert_response(response)
56
57
        _check_command_status(root)
58
59
        return response
60
61
62
class EtreeCheckCommandTransform(EtreeTransform):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
63
    """
64
    Transform a response into a lxml.etree root element and raise GmpError if
65
    response was an error response
66
    """
67
68
    def __call__(self, response):
69
        root = self._convert_response(response)
70
71
        _check_command_status(root)
72
73
        return root
74