mine.common   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 86
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A WarningFormatter.__init__() 0 4 1
A WarningFormatter.format() 0 8 2
A HelpFormatter.__init__() 0 3 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B configure_logging() 0 46 7
1
"""Common exceptions, classes, and functions."""
2
3 1
import argparse
4 1
import logging
5
6
from . import settings
7 1
8
9 1
MAX_VERBOSITY = 4
10
11 1
verbosity = 0
12
13
14 1
class HelpFormatter(argparse.HelpFormatter):
15
    """Command-line help text formatter with wider help text."""
16
17 1
    def __init__(self, *args, **kwargs):
18 1
        kwargs['max_help_position'] = 40
19
        super().__init__(*args, **kwargs)
20
21 1
22
class WarningFormatter(logging.Formatter):
23
    """Logging formatter that displays verbose formatting for WARNING+."""
24 1
25 1
    def __init__(self, default_format, verbose_format, *args, **kwargs):
26 1
        super().__init__(*args, **kwargs)
27 1
        self.default_format = default_format
28
        self.verbose_format = verbose_format
29 1
30
    def format(self, record):
31
        """Python 3 hack to change the formatting style dynamically."""
32 1
        # pylint: disable=protected-access
33 1
        if record.levelno > logging.INFO:
34
            self._style._fmt = self.verbose_format
35 1
        else:
36 1
            self._style._fmt = self.default_format
37
        return super().format(record)
38
39 1
40
def configure_logging(count=0):
41 1
    """Configure logging using the provided verbosity count."""
42
    assert MAX_VERBOSITY == 4
43 1
44 1
    if count == -1:
45 1
        level = settings.QUIET_LOGGING_LEVEL
46 1
        default_format = settings.DEFAULT_LOGGING_FORMAT
47 1
        verbose_format = settings.LEVELED_LOGGING_FORMAT
48 1
    elif count == 0:
49 1
        level = settings.DEFAULT_LOGGING_LEVEL
50 1
        default_format = settings.DEFAULT_LOGGING_FORMAT
51 1
        verbose_format = settings.LEVELED_LOGGING_FORMAT
52 1
    elif count == 1:
53 1
        level = settings.VERBOSE_LOGGING_LEVEL
54 1
        default_format = settings.VERBOSE_LOGGING_FORMAT
55 1
        verbose_format = settings.VERBOSE_LOGGING_FORMAT
56 1
    elif count == 2:
57 1
        level = settings.VERBOSE2_LOGGING_LEVEL
58 1
        default_format = settings.VERBOSE_LOGGING_FORMAT
59 1
        verbose_format = settings.VERBOSE_LOGGING_FORMAT
60 1
    elif count == 3:
61 1
        level = settings.VERBOSE2_LOGGING_LEVEL
62 1
        default_format = settings.VERBOSE2_LOGGING_FORMAT
63
        verbose_format = settings.VERBOSE2_LOGGING_FORMAT
64 1
    else:
65 1
        level = settings.VERBOSE2_LOGGING_LEVEL - 1
66 1
        default_format = settings.VERBOSE2_LOGGING_FORMAT
67
        verbose_format = settings.VERBOSE2_LOGGING_FORMAT
68
69 1
    # Set a custom formatter
70 1
    logging.basicConfig(level=level)
71 1
    logging.captureWarnings(True)
72
    formatter = WarningFormatter(
73 1
        default_format, verbose_format, datefmt=settings.LOGGING_DATEFMT
74 1
    )
75
    logging.root.handlers[0].setFormatter(formatter)
76
    logging.getLogger('yorm').setLevel(max(level, settings.YORM_LOGGING_LEVEL))
77
78 1
    # Warn about excessive verbosity
79 1
    global verbosity
80 1
    if count > MAX_VERBOSITY:
81 1
        msg = "Maximum verbosity level is {}".format(MAX_VERBOSITY)
82
        logging.warning(msg)
83 1
        verbosity = MAX_VERBOSITY
84
    else:
85
        verbosity = count
86