diff --git a/README.md b/README.md index bc7adc1..76140a0 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,16 @@ # pigz-python -The goal of this project is to create a pure Python implementation of the pigz project for parallelizing gzipping. +The goal of this project is to create a pure Python implementation of the pigz project for parallelizing gzipping. # Usage examples pigz-python can be utilized by creating a `PigzFile` object and calling the `process_compression_target()` method. +Note: `output` file name is optional and the default value would be existing filename. ```python from pigz_python import PigzFile -pigz_file = PigzFile('foo.txt') +pigz_file = PigzFile('foo.txt', 'compressed') pigz_file.process_compression_target() ``` @@ -18,5 +19,5 @@ Alternatively, the pigz_python module also provides a convenient helper method t ```python import pigz_python -pigz_python.compress_file('foo.txt') +pigz_python.compress_file('foo.txt', 'compressed') ``` diff --git a/pigz_python/pigz_python.py b/pigz_python/pigz_python.py index 4da266a..bc1f91d 100644 --- a/pigz_python/pigz_python.py +++ b/pigz_python/pigz_python.py @@ -27,11 +27,12 @@ class PigzFile: # pylint: disable=too-many-instance-attributes - """ Class to implement Pigz functionality in Python """ + """Class to implement Pigz functionality in Python""" def __init__( self, compression_target, + output_name=None, compresslevel=_COMPRESS_LEVEL_BEST, blocksize=DEFAULT_BLOCK_SIZE_KB, workers=CPU_COUNT, @@ -45,7 +46,7 @@ def __init__( self.workers = workers self.output_file = None - self.output_filename = None + self.output_filename = output_name # This is how we know if we're done reading, compressing, & writing the file self._last_chunk = -1 @@ -90,8 +91,11 @@ def _set_output_filename(self): """ Set the output filename based on the input filename """ - base = Path(self.compression_target).name - self.output_filename = base + ".gz" + if self.output_filename is None: + base = Path(self.compression_target).name + self.output_filename = base + ".gz" + elif ".gz" not in self.output_filename: + self.output_filename += ".gz" def _write_output_header(self): """ @@ -128,25 +132,25 @@ def _write_header_id(self): self.output_file.write((0x8B).to_bytes(1, sys.byteorder)) def _write_header_cm(self): - """ Write the CM (compression method) to file header """ + """Write the CM (compression method) to file header""" self.output_file.write((8).to_bytes(1, sys.byteorder)) def _write_header_flg(self, flags): - """ Write FLG (FLaGs) """ + """Write FLG (FLaGs)""" self.output_file.write((flags).to_bytes(1, sys.byteorder)) def _write_header_mtime(self): - """ Write MTIME (Modification time) """ + """Write MTIME (Modification time)""" mtime = self._determine_mtime() self.output_file.write((mtime).to_bytes(4, sys.byteorder)) def _write_header_xfl(self): - """ Write XFL (eXtra FLags) """ + """Write XFL (eXtra FLags)""" extra_flags = self._determine_extra_flags(self.compression_level) self.output_file.write((extra_flags).to_bytes(1, sys.byteorder)) def _write_header_os(self): - """ Write OS """ + """Write OS""" os_number = self._determine_operating_system() self.output_file.write((os_number).to_bytes(1, sys.byteorder)) @@ -351,10 +355,11 @@ def _close_workers(self): def compress_file( source_file, + output_name=None, compresslevel=_COMPRESS_LEVEL_BEST, blocksize=DEFAULT_BLOCK_SIZE_KB, workers=CPU_COUNT, ): - """ Helper function to call underlying class and compression method """ - pigz_file = PigzFile(source_file, compresslevel, blocksize, workers) + """Helper function to call underlying class and compression method""" + pigz_file = PigzFile(source_file, output_name, compresslevel, blocksize, workers) pigz_file.process_compression_target()