<s> """<STR_LIT>""" <EOL> import copy <EOL> import itertools <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def previous_current_next ( items ) : <EOL> """<STR_LIT>""" <EOL> extend = itertools . chain ( [ None ] , items , [ None ] ) <EOL> previous , current , next = itertools . tee ( extend , <NUM_LIT:3> ) <EOL> try : <EOL> current . next ( ) <EOL> next . next ( ) <EOL> next . next ( ) <EOL> except StopIteration : <EOL> pass <EOL> return itertools . izip ( previous , current , next ) <EOL> def tree_item_iterator ( items , ancestors = False ) : <EOL> """<STR_LIT>""" <EOL> structure = { } <EOL> opts = None <EOL> for previous , current , next in previous_current_next ( items ) : <EOL> if opts is None : <EOL> opts = current . _meta <EOL> current_level = getattr ( current , opts . level_attr ) <EOL> if previous : <EOL> structure [ '<STR_LIT>' ] = ( getattr ( previous , <EOL> opts . level_attr ) < current_level ) <EOL> if ancestors : <EOL> if structure [ '<STR_LIT>' ] : <EOL> structure [ '<STR_LIT>' ] = structure [ '<STR_LIT>' ] [ : - len ( structure [ '<STR_LIT>' ] ) ] <EOL> if structure [ '<STR_LIT>' ] : <EOL> structure [ '<STR_LIT>' ] . append ( unicode ( previous ) ) <EOL> else : <EOL> structure [ '<STR_LIT>' ] = True <EOL> if ancestors : <EOL> structure [ '<STR_LIT>' ] = [ ] <EOL> if next : <EOL> structure [ '<STR_LIT>' ] = range ( current_level , <EOL> getattr ( next , <EOL> opts . level_attr ) , - <NUM_LIT:1> ) <EOL> else : <EOL> structure [ '<STR_LIT>' ] = range ( current_level , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> yield current , copy . deepcopy ( structure ) <EOL> def drilldown_tree_for_node ( node , rel_cls = None , rel_field = None , count_attr = None , <EOL> cumulative = False ) : <EOL> """<STR_LIT>""" <EOL> if rel_cls and rel_field and count_attr : <EOL> children = node . _tree_manager . add_related_count ( <EOL> node . get_children ( ) , rel_cls , rel_field , count_attr , cumulative ) <EOL> else : <EOL> children = node . get_children ( ) <EOL> return itertools . chain ( node . get_ancestors ( ) , [ node ] , children ) </s>
