@@ -273,7 +273,6 @@ def _list_outputs(self):
273
273
274
274
class GunzipInputSpec (BaseInterfaceInputSpec ):
275
275
in_file = File (exists = True , mandatory = True )
276
- compress = traits .Bool (default_value = False )
277
276
278
277
279
278
class GunzipOutputSpec (TraitedSpec ):
@@ -288,10 +287,6 @@ class Gunzip(BaseInterface):
288
287
>>> res = gunzip.run()
289
288
>>> res.outputs.out_file # doctest: +ELLIPSIS
290
289
'.../tpms_msk.nii'
291
- >>> gunzip = Gunzip(in_file='tpms_msk.nii', compress=True)
292
- >>> res = gunzip.run()
293
- >>> res.outputs.out_file # doctest: +ELLIPSIS
294
- '.../tpms_msk.nii.gz'
295
290
296
291
.. testcleanup::
297
292
@@ -303,35 +298,71 @@ class Gunzip(BaseInterface):
303
298
304
299
def _gen_output_file_name (self ):
305
300
_ , base , ext = split_filename (self .inputs .in_file )
301
+ if ext [- 3 :].lower () == ".gz" :
302
+ ext = ext [:- 3 ]
303
+ return os .path .abspath (base + ext )
304
+
305
+ def _run_interface (self , runtime ):
306
+ import gzip
307
+ import shutil
308
+
309
+ with gzip .open (self .inputs .in_file , "rb" ) as in_file :
310
+ with open (self ._gen_output_file_name (), "wb" ) as out_file :
311
+ shutil .copyfileobj (in_file , out_file )
312
+ return runtime
313
+
314
+ def _list_outputs (self ):
315
+ outputs = self ._outputs ().get ()
316
+ outputs ["out_file" ] = self ._gen_output_file_name ()
317
+ return outputs
318
+
319
+
320
+ class GzipInputSpec (GunzipInputSpec ):
321
+ mode = traits .Enum ("compress" , "decompress" , usedefault = True )
322
+
323
+
324
+ class Gzip (Gunzip ):
325
+ """Gzip wrapper supporting both compression and decompression.
326
+
327
+ >>> from nipype.algorithms.misc import Gzip
328
+ >>> gzip = Gzip(in_file='tpms_msk.nii.gz', mode="decompress")
329
+ >>> res = gzip.run()
330
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
331
+ '.../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'
306
336
307
- if self .inputs .compress :
308
- ext += ".gz"
337
+ .. testcleanup::
338
+
339
+ >>> os.unlink('tpms_msk.nii')
340
+ """
341
+
342
+ input_spec = GzipInputSpec
343
+
344
+ def _gen_output_file_name (self ):
345
+ if mode == "decompress" :
346
+ filename = super ()._gen_output_file_name ()
309
347
else :
310
- if ext [ - 3 :]. lower () == ".gz" :
311
- ext = ext [: - 3 ]
348
+ _ , base , ext = split_filename ( self . inputs . in_file )
349
+ filename = os . path . abspath ( base + ext + ".gz" )
312
350
313
- return os . path . abspath ( base + ext )
351
+ return filename
314
352
315
353
def _run_interface (self , runtime ):
316
354
import gzip
317
355
import shutil
318
356
319
- if self .inputs .compress :
357
+ if mode == "decompress" :
358
+ runtime = super ()._run_interface (runtime )
359
+ else :
320
360
with open (self .inputs .in_file , "rb" ) as in_file :
321
361
with gzip .open (self ._gen_output_file_name (), "wb" ) as out_file :
322
362
shutil .copyfileobj (in_file , out_file )
323
- else :
324
- with gzip .open (self .inputs .in_file , "rb" ) as in_file :
325
- with open (self ._gen_output_file_name (), "wb" ) as out_file :
326
- shutil .copyfileobj (in_file , out_file )
327
363
328
364
return runtime
329
365
330
- def _list_outputs (self ):
331
- outputs = self ._outputs ().get ()
332
- outputs ["out_file" ] = self ._gen_output_file_name ()
333
- return outputs
334
-
335
366
336
367
def replaceext (in_list , ext ):
337
368
out_list = list ()
0 commit comments