<s> from __future__ import absolute_import <EOL> import contextlib <EOL> import logging <EOL> import logging . handlers <EOL> import os <EOL> try : <EOL> import threading <EOL> except ImportError : <EOL> import dummy_threading as threading <EOL> from pip . compat import WINDOWS <EOL> from pip . utils import ensure_dir <EOL> try : <EOL> from pip . _vendor import colorama <EOL> except Exception : <EOL> colorama = None <EOL> _log_state = threading . local ( ) <EOL> _log_state . indentation = <NUM_LIT:0> <EOL> @ contextlib . contextmanager <EOL> def indent_log ( num = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> _log_state . indentation += num <EOL> try : <EOL> yield <EOL> finally : <EOL> _log_state . indentation -= num <EOL> def get_indentation ( ) : <EOL> return getattr ( _log_state , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> class IndentingFormatter ( logging . Formatter ) : <EOL> def format ( self , record ) : <EOL> """<STR_LIT>""" <EOL> formatted = logging . Formatter . format ( self , record ) <EOL> formatted = "<STR_LIT>" . join ( [ <EOL> ( "<STR_LIT:U+0020>" * get_indentation ( ) ) + line <EOL> for line in formatted . splitlines ( True ) <EOL> ] ) <EOL> return formatted <EOL> def _color_wrap ( * colors ) : <EOL> def wrapped ( inp ) : <EOL> return "<STR_LIT>" . join ( list ( colors ) + [ inp , colorama . Style . RESET_ALL ] ) <EOL> return wrapped <EOL> class ColorizedStreamHandler ( logging . StreamHandler ) : <EOL> if colorama : <EOL> COLORS = [ <EOL> ( logging . ERROR , _color_wrap ( colorama . Fore . RED ) ) , <EOL> ( logging . WARNING , _color_wrap ( colorama . Fore . YELLOW ) ) , <EOL> ] <EOL> else : <EOL> COLORS = [ ] <EOL> def __init__ ( self , stream = None ) : <EOL> logging . StreamHandler . __init__ ( self , stream ) <EOL> if WINDOWS and colorama : <EOL> self . stream = colorama . AnsiToWin32 ( self . stream ) <EOL> def should_color ( self ) : <EOL> if not colorama : <EOL> return False <EOL> real_stream = ( <EOL> self . stream if not isinstance ( self . stream , colorama . AnsiToWin32 ) <EOL> else self . stream . wrapped <EOL> ) <EOL> if hasattr ( real_stream , "<STR_LIT>" ) and real_stream . isatty ( ) : <EOL> return True <EOL> if os . environ . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> return True <EOL> return False <EOL> def format ( self , record ) : <EOL> msg = logging . StreamHandler . format ( self , record ) <EOL> if self . should_color ( ) : <EOL> for level , color in self . COLORS : <EOL> if record . levelno >= level : <EOL> msg = color ( msg ) <EOL> break <EOL> return msg <EOL> class BetterRotatingFileHandler ( logging . handlers . RotatingFileHandler ) : <EOL> def _open ( self ) : <EOL> ensure_dir ( os . path . dirname ( self . baseFilename ) ) <EOL> return logging . handlers . RotatingFileHandler . _open ( self ) <EOL> class MaxLevelFilter ( logging . Filter ) : <EOL> def __init__ ( self , level ) : <EOL> self . level = level <EOL> def filter ( self , record ) : <EOL> return record . levelno < self . level </s>
