@@ -143,95 +143,12 @@ def run_func(self, function, force_include=False, force_exclude=False) -> list[s
143143 else :
144144 self .logger .debug ("[%s] Function returned no output" % function .__name__ )
145145
146- def sort_hook_functions (self , hook : str ) -> None :
147- """Sorts the functions for the specified hook based on the import order.
148- "before" functions are moved before the target function,
149- "after" functions' target function is moved before the current function.
150-
151- Filters orders which do not contain functions or targets in the current hook.
152- """
153- func_names = [func .__name__ for func in self ["imports" ].get (hook , [])]
154- if not func_names :
155- return self .logger .debug ("No functions for hook: %s" % hook )
156-
157- b = self ["import_order" ].get ("before" , {})
158- before = {k : v for k , v in b .items () if k in func_names and any (subv in func_names for subv in b [k ])}
159- a = self ["import_order" ].get ("after" , {})
160- after = {k : v for k , v in a .items () if k in func_names and any (subv in func_names for subv in a [k ])}
161-
162- if not before and not after :
163- return self .logger .debug ("No import order specified for hook: %s" % hook )
164-
165- def iter_order (order , direction ):
166- """ Iterate over all functions in an import order list,
167- using this information to move the order of function names in the import list.
168-
169- Returns True if any changes were made, False otherwise."""
170- changed = False
171-
172- for func_name , other_funcs in order .items ():
173- func_index = func_names .index (func_name ) # Get the index based on the current position
174- assert func_index >= 0 , "Function not found in import list: %s" % func_name
175- for other_func in other_funcs :
176- try :
177- other_index = func_names .index (other_func )
178- except ValueError :
179- continue
180- assert other_index >= 0 , "Function not found in import list: %s" % other_func
181-
182- def reorder_func (direction ):
183- """Reorders the function based on the direction."""
184- if direction == "before" : # Move the function before the other function
185- self .logger .debug ("[%s] Moving function before: %s" % (func_name , other_func ))
186- func_names .insert (other_index , func_names .pop (func_index ))
187- self ["imports" ][hook ].insert (other_index , self ["imports" ][hook ].pop (func_index ))
188- elif direction == "after" : # Move the other function before the current function
189- self .logger .debug ("[%s] Moving function before: %s" % (other_func , func_name ))
190- func_names .insert (func_index , func_names .pop (other_index ))
191- self ["imports" ][hook ].insert (func_index , self ["imports" ][hook ].pop (other_index ))
192- else :
193- raise ValueError ("Invalid direction: %s" % direction )
194-
195- self .logger .log (5 , "[%s] Imports:\n %s" , hook , ", " .join (i .__name__ for i in self ["imports" ][hook ]))
196- if direction == "before" : # func_index should be before other_index
197- if func_index > other_index : # If the current function is after the other function
198- reorder_func ("before" ) # Move the current function before the other function)
199- changed = True
200- else : # Log otherwise
201- self .logger .log (5 , "Function %s already before: %s" % (func_name , other_func ))
202- elif direction == "after" : # func_index should be after other_index
203- if func_index < other_index : # If the current function is before the other function
204- reorder_func ("after" ) # Move the current function after the other function
205- changed = True
206- else :
207- self .logger .log (5 , "Function %s already after: %s" % (func_name , other_func ))
208- else :
209- raise ValueError ("Invalid direction: %s" % direction )
210- func_index = func_names .index (func_name ) # Update the index after moving
211- return changed
212-
213- max_iterations = len (func_names ) * (len (before ) + 1 ) * (len (after ) + 1 ) # Prevent infinite loops
214- iterations = max_iterations
215- while iterations :
216- iterations -= 1
217- # Update the order based on the before and after lists
218- if not any ([iter_order (before , "before" ), iter_order (after , "after" )]):
219- self .logger .debug (
220- "[%s] Import order converged after %s iterations" % (hook , max_iterations - iterations )
221- )
222- break # Keep going until no changes are made
223- else : # If the loop completes without breaking, raise an error
224- self .logger .error ("Import list: %s" % func_names )
225- self .logger .error ("Before: %s" % before )
226- self .logger .error ("After: %s" % after )
227- raise ValueError ("Import order did not converge after %s iterations" % max_iterations )
228-
229146 def run_hook (self , hook : str , * args , ** kwargs ) -> list [str ]:
230147 """Runs all functions for the specified hook.
231148 If the function is masked, it will be skipped.
232149 If the function is in import_order, handle the ordering
233150 """
234- self .sort_hook_functions (hook )
151+ self .sort_hook_functions (hook ) # This is in generator_helpers.py
235152 out = []
236153 for function in self ["imports" ].get (hook , []):
237154 if function .__name__ in self ["masks" ].get (hook , []):
0 commit comments