<s> '''<STR_LIT>''' <EOL> from kivy . properties import ListProperty , ObservableDict , ObjectProperty <EOL> from kivy . event import EventDispatcher <EOL> from functools import partial <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def recondition_slice_assign ( val , last_len , new_len ) : <EOL> if not isinstance ( val , slice ) : <EOL> return slice ( val , val + <NUM_LIT:1> ) <EOL> diff = new_len - last_len <EOL> start , stop , step = val . start , val . stop , val . step <EOL> if stop <= start : <EOL> return slice ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if step is not None and step != <NUM_LIT:1> : <EOL> assert last_len == new_len <EOL> if stop < <NUM_LIT:0> : <EOL> stop = max ( <NUM_LIT:0> , last_len + stop ) <EOL> stop = min ( last_len , stop ) <EOL> if start < <NUM_LIT:0> : <EOL> start = max ( <NUM_LIT:0> , last_len + start ) <EOL> start = min ( last_len , start ) <EOL> return slice ( start , stop , step ) <EOL> if start < <NUM_LIT:0> : <EOL> start = last_len + start <EOL> if stop < <NUM_LIT:0> : <EOL> stop = last_len + stop <EOL> if ( start < <NUM_LIT:0> or stop < <NUM_LIT:0> or start > last_len or stop > last_len or <EOL> new_len != last_len ) : <EOL> return None <EOL> return slice ( start , stop ) <EOL> class RecycleDataModelBehavior ( object ) : <EOL> """<STR_LIT>""" <EOL> __events__ = ( "<STR_LIT>" , ) <EOL> recycleview = ObjectProperty ( None , allownone = True ) <EOL> '''<STR_LIT>''' <EOL> def attach_recycleview ( self , rv ) : <EOL> '''<STR_LIT>''' <EOL> self . recycleview = rv <EOL> if rv : <EOL> self . fbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> def detach_recycleview ( self ) : <EOL> '''<STR_LIT>''' <EOL> rv = self . recycleview <EOL> if rv : <EOL> self . funbind ( '<STR_LIT>' , rv . refresh_from_data ) <EOL> self . recycleview = None <EOL> def on_data_changed ( self , * largs , ** kwargs ) : <EOL> pass <EOL> class RecycleDataModel ( RecycleDataModelBehavior , EventDispatcher ) : <EOL> '''<STR_LIT>''' <EOL> data = ListProperty ( [ ] ) <EOL> '''<STR_LIT>''' <EOL> _last_len = <NUM_LIT:0> <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . fbind ( '<STR_LIT:data>' , self . _on_data_callback ) <EOL> super ( RecycleDataModel , self ) . __init__ ( ** kwargs ) <EOL> def __getitem__ ( self , index ) : <EOL> return self . data [ index ] <EOL> @ property <EOL> def observable_dict ( self ) : <EOL> '''<STR_LIT>''' <EOL> return partial ( ObservableDict , self . __class__ . data , self ) <EOL> def attach_recycleview ( self , rv ) : <EOL> super ( RecycleDataModel , self ) . attach_recycleview ( rv ) <EOL> if rv : <EOL> self . fbind ( '<STR_LIT:data>' , rv . _dispatch_prop_on_source , '<STR_LIT:data>' ) <EOL> def detach_recycleview ( self ) : <EOL> rv = self . recycleview <EOL> if rv : <EOL> self . funbind ( '<STR_LIT:data>' , rv . _dispatch_prop_on_source , '<STR_LIT:data>' ) <EOL> super ( RecycleDataModel , self ) . detach_recycleview ( ) <EOL> def _on_data_callback ( self , instance , value ) : <EOL> last_len = self . _last_len <EOL> new_len = self . _last_len = len ( self . data ) <EOL> op , val = value . last_op <EOL> if op == '<STR_LIT>' : <EOL> val = recondition_slice_assign ( val , last_len , new_len ) <EOL> if val is not None : <EOL> self . dispatch ( '<STR_LIT>' , modified = val ) <EOL> else : <EOL> self . dispatch ( '<STR_LIT>' ) <EOL> elif op == '<STR_LIT>' : <EOL> self . dispatch ( '<STR_LIT>' , removed = val ) <EOL> elif op == '<STR_LIT>' : <EOL> val = recondition_slice_assign ( slice ( * val ) , last_len , new_len ) <EOL> if val is not None : <EOL> self . dispatch ( '<STR_LIT>' , modified = val ) <EOL> else : <EOL> self . dispatch ( '<STR_LIT>' ) <EOL> elif op == '<STR_LIT>' : <EOL> self . dispatch ( '<STR_LIT>' , removed = slice ( * val ) ) <EOL> elif op == '<STR_LIT>' or op == '<STR_LIT>' : <EOL> self . dispatch ( '<STR_LIT>' , appended = slice ( last_len , new_len ) ) <EOL> elif op == '<STR_LIT>' : <EOL> self . dispatch ( '<STR_LIT>' , appended = slice ( last_len , new_len ) ) <EOL> elif op == '<STR_LIT>' : <EOL> self . dispatch ( '<STR_LIT>' , inserted = val ) <EOL> elif op == '<STR_LIT>' : <EOL> if val : <EOL> self . dispatch ( '<STR_LIT>' , removed = val [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . dispatch ( '<STR_LIT>' , removed = last_len - <NUM_LIT:1> ) <EOL> elif op == '<STR_LIT>' : <EOL> self . dispatch ( '<STR_LIT>' , appended = slice ( last_len , new_len ) ) <EOL> else : <EOL> self . dispatch ( '<STR_LIT>' ) </s>
