1
|
|
|
"""Common classes and functions.""" |
2
|
|
|
|
3
|
1 |
|
import argparse |
4
|
1 |
|
import logging |
5
|
|
|
|
6
|
1 |
|
from dtb import VERSION |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class HelpFormatter(argparse.HelpFormatter): |
10
|
|
|
"""Command-line help text formatter with wider help text.""" |
11
|
|
|
|
12
|
1 |
|
def __init__(self, *args, **kwargs): |
13
|
1 |
|
super().__init__(*args, max_help_position=40, **kwargs) |
14
|
|
|
|
15
|
|
|
|
16
|
1 |
|
class WarningFormatter(logging.Formatter, object): |
17
|
|
|
"""Displays verbose logging for level WARNING or higher.""" |
18
|
|
|
|
19
|
1 |
|
def __init__(self, default_format, verbose_format, *args, **kwargs): |
20
|
1 |
|
super().__init__(*args, **kwargs) |
21
|
1 |
|
self.default_format = default_format |
22
|
1 |
|
self.verbose_format = verbose_format |
23
|
|
|
|
24
|
1 |
|
def format(self, record): |
25
|
|
|
"""Python 3 hack to change the formatting style dynamically.""" |
26
|
1 |
|
if record.levelno > logging.INFO: |
27
|
1 |
|
self._style._fmt = self.verbose_format # pylint: disable=W0212 |
28
|
|
|
else: |
29
|
1 |
|
self._style._fmt = self.default_format # pylint: disable=W0212 |
30
|
1 |
|
return super().format(record) |
31
|
|
|
|
32
|
|
|
# Shared command-line arguments |
33
|
1 |
|
DEBUG = argparse.ArgumentParser(add_help=False) |
34
|
1 |
|
DEBUG.add_argument('-V', '--version', action='version', version=VERSION) |
35
|
1 |
|
DEBUG.add_argument('-v', '--verbose', action='count', default=0, |
36
|
|
|
help="enable verbose logging") |
37
|
|
|
SHARED = {'formatter_class': HelpFormatter, 'parents': [DEBUG]} |
38
|
|
|
|