| 1 | 1 |  | import os | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     def get_src(src): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |         _dir = os.path.dirname(os.path.realpath(__file__)) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 1 |  |         return str(os.path.join(_dir, src)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |     def prepare_data(self, rules): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         out_src = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |         oval_tree_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         self._prepare_data(rules, oval_tree_dict, out_src) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         self.open_results_in_web_browser(out_src) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         return out_src | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |     def _prepare_data(self, rules, dict_oval_trees, out_src): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         for rule in rules['rules']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |                 self._put_to_dict_oval_trees(dict_oval_trees, rule) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |                 if not self.all_in_one: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |                     self._build_and_save_html( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                         dict_oval_trees, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                         self._get_src_for_one_graph(rule), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         dict(rules=[rule]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                         out_src | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |                     dict_oval_trees = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             except NotChecked as error: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 start_red_color = '\033[91m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 end_red_color = '\033[0m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 print(start_red_color + str(error) + end_red_color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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_src_for_one_graph(self, rule): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         return self.get_save_src(rule + self.date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |     def get_save_src(self, rule): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         if self.out is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             os.makedirs(self.out, exist_ok=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             return os.path.join( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 self.out, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 self.start_of_file_name + rule + '.html') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         return os.path.join( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             tempfile.gettempdir(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             self.start_of_file_name + rule + '.html') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |     def open_results_in_web_browser(self, paths_to_results): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         if self.display_html: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |                 self.web_browsers.append( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     subprocess.Popen(["firefox", *paths_to_results])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             except subprocess.CalledProcessError: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 default_web_browser_name = webbrowser.get().name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 self.web_browsers.append( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     subprocess.Popen([default_web_browser_name, *paths_to_results])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |     def kill_web_browsers(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         for web_browser in self.web_browsers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |             web_browser.kill() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |     def prepare_args_when_output_is_html(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         self.parser.add_argument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             '-i', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             '--all-in-one', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             action="store_true", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             default=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             help="Processes all rules into one file.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         self.parser.add_argument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             '-d', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             '--display', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             action="store_true", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             default=False, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |             help="Enables opening a web browser with a graph, when is used --output.") | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |