<s> import urwid <EOL> TABSTOP = <NUM_LIT:8> <EOL> class SourceLine ( urwid . FlowWidget ) : <EOL> def __init__ ( self , dbg_ui , text , line_nr = '<STR_LIT>' , attr = None , has_breakpoint = False ) : <EOL> self . dbg_ui = dbg_ui <EOL> self . text = text <EOL> self . attr = attr <EOL> self . line_nr = line_nr <EOL> self . has_breakpoint = has_breakpoint <EOL> self . is_current = False <EOL> self . highlight = False <EOL> def selectable ( self ) : <EOL> return True <EOL> def set_current ( self , is_current ) : <EOL> self . is_current = is_current <EOL> self . _invalidate ( ) <EOL> def set_highlight ( self , highlight ) : <EOL> self . highlight = highlight <EOL> self . _invalidate ( ) <EOL> def set_breakpoint ( self , has_breakpoint ) : <EOL> self . has_breakpoint = has_breakpoint <EOL> self . _invalidate ( ) <EOL> def rows ( self , size , focus = False ) : <EOL> return <NUM_LIT:1> <EOL> def render ( self , size , focus = False ) : <EOL> from pudb . debugger import CONFIG <EOL> render_line_nr = CONFIG [ "<STR_LIT>" ] <EOL> maxcol = size [ <NUM_LIT:0> ] <EOL> hscroll = self . dbg_ui . source_hscroll_start <EOL> attrs = [ ] <EOL> if self . is_current : <EOL> crnt = "<STR_LIT:>>" <EOL> attrs . append ( "<STR_LIT>" ) <EOL> else : <EOL> crnt = "<STR_LIT:U+0020>" <EOL> if self . has_breakpoint : <EOL> bp = "<STR_LIT:*>" <EOL> attrs . append ( "<STR_LIT>" ) <EOL> else : <EOL> bp = "<STR_LIT:U+0020>" <EOL> if focus : <EOL> attrs . append ( "<STR_LIT>" ) <EOL> elif self . highlight : <EOL> if not self . has_breakpoint : <EOL> attrs . append ( "<STR_LIT>" ) <EOL> text = self . text <EOL> if not attrs and self . attr is not None : <EOL> attr = self . attr + [ ( "<STR_LIT:source>" , None ) ] <EOL> else : <EOL> attr = [ ( "<STR_LIT:U+0020>" . join ( attrs + [ "<STR_LIT:source>" ] ) , None ) ] <EOL> from urwid . util import apply_target_encoding , trim_text_attr_cs <EOL> line_prefix = "<STR_LIT>" <EOL> line_prefix_attr = [ ] <EOL> if render_line_nr : <EOL> line_prefix_attr = [ ( "<STR_LIT>" , len ( self . line_nr ) ) ] <EOL> line_prefix = self . line_nr <EOL> line_prefix = crnt + bp + line_prefix <EOL> line_prefix_attr = [ ( "<STR_LIT:source>" , <NUM_LIT:1> ) , ( "<STR_LIT>" , <NUM_LIT:1> ) ] + line_prefix_attr <EOL> line_prefix_len = len ( line_prefix ) <EOL> encoded_line_prefix , line_prefix_cs = apply_target_encoding ( line_prefix ) <EOL> assert len ( encoded_line_prefix ) == len ( line_prefix ) <EOL> cs = [ ] <EOL> encoded_text_segs = [ ] <EOL> encoded_attr = [ ] <EOL> i = <NUM_LIT:0> <EOL> for seg_attr , seg_len in attr : <EOL> if seg_len is None : <EOL> l = hscroll + maxcol <EOL> remaining_text = text [ i : ] <EOL> encoded_seg_text , seg_cs = apply_target_encoding ( <EOL> remaining_text + l * "<STR_LIT:U+0020>" ) <EOL> encoded_attr . append ( ( seg_attr , len ( remaining_text ) + l ) ) <EOL> else : <EOL> unencoded_seg_text = text [ i : i + seg_len ] <EOL> encoded_seg_text , seg_cs = apply_target_encoding ( unencoded_seg_text ) <EOL> adjustment = len ( encoded_seg_text ) - len ( unencoded_seg_text ) <EOL> encoded_attr . append ( ( seg_attr , seg_len + adjustment ) ) <EOL> i += seg_len <EOL> encoded_text_segs . append ( encoded_seg_text ) <EOL> cs . extend ( seg_cs ) <EOL> encoded_text = b"<STR_LIT>" . join ( encoded_text_segs ) <EOL> encoded_text , encoded_attr , cs = trim_text_attr_cs ( <EOL> encoded_text , encoded_attr , cs , <EOL> hscroll , hscroll + maxcol - line_prefix_len ) <EOL> encoded_text = encoded_line_prefix + encoded_text <EOL> encoded_attr = line_prefix_attr + encoded_attr <EOL> cs = line_prefix_cs + cs <EOL> return urwid . TextCanvas ( [ encoded_text ] , [ encoded_attr ] , [ cs ] , maxcol = maxcol ) <EOL> def keypress ( self , size , key ) : <EOL> return key <EOL> def format_source ( debugger_ui , lines , breakpoints ) : <EOL> lineno_format = "<STR_LIT>" % ( len ( str ( len ( lines ) ) ) ) <EOL> try : <EOL> import pygments <EOL> except ImportError : <EOL> return [ SourceLine ( debugger_ui , <EOL> line . rstrip ( "<STR_LIT>" ) . expandtabs ( TABSTOP ) , <EOL> lineno_format % ( i + <NUM_LIT:1> ) , None , <EOL> has_breakpoint = i + <NUM_LIT:1> in breakpoints ) <EOL> for i , line in enumerate ( lines ) ] <EOL> else : <EOL> from pygments import highlight <EOL> from pygments . lexers import PythonLexer <EOL> from pygments . formatter import Formatter <EOL> import pygments . token as t <EOL> result = [ ] <EOL> ATTR_MAP = { <EOL> t . Token : "<STR_LIT:source>" , <EOL> t . Keyword : "<STR_LIT>" , <EOL> t . Literal : "<STR_LIT>" , <EOL> t . Name . Function : "<STR_LIT:name>" , <EOL> t . Name . Class : "<STR_LIT:name>" , <EOL> t . Punctuation : "<STR_LIT>" , <EOL> t . String : "<STR_LIT:string>" , <EOL> t . String . Double : "<STR_LIT>" , <EOL> t . String . Single : "<STR_LIT>" , <EOL> t . String . Backtick : "<STR_LIT>" , <EOL> t . String . Doc : "<STR_LIT>" , <EOL> t . Comment : "<STR_LIT>" , <EOL> } <EOL> class UrwidFormatter ( Formatter ) : <EOL> def __init__ ( subself , ** options ) : <EOL> Formatter . __init__ ( subself , ** options ) <EOL> subself . current_line = "<STR_LIT>" <EOL> subself . current_attr = [ ] <EOL> subself . lineno = <NUM_LIT:1> <EOL> def format ( subself , tokensource , outfile ) : <EOL> def add_snippet ( ttype , s ) : <EOL> if not s : <EOL> return <EOL> while not ttype in ATTR_MAP : <EOL> if ttype . parent is not None : <EOL> ttype = ttype . parent <EOL> else : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % str ( ttype ) ) <EOL> attr = ATTR_MAP [ ttype ] <EOL> subself . current_line += s <EOL> subself . current_attr . append ( ( attr , len ( s ) ) ) <EOL> def shipout_line ( ) : <EOL> result . append ( <EOL> SourceLine ( debugger_ui , <EOL> subself . current_line , <EOL> lineno_format % subself . lineno , <EOL> subself . current_attr , <EOL> has_breakpoint = subself . lineno in breakpoints ) ) <EOL> subself . current_line = "<STR_LIT>" <EOL> subself . current_attr = [ ] <EOL> subself . lineno += <NUM_LIT:1> <EOL> for ttype , value in tokensource : <EOL> while True : <EOL> newline_pos = value . find ( "<STR_LIT:\n>" ) <EOL> if newline_pos == - <NUM_LIT:1> : <EOL> add_snippet ( ttype , value ) <EOL> break <EOL> else : <EOL> add_snippet ( ttype , value [ : newline_pos ] ) <EOL> shipout_line ( ) <EOL> value = value [ newline_pos + <NUM_LIT:1> : ] <EOL> if subself . current_line : <EOL> shipout_line ( ) <EOL> highlight ( "<STR_LIT>" . join ( l . expandtabs ( TABSTOP ) for l in lines ) , <EOL> PythonLexer ( stripnl = False ) , UrwidFormatter ( ) ) <EOL> return result </s>
