@@ -271,43 +271,57 @@ def _list_outputs(self):
271
271
return outputs
272
272
273
273
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" )
276
278
277
279
278
- class GunzipOutputSpec (TraitedSpec ):
279
- out_file = File (exists = True )
280
+ class GzipOutputSpec (TraitedSpec ):
281
+ out_file = File ()
280
282
281
283
282
- class Gunzip (BaseInterface ):
283
- """Gunzip wrapper
284
+ class Gzip (BaseInterface ):
285
+ """Gzip wrapper
284
286
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()
288
290
>>> res.outputs.out_file # doctest: +ELLIPSIS
289
291
'.../tpms_msk.nii'
290
292
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
+
291
298
.. testcleanup::
292
299
293
300
>>> os.unlink('tpms_msk.nii')
294
301
"""
295
302
296
- input_spec = GunzipInputSpec
297
- output_spec = GunzipOutputSpec
303
+ input_spec = GzipInputSpec
304
+ output_spec = GzipOutputSpec
298
305
299
306
def _gen_output_file_name (self ):
300
307
_ , 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" :
302
309
ext = ext [:- 3 ]
310
+ elif self .inputs .mode == "compress" :
311
+ ext = f"{ ext } .gz"
303
312
return os .path .abspath (base + ext )
304
313
305
314
def _run_interface (self , runtime ):
306
315
import gzip
307
316
import shutil
308
317
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 :
311
325
shutil .copyfileobj (in_file , out_file )
312
326
return runtime
313
327
@@ -317,51 +331,26 @@ def _list_outputs(self):
317
331
return outputs
318
332
319
333
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" )
322
337
323
338
324
- class Gzip ( Gunzip ):
325
- """Gzip wrapper supporting both compression and decompression.
339
+ class Gunzip ( Gzip ):
340
+ """Gunzip wrapper
326
341
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()
330
345
>>> res.outputs.out_file # doctest: +ELLIPSIS
331
346
'.../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'
336
347
337
348
.. testcleanup::
338
349
339
350
>>> os.unlink('tpms_msk.nii')
340
351
"""
341
352
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
365
354
366
355
367
356
def replaceext (in_list , ext ):
0 commit comments