Completed
Push — master ( b1214c...aea294 )
by Björn
28s queued 20s
created

get_help_format_from_string()   A

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 1
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
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
from enum import Enum
20
from typing import Any, Optional
21
22
from gvm.errors import InvalidArgument, InvalidArgumentType
23
from gvm.xml import XmlCommand
24
25
26
class HelpFormat(Enum):
27
    """Enum for the help format"""
28
29
    HTML = "html"
30
    RNC = "rnc"
31
    TEXT = "text"
32
    XML = "xml"
33
34
35
def get_help_format_from_string(
36
    sort_order: Optional[str],
37
) -> Optional[HelpFormat]:
38
    """
39
    Convert a sort order string to an actual SortOrder instance.
40
41
    Arguments:
42
        sort_order: Sort order string to convert to a SortOrder
43
    """
44
    if not sort_order:
45
        return None
46
47
    try:
48
        return HelpFormat[sort_order.upper()]
49
    except KeyError:
50
        raise InvalidArgument(
51
            argument='sort_order', function=get_help_format_from_string.__name__
52
        ) from None
53
54
55
class HelpMixin:
56
    def help(
57
        self,
58
        *,
59
        help_format: Optional[HelpFormat] = None,
60
        brief: Optional[bool] = None,
61
    ) -> Any:
62
        """Get the help text
63
64
        Arguments:
65
            help_format: Format of of the help:
66
                "html", "rnc", "text" or "xml
67
            brief: If True help is brief
68
69
        Returns:
70
            The response. See :py:meth:`send_command` for details.
71
        """
72
        cmd = XmlCommand("help")
73
74
        help_type = ""
75
        if brief:
76
            help_type = "brief"
77
78
        cmd.set_attribute("type", help_type)
79
80
        if help_format:
81
            if not isinstance(help_format, HelpFormat):
82
                raise InvalidArgumentType(
83
                    function=self.help.__name__,
84
                    argument='feed_type',
85
                    arg_type=HelpFormat.__name__,
86
                )
87
88
            cmd.set_attribute("format", help_format.value)
89
90
        return self._send_xml_command(cmd)
91