Passed
Pull Request — master (#156)
by Jan
04:28 queued 02:01
created

oval_graph.command_line_client.client_html_output   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 97
ccs 54
cts 62
cp 0.871
rs 10
c 0
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A ClientHtmlOutput._build_and_save_html() 0 3 1
A ClientHtmlOutput.kill_web_browsers() 0 3 2
A ClientHtmlOutput.prepare_args_when_output_is_html() 0 13 1
A ClientHtmlOutput._prepare_data() 0 16 5
A ClientHtmlOutput.get_src() 0 3 1
A ClientHtmlOutput.__init__() 0 10 3
A ClientHtmlOutput.get_save_src() 0 9 2
A ClientHtmlOutput.open_results_in_web_browser() 0 9 3
A ClientHtmlOutput.print_red_text() 0 4 1
A ClientHtmlOutput.prepare_data() 0 6 1
1 1
import webbrowser
2 1
import os
3 1
import tempfile
4 1
import subprocess
5
6 1
from .client import Client
7 1
from .._builder_html_graph import BuilderHtmlGraph
8 1
from ..exceptions import NotChecked
9
10
11 1
class ClientHtmlOutput(Client):
12 1
    def __init__(self, args):
13 1
        super().__init__(args)
14 1
        self.parts = self.get_src('../parts')
15 1
        self.START_OF_FILE_NAME = 'graph-of-'
16
17 1
        self.all_in_one = self.arg.all_in_one
18 1
        self.all_rules = True if self.all_in_one else self.arg.all
19 1
        self.display_html = True if self.out is None else self.arg.display
20 1
        self.html_builder = BuilderHtmlGraph(self.parts, self.verbose, self.all_in_one)
21 1
        self.web_browsers = []
22
23 1
    def print_red_text(self, text):
24
        CRED = '\033[91m'
25
        CEND = '\033[0m'
26
        print(CRED + str(text) + CEND)
27
28 1
    def get_src(self, src):
29 1
        _dir = os.path.dirname(os.path.realpath(__file__))
30 1
        return str(os.path.join(_dir, src))
31
32 1
    def prepare_data(self, rules):
33 1
        out_src = []
34 1
        oval_tree_dict = dict()
35 1
        self._prepare_data(rules, oval_tree_dict, out_src)
36 1
        self.open_results_in_web_browser(out_src)
37 1
        return out_src
38
39 1
    def _prepare_data(self, rules, dict_oval_trees, out_src):
40 1
        for rule in rules['rules']:
41 1
            try:
42 1
                self._put_to_dict_oval_trees(dict_oval_trees, rule)
43 1
                if not self.all_in_one:
44 1
                    self._build_and_save_html(
45
                        dict_oval_trees, self._get_src_for_one_graph(
46
                            rule), dict(
47
                            rules=[rule]), out_src)
48 1
                    dict_oval_trees = {}
49 1
            except NotChecked as error:
50
                self.print_red_text(error)
51 1
        if self.all_in_one:
52
            self._build_and_save_html(
53
                dict_oval_trees, self.get_save_src(
54
                    'rules' + self.date), rules, out_src)
55
56 1
    def _build_and_save_html(self, dict_oval_trees, src, rules, out_src):
57 1
        self.html_builder.save_html(dict_oval_trees, src, rules)
58 1
        out_src.append(src)
59
60 1
    def get_save_src(self, rule):
61 1
        if self.out is not None:
62 1
            os.makedirs(self.out, exist_ok=True)
63 1
            return os.path.join(
64
                self.out,
65
                self.START_OF_FILE_NAME + rule + '.html')
66 1
        return os.path.join(
67
            tempfile.gettempdir(),
68
            self.START_OF_FILE_NAME + rule + '.html')
69
70 1
    def open_results_in_web_browser(self, paths_to_results):
71 1
        if self.display_html:
72 1
            try:
73 1
                self.web_browsers.append(subprocess.Popen(
74
                    ["firefox", *paths_to_results]))
75
            except BaseException:
76
                default_web_browser_name = webbrowser.get().name
77
                self.web_browsers.append(subprocess.Popen(
78
                    [default_web_browser_name, *paths_to_results]))
79
80 1
    def kill_web_browsers(self):
81 1
        for web_browser in self.web_browsers:
82 1
            web_browser.kill()
83
84 1
    def prepare_args_when_output_is_html(self):
85 1
        self.parser.add_argument(
86
            '-i',
87
            '--all-in-one',
88
            action="store_true",
89
            default=False,
90
            help="Processes all rules into one file.")
91 1
        self.parser.add_argument(
92
            '-d',
93
            '--display',
94
            action="store_true",
95
            default=False,
96
            help="Enables opening a web browser with a graph, when is used --output.")
97