@@ -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
@@ -304,27 +299,17 @@ class Gunzip(BaseInterface):
304
299
def _gen_output_file_name (self ):
305
300
_ , base , ext = split_filename (self .inputs .in_file )
306
301
307
- if self .inputs .compress :
308
- ext += ".gz"
309
- else :
310
- if ext [- 3 :].lower () == ".gz" :
311
- ext = ext [:- 3 ]
312
-
302
+ if ext [- 3 :].lower () == ".gz" :
303
+ ext = ext [:- 3 ]
313
304
return os .path .abspath (base + ext )
314
305
315
306
def _run_interface (self , runtime ):
316
307
import gzip
317
308
import shutil
318
309
319
- if self .inputs .compress :
320
- with open (self .inputs .in_file , "rb" ) as in_file :
321
- with gzip .open (self ._gen_output_file_name (), "wb" ) as out_file :
322
- 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
-
310
+ with gzip .open (self .inputs .in_file , "rb" ) as in_file :
311
+ with open (self ._gen_output_file_name (), "wb" ) as out_file :
312
+ shutil .copyfileobj (in_file , out_file )
328
313
return runtime
329
314
330
315
def _list_outputs (self ):
@@ -333,6 +318,52 @@ def _list_outputs(self):
333
318
return outputs
334
319
335
320
321
+ class GzipInputSpec (GunzipInputSpec ):
322
+ mode = traits .Enum ("compress" , "decompress" , usedefault = True )
323
+
324
+
325
+ class Gzip (Gunzip ):
326
+ """Gzip wrapper supporting both compression and decompression.
327
+
328
+ >>> from nipype.algorithms.misc import Gzip
329
+ >>> gzip = Gzip(in_file='tpms_msk.nii.gz', mode="decompress")
330
+ >>> res = gzip.run()
331
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
332
+ '.../tpms_msk.nii'
333
+ >>> gzip = Gzip(in_file='tpms_msk.nii')
334
+ >>> res = gzip.run()
335
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
336
+ '.../tpms_msk.nii.gz'
337
+
338
+ .. testcleanup::
339
+
340
+ >>> os.unlink('tpms_msk.nii')
341
+ """
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
365
+
366
+
336
367
def replaceext (in_list , ext ):
337
368
out_list = list ()
338
369
for filename in in_list :
0 commit comments