<s> """<STR_LIT>""" <EOL> from . . orm . collections import collection , collection_adapter <EOL> from . . import util <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def ordering_list ( attr , count_from = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw = _unsugar_count_from ( count_from = count_from , ** kw ) <EOL> return lambda : OrderingList ( attr , ** kw ) <EOL> def count_from_0 ( index , collection ) : <EOL> """<STR_LIT>""" <EOL> return index <EOL> def count_from_1 ( index , collection ) : <EOL> """<STR_LIT>""" <EOL> return index + <NUM_LIT:1> <EOL> def count_from_n_factory ( start ) : <EOL> """<STR_LIT>""" <EOL> def f ( index , collection ) : <EOL> return index + start <EOL> try : <EOL> f . __name__ = '<STR_LIT>' % start <EOL> except TypeError : <EOL> pass <EOL> return f <EOL> def _unsugar_count_from ( ** kw ) : <EOL> """<STR_LIT>""" <EOL> count_from = kw . pop ( '<STR_LIT>' , None ) <EOL> if kw . get ( '<STR_LIT>' , None ) is None and count_from is not None : <EOL> if count_from == <NUM_LIT:0> : <EOL> kw [ '<STR_LIT>' ] = count_from_0 <EOL> elif count_from == <NUM_LIT:1> : <EOL> kw [ '<STR_LIT>' ] = count_from_1 <EOL> else : <EOL> kw [ '<STR_LIT>' ] = count_from_n_factory ( count_from ) <EOL> return kw <EOL> class OrderingList ( list ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ordering_attr = None , ordering_func = None , <EOL> reorder_on_append = False ) : <EOL> """<STR_LIT>""" <EOL> self . ordering_attr = ordering_attr <EOL> if ordering_func is None : <EOL> ordering_func = count_from_0 <EOL> self . ordering_func = ordering_func <EOL> self . reorder_on_append = reorder_on_append <EOL> def _get_order_value ( self , entity ) : <EOL> return getattr ( entity , self . ordering_attr ) <EOL> def _set_order_value ( self , entity , value ) : <EOL> setattr ( entity , self . ordering_attr , value ) <EOL> def reorder ( self ) : <EOL> """<STR_LIT>""" <EOL> for index , entity in enumerate ( self ) : <EOL> self . _order_entity ( index , entity , True ) <EOL> _reorder = reorder <EOL> def _order_entity ( self , index , entity , reorder = True ) : <EOL> have = self . _get_order_value ( entity ) <EOL> if have is not None and not reorder : <EOL> return <EOL> should_be = self . ordering_func ( index , self ) <EOL> if have != should_be : <EOL> self . _set_order_value ( entity , should_be ) <EOL> def append ( self , entity ) : <EOL> super ( OrderingList , self ) . append ( entity ) <EOL> self . _order_entity ( len ( self ) - <NUM_LIT:1> , entity , self . reorder_on_append ) <EOL> def _raw_append ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> super ( OrderingList , self ) . append ( entity ) <EOL> _raw_append = collection . adds ( <NUM_LIT:1> ) ( _raw_append ) <EOL> def insert ( self , index , entity ) : <EOL> super ( OrderingList , self ) . insert ( index , entity ) <EOL> self . _reorder ( ) <EOL> def remove ( self , entity ) : <EOL> super ( OrderingList , self ) . remove ( entity ) <EOL> adapter = collection_adapter ( self ) <EOL> if adapter and adapter . _referenced_by_owner : <EOL> self . _reorder ( ) <EOL> def pop ( self , index = - <NUM_LIT:1> ) : <EOL> entity = super ( OrderingList , self ) . pop ( index ) <EOL> self . _reorder ( ) <EOL> return entity <EOL> def __setitem__ ( self , index , entity ) : <EOL> if isinstance ( index , slice ) : <EOL> step = index . step or <NUM_LIT:1> <EOL> start = index . start or <NUM_LIT:0> <EOL> if start < <NUM_LIT:0> : <EOL> start += len ( self ) <EOL> stop = index . stop or len ( self ) <EOL> if stop < <NUM_LIT:0> : <EOL> stop += len ( self ) <EOL> for i in range ( start , stop , step ) : <EOL> self . __setitem__ ( i , entity [ i ] ) <EOL> else : <EOL> self . _order_entity ( index , entity , True ) <EOL> super ( OrderingList , self ) . __setitem__ ( index , entity ) <EOL> def __delitem__ ( self , index ) : <EOL> super ( OrderingList , self ) . __delitem__ ( index ) <EOL> self . _reorder ( ) <EOL> def __setslice__ ( self , start , end , values ) : <EOL> super ( OrderingList , self ) . __setslice__ ( start , end , values ) <EOL> self . _reorder ( ) <EOL> def __delslice__ ( self , start , end ) : <EOL> super ( OrderingList , self ) . __delslice__ ( start , end ) <EOL> self . _reorder ( ) <EOL> def __reduce__ ( self ) : <EOL> return _reconstitute , ( self . __class__ , self . __dict__ , list ( self ) ) <EOL> for func_name , func in list ( locals ( ) . items ( ) ) : <EOL> if ( util . callable ( func ) and func . __name__ == func_name and <EOL> not func . __doc__ and hasattr ( list , func_name ) ) : <EOL> func . __doc__ = getattr ( list , func_name ) . __doc__ <EOL> del func_name , func <EOL> def _reconstitute ( cls , dict_ , items ) : <EOL> """<STR_LIT>""" <EOL> obj = cls . __new__ ( cls ) <EOL> obj . __dict__ . update ( dict_ ) <EOL> list . extend ( obj , items ) <EOL> return obj </s>
