Passed
Push — master ( 73c0d2...e33474 )
by Matěj
01:54 queued 10s
created

oval_graph.json_to_html   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 22
eloc 103
dl 0
loc 122
ccs 84
cts 88
cp 0.9545
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A JsonToHtml.get_rules_id() 0 5 2
A JsonToHtml.get_json_data_file() 0 8 3
A JsonToHtml.load_rule_names() 0 2 1
A JsonToHtml.load_json_to_oval_tree() 0 8 3
A JsonToHtml.get_choices() 0 6 2
A JsonToHtml.create_dict_of_oval_node() 0 3 1
A JsonToHtml._get_message() 0 7 1
A JsonToHtml.__init__() 0 21 2
A JsonToHtml.search_rules_id() 0 4 1
A JsonToHtml.create_dict_of_rule() 0 3 1
A JsonToHtml.prepare_data() 0 9 2
A JsonToHtml.prepare_parser() 0 12 1
A JsonToHtml._put_to_dict_oval_trees() 0 2 1
A JsonToHtml._get_src_for_one_graph() 0 2 1
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
16 1
class JsonToHtml(Client):
17 1
    def __init__(self, args):
18 1
        self.parser = None
19 1
        self.MESSAGES = self._get_message()
20 1
        self.arg = self.parse_arguments(args)
21 1
        self.off_webbrowser = self.arg.off_web_browser
22 1
        self.hide_passing_tests = self.arg.hide_passing_tests
23 1
        self.source_filename = self.arg.source_filename
24 1
        self.rule_name = self.arg.rule_id
25 1
        self.out = self.arg.output
26 1
        self.all_in_one = self.arg.all_in_one
27 1
        if self.all_in_one:
28
            self.all_rules = True
29
        else:
30 1
            self.all_rules = self.arg.all
31 1
        self.isatty = sys.stdout.isatty()
32 1
        self.show_failed_rules = False
33 1
        self.show_not_selected_rules = False
34 1
        self.oval_tree = None
35 1
        self.off_webbrowser = self.arg.off_web_browser
36 1
        self.json_data_file = self.get_json_data_file()
37 1
        self.parts = self.get_src('parts')
38
39 1
    def _get_message(self):
40 1
        MESSAGES = {
41
            'description': 'Client for visualization of JSON created by command arf-to-json',
42
            '--output': 'The directory where to save output directory with files.',
43
            'source_filename': 'JSON file',
44
        }
45 1
        return MESSAGES
46
47 1
    def get_json_data_file(self):
48 1
        with open(self.source_filename, 'r') as f:
49 1
            try:
50 1
                return json.load(f)
51 1
            except Exception as error:
52 1
                raise ValueError(
53
                    'Used file "{}" is not valid json.'.format(
54
                        self.source_filename))
55
56 1
    def load_json_to_oval_tree(self, rule):
57 1
        dict_of_tree = self.json_data_file[rule]
58 1
        if isinstance(dict_of_tree, str):
59
            raise NotChecked(dict_of_tree)
60 1
        try:
61 1
            return restore_dict_to_tree(dict_of_tree)
62 1
        except Exception as error:
63 1
            raise ValueError('Data is not valid for OVAL tree.')
64
65 1
    def create_dict_of_oval_node(self, oval_node):
66 1
        converter = Converter(oval_node)
67 1
        return converter.to_JsTree_dict(self.hide_passing_tests)
68
69 1
    def load_rule_names(self):
70 1
        return self.json_data_file.keys()
71
72 1
    def get_rules_id(self):
73 1
        out = []
74 1
        for id_ in self.load_rule_names():
75 1
            out.append(id_)
76 1
        return out
77
78 1
    def get_choices(self):
79 1
        rules = self.search_rules_id()
80 1
        choices = []
81 1
        for rule in rules:
82 1
            choices.append(rule)
83 1
        return choices
84
85 1
    def search_rules_id(self):
86 1
        rules = self._get_wanted_rules_from_array_of_IDs(self.get_rules_id())
87 1
        notselected_rules = []
88 1
        return self._check_rules_id(rules, notselected_rules)
89
90 1
    def create_dict_of_rule(self, rule):
91 1
        self.oval_tree = self.load_json_to_oval_tree(rule)
92 1
        return self.create_dict_of_oval_node(self.oval_tree)
93
94 1
    def _put_to_dict_oval_trees(self, dict_oval_trees, rule, date=None):
95
        dict_oval_trees[rule] = self.create_dict_of_rule(rule)
96
97 1
    def _get_src_for_one_graph(self, rule, date=None):
98 1
        return self.get_save_src(rule.replace('graph-of-', '') + "-")
99
100 1
    def prepare_data(self, rules):
101 1
        out = []
102 1
        oval_tree_dict = dict()
103 1
        if self.all_in_one:
104
            out = self._prepare_all_in_one_data(
105
                rules, oval_tree_dict, out)
106
        else:
107 1
            out = self._prepare_data_by_one(rules, oval_tree_dict, out)
108 1
        return out
109
110 1
    def prepare_parser(self):
111 1
        super().prepare_parser()
112 1
        self.parser.add_argument(
113
            '--all-in-one',
114
            action="store_true",
115
            default=False,
116
            help="Processes all rules into one file.")
117 1
        self.parser.add_argument(
118
            '--off-web-browser',
119
            action="store_true",
120
            default=False,
121
            help="It does not start the web browser.")
122