Passed
Pull Request — master (#129)
by Jan
10:55 queued 04:58
created

oval_graph.arf_to_json   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 28.57 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 48
dl 18
loc 63
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A ArfToJson.file_is_empty() 0 2 1
A ArfToJson._get_message() 0 6 1
A ArfToJson.prepare_parser() 0 3 1
B ArfToJson.save_dict_as_json() 0 8 6
A ArfToJson._set_attributes() 0 4 1
A ArfToJson.prepare_data() 18 18 4
A ArfToJson.create_dict_of_rule() 0 2 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
'''
2
    This file contains a class for command arf-to-json
3
'''
4
5
import json
6
import os
7
8
from .client import Client
9
from .exceptions import NotChecked
10
from .xml_parser import XmlParser
11
12
13
class ArfToJson(Client):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
14
    def _set_attributes(self):
15
        self.show_failed_rules = self.arg.show_failed_rules
16
        self.show_not_selected_rules = self.arg.show_not_selected_rules
17
        self.xml_parser = XmlParser(self.source_filename)
18
19
    def _get_message(self):
20
        MESSAGES = {
0 ignored issues
show
Coding Style Naming introduced by
Variable name "MESSAGES" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
21
            'description': 'Client for generating JSON of SCAP rule evaluation results',
22
            'source_filename': 'ARF scan file',
23
        }
24
        return MESSAGES
25
26
    def create_dict_of_rule(self, rule_id):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
27
        return self.xml_parser.get_oval_tree(rule_id).save_tree_to_dict()
28
29
    def file_is_empty(self, path):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
30
        return os.stat(path).st_size == 0
31
32
    def save_dict_as_json(self, dict_, src):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
33
        if os.path.isfile(src) and not self.file_is_empty(src):
34
            with open(src, "r") as f:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "f" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
35
                data = json.load(f)
36
                for key in data:
37
                    dict_[key] = data[key]
38
        with open(src, "w+") as f:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "f" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
39
            json.dump(dict_, f)
40
41 View Code Duplication
    def prepare_data(self, rules):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
        out = []
43
        rule = None
44
        out_oval_tree_dict = dict()
45
        for rule in rules['rules']:
46
            try:
47
                out_oval_tree_dict[self.START_OF_FILE_NAME + rule +
48
                                   self.date] = self.create_dict_of_rule(rule)
49
            except NotChecked as error:
50
                out_oval_tree_dict[self.START_OF_FILE_NAME + rule +
51
                                   self.date] = str(error)
52
        if self.out is not None:
53
            self.save_dict_as_json(out_oval_tree_dict, self.out)
54
            out.append(self.out)
55
        else:
56
            print(
57
                str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4)))
58
        return out
59
60
    def prepare_parser(self):
61
        super().prepare_parser()
62
        self.prepare_args_when_user_can_list_in_rules()
63