miurahr / py7zr

7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.
https://pypi.org/project/py7zr/
GNU Lesser General Public License v2.1
463 stars 74 forks source link

add report_update() for logging large files extraction #558

Closed YeeaaahMan closed 10 months ago

YeeaaahMan commented 10 months ago

Pull request type

Which ticket is resolved?

What does this PR change?

Event "u" (update) is called when file is being extracted more that 1 second or when already extracted. Function report_update() receives decompressed_bytes count for decompressed part. Should be useful for progress bar implementation.

Example

import py7zr
from tqdm import tqdm

class ExtractProgressBar(py7zr.callbacks.ExtractCallback, tqdm):
    def __init__(self, *args, total_bytes, **kwargs):
        super().__init__(self, *args, total=total_bytes, **kwargs)

    def report_start_preparation(self):
        pass

    def report_start(self, processing_file_path, processing_bytes):
        pass

    def report_end(self, processing_file_path, wrote_bytes):
        pass

    def report_update(self, decompressed_bytes):
        self.update(int(decompressed_bytes))

    def report_postprocess(self):
        pass

    def report_warning(self, message):
        pass

def extract_7z(src_path, dst_path):
    with py7zr.SevenZipFile(src_path, 'r') as archive:
        archive_info = archive.archiveinfo()
        with ExtractProgressBar(
            unit='B',
            unit_scale=True,
            miniters=1,
            total_bytes=archive_info.uncompressed,
            desc=f"Extracting",
            ascii=True,
        ) as progress:
            archive.extractall(path=dst_path, callback=progress)
miurahr commented 10 months ago

Please add more description of pul-request like as following template?

Pull request type

select from below

  • Bug fix
  • Feature enhancement
  • Documentation
  • Build and release changes
  • Other

Which ticket is resolved?

  • bug ticket/ enhancement issue number

What does this PR change?

-

-

Other information

miurahr commented 10 months ago

Please fix CI check errors

tox: check check: commands[0]> flake8 py7zr tests py7zr/py7zr.py:1437:34: BLK100 Black would make changes. tests/test_misc.py:141:28: BLK100 Black would make changes.

miurahr commented 10 months ago

tox: mypy mypy: commands[0]> mypy --html-report build/mypy py7zr py7zr/cli.py:358:18: error: Cannot instantiate abstract class "CliExtractCallback" with abstract attribute "report_update" [abstract] Generated HTML report (via XSLT): /home/runner/work/py7zr/py7zr/build/mypy/index.html Found 1 error in 1 file (checked 12 source files) mypy: exit 1 (2.55 seconds) /home/runner/work/py7zr/py7zr> mypy --html-report build/mypy py7zr pid=2865 mypy: FAIL ✖ in 22.69 seconds