Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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()
```

Expand All @@ -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')
```
27 changes: 16 additions & 11 deletions pigz_python/pigz_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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()