db_sync_tool.utility.output   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 33
eloc 81
dl 0
loc 143
rs 9.76
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
F message() 0 42 14
A subject_to_host() 0 12 4
B extend_output_by_sync_mode() 0 23 8
A host_to_subject() 0 12 4
A remove_multiple_elements_from_string() 0 11 3
1
#!/usr/bin/env python3
2
# -*- coding: future_fstrings -*-
3
4
"""
5
Output script
6
"""
7
import time
0 ignored issues
show
Unused Code introduced by
The import time seems to be unused.
Loading history...
8
from yaspin import yaspin
9
from db_sync_tool.utility import log, mode, system
10
11
12
class CliFormat:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
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:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
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):
0 ignored issues
show
Comprehensibility Bug introduced by
message is re-defining a name which is already available in the outer-scope (previously defined on line 35).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
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 \
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
Unused Code introduced by
Consider merging these comparisons with "in" to 'header in (Subject.INFO, Subject.LOCAL, Subject.WARNING, Subject.ERROR)'
Loading history...
92
            header == Subject.WARNING or header == Subject.ERROR:
93
        return ''
94
    else:
95
        if mode.is_remote(subject_to_host(header)):
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
96
            return CliFormat.BLACK + '[REMOTE]' + CliFormat.ENDC + _debug
97
        else:
98
            if subject_to_host(header) == mode.Client.LOCAL:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
99
                return _debug
100
            else:
101
                return CliFormat.BLACK + '[LOCAL]' + CliFormat.ENDC + _debug
102
103
104
def host_to_subject(host):
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
105
    """
106
    Converting the client to the according subject
107
    :param host: String
108
    :return: String subject
109
    """
110
    if host == mode.Client.ORIGIN:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
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):
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
119
    """
120
    Converting the subject to the according host
121
    :param subject: String
122
    :return: String host
123
    """
124
    if subject == Subject.ORIGIN:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
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
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
144