Skip to content

Commit

Permalink
Merge pull request #182 from olehermanse/pretty
Browse files Browse the repository at this point in the history
Fixed pretty formatting of first cfbs init commit
  • Loading branch information
olehermanse committed Jan 11, 2024
2 parents 52983cb + 586a32d commit 7793c67
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 49 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, cfbs_default_sorting_rules
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, cfbs_default_sorting_rules) + "\n"
data = pretty(self._data, CFBS_DEFAULT_SORTING_RULES) + "\n"
with open(self.path, "w") as f:
f.write(data)

Expand Down
62 changes: 23 additions & 39 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@
)

from cfbs.args import get_args
from cfbs.pretty import pretty, pretty_check_file, pretty_file
from cfbs.pretty import (
pretty,
pretty_check_file,
pretty_file,
CFBS_DEFAULT_SORTING_RULES,
TOP_LEVEL_KEYS,
MODULE_KEYS,
)
from cfbs.build import (
init_out_folder,
perform_build_steps,
)
from cfbs.cfbs_json import TOP_LEVEL_KEYS, MODULE_KEYS
from cfbs.cfbs_config import CFBSConfig, CFBSReturnWithoutCommit
from cfbs.validate import validate_config
from cfbs.internal_file_management import (
Expand Down Expand Up @@ -95,34 +101,7 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int:
if not filenames:
user_error("Filenames missing for cfbs pretty command")

cfbs_sorting_rules = None
if not keep_order:
# These sorting rules achieve 3 things:
# 1. Top level keys are sorted according to a specified list
# 2. Module names in "index" and "provides" are sorted alphabetically
# 3. Fields inside module objects are sorted according to a specified list
# for "index", "provides", and "build"

module_key_sorting = (
MODULE_KEYS,
None,
)
cfbs_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},
),
},
),
}

sorting_rules = CFBS_DEFAULT_SORTING_RULES if keep_order else None
num_files = 0
for f in filenames:
if not f or not f.endswith(".json"):
Expand All @@ -132,11 +111,11 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int:
)
try:
if check:
if not pretty_check_file(f, cfbs_sorting_rules):
if not pretty_check_file(f, sorting_rules):
num_files += 1
print("Would reformat %s" % f)
else:
pretty_file(f, cfbs_sorting_rules)
pretty_file(f, sorting_rules)
except FileNotFoundError:
user_error("File '%s' not found" % f)
except json.decoder.JSONDecodeError as ex:
Expand All @@ -163,12 +142,14 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
default="Example description",
)

config = {
"name": name,
"type": "policy-set", # TODO: Prompt whether user wants to make a module
"description": description,
"build": [],
}
config = OrderedDict(
{
"name": name,
"type": "policy-set", # TODO: Prompt whether user wants to make a module
"description": description,
"build": [],
}
)
if index:
config["index"] = index

Expand Down Expand Up @@ -229,7 +210,10 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int:

config["git"] = do_git

write_json(cfbs_filename(), config)
data = pretty(config, CFBS_DEFAULT_SORTING_RULES) + "\n"
print(data)
with open(cfbs_filename(), "w") as f:
f.write(data)
assert is_cfbs_repo()

if do_git:
Expand Down
20 changes: 13 additions & 7 deletions cfbs/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,35 @@
)


module_key_sorting = (
# These sorting rules achieve 3 things:
# 1. Top level keys are sorted according to a specified list
# 2. Module names in "index" and "provides" are sorted alphabetically
# 3. Fields inside module objects are sorted according to a specified list
# for "index", "provides", and "build"

_module_key_sorting = (
MODULE_KEYS,
None,
)
cfbs_default_sorting_rules = {

CFBS_DEFAULT_SORTING_RULES = {
None: (
TOP_LEVEL_KEYS,
{
"(index|provides)": (
"alphabetic", # Module names are sorted alphabetically
{".*": module_key_sorting},
{".*": _module_key_sorting},
),
"build": ( # An array, not an object
None, # Don't sort elements of array
{".*": module_key_sorting},
{".*": _module_key_sorting},
),
},
),
}


def _children_sort(child, name, sorting_rules):
def _children_sort(child: OrderedDict, name, sorting_rules):
"""Recursively sort child objects in a JSON object.
:param child: child object to start with
Expand Down Expand Up @@ -120,8 +127,7 @@ def _children_sort(child, name, sorting_rules):
Only JSON objects (dictionaries) are sorted by this function, arrays are ignored.
"""
if type(child) is not OrderedDict:
return
assert type(child) is OrderedDict

for key in child:
if type(child[key]) not in (list, tuple):
Expand Down
2 changes: 1 addition & 1 deletion cfbs/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import OrderedDict

from cfbs.utils import is_a_commit_hash, user_error
from cfbs.cfbs_json import TOP_LEVEL_KEYS, MODULE_KEYS
from cfbs.pretty import TOP_LEVEL_KEYS, MODULE_KEYS
from cfbs.cfbs_config import CFBSConfig
from cfbs.build import AVAILABLE_BUILD_STEPS

Expand Down

0 comments on commit 7793c67

Please sign in to comment.