Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always run pretty with the default sort conf when saving cfbs.json #181

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading