@@ -1804,8 +1804,9 @@ def _get_ssh_client(self):
18041804
18051805
18061806class JSONFileGrabberInputSpec (DynamicTraitedSpec , BaseInterfaceInputSpec ):
1807- in_file = File (exists = True , mandatory = True ,
1808- desc = 'JSON source file' )
1807+ in_file = File (exists = True , desc = 'JSON source file' )
1808+ defaults = traits .Dict (desc = ('JSON dictionary that sets default output'
1809+ 'values, overridden by values found in in_file' ))
18091810
18101811
18111812class JSONFileGrabber (IOBase ):
@@ -1819,12 +1820,15 @@ class JSONFileGrabber(IOBase):
18191820
18201821 >>> from nipype.interfaces.io import JSONFileGrabber
18211822 >>> jsonSource = JSONFileGrabber()
1823+ >>> jsonSource.inputs.defaults = {'param1': 'overrideMe', 'param3': 1.0}
1824+ >>> res = jsonSource.run()
1825+ >>> res.outputs.get()
1826+ {'param1': 'overrideMe', 'param3': 1.0 }
18221827 >>> jsonSource.inputs.in_file = 'jsongrabber.txt'
18231828 >>> res = jsonSource.run()
1824- >>> print res.outputs.param1
1825- exampleStr
1826- >>> print res.outputs.param2
1827- 4
1829+ >>> res.outputs.get()
1830+ {'param1': 'exampleStr', 'param2': 4, 'param3': 1.0}
1831+
18281832
18291833 """
18301834 input_spec = JSONFileGrabberInputSpec
@@ -1834,15 +1838,22 @@ class JSONFileGrabber(IOBase):
18341838 def _list_outputs (self ):
18351839 import json
18361840
1837- with open (self .inputs .in_file , 'r' ) as f :
1838- data = json .load (f )
1841+ outputs = {}
1842+ if isdefined (self .inputs .in_file ):
1843+ with open (self .inputs .in_file , 'r' ) as f :
1844+ data = json .load (f )
18391845
1840- if not isinstance (data , dict ):
1841- raise RuntimeError ('JSON input has no dictionary structure' )
1846+ if not isinstance (data , dict ):
1847+ raise RuntimeError ('JSON input has no dictionary structure' )
18421848
1843- outputs = {}
1844- for key , value in data .iteritems ():
1845- outputs [key ] = value
1849+ for key , value in data .iteritems ():
1850+ outputs [key ] = value
1851+
1852+ if isdefined (self .inputs .defaults ):
1853+ defaults = self .inputs .defaults
1854+ for key , value in defaults .iteritems ():
1855+ if key not in outputs .keys ():
1856+ outputs [key ] = value
18461857
18471858 return outputs
18481859
0 commit comments