#NEWLINE# def groups_from_chapters( events , times ) : #NEWLINE# #INDENT# logging . debug ( 'Chapter start points: {0}' . format ( [ format_time ( t ) for t in times ] ) ) #NEWLINE# groups = [ [ ] ] #NEWLINE# chapter_times = iter ( ( times [ 1 : ] + [ 36000000000 ] ) ) #NEWLINE# current_chapter = next ( chapter_times ) #NEWLINE# for event in events : #NEWLINE# #INDENT# if ( event . end > current_chapter ) : #NEWLINE# #INDENT# groups . append ( [ ] ) #NEWLINE# while ( event . end > current_chapter ) : #NEWLINE# #INDENT# current_chapter = next ( chapter_times ) #NEWLINE# #UNINDENT# #UNINDENT# groups [ ( - 1 ) ] . append ( event ) #NEWLINE# #UNINDENT# groups = filter ( None , groups ) #NEWLINE# broken_groups = [ group for group in groups if ( not any ( ( e for e in group if ( not e . linked ) ) ) ) ] #NEWLINE# if broken_groups : #NEWLINE# #INDENT# for group in broken_groups : #NEWLINE# #INDENT# for event in group : #NEWLINE# #INDENT# parent = event . get_link_chain_end ( ) #NEWLINE# parent_group = next ( ( group for group in groups if ( parent in group ) ) ) #NEWLINE# parent_group . append ( event ) #NEWLINE# #UNINDENT# del group [ : ] #NEWLINE# #UNINDENT# groups = filter ( None , groups ) #NEWLINE# for group in groups : #NEWLINE# #INDENT# group . sort ( key = ( lambda event : event . start ) ) #NEWLINE# #UNINDENT# #UNINDENT# return groups