From 3cd73660061e4e2f6a637503384e8ed3fb3e7238 Mon Sep 17 00:00:00 2001 From: Ole Petter Date: Thu, 11 Jan 2024 09:47:11 +0100 Subject: [PATCH] Always run pretty with the default sort conf when saving cfbs.json Signed-off-by: Ole Petter --- cfbs/cfbs_config.py | 4 ++-- cfbs/cfbs_json.py | 23 +---------------------- cfbs/pretty.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/cfbs/cfbs_config.py b/cfbs/cfbs_config.py index 72e4556c..cc7d9317 100644 --- a/cfbs/cfbs_config.py +++ b/cfbs/cfbs_config.py @@ -17,7 +17,7 @@ fetch_archive, SUPPORTED_ARCHIVES, ) -from cfbs.pretty import pretty +from cfbs.pretty import pretty, cfbs_default_sorting_rules from cfbs.cfbs_json import CFBSJson from cfbs.module import Module from cfbs.prompts import prompt_user, YES_NO_CHOICES @@ -76,7 +76,7 @@ def __init__(self, filename="./cfbs.json", index=None, non_interactive=False): self.non_interactive = non_interactive def save(self): - data = pretty(self._data) + "\n" + data = pretty(self._data, cfbs_default_sorting_rules) + "\n" with open(self.path, "w") as f: f.write(data) diff --git a/cfbs/cfbs_json.py b/cfbs/cfbs_json.py index 8e523675..a0bde7b2 100644 --- a/cfbs/cfbs_json.py +++ b/cfbs/cfbs_json.py @@ -3,29 +3,8 @@ import logging as log from cfbs.index import Index +from cfbs.pretty import pretty, TOP_LEVEL_KEYS, MODULE_KEYS from cfbs.utils import read_json, user_error -from cfbs.pretty import pretty - -# Globals for the keys in cfbs.json and their order -# Used for validation and prettifying / sorting. -TOP_LEVEL_KEYS = ("name", "description", "type", "index", "git", "provides", "build") - -MODULE_KEYS = ( - "alias", - "name", - "description", - "tags", - "repo", - "url", - "by", - "version", - "commit", - "subdirectory", - "dependencies", - "added_by", - "steps", - "input", -) def _construct_provided_module(name, data, url, commit): diff --git a/cfbs/pretty.py b/cfbs/pretty.py index dd0f0844..8ac30a0e 100644 --- a/cfbs/pretty.py +++ b/cfbs/pretty.py @@ -3,6 +3,48 @@ from copy import copy from collections import OrderedDict +# Globals for the keys in cfbs.json and their order +# Used for validation and prettifying / sorting. +TOP_LEVEL_KEYS = ("name", "description", "type", "index", "git", "provides", "build") + +MODULE_KEYS = ( + "alias", + "name", + "description", + "tags", + "repo", + "url", + "by", + "version", + "commit", + "subdirectory", + "dependencies", + "added_by", + "steps", + "input", +) + + +module_key_sorting = ( + MODULE_KEYS, + None, +) +cfbs_default_sorting_rules = { + None: ( + TOP_LEVEL_KEYS, + { + "(index|provides)": ( + "alphabetic", # Module names are sorted alphabetically + {".*": module_key_sorting}, + ), + "build": ( # An array, not an object + None, # Don't sort elements of array + {".*": module_key_sorting}, + ), + }, + ), +} + def _children_sort(child, name, sorting_rules): """Recursively sort child objects in a JSON object.