@@ -271,43 +271,57 @@ def _list_outputs(self):
271271 return outputs
272272
273273
274- class GunzipInputSpec (BaseInterfaceInputSpec ):
275- in_file = File (exists = True , mandatory = True )
274+ class GzipInputSpec (TraitedSpec ):
275+ in_file = File (exists = True , mandatory = True , desc = "file to (de)compress" )
276+ mode = traits .Enum ("compress" , "decompress" , usedefault = True ,
277+ desc = "compress or decompress" )
276278
277279
278- class GunzipOutputSpec (TraitedSpec ):
279- out_file = File (exists = True )
280+ class GzipOutputSpec (TraitedSpec ):
281+ out_file = File ()
280282
281283
282- class Gunzip (BaseInterface ):
283- """Gunzip wrapper
284+ class Gzip (BaseInterface ):
285+ """Gzip wrapper
284286
285- >>> from nipype.algorithms.misc import Gunzip
286- >>> gunzip = Gunzip (in_file='tpms_msk.nii.gz')
287- >>> res = gunzip .run()
287+ >>> from nipype.algorithms.misc import Gzip
288+ >>> gzip = Gzip (in_file='tpms_msk.nii.gz', mode="decompress" )
289+ >>> res = gzip .run()
288290 >>> res.outputs.out_file # doctest: +ELLIPSIS
289291 '.../tpms_msk.nii'
290292
293+ >>> gzip = Gzip(in_file='tpms_msk.nii')
294+ >>> res = gzip.run()
295+ >>> res.outputs.out_file # doctest: +ELLIPSIS
296+ '.../tpms_msk.nii.gz'
297+
291298 .. testcleanup::
292299
293300 >>> os.unlink('tpms_msk.nii')
294301 """
295302
296- input_spec = GunzipInputSpec
297- output_spec = GunzipOutputSpec
303+ input_spec = GzipInputSpec
304+ output_spec = GzipOutputSpec
298305
299306 def _gen_output_file_name (self ):
300307 _ , base , ext = split_filename (self .inputs .in_file )
301- if ext [- 3 :].lower () == ".gz" :
308+ if self . inputs . mode == "decompress" and ext [- 3 :].lower () == ".gz" :
302309 ext = ext [:- 3 ]
310+ elif self .inputs .mode == "compress" :
311+ ext = f"{ ext } .gz"
303312 return os .path .abspath (base + ext )
304313
305314 def _run_interface (self , runtime ):
306315 import gzip
307316 import shutil
308317
309- with gzip .open (self .inputs .in_file , "rb" ) as in_file :
310- with open (self ._gen_output_file_name (), "wb" ) as out_file :
318+ if self .inputs .mode == "compress" :
319+ open_input , open_output = open , gzip .open
320+ else :
321+ open_input , open_output = gzip .open , open
322+
323+ with open_input (self .inputs .in_file , "rb" ) as in_file :
324+ with open_output (self ._gen_output_file_name (), "wb" ) as out_file :
311325 shutil .copyfileobj (in_file , out_file )
312326 return runtime
313327
@@ -317,51 +331,26 @@ def _list_outputs(self):
317331 return outputs
318332
319333
320- class GzipInputSpec (GunzipInputSpec ):
321- mode = traits .Enum ("compress" , "decompress" , usedefault = True )
334+ class GunzipInputSpec (GzipInputSpec ):
335+ mode = traits .Enum ("decompress" , usedefault = True ,
336+ desc = "decompress or compress" )
322337
323338
324- class Gzip ( Gunzip ):
325- """Gzip wrapper supporting both compression and decompression.
339+ class Gunzip ( Gzip ):
340+ """Gunzip wrapper
326341
327- >>> from nipype.algorithms.misc import Gzip
328- >>> gzip = Gzip (in_file='tpms_msk.nii.gz', mode="decompress" )
329- >>> res = gzip .run()
342+ >>> from nipype.algorithms.misc import Gunzip
343+ >>> gunzip = Gunzip (in_file='tpms_msk.nii.gz')
344+ >>> res = gunzip .run()
330345 >>> res.outputs.out_file # doctest: +ELLIPSIS
331346 '.../tpms_msk.nii'
332- >>> gzip = Gzip(in_file='tpms_msk.nii')
333- >>> res = gzip.run()
334- >>> res.outputs.out_file # doctest: +ELLIPSIS
335- '.../tpms_msk.nii.gz'
336347
337348 .. testcleanup::
338349
339350 >>> os.unlink('tpms_msk.nii')
340351 """
341352
342- input_spec = GzipInputSpec
343-
344- def _gen_output_file_name (self ):
345- if mode == "decompress" :
346- filename = super ()._gen_output_file_name ()
347- else :
348- _ , base , ext = split_filename (self .inputs .in_file )
349- filename = os .path .abspath (base + ext + ".gz" )
350-
351- return filename
352-
353- def _run_interface (self , runtime ):
354- import gzip
355- import shutil
356-
357- if mode == "decompress" :
358- runtime = super ()._run_interface (runtime )
359- else :
360- with open (self .inputs .in_file , "rb" ) as in_file :
361- with gzip .open (self ._gen_output_file_name (), "wb" ) as out_file :
362- shutil .copyfileobj (in_file , out_file )
363-
364- return runtime
353+ input_spec = GunzipInputSpec
365354
366355
367356def replaceext (in_list , ext ):
0 commit comments