Skip to content

Commit

Permalink
Streamline open with contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
FredLoney committed Jun 19, 2017
1 parent f193e76 commit c76041d
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions qipipe/interfaces/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class CompressInputSpec(BaseInterfaceInputSpec):
in_file = File(exists=True, mandatory=True, desc='The file to compress')

dest = Directory(desc='The optional directory to write the compressed file'
'(default current directory)')
dest = Directory(desc='The optional directory to write the compressed'
' file (default current directory)')


class CompressOutputSpec(TraitedSpec):
Expand All @@ -21,8 +21,8 @@ class Compress(BaseInterface):
output_spec = CompressOutputSpec

def _run_interface(self, runtime):
self.out_file = self._compress(
self.inputs.in_file, dest=self.inputs.dest)
self.out_file = self._compress(self.inputs.in_file,
dest=self.inputs.dest)

return runtime

Expand All @@ -40,16 +40,15 @@ def _compress(self, in_file, dest=None):
:param dest: the destination (default is the working directory)
:return: the compressed file path
"""
if not dest:
if dest:
if not os.path.exists(dest):
os.makedirs(dest)
else:
dest = os.getcwd()
if not os.path.exists(dest):
os.makedirs(dest)
_, base_name = os.path.split(in_file)
out_file = os.path.join(dest, base_name + '.gz')
f = open(in_file, 'rb')
cf = gzip.open(out_file, 'wb')
cf.writelines(f)
f.close()
cf.close()
with open(in_file, 'rb') as f:
with gzip.open(out_file, 'wb') as z:
z.writelines(f)

return os.path.abspath(out_file)

0 comments on commit c76041d

Please sign in to comment.