Skip to content

Commit

Permalink
Merge pull request #181 from oleorhagen/cfe-4305
Browse files Browse the repository at this point in the history
Always run pretty with the default sort conf when saving cfbs.json
  • Loading branch information
olehermanse authored Jan 11, 2024
2 parents 6f31193 + 3cd7366 commit 52983cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 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
23 changes: 1 addition & 22 deletions cfbs/cfbs_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
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 52983cb

Please sign in to comment.