Completed
Push — develop ( b433df...d47bf1 )
by Jace
01:48
created

gdm.CallException

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 2
ccs 1
cts 1
cp 1
wmc 0
1
"""Common exceptions, classes, and functions."""
2
3 1
import sys
4 1
import argparse
5 1
import logging
6
7 1
from . import settings
8
9
10 1
class WideHelpFormatter(argparse.HelpFormatter):
11
    """Command-line help text formatter with wider help text."""
12
13 1
    def __init__(self, *args, **kwargs):
14 1
        super().__init__(*args, max_help_position=40, **kwargs)
15
16
17 1
class WarningFormatter(logging.Formatter):
18
    """Logging formatter that displays verbose formatting for WARNING+."""
19
20 1
    def __init__(self, default_format, verbose_format, *args, **kwargs):
21 1
        super().__init__(*args, **kwargs)
22 1
        self.default_format = default_format
23 1
        self.verbose_format = verbose_format
24
25 1
    def format(self, record):
26
        """A hack to change the formatting style dynamically."""
27
        # pylint: disable=W0212
28 1
        if record.levelno > logging.INFO:
29 1
            self._style._fmt = self.verbose_format
30
        else:
31 1
            self._style._fmt = self.default_format
32 1
        return super().format(record)
33
34
35 1
def positive_int(value):
36
    """Custom `int` that must be positive."""
37 1
    value = int(value)
38 1
    if value < 1:
39 1
        raise TypeError
40 1
    return value
41
42
43 1
class _Config:
44
    """Share configuration options."""
45
46 1
    MAX_VERBOSITY = 4
47
48 1
    verbosity = 0
49 1
    indent_level = 0
50
51
52 1
def configure_logging(count=0):
53
    """Configure logging using the provided verbosity count."""
54 1
    assert _Config.MAX_VERBOSITY == 4
55
56 1
    if count == -1:
57 1
        level = settings.QUIET_LOGGING_LEVEL
58 1
        default_format = settings.DEFAULT_LOGGING_FORMAT
59 1
        verbose_format = settings.LEVELED_LOGGING_FORMAT
60 1
    elif count == 0:
61 1
        level = settings.DEFAULT_LOGGING_LEVEL
62 1
        default_format = settings.DEFAULT_LOGGING_FORMAT
63 1
        verbose_format = settings.LEVELED_LOGGING_FORMAT
64 1
    elif count == 1:
65 1
        level = settings.VERBOSE_LOGGING_LEVEL
66 1
        default_format = settings.VERBOSE_LOGGING_FORMAT
67 1
        verbose_format = settings.VERBOSE_LOGGING_FORMAT
68 1
    elif count == 2:
69 1
        level = settings.VERBOSE2_LOGGING_LEVEL
70 1
        default_format = settings.VERBOSE_LOGGING_FORMAT
71 1
        verbose_format = settings.VERBOSE_LOGGING_FORMAT
72 1
    elif count == 3:
73 1
        level = settings.VERBOSE2_LOGGING_LEVEL
74 1
        default_format = settings.VERBOSE2_LOGGING_FORMAT
75 1
        verbose_format = settings.VERBOSE2_LOGGING_FORMAT
76
    else:
77 1
        level = settings.VERBOSE2_LOGGING_LEVEL - 1
78 1
        default_format = settings.VERBOSE2_LOGGING_FORMAT
79 1
        verbose_format = settings.VERBOSE2_LOGGING_FORMAT
80
81
    # Set a custom formatter
82 1
    logging.basicConfig(level=level)
83 1
    logging.captureWarnings(True)
84 1
    formatter = WarningFormatter(default_format, verbose_format,
85
                                 datefmt=settings.LOGGING_DATEFMT)
86 1
    logging.root.handlers[0].setFormatter(formatter)
87 1
    logging.getLogger('yorm').setLevel(max(level, settings.YORM_LOGGING_LEVEL))
88 1
    logging.getLogger('sh').setLevel(max(level, settings.SH_LOGGING_LEVEL))
89
90
    # Warn about excessive verbosity
91 1
    if count > _Config.MAX_VERBOSITY:
92 1
        msg = "maximum verbosity level is {}".format(_Config.MAX_VERBOSITY)
93 1
        logging.warning(msg)
94 1
        _Config.verbosity = _Config.MAX_VERBOSITY
95
    else:
96 1
        _Config.verbosity = count
97
98
99 1
def indent():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
100 1
    _Config.indent_level += 1
101
102
103 1
def dedent(level=None):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
104 1
    if level is None:
105 1
        _Config.indent_level = max(0, _Config.indent_level - 1)
106
    else:
107 1
        _Config.indent_level = level
108
109
110 1
def show(message="", file=sys.stdout, log=logging.getLogger(__name__)):
111
    """Write to standard output or error if enabled."""
112 1
    if _Config.verbosity == 0:
113 1
        print("  " * _Config.indent_level + message, file=file)
114 1
    elif _Config.verbosity >= 1:
115 1
        message = message.strip()
116 1
        if message and log:
117
            log.info(message)
118