Passed
Push — master ( b25b04...24a582 )
by Jan
04:05 queued 11s
created

oval_graph.arf_to_json   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 75
rs 10
c 0
b 0
f 0
ccs 49
cts 50
cp 0.98
wmc 15

7 Methods

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