<s> from urwid . compat import PYTHON3 <EOL> def _call_modified ( fn ) : <EOL> def call_modified_wrapper ( self , * args , ** kwargs ) : <EOL> rval = fn ( self , * args , ** kwargs ) <EOL> self . _modified ( ) <EOL> return rval <EOL> return call_modified_wrapper <EOL> class MonitoredList ( list ) : <EOL> """<STR_LIT>""" <EOL> def _modified ( self ) : <EOL> pass <EOL> def set_modified_callback ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _modified = callback <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , list ( self ) ) <EOL> __add__ = _call_modified ( list . __add__ ) <EOL> __delitem__ = _call_modified ( list . __delitem__ ) <EOL> if not PYTHON3 : <EOL> __delslice__ = _call_modified ( list . __delslice__ ) <EOL> __iadd__ = _call_modified ( list . __iadd__ ) <EOL> __imul__ = _call_modified ( list . __imul__ ) <EOL> __rmul__ = _call_modified ( list . __rmul__ ) <EOL> __setitem__ = _call_modified ( list . __setitem__ ) <EOL> if not PYTHON3 : <EOL> __setslice__ = _call_modified ( list . __setslice__ ) <EOL> append = _call_modified ( list . append ) <EOL> extend = _call_modified ( list . extend ) <EOL> insert = _call_modified ( list . insert ) <EOL> pop = _call_modified ( list . pop ) <EOL> remove = _call_modified ( list . remove ) <EOL> reverse = _call_modified ( list . reverse ) <EOL> sort = _call_modified ( list . sort ) <EOL> class MonitoredFocusList ( MonitoredList ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * argl , ** argd ) : <EOL> """<STR_LIT>""" <EOL> focus = argd . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> super ( MonitoredFocusList , self ) . __init__ ( * argl , ** argd ) <EOL> self . _focus = focus <EOL> self . _focus_modified = lambda ml , indices , new_items : None <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , list ( self ) , self . focus ) <EOL> def _get_focus ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self : <EOL> return None <EOL> return self . _focus <EOL> def _set_focus ( self , index ) : <EOL> """<STR_LIT>""" <EOL> if not self : <EOL> self . _focus = <NUM_LIT:0> <EOL> return <EOL> if index < <NUM_LIT:0> or index >= len ( self ) : <EOL> raise IndexError , '<STR_LIT>' % ( index , ) <EOL> if index != int ( index ) : <EOL> raise IndexError , '<STR_LIT>' % ( index , ) <EOL> index = int ( index ) <EOL> if index != self . _focus : <EOL> self . _focus_changed ( index ) <EOL> self . _focus = index <EOL> focus = property ( _get_focus , _set_focus , doc = """<STR_LIT>""" ) <EOL> def _focus_changed ( self , new_focus ) : <EOL> pass <EOL> def set_focus_changed_callback ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _focus_changed = callback <EOL> def _validate_contents_modified ( self , indices , new_items ) : <EOL> return None <EOL> def set_validate_contents_modified ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _validate_contents_modified = callback <EOL> def _adjust_focus_on_contents_modified ( self , slc , new_items = ( ) ) : <EOL> """<STR_LIT>""" <EOL> num_new_items = len ( new_items ) <EOL> start , stop , step = indices = slc . indices ( len ( self ) ) <EOL> num_removed = len ( range ( * indices ) ) <EOL> focus = self . _validate_contents_modified ( indices , new_items ) <EOL> if focus is not None : <EOL> return focus <EOL> focus = self . _focus <EOL> if step == <NUM_LIT:1> : <EOL> if start + num_new_items <= focus < stop : <EOL> focus = stop <EOL> if stop <= focus : <EOL> focus += num_new_items - ( stop - start ) <EOL> else : <EOL> if not num_new_items : <EOL> if focus in range ( start , stop , step ) : <EOL> focus += <NUM_LIT:1> <EOL> focus -= len ( range ( start , min ( focus , stop ) , step ) ) <EOL> return min ( focus , len ( self ) + num_new_items - num_removed - <NUM_LIT:1> ) <EOL> def __delitem__ ( self , y ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( y , slice ) : <EOL> focus = self . _adjust_focus_on_contents_modified ( y ) <EOL> else : <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( y , y + <NUM_LIT:1> ) ) <EOL> rval = super ( MonitoredFocusList , self ) . __delitem__ ( y ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def __setitem__ ( self , i , y ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( i , slice ) : <EOL> focus = self . _adjust_focus_on_contents_modified ( i , y ) <EOL> else : <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( i , i + <NUM_LIT:1> or None ) , [ y ] ) <EOL> rval = super ( MonitoredFocusList , self ) . __setitem__ ( i , y ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> if not PYTHON3 : <EOL> def __delslice__ ( self , i , j ) : <EOL> """<STR_LIT>""" <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( i , j ) ) <EOL> rval = super ( MonitoredFocusList , self ) . __delslice__ ( i , j ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def __setslice__ ( self , i , j , y ) : <EOL> """<STR_LIT>""" <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( i , j ) , y ) <EOL> rval = super ( MonitoredFocusList , self ) . __setslice__ ( i , j , y ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def __imul__ ( self , n ) : <EOL> """<STR_LIT>""" <EOL> if n > <NUM_LIT:0> : <EOL> focus = self . _adjust_focus_on_contents_modified ( <EOL> slice ( len ( self ) , len ( self ) ) , list ( self ) * ( n - <NUM_LIT:1> ) ) <EOL> else : <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( <NUM_LIT:0> , len ( self ) ) ) <EOL> rval = super ( MonitoredFocusList , self ) . __imul__ ( n ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def append ( self , item ) : <EOL> """<STR_LIT>""" <EOL> focus = self . _adjust_focus_on_contents_modified ( <EOL> slice ( len ( self ) , len ( self ) ) , [ item ] ) <EOL> rval = super ( MonitoredFocusList , self ) . append ( item ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def extend ( self , items ) : <EOL> """<STR_LIT>""" <EOL> focus = self . _adjust_focus_on_contents_modified ( <EOL> slice ( len ( self ) , len ( self ) ) , items ) <EOL> rval = super ( MonitoredFocusList , self ) . extend ( items ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def insert ( self , index , item ) : <EOL> """<STR_LIT>""" <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( index , index ) , <EOL> [ item ] ) <EOL> rval = super ( MonitoredFocusList , self ) . insert ( index , item ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def pop ( self , index = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( index , <EOL> index + <NUM_LIT:1> or None ) ) <EOL> rval = super ( MonitoredFocusList , self ) . pop ( index ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def remove ( self , value ) : <EOL> """<STR_LIT>""" <EOL> index = self . index ( value ) <EOL> focus = self . _adjust_focus_on_contents_modified ( slice ( index , <EOL> index + <NUM_LIT:1> or None ) ) <EOL> rval = super ( MonitoredFocusList , self ) . remove ( value ) <EOL> self . _set_focus ( focus ) <EOL> return rval <EOL> def reverse ( self ) : <EOL> """<STR_LIT>""" <EOL> rval = super ( MonitoredFocusList , self ) . reverse ( ) <EOL> self . _set_focus ( max ( <NUM_LIT:0> , len ( self ) - self . _focus - <NUM_LIT:1> ) ) <EOL> return rval <EOL> def sort ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self : <EOL> return <EOL> value = self [ self . _focus ] <EOL> rval = super ( MonitoredFocusList , self ) . sort ( ) <EOL> self . _set_focus ( self . index ( value ) ) <EOL> return rval <EOL> def _test ( ) : <EOL> import doctest <EOL> doctest . testmod ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
