Passed
Pull Request — master (#94)
by Juan José
01:18
created

ospd.vtfilter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 113
Duplicated Lines 78.76 %

Importance

Changes 0
Metric Value
eloc 47
dl 89
loc 113
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A VtsFilter.parse_filters() 22 22 5
A VtsFilter.format_filter_value() 6 6 1
A VtsFilter.__init__() 11 11 1
B VtsFilter.get_filtered_vts_list() 31 31 7
A VtsFilter.format_vt_creation_time() 5 5 1
A VtsFilter.format_vt_modification_time() 5 5 1

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
""" Vulnerability Test Filter class.
20
"""
21
import re
22
import operator
23
24 View Code Duplication
class VtsFilter(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
    """ Helper class to filter Vulnerability Tests """
26
27
    def __init__(self):
28
        """ Initialize filter operator and allowed filters. """
29
        self.filter_operator = {
30
            '<': operator.lt,
31
            '>': operator.gt,
32
            '=': operator.eq,
33
        }
34
35
        self.allowed_filter = {
36
            'creation_time': self.format_vt_creation_time,
37
            'modification_time': self.format_vt_modification_time,
38
        }
39
40
    def parse_filters(self, vt_filter):
41
        """ Parse a string containing one or more filters
42
        and return a list of filters
43
44
        Arguments:
45
            vt_filter (string): String containing filters separated with
46
                semicolon.
47
        Return:
48
            List with filters. Each filters is a list with 3 elements
49
            e.g. [arg, operator, value]
50
        """
51
52
        filter_list = vt_filter.split(';')
53
        filters = list()
54
        for single_filter in filter_list:
55
           filters.append(re.split('(\W)', single_filter, 1))
56
57
        for _element, _oper, _val in filters:
58
            if _element not in self.allowed_filter or _oper not in self.filter_operator:
59
                filters.remove([_element, _oper, _val])
60
61
        return filters
62
63
    def format_vt_creation_time(self, value):
64
        """ In case the given creationdatetime value must be formated,
65
        this function must be implemented by the wrapper
66
        """
67
        return value
68
69
    def format_vt_modification_time(self, value):
70
        """ In case the given modification datetime value must be formated,
71
        this function must be implemented by the wrapper
72
        """
73
        return value
74
75
    def format_filter_value(self, element, value):
76
        """ Calls the specific function to format value,
77
        depending on the given element.
78
        """
79
        format_func = self.allowed_filter.get(element)
80
        return format_func(value)
81
82
    def get_filtered_vts_list(self, vts, vt_filter):
83
        """ Gets a collection of vulnerability test from the vts dictionary,
84
        which match the filter.
85
86
        Arguments:
87
            vt_filter (string): Filter to apply to the vts collection.
88
89
        Returns:
90
            Dictionary with filtered vulnerability tests.
91
        """
92
        if not vt_filter:
93
            raise RequiredArgument('vt_filter: A valid filter is required.')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable RequiredArgument does not seem to be defined.
Loading history...
94
95
        filters = self.parse_filters(vt_filter)
96
        if not filters:
97
            return None
98
99
        _vts_aux = vts.copy()
100
        for _element, _oper, _filter_val in filters:
101
            for vt_id in _vts_aux.copy():
102
                if not _vts_aux[vt_id].get(_element):
103
                    _vts_aux.pop(vt_id)
104
                    continue
105
                _elem_val = _vts_aux[vt_id].get(_element)
106
                _val = self.format_filter_value(_element, _elem_val)
107
                if self.filter_operator[_oper](_val, _filter_val):
108
                    continue
109
                else:
110
                    _vts_aux.pop(vt_id)
111
112
        return _vts_aux
113