Passed
Push — master ( 24a582...ab5396 )
by Jan
05:47 queued 03:29
created

JsonToHtml.get_json_data_file()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1 1
import webbrowser
2 1
import json
3 1
import os
4 1
import argparse
5 1
import shutil
6 1
from datetime import datetime
7 1
import sys
8 1
import re
9
10 1
from .client import Client
11 1
from .oval_node import restore_dict_to_tree
12 1
from .converter import Converter
13 1
from .exceptions import NotChecked
14
15 1
class JsonToHtml(Client):
16 1
    def __init__(self, args):
17 1
        self.parser = None
18 1
        self.MESSAGES = self._get_message()
19 1
        self.arg = self.parse_arguments(args)
20 1
        self.off_webbrowser = self.arg.off_web_browser
21 1
        self.hide_passing_tests = self.arg.hide_passing_tests
22 1
        self.source_filename = self.arg.source_filename
23 1
        self.rule_name = self.arg.rule_id
24 1
        self.out = self.arg.output
25 1
        self.all_rules = self.arg.all
26 1
        self.isatty = sys.stdout.isatty()
27 1
        self.show_fail_rules = False
28 1
        self.show_not_selected_rules = False
29 1
        self.oval_tree = None
30 1
        self.off_webbrowser = self.arg.off_web_browser
31 1
        self.json_data_file = self.get_json_data_file()
32 1
        self.parts = self.get_src('parts')
33
34 1
    def _get_message(self):
35 1
        MESSAGES = {
36
            'description': 'Client for visualization of JSON created by command arf-to-json',
37
            '--output': 'The directory where to save output directory with files.',
38
            'source_filename': 'JSON file',
39
        }
40 1
        return MESSAGES
41
42 1
    def get_json_data_file(self):
43 1
        with open(self.source_filename, 'r') as f:
44 1
            try:
45 1
                return json.load(f)
46 1
            except Exception as error:
47 1
                raise ValueError(
48
                    'Used file "{}" is not valid json.'.format(
49
                        self.source_filename))
50
51 1
    def load_json_to_oval_tree(self, rule):
52 1
        dict_of_tree = self.json_data_file[rule]
53 1
        if isinstance(dict_of_tree, str):
54
            raise NotChecked(dict_of_tree)
55 1
        try:
56 1
            return restore_dict_to_tree(dict_of_tree)
57 1
        except Exception as error:
58 1
            raise ValueError('Data is not valid for OVAL tree.')
59
60 1
    def create_dict_of_oval_node(self, oval_node):
61 1
        converter = Converter(oval_node)
62 1
        return converter.to_JsTree_dict(self.hide_passing_tests)
63
64 1
    def load_rule_names(self):
65 1
        return self.json_data_file.keys()
66
67 1
    def get_rules_id(self):
68 1
        out = []
69 1
        for id_ in self.load_rule_names():
70 1
            out.append(id_)
71 1
        return out
72
73 1
    def get_choices(self):
74
        rules = self.search_rules_id()
75
        choices = []
76
        for rule in rules:
77
            choices.append(rule)
78
        return choices
79
80 1
    def search_rules_id(self):
81 1
        rules = self._get_wanted_rules_from_array_of_IDs(self.get_rules_id())
82 1
        notselected_rules = []
83 1
        return self._check_rules_id(rules, notselected_rules)
84
85 1
    def prepare_data(self, rules):
86 1
        out = []
87 1
        for rule in rules["rules"]:
88 1
            try:
89 1
                self.oval_tree = self.load_json_to_oval_tree(rule)
90 1
                oval_tree_dict = self.create_dict_of_oval_node(
91
                    self.oval_tree)
92 1
                src = self.get_save_src(
93
                    rule.replace('graph-of-', '') + "-")
94 1
                self.save_html_and_open_html(
95
                    oval_tree_dict, src, rule, out)
96 1
            except NotChecked as error:
97
                self.print_red_text(error)
98 1
        return out
99
100 1
    def prepare_parser(self):
101 1
        super().prepare_parser()
102 1
        self.parser.add_argument(
103
            '--off-web-browser',
104
            action="store_true",
105
            default=False,
106
            help="It does not start the web browser.")
107