| 1 | 1 |  | import sys | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | import traceback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 | 1 |  | from .command_line_client.arf_to_html import ArfToHtml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | from .command_line_client.arf_to_html_report import ArfToHtmlReport | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | from .command_line_client.arf_to_json import ArfToJson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 | 1 |  | from .command_line_client.json_to_html import JsonToHtml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 1 |  | CRED = '\033[91m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 1 |  | CEND = '\033[0m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 1 |  | def print_where_is_saved_result(results_src): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  |     if results_src: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 1 |  |         print("Results are saved:", file=sys.stderr) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |         for src in results_src: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 1 |  |             print(src, file=sys.stderr) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  | def print_detail_traceback_if_verbose(args): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |     if any(arg in args for arg in ("-v", "--verbose")): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |         traceback.print_exc() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  | def arf_to_report(args=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     try: | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         main(ArfToHtmlReport(args)) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     except Exception as error: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         print_detail_traceback_if_verbose(args) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         print((CRED + 'Error: {}' + CEND).format(error)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  | def arf_to_graph(args=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         main(ArfToHtml(args)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |     except Exception as error: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         print_detail_traceback_if_verbose(args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         print((CRED + 'Error: {}' + CEND).format(error)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  | def arf_to_json(args=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         main(ArfToJson(args)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     except Exception as error: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         print_detail_traceback_if_verbose(args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         print((CRED + 'Error: {}' + CEND).format(error)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  | def json_to_graph(args=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |     try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         main(JsonToHtml(args)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |     except Exception as error: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         print_detail_traceback_if_verbose(args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         print((CRED + 'Error: {}' + CEND).format(error)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  | def main(client): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |     results_src = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     rules = client.search_rules_id() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     if len(rules) > 1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         answers = client.run_gui_and_return_answers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         if answers is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             results_src = client.prepare_data(answers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         results_src = client.prepare_data({'rules': [rules[0]]}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     if client.arg.verbose: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         print_where_is_saved_result(results_src) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  | if __name__ == '__main__': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |     import argparse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |     parser = argparse.ArgumentParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |     subparsers = parser.add_subparsers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |     parser_arf_to_graph = subparsers.add_parser( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         'arf-to-graph', help='Executes the arf-to-graph command.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |     parser_arf_to_graph.set_defaults(command=arf_to_graph) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |     parser_arf_to_json = subparsers.add_parser( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         'arf-to-json', help='Executes the arf-to-json command.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |     parser_arf_to_json.set_defaults(command=arf_to_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |     parser_json_to_graph = subparsers.add_parser( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         'json-to-graph', help='Executes the json-to-graph command.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |     parser_json_to_graph.set_defaults(command=json_to_graph) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |     parser_arf_to_report = subparsers.add_parser( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         'arf-to-report', help='Executes the arf-to-report command.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |     parser_arf_to_report.set_defaults(command=arf_to_report) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     args, command_args = parser.parse_known_args() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |     args.command(command_args) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |