Passed
Pull Request — master (#165)
by
unknown
02:17
created

gvm.protocols.gmp   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Gmp.__init__() 0 8 1
B Gmp.__enter__() 0 31 5
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
19
"""
20
Module for communication with gvmd
21
"""
22
from typing import Any, Optional, Callable
23
24
from gvm.errors import GvmError
25
26
from gvm.protocols.base import GvmProtocol, GvmConnection
27
28
from gvm.protocols.gmpv7 import Gmp as Gmpv7
29
from gvm.protocols.gmpv8 import Gmp as Gmpv8
30
from gvm.protocols.gmpv9 import Gmp as Gmpv9
31
32
from gvm.transforms import EtreeCheckCommandTransform
33
34
from gvm.xml import XmlCommand
35
36
37
class Gmp(GvmProtocol):
38
    """Dynamically select supported GMP protocol of the remote manager daemon.
39
40
    Must be used as a `Context Manager
41
    <https://docs.python.org/3/reference/datamodel.html#context-managers>`_
42
43
    Example:
44
45
        .. code-block:: python
46
47
            from gvm.protocols.gmp import Gmp
48
49
            with Gmp(connection) as gmp:
50
                # gmp can be an instance of gvm.protocols.gmpv7.Gmp,
51
                # gvm.protocols.gmpv8.Gmp or gvm.protocols.gmpv9.Gmp depending
52
                # on the supported GMP version of the remote manager daemon
53
                resp = gmp.get_tasks()
54
55
    Attributes:
56
        connection: Connection to use to talk with the remote daemon. See
57
            :mod:`gvm.connections` for possible connection types.
58
        transform: Optional transform `callable`_ to convert response data.
59
            After each request the callable gets passed the plain response data
60
            which can be used to check the data and/or conversion into different
61
            representations like a xml dom.
62
63
            See :mod:`gvm.transforms` for existing transforms.
64
65
    .. _callable:
66
        https://docs.python.org/3/library/functions.html#callable
67
    """
68
69
    def __init__(
70
        self,
71
        connection: GvmConnection,
72
        *,
73
        transform: Optional[Callable[[str], Any]] = None
74
    ):
75
        super().__init__(connection, transform=EtreeCheckCommandTransform())
76
        self._gmp_transform = transform
77
78
    def __enter__(self):
79
        self.connect()
80
        resp = self._send_xml_command(XmlCommand("get_version"))
81
        self.disconnect()
82
83
        version_el = resp.find('version')
84
        if version_el is None:
85
            raise GvmError(
86
                'Invalid response from manager daemon while requesting the '
87
                'version information.'
88
            )
89
90
        version = version_el.text
91
        major_version = int(version[0])
92
93
        if major_version == 7:
94
            gmp_class = Gmpv7
95
        elif major_version == 8:
96
            gmp_class = Gmpv8
97
        elif major_version >= 9:
98
            gmp_class = Gmpv9
99
        else:
100
            raise GvmError(
101
                'Remote manager daemon uses an unsupported version of GMP. '
102
                'The GMP version was {}.'.format(version)
103
            )
104
105
        gmp = gmp_class(self._connection, transform=self._gmp_transform)
106
        gmp.connect()
107
108
        return gmp
109