1313limitations under the License.
1414"""
1515
16+ import itertools
1617from functools import total_ordering
1718from typing import Iterator , Iterable , Optional
1819
@@ -68,10 +69,33 @@ def has_diffs(self) -> bool:
6869 return False
6970
7071 def get_children (self ) -> Iterator ["DiffElement" ]:
71- """Iterate over all child elements in all groups in self.children."""
72+ """Iterate over all child elements in all groups in self.children.
73+
74+ For each group of children, check if an order method is defined,
75+ Otherwise use the default method.
76+ """
77+ order_default = "order_children_default"
78+
79+ children : Iterator ["DiffElement" ] = itertools .chain ()
7280 for group in self .groups ():
73- for child in self .children [group ].values ():
74- yield child
81+ order_method_name = f"order_children_{ group } "
82+ if hasattr (self , order_method_name ):
83+ order_method = getattr (self , order_method_name )
84+ else :
85+ order_method = getattr (self , order_default )
86+
87+ children = itertools .chain (children , order_method (self .children [group ]))
88+
89+ return children
90+
91+ @classmethod
92+ def order_children_default (cls , children : dict ) -> Iterator ["DiffElement" ]:
93+ """Default method to an Iterator for children.
94+
95+ Since children is already an OrderedDefaultDict, this method is not doing anything special.
96+ """
97+ for child in children .values ():
98+ yield child
7599
76100 def print_detailed (self , indent : int = 0 ):
77101 """Print all diffs to screen for all child elements.
@@ -82,7 +106,7 @@ def print_detailed(self, indent: int = 0):
82106 margin = " " * indent
83107 for group in self .groups ():
84108 print (f"{ margin } { group } " )
85- for child in self .children [ group ]. values ():
109+ for child in self .get_children ():
86110 if child .has_diffs ():
87111 child .print_detailed (indent + 2 )
88112
0 commit comments