Passed
Push — master ( b75d19...555acd )
by Matěj
04:11 queued 10s
created

ArfToJson.save_dict_as_json()   B

Complexity

Conditions 6

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nop 3
dl 0
loc 8
ccs 8
cts 8
cp 1
crap 6
rs 8.6666
c 0
b 0
f 0
1 1
import webbrowser
2 1
import json
3 1
import os
4 1
import shutil
5 1
import sys
6 1
import uuid
7
8 1
from .converter import Converter
9 1
from .client import Client
10 1
from .exceptions import NotChecked
11
12
13 1
class ArfToJson(Client):
14 1
    def __init__(self, args):
15 1
        super().__init__(args)
16 1
        self.show_failed_rules = self.arg.show_failed_rules
17 1
        self.show_not_selected_rules = self.arg.show_not_selected_rules
18
19 1
    def _get_message(self):
20 1
        MESSAGES = {
21
            'description': 'Client for generating JSON of SCAP rule evaluation results',
22
            '--output': 'The file where to save output.',
23
            'source_filename': 'ARF scan file',
24
        }
25 1
        return MESSAGES
26
27 1
    def create_dict_of_rule(self, rule_id):
28 1
        return self.xml_parser.get_oval_tree(rule_id).save_tree_to_dict()
29
30 1
    def file_is_empty(self, path):
31 1
        return os.stat(path).st_size == 0
32
33 1
    def save_dict_as_json(self, dict_, src):
34 1
        if os.path.isfile(src) and not self.file_is_empty(src):
35 1
            with open(src, "r") as f:
36 1
                data = json.load(f)
37 1
                for key in data:
38 1
                    dict_[key] = data[key]
39 1
        with open(src, "w+") as f:
40 1
            json.dump(dict_, f)
41
42 1
    def prepare_data(self, rules):
43 1
        out = []
44 1
        rule = None
45 1
        out_oval_tree_dict = dict()
46 1
        for rule in rules['rules']:
47 1
            try:
48 1
                out_oval_tree_dict[self.START_OF_FILE_NAME + rule +
49
                                   self.date] = self.create_dict_of_rule(rule)
50 1
            except NotChecked as error:
51
                out_oval_tree_dict[self.START_OF_FILE_NAME + rule +
52
                                   self.date] = str(error)
53 1
        if self.out is not None:
54 1
            self.save_dict_as_json(out_oval_tree_dict, self.out)
55 1
            out.append(self.out)
56
        else:
57 1
            print(
58
                str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4)))
59 1
        return out
60
61 1
    def prepare_parser(self):
62 1
        super().prepare_parser()
63 1
        self.parser.add_argument(
64
            '--show-failed-rules',
65
            action="store_true",
66
            default=False,
67
            help="Show only FAILED rules")
68 1
        self.parser.add_argument(
69
            '--show-not-selected-rules',
70
            action="store_true",
71
            default=False,
72
            help="Show notselected rules. These rules will not be visualized.")
73