diff options
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/logging.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/logging.py | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/logging.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/logging.py new file mode 100644 index 0000000..1fb3e8a --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/logging.py | |||
@@ -0,0 +1,132 @@ | |||
1 | from __future__ import absolute_import | ||
2 | |||
3 | import contextlib | ||
4 | import logging | ||
5 | import logging.handlers | ||
6 | import os | ||
7 | |||
8 | from pip._internal.compat import WINDOWS | ||
9 | from pip._internal.utils.misc import ensure_dir | ||
10 | |||
11 | try: | ||
12 | import threading | ||
13 | except ImportError: | ||
14 | import dummy_threading as threading # type: ignore | ||
15 | |||
16 | |||
17 | try: | ||
18 | from pip._vendor import colorama | ||
19 | # Lots of different errors can come from this, including SystemError and | ||
20 | # ImportError. | ||
21 | except Exception: | ||
22 | colorama = None | ||
23 | |||
24 | |||
25 | _log_state = threading.local() | ||
26 | _log_state.indentation = 0 | ||
27 | |||
28 | |||
29 | @contextlib.contextmanager | ||
30 | def indent_log(num=2): | ||
31 | """ | ||
32 | A context manager which will cause the log output to be indented for any | ||
33 | log messages emitted inside it. | ||
34 | """ | ||
35 | _log_state.indentation += num | ||
36 | try: | ||
37 | yield | ||
38 | finally: | ||
39 | _log_state.indentation -= num | ||
40 | |||
41 | |||
42 | def get_indentation(): | ||
43 | return getattr(_log_state, 'indentation', 0) | ||
44 | |||
45 | |||
46 | class IndentingFormatter(logging.Formatter): | ||
47 | |||
48 | def format(self, record): | ||
49 | """ | ||
50 | Calls the standard formatter, but will indent all of the log messages | ||
51 | by our current indentation level. | ||
52 | """ | ||
53 | formatted = logging.Formatter.format(self, record) | ||
54 | formatted = "".join([ | ||
55 | (" " * get_indentation()) + line | ||
56 | for line in formatted.splitlines(True) | ||
57 | ]) | ||
58 | return formatted | ||
59 | |||
60 | |||
61 | def _color_wrap(*colors): | ||
62 | def wrapped(inp): | ||
63 | return "".join(list(colors) + [inp, colorama.Style.RESET_ALL]) | ||
64 | return wrapped | ||
65 | |||
66 | |||
67 | class ColorizedStreamHandler(logging.StreamHandler): | ||
68 | |||
69 | # Don't build up a list of colors if we don't have colorama | ||
70 | if colorama: | ||
71 | COLORS = [ | ||
72 | # This needs to be in order from highest logging level to lowest. | ||
73 | (logging.ERROR, _color_wrap(colorama.Fore.RED)), | ||
74 | (logging.WARNING, _color_wrap(colorama.Fore.YELLOW)), | ||
75 | ] | ||
76 | else: | ||
77 | COLORS = [] | ||
78 | |||
79 | def __init__(self, stream=None, no_color=None): | ||
80 | logging.StreamHandler.__init__(self, stream) | ||
81 | self._no_color = no_color | ||
82 | |||
83 | if WINDOWS and colorama: | ||
84 | self.stream = colorama.AnsiToWin32(self.stream) | ||
85 | |||
86 | def should_color(self): | ||
87 | # Don't colorize things if we do not have colorama or if told not to | ||
88 | if not colorama or self._no_color: | ||
89 | return False | ||
90 | |||
91 | real_stream = ( | ||
92 | self.stream if not isinstance(self.stream, colorama.AnsiToWin32) | ||
93 | else self.stream.wrapped | ||
94 | ) | ||
95 | |||
96 | # If the stream is a tty we should color it | ||
97 | if hasattr(real_stream, "isatty") and real_stream.isatty(): | ||
98 | return True | ||
99 | |||
100 | # If we have an ASNI term we should color it | ||
101 | if os.environ.get("TERM") == "ANSI": | ||
102 | return True | ||
103 | |||
104 | # If anything else we should not color it | ||
105 | return False | ||
106 | |||
107 | def format(self, record): | ||
108 | msg = logging.StreamHandler.format(self, record) | ||
109 | |||
110 | if self.should_color(): | ||
111 | for level, color in self.COLORS: | ||
112 | if record.levelno >= level: | ||
113 | msg = color(msg) | ||
114 | break | ||
115 | |||
116 | return msg | ||
117 | |||
118 | |||
119 | class BetterRotatingFileHandler(logging.handlers.RotatingFileHandler): | ||
120 | |||
121 | def _open(self): | ||
122 | ensure_dir(os.path.dirname(self.baseFilename)) | ||
123 | return logging.handlers.RotatingFileHandler._open(self) | ||
124 | |||
125 | |||
126 | class MaxLevelFilter(logging.Filter): | ||
127 | |||
128 | def __init__(self, level): | ||
129 | self.level = level | ||
130 | |||
131 | def filter(self, record): | ||
132 | return record.levelno < self.level | ||