<s> import re <EOL> import sys <EOL> import os <EOL> from . ansi import AnsiFore , AnsiBack , AnsiStyle , Style <EOL> from . winterm import WinTerm , WinColor , WinStyle <EOL> from . win32 import windll , winapi_test <EOL> winterm = None <EOL> if windll is not None : <EOL> winterm = WinTerm ( ) <EOL> def is_stream_closed ( stream ) : <EOL> return not hasattr ( stream , '<STR_LIT>' ) or stream . closed <EOL> def is_a_tty ( stream ) : <EOL> return hasattr ( stream , '<STR_LIT>' ) and stream . isatty ( ) <EOL> class StreamWrapper ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , wrapped , converter ) : <EOL> self . __wrapped = wrapped <EOL> self . __convertor = converter <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . __wrapped , name ) <EOL> def write ( self , text ) : <EOL> self . __convertor . write ( text ) <EOL> class AnsiToWin32 ( object ) : <EOL> '''<STR_LIT>''' <EOL> ANSI_CSI_RE = re . compile ( '<STR_LIT>' ) <EOL> ANSI_OSC_RE = re . compile ( '<STR_LIT>' ) <EOL> def __init__ ( self , wrapped , convert = None , strip = None , autoreset = False ) : <EOL> self . wrapped = wrapped <EOL> self . autoreset = autoreset <EOL> self . stream = StreamWrapper ( wrapped , self ) <EOL> on_windows = os . name == '<STR_LIT>' <EOL> conversion_supported = on_windows and winapi_test ( ) <EOL> if strip is None : <EOL> strip = conversion_supported or ( not is_stream_closed ( wrapped ) and not is_a_tty ( wrapped ) ) <EOL> self . strip = strip <EOL> if convert is None : <EOL> convert = conversion_supported and not is_stream_closed ( wrapped ) and is_a_tty ( wrapped ) <EOL> self . convert = convert <EOL> self . win32_calls = self . get_win32_calls ( ) <EOL> self . on_stderr = self . wrapped is sys . stderr <EOL> def should_wrap ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . convert or self . strip or self . autoreset <EOL> def get_win32_calls ( self ) : <EOL> if self . convert and winterm : <EOL> return { <EOL> AnsiStyle . RESET_ALL : ( winterm . reset_all , ) , <EOL> AnsiStyle . BRIGHT : ( winterm . style , WinStyle . BRIGHT ) , <EOL> AnsiStyle . DIM : ( winterm . style , WinStyle . NORMAL ) , <EOL> AnsiStyle . NORMAL : ( winterm . style , WinStyle . NORMAL ) , <EOL> AnsiFore . BLACK : ( winterm . fore , WinColor . BLACK ) , <EOL> AnsiFore . RED : ( winterm . fore , WinColor . RED ) , <EOL> AnsiFore . GREEN : ( winterm . fore , WinColor . GREEN ) , <EOL> AnsiFore . YELLOW : ( winterm . fore , WinColor . YELLOW ) , <EOL> AnsiFore . BLUE : ( winterm . fore , WinColor . BLUE ) , <EOL> AnsiFore . MAGENTA : ( winterm . fore , WinColor . MAGENTA ) , <EOL> AnsiFore . CYAN : ( winterm . fore , WinColor . CYAN ) , <EOL> AnsiFore . WHITE : ( winterm . fore , WinColor . GREY ) , <EOL> AnsiFore . RESET : ( winterm . fore , ) , <EOL> AnsiFore . LIGHTBLACK_EX : ( winterm . fore , WinColor . BLACK , True ) , <EOL> AnsiFore . LIGHTRED_EX : ( winterm . fore , WinColor . RED , True ) , <EOL> AnsiFore . LIGHTGREEN_EX : ( winterm . fore , WinColor . GREEN , True ) , <EOL> AnsiFore . LIGHTYELLOW_EX : ( winterm . fore , WinColor . YELLOW , True ) , <EOL> AnsiFore . LIGHTBLUE_EX : ( winterm . fore , WinColor . BLUE , True ) , <EOL> AnsiFore . LIGHTMAGENTA_EX : ( winterm . fore , WinColor . MAGENTA , True ) , <EOL> AnsiFore . LIGHTCYAN_EX : ( winterm . fore , WinColor . CYAN , True ) , <EOL> AnsiFore . LIGHTWHITE_EX : ( winterm . fore , WinColor . GREY , True ) , <EOL> AnsiBack . BLACK : ( winterm . back , WinColor . BLACK ) , <EOL> AnsiBack . RED : ( winterm . back , WinColor . RED ) , <EOL> AnsiBack . GREEN : ( winterm . back , WinColor . GREEN ) , <EOL> AnsiBack . YELLOW : ( winterm . back , WinColor . YELLOW ) , <EOL> AnsiBack . BLUE : ( winterm . back , WinColor . BLUE ) , <EOL> AnsiBack . MAGENTA : ( winterm . back , WinColor . MAGENTA ) , <EOL> AnsiBack . CYAN : ( winterm . back , WinColor . CYAN ) , <EOL> AnsiBack . WHITE : ( winterm . back , WinColor . GREY ) , <EOL> AnsiBack . RESET : ( winterm . back , ) , <EOL> AnsiBack . LIGHTBLACK_EX : ( winterm . back , WinColor . BLACK , True ) , <EOL> AnsiBack . LIGHTRED_EX : ( winterm . back , WinColor . RED , True ) , <EOL> AnsiBack . LIGHTGREEN_EX : ( winterm . back , WinColor . GREEN , True ) , <EOL> AnsiBack . LIGHTYELLOW_EX : ( winterm . back , WinColor . YELLOW , True ) , <EOL> AnsiBack . LIGHTBLUE_EX : ( winterm . back , WinColor . BLUE , True ) , <EOL> AnsiBack . LIGHTMAGENTA_EX : ( winterm . back , WinColor . MAGENTA , True ) , <EOL> AnsiBack . LIGHTCYAN_EX : ( winterm . back , WinColor . CYAN , True ) , <EOL> AnsiBack . LIGHTWHITE_EX : ( winterm . back , WinColor . GREY , True ) , <EOL> } <EOL> return dict ( ) <EOL> def write ( self , text ) : <EOL> if self . strip or self . convert : <EOL> self . write_and_convert ( text ) <EOL> else : <EOL> self . wrapped . write ( text ) <EOL> self . wrapped . flush ( ) <EOL> if self . autoreset : <EOL> self . reset_all ( ) <EOL> def reset_all ( self ) : <EOL> if self . convert : <EOL> self . call_win32 ( '<STR_LIT:m>' , ( <NUM_LIT:0> , ) ) <EOL> elif not self . strip and not is_stream_closed ( self . wrapped ) : <EOL> self . wrapped . write ( Style . RESET_ALL ) <EOL> def write_and_convert ( self , text ) : <EOL> '''<STR_LIT>''' <EOL> cursor = <NUM_LIT:0> <EOL> text = self . convert_osc ( text ) <EOL> for match in self . ANSI_CSI_RE . finditer ( text ) : <EOL> start , end = match . span ( ) <EOL> self . write_plain_text ( text , cursor , start ) <EOL> self . convert_ansi ( * match . groups ( ) ) <EOL> cursor = end <EOL> self . write_plain_text ( text , cursor , len ( text ) ) <EOL> def write_plain_text ( self , text , start , end ) : <EOL> if start < end : <EOL> self . wrapped . write ( text [ start : end ] ) <EOL> self . wrapped . flush ( ) <EOL> def convert_ansi ( self , paramstring , command ) : <EOL> if self . convert : <EOL> params = self . extract_params ( command , paramstring ) <EOL> self . call_win32 ( command , params ) <EOL> def extract_params ( self , command , paramstring ) : <EOL> if command in '<STR_LIT>' : <EOL> params = tuple ( int ( p ) if len ( p ) != <NUM_LIT:0> else <NUM_LIT:1> for p in paramstring . split ( '<STR_LIT:;>' ) ) <EOL> while len ( params ) < <NUM_LIT:2> : <EOL> params = params + ( <NUM_LIT:1> , ) <EOL> else : <EOL> params = tuple ( int ( p ) for p in paramstring . split ( '<STR_LIT:;>' ) if len ( p ) != <NUM_LIT:0> ) <EOL> if len ( params ) == <NUM_LIT:0> : <EOL> if command in '<STR_LIT>' : <EOL> params = ( <NUM_LIT:0> , ) <EOL> elif command in '<STR_LIT>' : <EOL> params = ( <NUM_LIT:1> , ) <EOL> return params <EOL> def call_win32 ( self , command , params ) : <EOL> if command == '<STR_LIT:m>' : <EOL> for param in params : <EOL> if param in self . win32_calls : <EOL> func_args = self . win32_calls [ param ] <EOL> func = func_args [ <NUM_LIT:0> ] <EOL> args = func_args [ <NUM_LIT:1> : ] <EOL> kwargs = dict ( on_stderr = self . on_stderr ) <EOL> func ( * args , ** kwargs ) <EOL> elif command in '<STR_LIT>' : <EOL> winterm . erase_screen ( params [ <NUM_LIT:0> ] , on_stderr = self . on_stderr ) <EOL> elif command in '<STR_LIT>' : <EOL> winterm . erase_line ( params [ <NUM_LIT:0> ] , on_stderr = self . on_stderr ) <EOL> elif command in '<STR_LIT>' : <EOL> winterm . set_cursor_position ( params , on_stderr = self . on_stderr ) <EOL> elif command in '<STR_LIT>' : <EOL> n = params [ <NUM_LIT:0> ] <EOL> x , y = { '<STR_LIT:A>' : ( <NUM_LIT:0> , - n ) , '<STR_LIT:B>' : ( <NUM_LIT:0> , n ) , '<STR_LIT:C>' : ( n , <NUM_LIT:0> ) , '<STR_LIT:D>' : ( - n , <NUM_LIT:0> ) } [ command ] <EOL> winterm . cursor_adjust ( x , y , on_stderr = self . on_stderr ) <EOL> def convert_osc ( self , text ) : <EOL> for match in self . ANSI_OSC_RE . finditer ( text ) : <EOL> start , end = match . span ( ) <EOL> text = text [ : start ] + text [ end : ] <EOL> paramstring , command = match . groups ( ) <EOL> if command in '<STR_LIT>' : <EOL> params = paramstring . split ( "<STR_LIT:;>" ) <EOL> if params [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> winterm . set_title ( params [ <NUM_LIT:1> ] ) <EOL> return text </s>
