<s> from __future__ import print_function , unicode_literals <EOL> import json <EOL> import socket <EOL> import time <EOL> from threading import Thread <EOL> class UnknownBreakpoint ( Exception ) : <EOL> pass <EOL> class ConnectionNotBootstrapped ( Exception ) : <EOL> pass <EOL> class Breakpoint ( object ) : <EOL> def __init__ ( self , bpnum , filename , line , enabled = True , temporary = False , funcname = None ) : <EOL> self . bpnum = bpnum <EOL> self . filename = filename <EOL> self . line = line <EOL> self . enabled = enabled <EOL> self . temporary = temporary <EOL> self . funcname = funcname <EOL> def __str__ ( self ) : <EOL> return str ( '<STR_LIT>' % ( self . filename , self . line ) ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % ( self . filename , self . line ) <EOL> def command_buffer ( debugger ) : <EOL> "<STR_LIT>" <EOL> remainder = b'<STR_LIT>' <EOL> while True : <EOL> new_buffer = debugger . socket . recv ( <NUM_LIT> ) <EOL> if not new_buffer : <EOL> break <EOL> else : <EOL> if new_buffer [ - <NUM_LIT:1> ] == debugger . ETX : <EOL> terminator = new_buffer [ - <NUM_LIT:1> ] <EOL> full_buffer = remainder + new_buffer [ : - <NUM_LIT:1> ] <EOL> else : <EOL> terminator = None <EOL> full_buffer = remainder + new_buffer <EOL> messages = full_buffer . split ( debugger . ETX ) <EOL> if terminator is None : <EOL> remainder = messages . pop ( ) <EOL> else : <EOL> remainder = '<STR_LIT>' <EOL> for message in messages : <EOL> message = message . decode ( '<STR_LIT:utf8>' ) <EOL> event , data = json . loads ( message ) <EOL> if hasattr ( debugger , '<STR_LIT>' % event ) : <EOL> getattr ( debugger , '<STR_LIT>' % event ) ( ** data ) <EOL> else : <EOL> print ( "<STR_LIT>" , event ) <EOL> class Debugger ( object ) : <EOL> "<STR_LIT>" <EOL> ETX = b'<STR_LIT>' <EOL> def __init__ ( self , host , port , proc = None ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . proc = proc <EOL> self . view = None <EOL> def start ( self ) : <EOL> "<STR_LIT>" <EOL> connected = False <EOL> while not connected : <EOL> try : <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . socket . connect ( ( self . host , self . port ) ) <EOL> connected = True <EOL> except socket . error as e : <EOL> print ( "<STR_LIT>" , e ) <EOL> time . sleep ( <NUM_LIT:1.0> ) <EOL> t = Thread ( target = command_buffer , args = ( self , ) ) <EOL> t . daemon = True <EOL> t . start ( ) <EOL> def stop ( self ) : <EOL> "<STR_LIT>" <EOL> if self . proc is not None : <EOL> self . output ( '<STR_LIT>' ) <EOL> self . socket . shutdown ( socket . SHUT_WR ) <EOL> if self . proc is not None : <EOL> self . proc . wait ( ) <EOL> def output ( self , event , ** data ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> dumped = json . dumps ( ( event , data ) ) <EOL> if not isinstance ( dumped , bytes ) : <EOL> dumped = dumped . encode ( '<STR_LIT:utf8>' ) <EOL> self . socket . sendall ( dumped + Debugger . ETX ) <EOL> except socket . error as e : <EOL> print ( "<STR_LIT>" , e ) <EOL> except AttributeError as e : <EOL> print ( "<STR_LIT>" , e ) <EOL> def breakpoint ( self , bp ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( bp , tuple ) : <EOL> filename , line = bp <EOL> return self . bp_index [ filename ] [ line ] <EOL> else : <EOL> return self . bp_list [ bp ] <EOL> except AttributeError : <EOL> raise ConnectionNotBootstrapped ( ) <EOL> except KeyError : <EOL> raise UnknownBreakpoint ( ) <EOL> def breakpoints ( self , filename ) : <EOL> try : <EOL> return self . bp_index . get ( filename , { } ) <EOL> except AttributeError : <EOL> raise ConnectionNotBootstrapped ( ) <EOL> def create_breakpoint ( self , filename , line , temporary = False ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' , filename = filename , line = line , temporary = temporary ) <EOL> def enable_breakpoint ( self , breakpoint ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' , bpnum = breakpoint . bpnum ) <EOL> def disable_breakpoint ( self , breakpoint ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' , bpnum = breakpoint . bpnum ) <EOL> def ignore_breakpoint ( self , breakpoint , count ) : <EOL> """<STR_LIT>""" <EOL> self . output ( '<STR_LIT:ignore>' , bpnum = breakpoint . bpnum , count = count ) <EOL> def clear_breakpoint ( self , breakpoint ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' , bpnum = breakpoint . bpnum ) <EOL> def do_run ( self ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' ) <EOL> def do_step ( self ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' ) <EOL> def do_next ( self ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' ) <EOL> def do_return ( self ) : <EOL> "<STR_LIT>" <EOL> self . output ( '<STR_LIT>' ) <EOL> def on_bootstrap ( self , breakpoints ) : <EOL> self . bp_index = { } <EOL> self . bp_list = [ None ] <EOL> for bp_data in breakpoints : <EOL> self . on_breakpoint_create ( ** bp_data ) <EOL> def on_breakpoint_create ( self , ** bp_data ) : <EOL> bp = Breakpoint ( ** bp_data ) <EOL> self . bp_index . setdefault ( bp . filename , { } ) . setdefault ( bp . line , bp ) <EOL> self . bp_list . append ( bp ) <EOL> if bp . enabled : <EOL> self . view . on_breakpoint_enable ( bp = bp ) <EOL> else : <EOL> self . view . on_breakpoint_disable ( bp = bp ) <EOL> def on_breakpoint_enable ( self , bpnum ) : <EOL> bp = self . bp_list [ bpnum ] <EOL> bp . enabled = True <EOL> self . view . on_breakpoint_enable ( bp = bp ) <EOL> def on_breakpoint_disable ( self , bpnum ) : <EOL> bp = self . bp_list [ bpnum ] <EOL> bp . enabled = False <EOL> self . view . on_breakpoint_disable ( bp = bp ) <EOL> def on_breakpoint_ignore ( self , bpnum , count ) : <EOL> bp = self . bp_list [ bpnum ] <EOL> bp . ignore = count <EOL> self . view . on_breakpoint_ignore ( bp = bp , count = count ) <EOL> def on_breakpoint_clear ( self , bpnum ) : <EOL> bp = self . bp_list [ bpnum ] <EOL> self . view . on_breakpoint_clear ( bp = bp ) <EOL> def on_stack ( self , stack ) : <EOL> self . stack = stack <EOL> self . view . on_stack ( stack = stack ) <EOL> def on_restart ( self ) : <EOL> self . view . on_restart ( ) <EOL> def on_call ( self , args ) : <EOL> self . view . on_call ( args ) <EOL> def on_return ( self , retval ) : <EOL> self . view . on_return ( retval ) <EOL> def on_line ( self , filename , line ) : <EOL> self . view . on_line ( filename , line ) <EOL> def on_exception ( self , name , value ) : <EOL> self . view . on_exception ( name = name , value = value ) <EOL> def on_postmortem ( self ) : <EOL> self . view . on_postmortem ( ) <EOL> def on_info ( self , message ) : <EOL> self . view . on_info ( message = message ) <EOL> def on_warning ( self , message ) : <EOL> self . view . on_warning ( message = message ) <EOL> def on_error ( self , message ) : <EOL> self . view . on_error ( message = message ) </s>
