Passed
Push — master ( be999b...2f9937 )
by Jan
02:07 queued 11s
created

JsonToHtml.get_json_data_file()   A

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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