| 1 |  |  | #!/usr/bin/env python3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # -*- coding: future_fstrings -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | Output script | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import time | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from yaspin import yaspin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from db_sync_tool.utility import log, mode, system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CliFormat: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     BEIGE = '\033[96m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     PURPLE = '\033[95m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     BLUE = '\033[94m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     YELLOW = '\033[93m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     GREEN = '\033[92m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     RED = '\033[91m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     BLACK = '\033[90m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ENDC = '\033[0m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     BOLD = '\033[1m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     UNDERLINE = '\033[4m' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class Subject: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     INFO = CliFormat.GREEN + '[INFO]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     LOCAL = CliFormat.BEIGE + '[LOCAL]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     TARGET = CliFormat.BLUE + '[TARGET]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ORIGIN = CliFormat.PURPLE + '[ORIGIN]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ERROR = CliFormat.RED + '[ERROR]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     WARNING = CliFormat.YELLOW + '[WARNING]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     DEBUG = CliFormat.BLACK + '[DEBUG]' + CliFormat.ENDC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | def message(header, message, do_print=True, do_log=False, debug=False, verbose_only=False): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     Formatting a message for print or log | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     :param header: String | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     :param message: String | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     :param do_print: Boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     :param do_log: Boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     :param debug: Boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     :param verbose_only: Boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     :return: String message | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     # Logging if explicitly forced or verbose option is active | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     if do_log or system.config['verbose']: | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         _message = remove_multiple_elements_from_string([CliFormat.BEIGE, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                                                          CliFormat.PURPLE, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                                                          CliFormat.BLUE, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                                                          CliFormat.YELLOW, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                                                          CliFormat.GREEN, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                                                          CliFormat.RED, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                                                          CliFormat.BLACK, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                                                          CliFormat.ENDC, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                                                          CliFormat.BOLD, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                                                          CliFormat.UNDERLINE], message) | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         # @ToDo: Can this be done better? Dynamic functions? | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         if debug: | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             log.get_logger().debug(_message) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         elif header == Subject.WARNING: | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             log.get_logger().warning(_message) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         elif header == Subject.ERROR: | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             log.get_logger().error(_message) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         else: | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             log.get_logger().info(_message) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     # Formatting message if mute option is inactive | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     if (system.config['mute'] and header == Subject.ERROR) or (not system.config['mute']): | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         if do_print: | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             if not verbose_only or (verbose_only and system.config['verbose']): | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 _message = header + extend_output_by_sync_mode(header, debug) + ' ' + message | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 with yaspin(text=_message, color="yellow", side="right") as spinner: | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                     spinner.ok("\b\b") | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         else: | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             return header + extend_output_by_sync_mode(header, debug) + ' ' + message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | def extend_output_by_sync_mode(header, debug=False): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     Extending the output by a client information (LOCAL|REMOTE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     :param header: String | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     :return: String message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     _sync_mode = mode.get_sync_mode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     _debug = '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     if debug: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         _debug = Subject.DEBUG | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     if header == Subject.INFO or header == Subject.LOCAL or \ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             header == Subject.WARNING or header == Subject.ERROR: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if mode.is_remote(subject_to_host(header)): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             return CliFormat.BLACK + '[REMOTE]' + CliFormat.ENDC + _debug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if subject_to_host(header) == mode.Client.LOCAL: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 return _debug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 return CliFormat.BLACK + '[LOCAL]' + CliFormat.ENDC + _debug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | def host_to_subject(host): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     Converting the client to the according subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     :param host: String | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     :return: String subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     if host == mode.Client.ORIGIN: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return Subject.ORIGIN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     elif host == mode.Client.TARGET: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return Subject.TARGET | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     elif host == mode.Client.LOCAL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return Subject.LOCAL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | def subject_to_host(subject): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     Converting the subject to the according host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     :param subject: String | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     :return: String host | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     if subject == Subject.ORIGIN: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return mode.Client.ORIGIN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     elif subject == Subject.TARGET: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return mode.Client.TARGET | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     elif subject == Subject.LOCAL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return mode.Client.LOCAL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | def remove_multiple_elements_from_string(elements, string): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     Removing multiple elements from a string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     :param elements: List | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     :param string: String | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     :return: String string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     for element in elements: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         if element in string: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             string = string.replace(element, '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  |  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |