Passed
Pull Request — master (#442)
by Jaspar
01:23
created

gvm.utils.check_command_status()   A

Complexity

Conditions 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nop 1
dl 0
loc 27
rs 9.1832
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-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=arguments-differ, redefined-builtin
20
21
import base64
22
import warnings
23
import logging
24
25
from typing import Any, List
26
from lxml import etree
27
28
from gvm.xml import create_parser
29
30
logger = logging.getLogger(__name__)
31
32
33
def deprecation(message: str):
34
    warnings.warn(message, DeprecationWarning, stacklevel=2)
35
36
37
def check_command_status(xml: str) -> bool:
38
    """Check gmp response
39
40
    Look into the gmp response and check for the status in the root element
41
42
    Arguments:
43
        xml: XML-Source
44
45
    Returns:
46
        True if valid, otherwise False
47
    """
48
49
    if xml == 0 or xml is None:
50
        logger.error("XML Command is empty.")
51
        return False
52
53
    try:
54
        root = etree.XML(xml, parser=create_parser())
55
        print(etree.tostring(root))
56
        status = root.attrib["status"]
57
        return status is not None and status[0] == "2"
58
    except KeyError as e:
59
        print(logger)
60
        logger.error("Not received an status code within the response.")
61
    except etree.Error as e:
62
        logger.error("etree.XML(xml): %s", e)
63
        return False
64
65
66
def to_bool(value: bool) -> str:
67
    return "1" if value else "0"
68
69
70
def to_base64(value: str) -> bytes:
71
    return base64.b64encode(value.encode("utf-8"))
72
73
74
def to_comma_list(value: List) -> str:
75
    return ",".join(value)
76
77
78
def add_filter(cmd, filter, filter_id):
79
    if filter:
80
        cmd.set_attribute("filter", filter)
81
82
    if filter_id:
83
        cmd.set_attribute("filt_id", filter_id)
84
85
86
def is_list_like(value: Any) -> bool:
87
    return isinstance(value, (list, tuple))
88