Passed
Pull Request — master (#156)
by Jan
05:07
created

ArfToJson.prepare_data()   A

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
cc 4
eloc 17
nop 2
dl 0
loc 18
ccs 13
cts 14
cp 0.9286
crap 4.0058
rs 9.55
c 0
b 0
f 0
1 1
import json
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2 1
import os
3
4 1
from ..exceptions import NotChecked
5 1
from .client_arf_input import ClientArfInput
6
7
8 1
class ArfToJson(ClientArfInput):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
9 1
    def __init__(self, args):
10 1
        super().__init__(args)
11 1
        self.out = self.arg.output
12 1
        self.verbose = self.arg.verbose
13
14 1
    def _get_message(self):
15 1
        return {
16
            'description': 'Client for generating JSON of SCAP rule evaluation results',
17
            'source_filename': 'ARF scan file',
18
        }
19
20 1
    def create_dict_of_rule(self, rule_id):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
21 1
        return self.arf_xml_parser.get_oval_tree(rule_id).save_tree_to_dict()
22
23 1
    @staticmethod
24
    def file_is_empty(path):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
25 1
        return os.stat(path).st_size == 0
26
27 1
    def save_dict_as_json(self, dict_, src):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
28 1
        if os.path.isfile(src) and not self.file_is_empty(src):
29 1
            with open(src, "r") as file_:
30 1
                data = json.load(file_)
31 1
                for key in data:
32 1
                    dict_[key] = data[key]
33 1
        with open(src, "w+") as file_:
34 1
            json.dump(dict_, file_)
35
36 1
    def prepare_data(self, rules):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
37 1
        out = []
38 1
        rule = None
39 1
        out_oval_tree_dict = dict()
40 1
        for rule in rules['rules']:
41 1
            try:
42 1
                out_oval_tree_dict[
43
                    rule + self._get_date()] = self.create_dict_of_rule(rule)
44 1
            except NotChecked as error:
45
                out_oval_tree_dict[
46
                    rule + self._get_date()] = str(error)
47 1
        if self.out is not None:
48 1
            self.save_dict_as_json(out_oval_tree_dict, self.out)
49 1
            out.append(self.out)
50
        else:
51 1
            print(
52
                str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4)))
53 1
        return out
54
55 1
    def prepare_parser(self, parser):
56 1
        super().prepare_parser(parser)
57
        self.prepare_args_when_user_can_list_in_rules(parser)
58