From 60eae9c4d1229f8d1ae31361c1298674808ef0f6 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Sun, 29 Mar 2020 15:49:09 -0400 Subject: [PATCH 1/2] RF: by default dump json with indent=2 --- heudiconv/bids.py | 12 +++++------- heudiconv/utils.py | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/heudiconv/bids.py b/heudiconv/bids.py index 3092cbf6..6a9c136c 100644 --- a/heudiconv/bids.py +++ b/heudiconv/bids.py @@ -171,7 +171,7 @@ def populate_aggregated_jsons(path): act = "Generating" lgr.debug("%s %s", act, task_file) fields.update(placeholders) - save_json(task_file, fields, indent=2, sort_keys=True, pretty=True) + save_json(task_file, fields, sort_keys=True, pretty=True) def tuneup_bids_json_files(json_files): @@ -193,7 +193,7 @@ def tuneup_bids_json_files(json_files): # Let's hope no word 'Date' comes within a study name or smth like # that raise ValueError("There must be no dates in .json sidecar") - save_json(jsonfile, json_, indent=2) + save_json(jsonfile, json_) # Load the beast seqtype = op.basename(op.dirname(jsonfile)) @@ -223,7 +223,7 @@ def tuneup_bids_json_files(json_files): was_readonly = is_readonly(json_phasediffname) if was_readonly: set_readonly(json_phasediffname, False) - save_json(json_phasediffname, json_, indent=2) + save_json(json_phasediffname, json_) if was_readonly: set_readonly(json_phasediffname) @@ -259,8 +259,7 @@ def add_participant_record(studydir, subject, age, sex): ("Description", "(TODO: adjust - by default everyone is in " "control group)")])), ]), - sort_keys=False, - indent=2) + sort_keys=False) # Add a new participant with open(participants_tsv, 'a') as f: f.write( @@ -373,8 +372,7 @@ def add_rows_to_scans_keys_file(fn, newrows): ("LongName", "Random string"), ("Description", "md5 hash of UIDs")])), ]), - sort_keys=False, - indent=2) + sort_keys=False) header = ['filename', 'acq_time', 'operator', 'randstr'] # prepare all the data rows diff --git a/heudiconv/utils.py b/heudiconv/utils.py index 5925e8e5..16760c55 100644 --- a/heudiconv/utils.py +++ b/heudiconv/utils.py @@ -188,7 +188,7 @@ def assure_no_file_exists(path): os.unlink(path) -def save_json(filename, data, indent=4, sort_keys=True, pretty=False): +def save_json(filename, data, indent=2, sort_keys=True, pretty=False): """Save data to a json file Parameters From 095679b61e6824ecc024223345a4b30b1ac00675 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Sun, 29 Mar 2020 15:59:02 -0400 Subject: [PATCH 2/2] RF: move fallback for pretty json saving into save_json, simplify treat_infofile Since save_json first removes file, we should be ok and no set_readonly(.., False) should longer be needed --- heudiconv/utils.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/heudiconv/utils.py b/heudiconv/utils.py index 16760c55..a4ffc001 100644 --- a/heudiconv/utils.py +++ b/heudiconv/utils.py @@ -203,11 +203,25 @@ def save_json(filename, data, indent=2, sort_keys=True, pretty=False): """ assure_no_file_exists(filename) + dumps_kw = dict(sort_keys=sort_keys, indent=indent) + j = None + if pretty: + try: + j = json_dumps_pretty(data, **dumps_kw) + except AssertionError as exc: + pretty = False + lgr.warning( + "Prettyfication of .json failed (%s). " + "Original .json will be kept as is. Please share (if you " + "could) " + "that file (%s) with HeuDiConv developers" + % (str(exc), filename) + ) + if not pretty: + j = _canonical_dumps(data, **dumps_kw) + assert j is not None # one way or another it should have been set to a str with open(filename, 'w') as fp: - fp.write( - (json_dumps_pretty if pretty else _canonical_dumps)( - data, sort_keys=sort_keys, indent=indent) - ) + fp.write(j) def json_dumps_pretty(j, indent=2, sort_keys=True): @@ -252,25 +266,9 @@ def json_dumps_pretty(j, indent=2, sort_keys=True): def treat_infofile(filename): """Tune up generated .json file (slim down, pretty-print for humans). """ - with open(filename) as f: - j = json.load(f) - + j = load_json(filename) j_slim = slim_down_info(j) - dumps_kw = dict(indent=2, sort_keys=True) - try: - j_pretty = json_dumps_pretty(j_slim, **dumps_kw) - except AssertionError as exc: - lgr.warning( - "Prettyfication of .json failed (%s). " - "Original .json will be kept as is. Please share (if you could) " - "that file (%s) with HeuDiConv developers" - % (str(exc), filename) - ) - j_pretty = json.dumps(j_slim, **dumps_kw) - - set_readonly(filename, False) - with open(filename, 'wt') as fp: - fp.write(j_pretty) + save_json(filename, j_slim, sort_keys=True, pretty=True) set_readonly(filename)