diff --git a/griffon/helpers.py b/griffon/helpers.py index c006976..04ac75a 100644 --- a/griffon/helpers.py +++ b/griffon/helpers.py @@ -1,7 +1,9 @@ """ Helpers for direct usage or debbuging """ + import json +import re from enum import Enum from typing import Callable, Optional, Type, Union @@ -106,3 +108,9 @@ class Style(Enum): def __str__(self): return str(self.value) + + +def natural_sort_key(string): + """Key for builtin sorted function to perform natural sort""" + split_by_digit = re.split("([0-9]+)", string) + return [int(part) if part.isdigit() else part.lower() for part in split_by_digit] diff --git a/griffon/output.py b/griffon/output.py index f389cf4..683f8c6 100644 --- a/griffon/output.py +++ b/griffon/output.py @@ -17,6 +17,8 @@ from rich.text import Text from rich.tree import Tree +from .helpers import natural_sort_key + console = Console(color_system="auto") logger = logging.getLogger("griffon") @@ -511,7 +513,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -579,7 +583,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -703,7 +709,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -856,7 +864,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"], @@ -971,7 +981,9 @@ def text_output_products_contain_component( for ps in result_tree[pv].keys(): for cn in sorted(result_tree[pv][ps].keys()): # select the latest nvr (from sorted list) - nvr = list(result_tree[pv][ps][cn].keys())[-1] + nvr = sorted( + list(result_tree[pv][ps][cn].keys()), key=natural_sort_key + )[-1] product_color = process_product_color( result_tree[pv][ps][cn][nvr]["product_stream_relations"], result_tree[pv][ps][cn][nvr]["build_type"],