Skip to content

Commit

Permalink
Always run pretty with the default sort conf when saving cfbs.json
Browse files Browse the repository at this point in the history
Signed-off-by: Ole Petter <[email protected]>
  • Loading branch information
oleorhagen committed Jan 11, 2024
1 parent 4884287 commit 5e4f3a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cfbs/cfbs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 1 addition & 23 deletions cfbs/cfbs_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +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):
# At this point the @commmit part should be removed from url so:
Expand Down
42 changes: 42 additions & 0 deletions cfbs/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5e4f3a9

Please sign in to comment.