Passed
Push — master ( cea4ff...5c2229 )
by Konrad
01:16
created

db_sync_tool.utility.output   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 29
eloc 60
dl 0
loc 126
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
D message() 0 31 13
A subject_to_host() 0 10 3
B extend_output_by_sync_mode() 0 19 7
A host_to_subject() 0 10 3
A remove_multiple_elements_from_string() 0 11 3
1
#!/usr/bin/env python3
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# -*- coding: future_fstrings -*-
3
4
from db_sync_tool.utility import log, mode, system
5
6
7
#
8
# GLOBALS
9
#
10
11
class CliFormat:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
12
    BEIGE = '\033[96m'
13
    PURPLE = '\033[95m'
14
    BLUE = '\033[94m'
15
    YELLOW = '\033[93m'
16
    GREEN = '\033[92m'
17
    RED = '\033[91m'
18
    BLACK = '\033[90m'
19
    ENDC = '\033[0m'
20
    BOLD = '\033[1m'
21
    UNDERLINE = '\033[4m'
22
23
24
class Subject:
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
25
    INFO = CliFormat.GREEN + '[INFO]' + CliFormat.ENDC
26
    LOCAL = CliFormat.BEIGE + '[LOCAL]' + CliFormat.ENDC
27
    TARGET = CliFormat.BLUE + '[TARGET]' + CliFormat.ENDC
28
    ORIGIN = CliFormat.PURPLE + '[ORIGIN]' + CliFormat.ENDC
29
    ERROR = CliFormat.RED + '[ERROR]' + CliFormat.ENDC
30
    WARNING = CliFormat.YELLOW + '[WARNING]' + CliFormat.ENDC
31
32
33
#
34
# FUNCTIONS
35
#
36
37
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 37).

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...
38
    """
39
    Formatting a message for print or log
40
    :param header: String
41
    :param message: String
42
    :param do_print: Boolean
43
    :param do_log: Boolean
44
    :param debug: Boolean
45
    :param verbose_only: Boolean
46
    :return: String message
47
    """
48
    # Logging if explicitly forced or verbose option is active
49
    if do_log or system.config['verbose']:
50
        _message = remove_multiple_elements_from_string([CliFormat.BEIGE, CliFormat.PURPLE, CliFormat.BLUE, CliFormat.YELLOW, CliFormat.GREEN, CliFormat.RED, CliFormat.BLACK, CliFormat.ENDC, CliFormat.BOLD, CliFormat.UNDERLINE], message)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (237/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
51
        # @ToDo: Can this be done better? Dynamic functions?
52
        if debug:
53
            log.get_logger().debug(_message)
54
        elif header == Subject.WARNING:
55
            log.get_logger().warning(_message)
56
        elif header == Subject.ERROR:
57
            log.get_logger().error(_message)
58
        else:
59
            log.get_logger().info(_message)
60
61
    # Formatting message if mute option is inactive
62
    if (system.config['mute'] and header == Subject.ERROR) or (not system.config['mute']):
63
        if do_print:
64
            if not verbose_only or (verbose_only and system.config['verbose']):
65
                print(header + extend_output_by_sync_mode(header, debug) + ' ' + message)
66
        else:
67
            return header + extend_output_by_sync_mode(header, debug) + ' ' + message
68
69
70
def extend_output_by_sync_mode(header, debug=False):
71
    """
72
    Extending the output by a client information (LOCAL|REMOTE)
73
    :param header: String
74
    :return: String message
75
    """
76
    _sync_mode = mode.get_sync_mode()
77
    _debug = ''
78
79
    if debug:
80
        _debug = CliFormat.BLACK + '[DEBUG]' + CliFormat.ENDC
81
82
    if header == Subject.INFO or header == Subject.LOCAL or header == Subject.WARNING or header == Subject.ERROR:
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (113/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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...
83
        return ''
84
    else:
85
        if mode.is_remote(subject_to_host(header)):
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
86
            return CliFormat.BLACK + '[REMOTE]' + CliFormat.ENDC + _debug
87
        else:
88
            return CliFormat.BLACK + '[LOCAL]' + CliFormat.ENDC + _debug
89
90
91
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...
92
    """
93
    Converting the client to the according subject
94
    :param host: String
95
    :return: String subject
96
    """
97
    if host == mode.Client.ORIGIN:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
98
        return Subject.ORIGIN
99
    elif host == mode.Client.TARGET:
100
        return Subject.TARGET
101
102
103
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...
104
    """
105
    Converting the subject to the according host
106
    :param subject: String
107
    :return: String host
108
    """
109
    if subject == Subject.ORIGIN:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
110
        return mode.Client.ORIGIN
111
    elif subject == Subject.TARGET:
112
        return mode.Client.TARGET
113
114
115
def remove_multiple_elements_from_string(elements, string):
116
    """
117
    Removing multiple elements from a string
118
    :param elements: List
119
    :param string: String
120
    :return: String string
121
    """
122
    for element in elements:
123
        if element in string:
124
            string = string.replace(element, '')
125
    return string
126
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
127