Skip to content

Commit

Permalink
Human sort the sections in fix_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Mab879 committed Feb 22, 2024
1 parent 41f9751 commit 24817c6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions utils/fix_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,16 @@ def has_duplicated_subkeys(rule_path, rule, rule_lines):
return ssg.rule_yaml.has_duplicated_subkeys(rule_path, rule_lines, TO_SORT)


def _human_sort(line):
# Based on: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
def convert(text): return int(text) if text.isdigit() else text
return [convert(text) for text in re.split(r'(\d+)', line)]


def has_unordered_sections(rule_path, rule, rule_lines):
if 'references' in rule or 'identifiers' in rule:
new_lines = ssg.rule_yaml.sort_section_keys(rule_path, rule_lines, TO_SORT)
new_lines = ssg.rule_yaml.sort_section_keys(rule_path, rule_lines, TO_SORT,
sort_func=_human_sort)

# Compare string representations to avoid issues with references being
# different.
Expand Down Expand Up @@ -678,7 +685,7 @@ def find_int_references(args, product_yaml):
product_yaml = result[2]

if args.dry_run:
print(rule_path + " has one or more unsorted references")
print(rule_path + " has one or more unsorted integer references")
continue

fix_file_prompt(rule_path, product_yaml, fix_int_reference, args)
Expand Down

0 comments on commit 24817c6

Please sign in to comment.