Skip to content

Commit 5681e0d

Browse files
authored
Merge pull request #436 from dbic/rf-indent-2
RF+ENH: save_json - indent=2 by default
2 parents d2e47cb + 095679b commit 5681e0d

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

heudiconv/bids.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def populate_aggregated_jsons(path):
171171
act = "Generating"
172172
lgr.debug("%s %s", act, task_file)
173173
fields.update(placeholders)
174-
save_json(task_file, fields, indent=2, sort_keys=True, pretty=True)
174+
save_json(task_file, fields, sort_keys=True, pretty=True)
175175

176176

177177
def tuneup_bids_json_files(json_files):
@@ -193,7 +193,7 @@ def tuneup_bids_json_files(json_files):
193193
# Let's hope no word 'Date' comes within a study name or smth like
194194
# that
195195
raise ValueError("There must be no dates in .json sidecar")
196-
save_json(jsonfile, json_, indent=2)
196+
save_json(jsonfile, json_)
197197

198198
# Load the beast
199199
seqtype = op.basename(op.dirname(jsonfile))
@@ -223,7 +223,7 @@ def tuneup_bids_json_files(json_files):
223223
was_readonly = is_readonly(json_phasediffname)
224224
if was_readonly:
225225
set_readonly(json_phasediffname, False)
226-
save_json(json_phasediffname, json_, indent=2)
226+
save_json(json_phasediffname, json_)
227227
if was_readonly:
228228
set_readonly(json_phasediffname)
229229

@@ -259,8 +259,7 @@ def add_participant_record(studydir, subject, age, sex):
259259
("Description", "(TODO: adjust - by default everyone is in "
260260
"control group)")])),
261261
]),
262-
sort_keys=False,
263-
indent=2)
262+
sort_keys=False)
264263
# Add a new participant
265264
with open(participants_tsv, 'a') as f:
266265
f.write(
@@ -373,8 +372,7 @@ def add_rows_to_scans_keys_file(fn, newrows):
373372
("LongName", "Random string"),
374373
("Description", "md5 hash of UIDs")])),
375374
]),
376-
sort_keys=False,
377-
indent=2)
375+
sort_keys=False)
378376

379377
header = ['filename', 'acq_time', 'operator', 'randstr']
380378
# prepare all the data rows

heudiconv/utils.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def assure_no_file_exists(path):
188188
os.unlink(path)
189189

190190

191-
def save_json(filename, data, indent=4, sort_keys=True, pretty=False):
191+
def save_json(filename, data, indent=2, sort_keys=True, pretty=False):
192192
"""Save data to a json file
193193
194194
Parameters
@@ -203,11 +203,25 @@ def save_json(filename, data, indent=4, sort_keys=True, pretty=False):
203203
204204
"""
205205
assure_no_file_exists(filename)
206+
dumps_kw = dict(sort_keys=sort_keys, indent=indent)
207+
j = None
208+
if pretty:
209+
try:
210+
j = json_dumps_pretty(data, **dumps_kw)
211+
except AssertionError as exc:
212+
pretty = False
213+
lgr.warning(
214+
"Prettyfication of .json failed (%s). "
215+
"Original .json will be kept as is. Please share (if you "
216+
"could) "
217+
"that file (%s) with HeuDiConv developers"
218+
% (str(exc), filename)
219+
)
220+
if not pretty:
221+
j = _canonical_dumps(data, **dumps_kw)
222+
assert j is not None # one way or another it should have been set to a str
206223
with open(filename, 'w') as fp:
207-
fp.write(
208-
(json_dumps_pretty if pretty else _canonical_dumps)(
209-
data, sort_keys=sort_keys, indent=indent)
210-
)
224+
fp.write(j)
211225

212226

213227
def json_dumps_pretty(j, indent=2, sort_keys=True):
@@ -252,25 +266,9 @@ def json_dumps_pretty(j, indent=2, sort_keys=True):
252266
def treat_infofile(filename):
253267
"""Tune up generated .json file (slim down, pretty-print for humans).
254268
"""
255-
with open(filename) as f:
256-
j = json.load(f)
257-
269+
j = load_json(filename)
258270
j_slim = slim_down_info(j)
259-
dumps_kw = dict(indent=2, sort_keys=True)
260-
try:
261-
j_pretty = json_dumps_pretty(j_slim, **dumps_kw)
262-
except AssertionError as exc:
263-
lgr.warning(
264-
"Prettyfication of .json failed (%s). "
265-
"Original .json will be kept as is. Please share (if you could) "
266-
"that file (%s) with HeuDiConv developers"
267-
% (str(exc), filename)
268-
)
269-
j_pretty = json.dumps(j_slim, **dumps_kw)
270-
271-
set_readonly(filename, False)
272-
with open(filename, 'wt') as fp:
273-
fp.write(j_pretty)
271+
save_json(filename, j_slim, sort_keys=True, pretty=True)
274272
set_readonly(filename)
275273

276274

0 commit comments

Comments
 (0)