Skip to content

Commit 49f339d

Browse files
committed
respond to review
- add explicit py2 check - change file saving strategy - use logger instead of print
1 parent e76429c commit 49f339d

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

heudiconv/tests/test_utils.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
import os
33
import os.path as op
44

5-
try:
6-
from json.decoder import JSONDecodeError
7-
except ImportError:
8-
JSONDecodeError = ValueError
5+
from ..utils import JSONDecodeError, create_tree, save_json
96

107
from heudiconv.utils import (
118
get_known_heuristics_with_descriptions,
@@ -69,24 +66,21 @@ def test_json_dumps_pretty():
6966
assert pretty({'WipMemBlock': tstr}) == '{\n "WipMemBlock": "%s"\n}' % tstr
7067

7168

72-
def test_load_json(tmp_path, capsys):
69+
def test_load_json(tmp_path, caplog):
7370
# test invalid json
74-
icontent = u"I'm Jason Bourne"
75-
ifname = "invalid.json"
76-
invalid_json_file = tmp_path / ifname
77-
invalid_json_file.write_text(icontent)
71+
ifname = 'invalid.json'
72+
invalid_json_file = str(tmp_path / ifname)
73+
create_tree(str(tmp_path), {ifname: u"I'm Jason Bourne"})
7874

7975
with pytest.raises(JSONDecodeError):
8076
load_json(str(invalid_json_file))
81-
captured = capsys.readouterr()
82-
assert ifname in captured.out
77+
78+
assert ifname in caplog.text
8379

8480
# test valid json
8581
vcontent = {"secret": "spy"}
8682
vfname = "valid.json"
87-
valid_json_file = tmp_path / vfname
88-
89-
with open(str(valid_json_file), "w") as vj:
90-
json.dump(vcontent, vj)
83+
valid_json_file = str(tmp_path / vfname)
84+
save_json(valid_json_file, vcontent)
9185

92-
assert load_json(str(valid_json_file)) == vcontent
86+
assert load_json(valid_json_file) == vcontent

heudiconv/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
from glob import glob
1515
from subprocess import check_output
1616

17-
try:
18-
from json.decoder import JSONDecodeError
19-
except ImportError:
20-
JSONDecodeError = ValueError
21-
2217
from nipype.utils.filemanip import which
2318

2419
import logging
2520
lgr = logging.getLogger(__name__)
2621

22+
if sys.version_info[0] > 2:
23+
from json.decoder import JSONDecodeError
24+
else:
25+
JSONDecodeError = ValueError
26+
2727

2828
seqinfo_fields = [
2929
'total_files_till_now', # 0
@@ -181,7 +181,7 @@ def load_json(filename):
181181
with open(filename, 'r') as fp:
182182
data = json.load(fp)
183183
except JSONDecodeError:
184-
print("{fname} is not a valid json file".format(fname=filename))
184+
lgr.error("{fname} is not a valid json file".format(fname=filename))
185185
raise
186186

187187
return data

0 commit comments

Comments
 (0)