<s> from contextlib import contextmanager <EOL> import os <EOL> import sys <EOL> try : <EOL> from ctypes import windll , Structure , c_short , c_ushort , byref <EOL> except ImportError : <EOL> windll = None <EOL> from robotide . lib . robot . errors import DataError <EOL> from robotide . lib . robot . utils import encode_output , isatty <EOL> class HighlightingStream ( object ) : <EOL> def __init__ ( self , stream , colors = '<STR_LIT>' ) : <EOL> self . stream = stream <EOL> self . _highlighter = self . _get_highlighter ( stream , colors ) <EOL> def _get_highlighter ( self , stream , colors ) : <EOL> options = { '<STR_LIT>' : Highlighter if isatty ( stream ) else NoHighlighting , <EOL> '<STR_LIT>' : Highlighter , <EOL> '<STR_LIT>' : NoHighlighting , <EOL> '<STR_LIT>' : AnsiHighlighter } <EOL> try : <EOL> highlighter = options [ colors . upper ( ) ] <EOL> except KeyError : <EOL> raise DataError ( "<STR_LIT>" <EOL> "<STR_LIT>" % colors ) <EOL> return highlighter ( stream ) <EOL> def write ( self , text , flush = True ) : <EOL> self . stream . write ( encode_output ( text ) ) <EOL> if flush : <EOL> self . flush ( ) <EOL> def flush ( self ) : <EOL> self . stream . flush ( ) <EOL> def highlight ( self , text , status = None , flush = True ) : <EOL> with self . _highlighting ( status or text ) : <EOL> self . write ( text , flush ) <EOL> def error ( self , message , level ) : <EOL> self . write ( '<STR_LIT>' , flush = False ) <EOL> self . highlight ( level , flush = False ) <EOL> self . write ( '<STR_LIT>' % message ) <EOL> @ contextmanager <EOL> def _highlighting ( self , status ) : <EOL> highlighter = self . _highlighter <EOL> start = { '<STR_LIT>' : highlighter . green , <EOL> '<STR_LIT>' : highlighter . red , <EOL> '<STR_LIT>' : highlighter . red , <EOL> '<STR_LIT>' : highlighter . yellow } [ status ] <EOL> start ( ) <EOL> try : <EOL> yield <EOL> finally : <EOL> highlighter . reset ( ) <EOL> def Highlighter ( stream ) : <EOL> if os . sep == '<STR_LIT:/>' : <EOL> return AnsiHighlighter ( stream ) <EOL> return DosHighlighter ( stream ) if windll else NoHighlighting ( stream ) <EOL> class AnsiHighlighter ( object ) : <EOL> _ANSI_GREEN = '<STR_LIT>' <EOL> _ANSI_RED = '<STR_LIT>' <EOL> _ANSI_YELLOW = '<STR_LIT>' <EOL> _ANSI_RESET = '<STR_LIT>' <EOL> def __init__ ( self , stream ) : <EOL> self . _stream = stream <EOL> def green ( self ) : <EOL> self . _set_color ( self . _ANSI_GREEN ) <EOL> def red ( self ) : <EOL> self . _set_color ( self . _ANSI_RED ) <EOL> def yellow ( self ) : <EOL> self . _set_color ( self . _ANSI_YELLOW ) <EOL> def reset ( self ) : <EOL> self . _set_color ( self . _ANSI_RESET ) <EOL> def _set_color ( self , color ) : <EOL> self . _stream . write ( color ) <EOL> class NoHighlighting ( AnsiHighlighter ) : <EOL> def _set_color ( self , color ) : <EOL> pass <EOL> class DosHighlighter ( object ) : <EOL> _FOREGROUND_GREEN = <NUM_LIT> <EOL> _FOREGROUND_RED = <NUM_LIT> <EOL> _FOREGROUND_YELLOW = <NUM_LIT> <EOL> _FOREGROUND_GREY = <NUM_LIT> <EOL> _FOREGROUND_INTENSITY = <NUM_LIT> <EOL> _BACKGROUND_MASK = <NUM_LIT> <EOL> _STDOUT_HANDLE = - <NUM_LIT:11> <EOL> _STDERR_HANDLE = - <NUM_LIT:12> <EOL> def __init__ ( self , stream ) : <EOL> self . _handle = self . _get_std_handle ( stream ) <EOL> self . _orig_colors = self . _get_colors ( ) <EOL> self . _background = self . _orig_colors & self . _BACKGROUND_MASK <EOL> def green ( self ) : <EOL> self . _set_foreground_colors ( self . _FOREGROUND_GREEN ) <EOL> def red ( self ) : <EOL> self . _set_foreground_colors ( self . _FOREGROUND_RED ) <EOL> def yellow ( self ) : <EOL> self . _set_foreground_colors ( self . _FOREGROUND_YELLOW ) <EOL> def reset ( self ) : <EOL> self . _set_colors ( self . _orig_colors ) <EOL> def _get_std_handle ( self , stream ) : <EOL> handle = self . _STDOUT_HANDLE if stream is sys . __stdout__ else self . _STDERR_HANDLE <EOL> return windll . kernel32 . GetStdHandle ( handle ) <EOL> def _get_colors ( self ) : <EOL> csbi = _CONSOLE_SCREEN_BUFFER_INFO ( ) <EOL> ok = windll . kernel32 . GetConsoleScreenBufferInfo ( self . _handle , byref ( csbi ) ) <EOL> if not ok : <EOL> return self . _FOREGROUND_GREY <EOL> return csbi . wAttributes <EOL> def _set_foreground_colors ( self , colors ) : <EOL> self . _set_colors ( colors | self . _FOREGROUND_INTENSITY | self . _background ) <EOL> def _set_colors ( self , colors ) : <EOL> windll . kernel32 . SetConsoleTextAttribute ( self . _handle , colors ) <EOL> if windll : <EOL> class _COORD ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:X>" , c_short ) , <EOL> ( "<STR_LIT:Y>" , c_short ) ] <EOL> class _SMALL_RECT ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , c_short ) , <EOL> ( "<STR_LIT>" , c_short ) , <EOL> ( "<STR_LIT>" , c_short ) , <EOL> ( "<STR_LIT>" , c_short ) ] <EOL> class _CONSOLE_SCREEN_BUFFER_INFO ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , _COORD ) , <EOL> ( "<STR_LIT>" , _COORD ) , <EOL> ( "<STR_LIT>" , c_ushort ) , <EOL> ( "<STR_LIT>" , _SMALL_RECT ) , <EOL> ( "<STR_LIT>" , _COORD ) ] </s>
