Skip to content

Commit

Permalink
Merge pull request #436 from dbic/rf-indent-2
Browse files Browse the repository at this point in the history
RF+ENH: save_json - indent=2 by default
  • Loading branch information
yarikoptic authored Apr 15, 2020
2 parents d2e47cb + 095679b commit 5681e0d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
12 changes: 5 additions & 7 deletions heudiconv/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
44 changes: 21 additions & 23 deletions heudiconv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -203,11 +203,25 @@ def save_json(filename, data, indent=4, 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):
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 5681e0d

Please sign in to comment.