@@ -707,12 +707,33 @@ def grow(self, path):
707707 del self .children [name ]
708708 log .debug ("Empty tree '{0}' removed." .format (child .name ))
709709
710- def climb (self , whole = False ):
711- """ Climb through the tree (iterate leaf/all nodes) """
710+ def climb (self , whole : bool = False , sort : bool = True ):
711+ """
712+ Climb through the tree (iterate over nodes)
713+
714+ :param whole: By default only leaf nodes are considered. When
715+ set to ``True`` all nodes are iterated, including parent
716+ branches.
717+
718+ :param sort: When iterating, child nodes are sorted by name by
719+ default. Set to ``False`` if you prefer to keep the order in
720+ which the child nodes were inserted into the tree.
721+ """
722+
723+ # Include branches when `whole` is enabled or the `select`
724+ # directive has been used to pick this node.
712725 if whole or self .select :
713726 yield self
714- for name , child in sorted (self .children .items ()):
715- for node in child .climb (whole ):
727+
728+ # Sort child nodes by name only if requested
729+ if sort :
730+ children = [child for _ , child in sorted (self .children .items ())]
731+ else :
732+ children = self .children .values ()
733+
734+ # Iterate through each child node
735+ for child in children :
736+ for node in child .climb (whole = whole , sort = sort ):
716737 yield node
717738
718739 @property
@@ -730,9 +751,36 @@ def find(self, name):
730751 return node
731752 return None
732753
733- def prune (self , whole = False , keys = None , names = None , filters = None ,
734- conditions = None , sources = None ):
735- """ Filter tree nodes based on given criteria """
754+ def prune (
755+ self ,
756+ whole : bool = False ,
757+ keys : Optional [list [str ]] = None ,
758+ names : Optional [list [str ]] = None ,
759+ filters : Optional [list [str ]] = None ,
760+ conditions : Optional [list [str ]] = None ,
761+ sources : Optional [list [str ]] = None ,
762+ sort : bool = True ):
763+ """
764+ Filter tree nodes based on given criteria
765+
766+ :param whole: By default only leaf nodes are considered. When
767+ set to ``True`` all nodes are iterated, including parent
768+ branches.
769+
770+ :param keys: Include only nodes containing given keys.
771+
772+ :param names: Include only nodes matching provided names.
773+
774+ :param filters: Include only nodes matching given filters.
775+
776+ :param conditions: Include only nodes satisfying the conditions.
777+
778+ :param sources: Filter by source fmf file names on disk.
779+
780+ :param sort: When iterating, child nodes are sorted by name by
781+ default. Set to ``False`` if you prefer to keep the order in
782+ which the child nodes were inserted into the tree.
783+ """
736784 keys = keys or []
737785 names = names or []
738786 filters = filters or []
@@ -742,7 +790,7 @@ def prune(self, whole=False, keys=None, names=None, filters=None,
742790 if sources :
743791 sources = {os .path .abspath (src ) for src in sources }
744792
745- for node in self .climb (whole ):
793+ for node in self .climb (whole , sort = sort ):
746794 # Select only nodes with key content
747795 if not all ([key in node .data for key in keys ]):
748796 continue
0 commit comments