@@ -271,43 +271,58 @@ 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 (
277
+ "compress" , "decompress" , usedefault = True , desc = "compress or decompress"
278
+ )
276
279
277
280
278
- class GunzipOutputSpec (TraitedSpec ):
279
- out_file = File (exists = True )
281
+ class GzipOutputSpec (TraitedSpec ):
282
+ out_file = File ()
280
283
281
284
282
- class Gunzip (BaseInterface ):
283
- """Gunzip wrapper
285
+ class Gzip (BaseInterface ):
286
+ """Gzip wrapper
284
287
285
- >>> from nipype.algorithms.misc import Gunzip
286
- >>> gunzip = Gunzip (in_file='tpms_msk.nii.gz')
287
- >>> res = gunzip .run()
288
+ >>> from nipype.algorithms.misc import Gzip
289
+ >>> gzip = Gzip (in_file='tpms_msk.nii.gz', mode="decompress" )
290
+ >>> res = gzip .run()
288
291
>>> res.outputs.out_file # doctest: +ELLIPSIS
289
292
'.../tpms_msk.nii'
290
293
294
+ >>> gzip = Gzip(in_file='tpms_msk.nii')
295
+ >>> res = gzip.run()
296
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
297
+ '.../tpms_msk.nii.gz'
298
+
291
299
.. testcleanup::
292
300
293
301
>>> os.unlink('tpms_msk.nii')
294
302
"""
295
303
296
- input_spec = GunzipInputSpec
297
- output_spec = GunzipOutputSpec
304
+ input_spec = GzipInputSpec
305
+ output_spec = GzipOutputSpec
298
306
299
307
def _gen_output_file_name (self ):
300
308
_ , base , ext = split_filename (self .inputs .in_file )
301
- if ext [- 3 :].lower () == ".gz" :
309
+ if self . inputs . mode == "decompress" and ext [- 3 :].lower () == ".gz" :
302
310
ext = ext [:- 3 ]
311
+ elif self .inputs .mode == "compress" :
312
+ ext = f"{ ext } .gz"
303
313
return os .path .abspath (base + ext )
304
314
305
315
def _run_interface (self , runtime ):
306
316
import gzip
307
317
import shutil
308
318
309
- with gzip .open (self .inputs .in_file , "rb" ) as in_file :
310
- with open (self ._gen_output_file_name (), "wb" ) as out_file :
319
+ if self .inputs .mode == "compress" :
320
+ open_input , open_output = open , gzip .open
321
+ else :
322
+ open_input , open_output = gzip .open , open
323
+
324
+ with open_input (self .inputs .in_file , "rb" ) as in_file :
325
+ with open_output (self ._gen_output_file_name (), "wb" ) as out_file :
311
326
shutil .copyfileobj (in_file , out_file )
312
327
return runtime
313
328
@@ -317,6 +332,27 @@ def _list_outputs(self):
317
332
return outputs
318
333
319
334
335
+ class GunzipInputSpec (GzipInputSpec ):
336
+ mode = traits .Enum ("decompress" , usedefault = True , desc = "decompress or compress" )
337
+
338
+
339
+ class Gunzip (Gzip ):
340
+ """Gunzip wrapper
341
+
342
+ >>> from nipype.algorithms.misc import Gunzip
343
+ >>> gunzip = Gunzip(in_file='tpms_msk.nii.gz')
344
+ >>> res = gunzip.run()
345
+ >>> res.outputs.out_file # doctest: +ELLIPSIS
346
+ '.../tpms_msk.nii'
347
+
348
+ .. testcleanup::
349
+
350
+ >>> os.unlink('tpms_msk.nii')
351
+ """
352
+
353
+ input_spec = GunzipInputSpec
354
+
355
+
320
356
def replaceext (in_list , ext ):
321
357
out_list = list ()
322
358
for filename in in_list :
0 commit comments