<s> from __future__ import absolute_import <EOL> import ast <EOL> import os <EOL> import sys <EOL> import threading <EOL> from collections import defaultdict <EOL> from itertools import chain <EOL> from colorama import AnsiToWin32 <EOL> from colorama import Back <EOL> from colorama import Fore <EOL> from colorama import Style <EOL> from six import string_types <EOL> from . util import Fields <EOL> EVENT_COLORS = { <EOL> '<STR_LIT>' : Style . RESET_ALL , <EOL> '<STR_LIT>' : Style . NORMAL , <EOL> '<STR_LIT:filename>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : Style . BRIGHT + Fore . BLACK , <EOL> '<STR_LIT>' : Style . RESET_ALL , <EOL> '<STR_LIT>' : Fore . CYAN , <EOL> '<STR_LIT>' : Style . BRIGHT + Fore . BLUE , <EOL> '<STR_LIT>' : Style . BRIGHT + Fore . BLUE , <EOL> '<STR_LIT>' : Style . BRIGHT + Fore . GREEN , <EOL> '<STR_LIT>' : Style . BRIGHT + Fore . RED , <EOL> '<STR_LIT>' : Style . NORMAL , <EOL> '<STR_LIT>' : Style . RESET_ALL + Fore . MAGENTA , <EOL> '<STR_LIT>' : Style . BRIGHT , <EOL> '<STR_LIT>' : Style . BRIGHT + Back . RED + Fore . RED , <EOL> '<STR_LIT>' : Fore . WHITE , <EOL> '<STR_LIT>' : Style . BRIGHT + Back . YELLOW + Fore . YELLOW , <EOL> '<STR_LIT>' : Fore . WHITE , <EOL> } <EOL> CODE_COLORS = { <EOL> '<STR_LIT>' : Fore . RESET + Style . BRIGHT , <EOL> '<STR_LIT>' : Fore . RESET , <EOL> '<STR_LIT>' : Fore . YELLOW , <EOL> '<STR_LIT>' : Fore . RED , <EOL> } <EOL> NO_COLORS = { key : '<STR_LIT>' for key in chain ( CODE_COLORS , EVENT_COLORS ) } <EOL> MISSING = type ( '<STR_LIT>' , ( ) , { '<STR_LIT>' : lambda _ : '<STR_LIT:?>' } ) ( ) <EOL> class Action ( object ) : <EOL> def __call__ ( self , event ) : <EOL> raise NotImplementedError ( ) <EOL> class Debugger ( Fields . klass . kwargs , Action ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass = lambda ** kwargs : __import__ ( '<STR_LIT>' ) . Pdb ( ** kwargs ) , ** kwargs ) : <EOL> self . klass = klass <EOL> self . kwargs = kwargs <EOL> def __call__ ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . klass ( ** self . kwargs ) . set_trace ( event . frame ) <EOL> class Manhole ( Action ) : <EOL> def __init__ ( self , ** options ) : <EOL> self . options = options <EOL> def __call__ ( self , event ) : <EOL> import manhole <EOL> inst = manhole . install ( strict = False , thread = False , ** self . options ) <EOL> inst . handle_oneshot ( ) <EOL> class ColorStreamAction ( Fields . stream . force_colors . filename_alignment . thread_alignment . repr_limit , Action ) : <EOL> _stream_cache = { } <EOL> _stream = None <EOL> _tty = None <EOL> def __init__ ( self , <EOL> stream = sys . stderr , <EOL> force_colors = False , <EOL> filename_alignment = <NUM_LIT> , <EOL> thread_alignment = <NUM_LIT:12> , <EOL> repr_limit = <NUM_LIT> ) : <EOL> self . force_colors = force_colors <EOL> self . stream = stream <EOL> self . filename_alignment = filename_alignment <EOL> self . thread_alignment = thread_alignment <EOL> self . repr_limit = repr_limit <EOL> @ property <EOL> def stream ( self ) : <EOL> return self . _stream <EOL> @ stream . setter <EOL> def stream ( self , value ) : <EOL> if isinstance ( value , string_types ) : <EOL> if value in self . _stream_cache : <EOL> value = self . _stream_cache [ value ] <EOL> else : <EOL> value = self . _stream_cache [ value ] = open ( value , '<STR_LIT:a>' , buffering = <NUM_LIT:0> ) <EOL> isatty = getattr ( value , '<STR_LIT>' , None ) <EOL> if self . force_colors or ( isatty and isatty ( ) and os . name != '<STR_LIT>' ) : <EOL> self . _stream = AnsiToWin32 ( value , strip = False ) <EOL> self . _tty = True <EOL> self . event_colors = EVENT_COLORS <EOL> self . code_colors = CODE_COLORS <EOL> else : <EOL> self . _tty = False <EOL> self . _stream = value <EOL> self . event_colors = NO_COLORS <EOL> self . code_colors = NO_COLORS <EOL> def _safe_repr ( self , obj ) : <EOL> limit = self . repr_limit <EOL> try : <EOL> s = repr ( obj ) <EOL> s = s . replace ( '<STR_LIT:\n>' , r'<STR_LIT:\n>' ) <EOL> if len ( s ) > limit : <EOL> cutoff = limit // <NUM_LIT:2> <EOL> return "<STR_LIT>" . format ( s [ : cutoff ] , s [ - cutoff : ] , ** self . event_colors ) <EOL> else : <EOL> return s <EOL> except Exception as exc : <EOL> return "<STR_LIT>" . format ( exc , ** self . event_colors ) <EOL> class CodePrinter ( ColorStreamAction ) : <EOL> """<STR_LIT>""" <EOL> def _safe_source ( self , event ) : <EOL> try : <EOL> lines = event . _raw_fullsource . rstrip ( ) . splitlines ( ) <EOL> if lines : <EOL> return lines <EOL> else : <EOL> return "<STR_LIT>" "<STR_LIT>" . format ( event . module , ** self . event_colors ) , <EOL> return lines <EOL> except Exception as exc : <EOL> return "<STR_LIT>" . format ( exc , ** self . event_colors ) , <EOL> def _format_filename ( self , event ) : <EOL> filename = event . filename or "<STR_LIT>" <EOL> if len ( filename ) > self . filename_alignment : <EOL> filename = '<STR_LIT>' . format ( filename [ <NUM_LIT:5> - self . filename_alignment : ] ) <EOL> return filename <EOL> def __call__ ( self , event , sep = os . path . sep , join = os . path . join ) : <EOL> """<STR_LIT>""" <EOL> lines = self . _safe_source ( event ) <EOL> thread_name = threading . current_thread ( ) . name if event . tracer . threading_support else '<STR_LIT>' <EOL> thread_align = self . thread_alignment if event . tracer . threading_support else '<STR_LIT>' <EOL> self . stream . write ( <EOL> "<STR_LIT>" . format ( <EOL> self . _format_filename ( event ) , <EOL> event . lineno , <EOL> event . kind , <EOL> lines [ <NUM_LIT:0> ] , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> code = self . code_colors [ event . kind ] , <EOL> ** self . event_colors <EOL> ) ) <EOL> for line in lines [ <NUM_LIT:1> : ] : <EOL> self . stream . write ( "<STR_LIT>" . format ( <EOL> "<STR_LIT>" , <EOL> r"<STR_LIT>" , <EOL> line , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> code = self . code_colors [ event . kind ] , <EOL> ** self . event_colors <EOL> ) ) <EOL> if event . kind in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . stream . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> event . kind , <EOL> self . _safe_repr ( event . arg ) , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> color = self . event_colors [ event . kind ] , <EOL> ** self . event_colors <EOL> ) ) <EOL> class CallPrinter ( CodePrinter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** options ) : <EOL> super ( CallPrinter , self ) . __init__ ( ** options ) <EOL> self . locals = defaultdict ( list ) <EOL> def __call__ ( self , event , sep = os . path . sep , join = os . path . join ) : <EOL> """<STR_LIT>""" <EOL> filename = self . _format_filename ( event ) <EOL> ident = event . module , event . function <EOL> thread = threading . current_thread ( ) <EOL> thread_name = thread . name if event . tracer . threading_support else '<STR_LIT>' <EOL> thread_align = self . thread_alignment if event . tracer . threading_support else '<STR_LIT>' <EOL> stack = self . locals [ thread . ident ] <EOL> if event . kind == '<STR_LIT>' : <EOL> code = event . code <EOL> stack . append ( ident ) <EOL> self . stream . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> filename , <EOL> event . lineno , <EOL> event . kind , <EOL> '<STR_LIT:U+0020>' * ( len ( stack ) - <NUM_LIT:1> ) , <EOL> event . function , <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( <EOL> var , <EOL> self . _safe_repr ( event . locals . get ( var , MISSING ) ) , <EOL> ** self . event_colors <EOL> ) for var in code . co_varnames [ : code . co_argcount ] ) , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> ** self . event_colors <EOL> ) ) <EOL> elif event . kind in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . stream . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> filename , <EOL> event . lineno , <EOL> event . kind , <EOL> '<STR_LIT:U+0020>' * ( len ( stack ) - <NUM_LIT:1> ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ event . kind ] , <EOL> event . function , <EOL> self . _safe_repr ( event . arg ) , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> code = self . event_colors [ event . kind ] , <EOL> ** self . event_colors <EOL> ) ) <EOL> if stack and stack [ - <NUM_LIT:1> ] == ident : <EOL> stack . pop ( ) <EOL> else : <EOL> self . stream . write ( <EOL> "<STR_LIT>" . format ( <EOL> filename , <EOL> event . lineno , <EOL> event . kind , <EOL> '<STR_LIT:U+0020>' * len ( stack ) , <EOL> event . source . strip ( ) , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> code = self . code_colors [ event . kind ] , <EOL> ** self . event_colors <EOL> ) ) <EOL> class VarsPrinter ( Fields . names . globals . stream . filename_alignment , ColorStreamAction ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * names , ** options ) : <EOL> if not names : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . names = { <EOL> name : set ( self . _iter_symbols ( name ) ) <EOL> for name in names <EOL> } <EOL> self . globals = options . pop ( '<STR_LIT>' , False ) <EOL> super ( VarsPrinter , self ) . __init__ ( ** options ) <EOL> @ staticmethod <EOL> def _iter_symbols ( code ) : <EOL> """<STR_LIT>""" <EOL> for node in ast . walk ( ast . parse ( code ) ) : <EOL> if isinstance ( node , ast . Name ) : <EOL> yield node . id <EOL> def _safe_eval ( self , code , event ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return eval ( code , event . globals if self . globals else { } , event . locals ) <EOL> except Exception as exc : <EOL> return "<STR_LIT>" . format ( exc , ** self . event_colors ) <EOL> def __call__ ( self , event ) : <EOL> """<STR_LIT>""" <EOL> first = True <EOL> frame_symbols = set ( event . locals ) <EOL> if self . globals : <EOL> frame_symbols |= set ( event . globals ) <EOL> thread_name = threading . current_thread ( ) . name if event . tracer . threading_support else '<STR_LIT>' <EOL> thread_align = self . thread_alignment if event . tracer . threading_support else '<STR_LIT>' <EOL> for code , symbols in self . names . items ( ) : <EOL> try : <EOL> obj = eval ( code , event . globals if self . globals else { } , event . locals ) <EOL> except AttributeError : <EOL> continue <EOL> except Exception as exc : <EOL> printout = "<STR_LIT>" . format ( exc , ** self . event_colors ) <EOL> else : <EOL> printout = self . _safe_repr ( obj ) <EOL> if frame_symbols >= symbols : <EOL> self . stream . write ( "<STR_LIT>" . format ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" if first else "<STR_LIT>" , <EOL> code , <EOL> printout , <EOL> thread = thread_name , thread_align = thread_align , <EOL> align = self . filename_alignment , <EOL> ** self . event_colors <EOL> ) ) <EOL> first = False </s>
