Passed
Pull Request — master (#149)
by Jan
04:51
created

ClientHtmlOutput.prepare_data()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 6
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1 1
import os
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2 1
import subprocess
3 1
import tempfile
4 1
import webbrowser
5
6 1
from .._builder_html_graph import BuilderHtmlGraph
7 1
from ..exceptions import NotChecked
8 1
from .client import Client
9
10 1
START_OF_FILE_NAME = 'graph-of-'
11
12
13 1
class ClientHtmlOutput(Client):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
14 1
    def __init__(self, args):
15 1
        super().__init__(args)
16 1
        self.out = self.arg.output
17 1
        self.part = self.get_src('../parts')
18 1
        if hasattr(self.arg, 'all_in_one'):
19 1
            self.all_in_one = self.arg.all_in_one
20 1
            self.all_rules = True if self.all_in_one else self.arg.all
21 1
            self.html_builder = BuilderHtmlGraph(self.part, self.arg.verbose, self.all_in_one)
22 1
        self.display_html = True if self.out is None else self.arg.display
23 1
        self.web_browsers = []
24
25 1
    @staticmethod
26
    def get_src(src):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
27 1
        _dir = os.path.dirname(os.path.realpath(__file__))
28 1
        return str(os.path.join(_dir, src))
29
30 1
    def prepare_data(self, rules):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
31 1
        out_src = []
32 1
        oval_tree_dict = dict()
33 1
        self._prepare_data(rules, oval_tree_dict, out_src)
34 1
        self.open_results_in_web_browser(out_src)
35 1
        return out_src
36
37 1
    def _put_to_dict_oval_trees(self, dict_oval_trees, rule):
38
        """
39
        The function inserts into dict_oval_trees a dictionary from
40
        the rule with the key as the rule id.
41
        """
42
        raise NotImplementedError
43
44 1
    def _prepare_data(self, rules, dict_oval_trees, out_src):
45 1
        for rule in rules['rules']:
46 1
            try:
47 1
                self._put_to_dict_oval_trees(dict_oval_trees, rule)
48 1
                if not self.all_in_one:
49 1
                    self._build_and_save_html(
50
                        dict_oval_trees,
51
                        self._get_src_for_one_graph(rule),
52
                        dict(rules=[rule]),
53
                        out_src
54
                    )
55 1
                    dict_oval_trees = {}
56 1
            except NotChecked as error:
57
                start_red_color = '\033[91m'
58
                end_red_color = '\033[0m'
59
                print(start_red_color + str(error) + end_red_color)
60 1
        if self.all_in_one:
61
            self._build_and_save_html(
62
                dict_oval_trees, self.get_save_src(
63
                    'rules' + self._get_date()), rules, out_src)
64
65 1
    def _build_and_save_html(self, dict_oval_trees, src, rules, out_src):
66 1
        self.html_builder.save_html(dict_oval_trees, src, rules)
67 1
        out_src.append(src)
68
69 1
    def _get_src_for_one_graph(self, rule):
70 1
        return self.get_save_src(rule + self._get_date())
71
72 1
    def get_save_src(self, rule):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
73 1
        if self.out is not None:
74 1
            os.makedirs(self.out, exist_ok=True)
75 1
            return os.path.join(
76
                self.out,
77
                START_OF_FILE_NAME + rule + '.html')
78 1
        return os.path.join(
79
            tempfile.gettempdir(),
80
            START_OF_FILE_NAME + rule + '.html')
81
82 1
    def open_results_in_web_browser(self, paths_to_results):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
83 1
        if self.display_html:
84 1
            try:
85 1
                self.web_browsers.append(
86
                    subprocess.Popen(["firefox", *paths_to_results]))
87
            except subprocess.CalledProcessError:
88
                default_web_browser_name = webbrowser.get().name
89
                self.web_browsers.append(
90
                    subprocess.Popen([default_web_browser_name, *paths_to_results]))
91
92 1
    def kill_web_browsers(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
93 1
        for web_browser in self.web_browsers:
94 1
            web_browser.kill()
95
96 1
    @staticmethod
97
    def prepare_args_all_in_one(parser):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
98 1
        parser.add_argument(
99
            '-i',
100
            '--all-in-one',
101
            action="store_true",
102
            default=False,
103
            help="Processes all rules into one file.")
104
105 1
    @staticmethod
106
    def prepare_args_display(parser):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
107 1
        parser.add_argument(
108
            '-d',
109
            '--display',
110
            action="store_true",
111
            default=False,
112
            help="Enables opening a web browser with a graph, when is used --output.")
113