<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from collections import deque <EOL> import urwid <EOL> from urwid import connect_signal as on <EOL> from . import sortkeys <EOL> from . stats import FlatFrozenStatistics <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> NESTED = <NUM_LIT:0> <EOL> FLAT = <NUM_LIT:1> <EOL> def get_func ( f ) : <EOL> if isinstance ( f , staticmethod ) : <EOL> return f . __func__ <EOL> return f <EOL> class Formatter ( object ) : <EOL> def _markup ( get_string , get_attr = None ) : <EOL> get_string = get_func ( get_string ) <EOL> get_attr = get_func ( get_attr ) <EOL> @ staticmethod <EOL> def markup ( * args , ** kwargs ) : <EOL> string = get_string ( * args , ** kwargs ) <EOL> if get_attr is None : <EOL> return string <EOL> attr = get_attr ( * args , ** kwargs ) <EOL> return ( attr , string ) <EOL> return markup <EOL> _numeric = { '<STR_LIT>' : '<STR_LIT:right>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def _make_text ( get_markup , ** text_kwargs ) : <EOL> get_markup = get_func ( get_markup ) <EOL> @ staticmethod <EOL> def make_text ( * args , ** kwargs ) : <EOL> markup = get_markup ( * args , ** kwargs ) <EOL> return urwid . Text ( markup , ** text_kwargs ) <EOL> return make_text <EOL> @ staticmethod <EOL> def format_percent ( ratio , denom = <NUM_LIT:1> , unit = False ) : <EOL> try : <EOL> ratio /= float ( denom ) <EOL> except ZeroDivisionError : <EOL> ratio = <NUM_LIT:0> <EOL> if round ( ratio , <NUM_LIT:2> ) >= <NUM_LIT:1> : <EOL> precision = <NUM_LIT:0> <EOL> elif round ( ratio , <NUM_LIT:2> ) >= <NUM_LIT:0.1> : <EOL> precision = <NUM_LIT:1> <EOL> else : <EOL> precision = <NUM_LIT:2> <EOL> string = ( '<STR_LIT>' + str ( precision ) + '<STR_LIT>' ) . format ( ratio * <NUM_LIT:100> ) <EOL> if unit : <EOL> return string + '<STR_LIT:%>' <EOL> else : <EOL> return string <EOL> @ staticmethod <EOL> def attr_ratio ( ratio , denom = <NUM_LIT:1> , unit = False ) : <EOL> try : <EOL> ratio /= float ( denom ) <EOL> except ZeroDivisionError : <EOL> ratio = <NUM_LIT:0> <EOL> if ratio > <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> elif ratio > <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> elif ratio > <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> elif ratio > <NUM_LIT:0.1> : <EOL> return '<STR_LIT>' <EOL> elif ratio <= <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> markup_percent = _markup ( format_percent , attr_ratio ) <EOL> make_percent_text = _make_text ( markup_percent , ** _numeric ) <EOL> @ staticmethod <EOL> def format_int ( num , units = '<STR_LIT>' ) : <EOL> unit = None <EOL> unit_iter = iter ( units ) <EOL> while abs ( round ( num , <NUM_LIT:1> ) ) >= <NUM_LIT> : <EOL> num /= <NUM_LIT> <EOL> try : <EOL> unit = next ( unit_iter ) <EOL> except StopIteration : <EOL> return '<STR_LIT>' if num > <NUM_LIT:0> else '<STR_LIT>' <EOL> if unit is None : <EOL> return '<STR_LIT>' . format ( num ) <EOL> else : <EOL> return '<STR_LIT>' . format ( num , unit ) <EOL> @ staticmethod <EOL> def attr_int ( num ) : <EOL> return None if num else '<STR_LIT>' <EOL> markup_int = _markup ( format_int , attr_int ) <EOL> make_int_text = _make_text ( markup_int , ** _numeric ) <EOL> @ staticmethod <EOL> def format_int_or_na ( num ) : <EOL> if num == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return Formatter . format_int ( num ) <EOL> markup_int_or_na = _markup ( format_int_or_na , attr_int ) <EOL> make_int_or_na_text = _make_text ( markup_int_or_na , ** _numeric ) <EOL> @ staticmethod <EOL> def format_time ( sec ) : <EOL> if sec == <NUM_LIT:0> : <EOL> return '<STR_LIT:0>' <EOL> elif sec < <NUM_LIT> : <EOL> return '<STR_LIT>' . format ( sec * <NUM_LIT> ) <EOL> elif sec < <NUM_LIT:1> : <EOL> return '<STR_LIT>' . format ( sec * <NUM_LIT> ) <EOL> elif sec < <NUM_LIT> : <EOL> return '<STR_LIT>' . format ( sec ) <EOL> elif sec < <NUM_LIT> : <EOL> return '<STR_LIT>' . format ( sec // <NUM_LIT> , sec % <NUM_LIT> ) <EOL> else : <EOL> return '<STR_LIT>' . format ( sec // <NUM_LIT> ) <EOL> @ staticmethod <EOL> def attr_time ( sec ) : <EOL> if sec == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif sec < <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> elif sec < <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> elif sec < <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> markup_time = _markup ( format_time , attr_time ) <EOL> make_time_text = _make_text ( markup_time , ** _numeric ) <EOL> @ staticmethod <EOL> def markup_stats ( stats ) : <EOL> if stats . name : <EOL> loc = ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( stats . module or stats . filename , stats . lineno ) ) <EOL> return [ ( '<STR_LIT:name>' , stats . name ) , '<STR_LIT:U+0020>' , ( '<STR_LIT>' , loc ) ] <EOL> else : <EOL> return ( '<STR_LIT>' , stats . module or stats . filename ) <EOL> make_stat_text = _make_text ( markup_stats , wrap = '<STR_LIT>' ) <EOL> del _markup <EOL> del _make_text <EOL> fmt = Formatter <EOL> class StatisticsWidget ( urwid . TreeWidget ) : <EOL> signals = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> icon_chars = ( '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:U+0020>' ) <EOL> def __init__ ( self , node ) : <EOL> super ( StatisticsWidget , self ) . __init__ ( node ) <EOL> self . _w = urwid . AttrWrap ( self . _w , None , StatisticsViewer . focus_map ) <EOL> def selectable ( self ) : <EOL> return True <EOL> @ property <EOL> def expanded ( self ) : <EOL> return self . _expanded <EOL> @ expanded . setter <EOL> def expanded ( self , expanded ) : <EOL> in_init = not hasattr ( self , '<STR_LIT>' ) <EOL> self . _expanded = expanded <EOL> if in_init : <EOL> return <EOL> if expanded : <EOL> urwid . emit_signal ( self , '<STR_LIT>' ) <EOL> else : <EOL> urwid . emit_signal ( self , '<STR_LIT>' ) <EOL> def get_mark ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_leaf : <EOL> char = self . icon_chars [ <NUM_LIT:2> ] <EOL> else : <EOL> char = self . icon_chars [ int ( self . expanded ) ] <EOL> return urwid . SelectableIcon ( ( '<STR_LIT>' , char ) , <NUM_LIT:0> ) <EOL> def load_inner_widget ( self ) : <EOL> node = self . get_node ( ) <EOL> return node . table . make_row ( node ) <EOL> def get_indented_widget ( self ) : <EOL> icon = self . get_mark ( ) <EOL> widget = self . get_inner_widget ( ) <EOL> node = self . get_node ( ) <EOL> widget = urwid . Columns ( [ ( '<STR_LIT>' , <NUM_LIT:1> , icon ) , widget ] , <NUM_LIT:1> ) <EOL> indent = ( node . get_depth ( ) - <NUM_LIT:1> ) <EOL> widget = urwid . Padding ( widget , left = indent ) <EOL> return widget <EOL> def update_mark ( self ) : <EOL> widget = self . _w . base_widget <EOL> try : <EOL> widget . widget_list [ <NUM_LIT:0> ] = self . get_mark ( ) <EOL> except ( TypeError , AttributeError ) : <EOL> return <EOL> def update_expanded_icon ( self ) : <EOL> self . update_mark ( ) <EOL> def expand ( self ) : <EOL> self . expanded = True <EOL> self . update_mark ( ) <EOL> def collapse ( self ) : <EOL> self . expanded = False <EOL> self . update_mark ( ) <EOL> def keypress ( self , size , key ) : <EOL> command = self . _command_map [ key ] <EOL> if command == urwid . ACTIVATE : <EOL> key = '<STR_LIT:->' if self . expanded else '<STR_LIT:+>' <EOL> elif command == urwid . CURSOR_RIGHT : <EOL> key = '<STR_LIT:+>' <EOL> elif self . expanded and command == urwid . CURSOR_LEFT : <EOL> key = '<STR_LIT:->' <EOL> return super ( StatisticsWidget , self ) . keypress ( size , key ) <EOL> class EmptyWidget ( urwid . Widget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rows = <NUM_LIT:0> ) : <EOL> super ( EmptyWidget , self ) . __init__ ( ) <EOL> self . _rows = rows <EOL> def rows ( self , size , focus = False ) : <EOL> return self . _rows <EOL> def render ( self , size , focus = False ) : <EOL> return urwid . SolidCanvas ( '<STR_LIT:U+0020>' , size [ <NUM_LIT:0> ] , self . rows ( size , focus ) ) <EOL> class RootStatisticsWidget ( StatisticsWidget ) : <EOL> def load_inner_widget ( self ) : <EOL> return EmptyWidget ( ) <EOL> def get_indented_widget ( self ) : <EOL> return self . get_inner_widget ( ) <EOL> def get_mark ( self ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def update ( self ) : <EOL> pass <EOL> def unexpand ( self ) : <EOL> pass <EOL> class StatisticsNodeBase ( urwid . TreeNode ) : <EOL> def __init__ ( self , stats = None , parent = None , key = None , depth = None , <EOL> table = None ) : <EOL> super ( StatisticsNodeBase , self ) . __init__ ( stats , parent , key , depth ) <EOL> self . table = table <EOL> def get_focus ( self ) : <EOL> widget , focus = super ( StatisticsNodeBase , self ) . get_focus ( ) <EOL> if self . table is not None : <EOL> self . table . walker . set_focus ( self ) <EOL> return widget , focus <EOL> def get_widget ( self , reload = False ) : <EOL> if self . _widget is None or reload : <EOL> self . _widget = self . load_widget ( ) <EOL> self . setup_widget ( self . _widget ) <EOL> return self . _widget <EOL> def load_widget ( self ) : <EOL> return self . _widget_class ( self ) <EOL> def setup_widget ( self , widget ) : <EOL> if self . table is None : <EOL> return <EOL> stats = self . get_value ( ) <EOL> if hash ( stats ) in self . table . _expanded_stat_hashes : <EOL> widget . expand ( ) <EOL> class NullStatisticsWidget ( StatisticsWidget ) : <EOL> def __init__ ( self , node ) : <EOL> urwid . TreeWidget . __init__ ( self , node ) <EOL> def get_inner_widget ( self ) : <EOL> widget = urwid . Text ( ( '<STR_LIT>' , '<STR_LIT>' ) , align = '<STR_LIT>' ) <EOL> widget = urwid . Filler ( widget ) <EOL> widget = urwid . BoxAdapter ( widget , <NUM_LIT:3> ) <EOL> return widget <EOL> class NullStatisticsNode ( StatisticsNodeBase ) : <EOL> _widget_class = NullStatisticsWidget <EOL> class LeafStatisticsNode ( StatisticsNodeBase ) : <EOL> _widget_class = StatisticsWidget <EOL> class StatisticsNode ( StatisticsNodeBase , urwid . ParentNode ) : <EOL> def deep_usage ( self ) : <EOL> stats = self . get_value ( ) <EOL> table = self . get_root ( ) <EOL> try : <EOL> return stats . deep_time / table . cpu_time <EOL> except AttributeError : <EOL> return <NUM_LIT:0.0> <EOL> def load_widget ( self ) : <EOL> if self . is_root ( ) : <EOL> widget_class = RootStatisticsWidget <EOL> else : <EOL> widget_class = StatisticsWidget <EOL> widget = widget_class ( self ) <EOL> widget . collapse ( ) <EOL> return widget <EOL> def setup_widget ( self , widget ) : <EOL> super ( StatisticsNode , self ) . setup_widget ( widget ) <EOL> if self . get_depth ( ) == <NUM_LIT:0> : <EOL> widget . expand ( ) <EOL> return <EOL> table = self . table <EOL> if table is None : <EOL> return <EOL> on ( widget , '<STR_LIT>' , table . _widget_expanded , widget ) <EOL> on ( widget , '<STR_LIT>' , table . _widget_collapsed , widget ) <EOL> def load_child_keys ( self ) : <EOL> stats = self . get_value ( ) <EOL> if stats is None : <EOL> return ( ) <EOL> return stats . sorted ( self . table . order ) <EOL> def load_child_node ( self , stats ) : <EOL> depth = self . get_depth ( ) + <NUM_LIT:1> <EOL> node_class = StatisticsNode if len ( stats ) else LeafStatisticsNode <EOL> return node_class ( stats , self , stats , depth , self . table ) <EOL> class StatisticsListBox ( urwid . TreeListBox ) : <EOL> signals = [ '<STR_LIT>' ] <EOL> def change_focus ( self , * args , ** kwargs ) : <EOL> super ( StatisticsListBox , self ) . change_focus ( * args , ** kwargs ) <EOL> focus = self . get_focus ( ) <EOL> urwid . emit_signal ( self , '<STR_LIT>' , focus ) <EOL> class StatisticsWalker ( urwid . TreeWalker ) : <EOL> signals = [ '<STR_LIT>' ] <EOL> def set_focus ( self , focus ) : <EOL> super ( StatisticsWalker , self ) . set_focus ( focus ) <EOL> urwid . emit_signal ( self , '<STR_LIT>' , focus ) <EOL> class StatisticsTable ( urwid . WidgetWrap ) : <EOL> columns = [ ( '<STR_LIT>' , '<STR_LIT:left>' , ( '<STR_LIT>' , <NUM_LIT:1> ) , sortkeys . by_function ) ] <EOL> order = sortkeys . by_function <EOL> layout = NESTED <EOL> title = None <EOL> stats = None <EOL> time = None <EOL> def __init__ ( self , viewer ) : <EOL> self . _expanded_stat_hashes = set ( ) <EOL> self . walker = StatisticsWalker ( NullStatisticsNode ( ) ) <EOL> on ( self . walker , '<STR_LIT>' , self . _walker_focus_changed ) <EOL> tbody = StatisticsListBox ( self . walker ) <EOL> thead = urwid . AttrMap ( self . make_columns ( [ <EOL> urwid . Text ( name , align , '<STR_LIT>' ) <EOL> for name , align , __ , __ in self . columns <EOL> ] ) , None ) <EOL> header = urwid . Columns ( [ ] ) <EOL> widget = urwid . Frame ( tbody , urwid . Pile ( [ header , thead ] ) ) <EOL> super ( StatisticsTable , self ) . __init__ ( widget ) <EOL> self . viewer = viewer <EOL> self . update_frame ( ) <EOL> def make_row ( self , node ) : <EOL> stats = node . get_value ( ) <EOL> return self . make_columns ( self . make_cells ( node , stats ) ) <EOL> def make_cells ( self , node , stats ) : <EOL> yield fmt . make_stat_text ( stats ) <EOL> @ classmethod <EOL> def make_columns ( cls , column_widgets ) : <EOL> widget_list = [ ] <EOL> widths = ( width for __ , __ , width , __ in cls . columns ) <EOL> for width , widget in zip ( widths , column_widgets ) : <EOL> widget_list . append ( width + ( widget , ) ) <EOL> return urwid . Columns ( widget_list , <NUM_LIT:1> ) <EOL> @ property <EOL> def tbody ( self ) : <EOL> return self . _w . body <EOL> @ tbody . setter <EOL> def tbody ( self , body ) : <EOL> self . _w . body = body <EOL> @ property <EOL> def thead ( self ) : <EOL> return self . _w . header . contents [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> @ thead . setter <EOL> def thead ( self , thead ) : <EOL> self . _w . header . contents [ <NUM_LIT:1> ] = ( thead , ( '<STR_LIT>' , None ) ) <EOL> @ property <EOL> def header ( self ) : <EOL> return self . _w . header . contents [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> @ header . setter <EOL> def header ( self , header ) : <EOL> self . _w . header . contents [ <NUM_LIT:0> ] = ( header , ( '<STR_LIT>' , None ) ) <EOL> @ property <EOL> def footer ( self ) : <EOL> return self . _w . footer <EOL> @ footer . setter <EOL> def footer ( self , footer ) : <EOL> self . _w . footer = footer <EOL> def get_focus ( self ) : <EOL> return self . tbody . get_focus ( ) <EOL> def set_focus ( self , focus ) : <EOL> self . tbody . set_focus ( focus ) <EOL> def get_path ( self ) : <EOL> """<STR_LIT>""" <EOL> path = deque ( ) <EOL> __ , node = self . get_focus ( ) <EOL> while not node . is_root ( ) : <EOL> stats = node . get_value ( ) <EOL> path . appendleft ( hash ( stats ) ) <EOL> node = node . get_parent ( ) <EOL> return path <EOL> def find_node ( self , node , path ) : <EOL> """<STR_LIT>""" <EOL> for hash_value in path : <EOL> if isinstance ( node , LeafStatisticsNode ) : <EOL> break <EOL> for stats in node . get_child_keys ( ) : <EOL> if hash ( stats ) == hash_value : <EOL> node = node . get_child_node ( stats ) <EOL> break <EOL> else : <EOL> break <EOL> return node <EOL> def get_stats ( self ) : <EOL> return self . stats <EOL> def set_result ( self , stats , cpu_time = <NUM_LIT:0.0> , wall_time = <NUM_LIT:0.0> , <EOL> title = None , at = None ) : <EOL> self . stats = stats <EOL> self . cpu_time = cpu_time <EOL> self . wall_time = wall_time <EOL> self . title = title <EOL> self . at = at <EOL> self . refresh ( ) <EOL> def set_layout ( self , layout ) : <EOL> if layout == self . layout : <EOL> return <EOL> self . layout = layout <EOL> self . refresh ( ) <EOL> def sort_stats ( self , order = sortkeys . by_deep_time ) : <EOL> assert callable ( order ) <EOL> if order == self . order : <EOL> return <EOL> self . order = order <EOL> self . refresh ( ) <EOL> def shift_order ( self , delta ) : <EOL> orders = [ order for __ , __ , __ , order in self . columns if order ] <EOL> x = orders . index ( self . order ) <EOL> order = orders [ ( x + delta ) % len ( orders ) ] <EOL> self . sort_stats ( order ) <EOL> def refresh ( self ) : <EOL> stats = self . get_stats ( ) <EOL> if stats is None : <EOL> return <EOL> if self . layout == FLAT : <EOL> stats = FlatFrozenStatistics . flatten ( stats ) <EOL> node = StatisticsNode ( stats , table = self ) <EOL> path = self . get_path ( ) <EOL> node = self . find_node ( node , path ) <EOL> self . set_focus ( node ) <EOL> def update_frame ( self , focus = None ) : <EOL> if self . viewer . paused : <EOL> thead_attr = '<STR_LIT>' <EOL> elif not self . viewer . active : <EOL> thead_attr = '<STR_LIT>' <EOL> else : <EOL> thead_attr = '<STR_LIT>' <EOL> self . thead . set_attr_map ( { None : thead_attr } ) <EOL> for x , ( __ , __ , __ , order ) in enumerate ( self . columns ) : <EOL> attr = thead_attr + '<STR_LIT>' if order is self . order else None <EOL> widget = self . thead . base_widget . contents [ x ] [ <NUM_LIT:0> ] <EOL> text , __ = widget . get_text ( ) <EOL> widget . set_text ( ( attr , text ) ) <EOL> if self . viewer . paused : <EOL> return <EOL> stats = self . get_stats ( ) <EOL> if stats is None : <EOL> return <EOL> title = self . title <EOL> time = self . time <EOL> if title or time : <EOL> if time is not None : <EOL> time_string = '<STR_LIT>' . format ( time ) <EOL> if title and time : <EOL> markup = [ ( '<STR_LIT>' , title ) , '<STR_LIT:U+0020>' , time_string ] <EOL> elif title : <EOL> markup = title <EOL> else : <EOL> markup = time_string <EOL> meta_info = urwid . Text ( markup , align = '<STR_LIT:right>' ) <EOL> else : <EOL> meta_info = None <EOL> fraction_string = '<STR_LIT>' . format ( <EOL> fmt . format_time ( self . cpu_time ) , <EOL> fmt . format_time ( self . wall_time ) ) <EOL> try : <EOL> cpu_usage = self . cpu_time / self . wall_time <EOL> except ZeroDivisionError : <EOL> cpu_usage = <NUM_LIT:0.0> <EOL> cpu_info = urwid . Text ( [ <EOL> '<STR_LIT>' , fmt . markup_percent ( cpu_usage , unit = True ) , <EOL> '<STR_LIT:U+0020>' , ( '<STR_LIT>' , fraction_string ) ] ) <EOL> col_opts = ( '<STR_LIT>' , <NUM_LIT:1> , False ) <EOL> self . header . contents = [ ( w , col_opts ) for w in [ cpu_info , meta_info ] if w ] <EOL> def focus_hotspot ( self , size ) : <EOL> widget , __ = self . tbody . get_focus ( ) <EOL> while widget : <EOL> node = widget . get_node ( ) <EOL> widget . expand ( ) <EOL> widget = widget . first_child ( ) <EOL> self . tbody . change_focus ( size , node ) <EOL> def defocus ( self ) : <EOL> __ , node = self . get_focus ( ) <EOL> self . set_focus ( node . get_root ( ) ) <EOL> def keypress ( self , size , key ) : <EOL> command = self . _command_map [ key ] <EOL> if key == '<STR_LIT:]>' : <EOL> self . shift_order ( + <NUM_LIT:1> ) <EOL> return True <EOL> elif key == '<STR_LIT:[>' : <EOL> self . shift_order ( - <NUM_LIT:1> ) <EOL> return True <EOL> elif key == '<STR_LIT:>>' : <EOL> self . focus_hotspot ( size ) <EOL> return True <EOL> elif key == '<STR_LIT:\\>' : <EOL> layout = { FLAT : NESTED , NESTED : FLAT } [ self . layout ] <EOL> self . set_layout ( layout ) <EOL> return True <EOL> command = self . _command_map [ key ] <EOL> if command == '<STR_LIT>' : <EOL> self . defocus ( ) <EOL> return True <EOL> elif command == urwid . CURSOR_RIGHT : <EOL> if self . layout == FLAT : <EOL> return True <EOL> widget , node = self . tbody . get_focus ( ) <EOL> if widget . expanded : <EOL> heavy_widget = widget . first_child ( ) <EOL> if heavy_widget is not None : <EOL> heavy_node = heavy_widget . get_node ( ) <EOL> self . tbody . change_focus ( size , heavy_node ) <EOL> return True <EOL> elif command == urwid . CURSOR_LEFT : <EOL> if self . layout == FLAT : <EOL> return True <EOL> widget , node = self . tbody . get_focus ( ) <EOL> if not widget . expanded : <EOL> parent_node = node . get_parent ( ) <EOL> if not parent_node . is_root ( ) : <EOL> self . tbody . change_focus ( size , parent_node ) <EOL> return True <EOL> elif command == urwid . ACTIVATE : <EOL> if self . viewer . paused : <EOL> self . viewer . resume ( ) <EOL> else : <EOL> self . viewer . pause ( ) <EOL> return True <EOL> return super ( StatisticsTable , self ) . keypress ( size , key ) <EOL> def _walker_focus_changed ( self , focus ) : <EOL> self . update_frame ( focus ) <EOL> def _widget_expanded ( self , widget ) : <EOL> stats = widget . get_node ( ) . get_value ( ) <EOL> self . _expanded_stat_hashes . add ( hash ( stats ) ) <EOL> def _widget_collapsed ( self , widget ) : <EOL> stats = widget . get_node ( ) . get_value ( ) <EOL> self . _expanded_stat_hashes . discard ( hash ( stats ) ) <EOL> class StatisticsViewer ( object ) : <EOL> weak_color = '<STR_LIT>' <EOL> palette = [ <EOL> ( '<STR_LIT>' , weak_color , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , weak_color , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , weak_color , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> for entry in palette [ : ] : <EOL> attr = entry [ <NUM_LIT:0> ] <EOL> if attr is not None and attr . startswith ( '<STR_LIT>' ) : <EOL> fg , bg , mono = entry [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> palette . append ( ( attr + '<STR_LIT>' , fg + '<STR_LIT>' , <EOL> bg , mono + '<STR_LIT>' ) ) <EOL> focus_map = { None : '<STR_LIT>' } <EOL> focus_map . update ( ( x [ <NUM_LIT:0> ] , '<STR_LIT>' ) for x in palette ) <EOL> active = False <EOL> paused = False <EOL> def unhandled_input ( self , key ) : <EOL> if key in ( '<STR_LIT:q>' , '<STR_LIT>' ) : <EOL> raise urwid . ExitMainLoop ( ) <EOL> def __init__ ( self ) : <EOL> self . table = StatisticsTable ( self ) <EOL> self . widget = urwid . Padding ( self . table , right = <NUM_LIT:1> ) <EOL> def loop ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . unhandled_input ) <EOL> loop = urwid . MainLoop ( self . widget , self . palette , * args , ** kwargs ) <EOL> return loop <EOL> def set_profiler_class ( self , profiler_class ) : <EOL> table_class = profiler_class . table_class <EOL> if type ( self . table ) is table_class : <EOL> return <EOL> self . table = table_class ( self ) <EOL> self . widget . original_widget = self . table <EOL> def set_result ( self , stats , cpu_time = <NUM_LIT:0.0> , wall_time = <NUM_LIT:0.0> , <EOL> title = None , at = None ) : <EOL> self . _final_result = ( stats , cpu_time , wall_time , title , at ) <EOL> if not self . paused : <EOL> self . update_result ( ) <EOL> def update_result ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . paused : <EOL> result = self . _paused_result <EOL> else : <EOL> result = self . _final_result <EOL> except AttributeError : <EOL> self . table . update_frame ( ) <EOL> return <EOL> stats , cpu_time , wall_time , title , at = result <EOL> self . table . set_result ( stats , cpu_time , wall_time , title , at ) <EOL> def activate ( self ) : <EOL> self . active = True <EOL> self . table . update_frame ( ) <EOL> def inactivate ( self ) : <EOL> self . active = False <EOL> self . table . update_frame ( ) <EOL> def pause ( self ) : <EOL> self . paused = True <EOL> try : <EOL> self . _paused_result = self . _final_result <EOL> except AttributeError : <EOL> pass <EOL> self . table . update_frame ( ) <EOL> def resume ( self ) : <EOL> self . paused = False <EOL> try : <EOL> del self . _paused_result <EOL> except AttributeError : <EOL> pass <EOL> self . update_result ( ) <EOL> def bind_vim_keys ( urwid = urwid ) : <EOL> urwid . command_map [ '<STR_LIT:h>' ] = urwid . command_map [ '<STR_LIT:left>' ] <EOL> urwid . command_map [ '<STR_LIT>' ] = urwid . command_map [ '<STR_LIT>' ] <EOL> urwid . command_map [ '<STR_LIT:k>' ] = urwid . command_map [ '<STR_LIT>' ] <EOL> urwid . command_map [ '<STR_LIT:l>' ] = urwid . command_map [ '<STR_LIT:right>' ] <EOL> def bind_game_keys ( urwid = urwid ) : <EOL> urwid . command_map [ '<STR_LIT:a>' ] = urwid . command_map [ '<STR_LIT:left>' ] <EOL> urwid . command_map [ '<STR_LIT:s>' ] = urwid . command_map [ '<STR_LIT>' ] <EOL> urwid . command_map [ '<STR_LIT:w>' ] = urwid . command_map [ '<STR_LIT>' ] <EOL> urwid . command_map [ '<STR_LIT:d>' ] = urwid . command_map [ '<STR_LIT:right>' ] </s>
