Passed
Pull Request — master (#95)
by Juan José
01:17
created

ospd.xml   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 48.57 %

Importance

Changes 0
Metric Value
eloc 27
dl 34
loc 70
rs 10
c 0
b 0
f 0
wmc 7

2 Functions

Rating   Name   Duplication   Size   Complexity  
A simple_response_str() 21 23 5
A get_result_xml() 13 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# Copyright (C) 2014-2018 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
""" OSP XML utils class.
20
"""
21
22
from xml.etree.ElementTree import tostring, Element, SubElement
23
from ospd.misc import ResultType
24
25 View Code Duplication
def get_result_xml(result):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
26
    """ Formats a scan result to XML format.
27
28
    Arguments:
29
        result (dict): Dictionary with a scan result.
30
31
    Return:
32
        Result as xml element object.
33
    """
34
    result_xml = Element('result')
35
    for name, value in [('name', result['name']),
36
                        ('type', ResultType.get_str(result['type'])),
37
                        ('severity', result['severity']),
38
                        ('host', result['host']),
39
                        ('test_id', result['test_id']),
40
                        ('port', result['port']),
41
                        ('qod', result['qod'])]:
42
        result_xml.set(name, str(value))
43
    result_xml.text = result['value']
44
    return result_xml
45
46
47 View Code Duplication
def simple_response_str(command, status, status_text, content=""):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
48
    """ Creates an OSP response XML string.
49
50
    Arguments:
51
        command (str): OSP Command to respond to.
52
        status (int): Status of the response.
53
        status_text (str): Status text of the response.
54
        content (str): Text part of the response XML element.
55
56
    Return:
57
        String of response in xml format.
58
    """
59
    response = Element('%s_response' % command)
60
    for name, value in [('status', str(status)), ('status_text', status_text)]:
61
        response.set(name, str(value))
62
    if isinstance(content, list):
63
        for elem in content:
64
            response.append(elem)
65
    elif isinstance(content, Element):
66
        response.append(content)
67
    else:
68
        response.text = content
69
    return tostring(response)
70