#NEWLINE# def sorted_relations( self ) : #NEWLINE# #INDENT# 'Topological sort of the relations for dependency management\n        ' #NEWLINE# if self . has_cycles ( ) : #NEWLINE# #INDENT# logger . warning ( 'Database has cycles' ) #NEWLINE# #UNINDENT# sorted_relations = [ ] #NEWLINE# graph = dict ( ( ( x . full_name , x . dependencies ) for x in self . relations ( ) ) ) #NEWLINE# while graph : #NEWLINE# #INDENT# acyclic = False #NEWLINE# for ( relation_name , dependencies ) in graph . items ( ) : #NEWLINE# #INDENT# for dependency in dependencies : #NEWLINE# #INDENT# if ( dependency in graph ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# acyclic = True #NEWLINE# graph . pop ( relation_name ) #NEWLINE# sorted_relations . append ( self . relation ( relation_name ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not acyclic ) : #NEWLINE# #INDENT# raise RuntimeError ( 'A cyclic dependency occurred' ) #NEWLINE# #UNINDENT# #UNINDENT# return sorted_relations