@@ -1843,3 +1843,57 @@ def _list_outputs(self):
18431843 outputs [key ] = value
18441844
18451845 return outputs
1846+
1847+
1848+ class JSONFileSinkInputSpec (DynamicTraitedSpec , BaseInterfaceInputSpec ):
1849+ out_file = File (desc = 'JSON sink file' )
1850+
1851+
1852+ class JSONFileSinkOutputSpec (TraitedSpec ):
1853+ out_file = File (desc = 'JSON sink file' )
1854+
1855+
1856+ class JSONFileSink (IOBase ):
1857+ """ Very simple frontend for storing values into a JSON file.
1858+
1859+ .. warning::
1860+
1861+ This is not a thread-safe node because it can write to a common
1862+ shared location. It will not complain when it overwrites a file.
1863+
1864+ Examples
1865+ --------
1866+
1867+ >>> jsonsink = JSONFileSink(input_names=['subject_id', 'some_measurement'])
1868+ >>> jsonsink.inputs.subject_id = 's1'
1869+ >>> jsonsink.inputs.some_measurement = 11.4
1870+ >>> jsonsink.run() # doctest: +SKIP
1871+
1872+ """
1873+ input_spec = JSONFileSinkInputSpec
1874+ output_spec = JSONFileSinkOutputSpec
1875+
1876+ def __init__ (self , input_names , ** inputs ):
1877+ super (JSONFileSink , self ).__init__ (** inputs )
1878+ self ._input_names = filename_to_list (input_names )
1879+ add_traits (self .inputs , [name for name in self ._input_names ])
1880+
1881+ def _list_outputs (self ):
1882+ import json
1883+ import os .path as op
1884+ if not isdefined (self .inputs .out_file ):
1885+ out_file = op .abspath ('datasink.json' )
1886+ else :
1887+ out_file = self .inputs .out_file
1888+
1889+ out_dict = dict ()
1890+ for name in self ._input_names :
1891+ val = getattr (self .inputs , name )
1892+ val = val if isdefined (val ) else 'undefined'
1893+ out_dict [name ] = val
1894+
1895+ with open (out_file , 'w' ) as f :
1896+ json .dump (out_dict , f )
1897+ outputs = self .output_spec ().get ()
1898+ outputs ['out_file' ] = out_file
1899+ return outputs
0 commit comments