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