From 49acf7d5290e3ca2a9f919bb835ffc9a9d75cc96 Mon Sep 17 00:00:00 2001 From: bouni Date: Fri, 13 Dec 2024 15:54:10 +0100 Subject: [PATCH 1/2] Add progress bar and logg messages to zip deflation --- library.py | 8 ++++---- unzip_parts.py | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/library.py b/library.py index 7f207be9..caed9799 100644 --- a/library.py +++ b/library.py @@ -119,7 +119,7 @@ def search(self, parameters): "Manufacturer", "Description", "Price", - "First Category" + "First Category", ] s = ",".join(f'"{c}"' for c in columns) query = f"SELECT {s} FROM parts WHERE " @@ -518,13 +518,13 @@ def download(self): return # Delete progress file to indicate the download is complete - if os.path.exists(progress_file): - os.remove(progress_file) + # if os.path.exists(progress_file): + # os.remove(progress_file) # Combine and extract downloaded files self.logger.debug("Combining and extracting zip part files...") try: - unzip_parts(self.datadir) + unzip_parts(self.parent, self.datadir) except Exception as e: wx.PostEvent( self.parent, diff --git a/unzip_parts.py b/unzip_parts.py index 0756e9f2..f76aca9a 100644 --- a/unzip_parts.py +++ b/unzip_parts.py @@ -1,12 +1,21 @@ #!/bin/env python3 """Module for unziping and merging split db zip file.""" +import logging import os from zipfile import ZipFile +import wx # pylint: disable=import-error -def unzip_parts(path): +from .events import ResetGaugeEvent, UpdateGaugeEvent + + +def unzip_parts(parent, path): """Unzip and merge split zip file.""" + logger = logging.getLogger(__name__) + logger.debug("Combine zip chunks") + # reset progress bar + wx.PostEvent(parent, ResetGaugeEvent()) # unzip (needs to go into download function finally) # Set the name of the original file db_zip_file = os.path.join(path, "parts-fts5.db.zip") @@ -22,7 +31,7 @@ def unzip_parts(path): split_files.sort(key=lambda f: int(f.split(".")[-1])) # Iterate over the split files and append their contents to the original file - for split_file_name in split_files: + for i, split_file_name in enumerate(split_files, 1): split_path = os.path.join(path, split_file_name) # Open the split file with open(split_path, "rb") as split_file: @@ -33,8 +42,20 @@ def unzip_parts(path): # Delete the split file os.unlink(split_path) + progress = 100 / len(split_files) * i + wx.PostEvent(parent, UpdateGaugeEvent(value=progress)) with ZipFile(db_zip_file, "r") as zf: - zf.extractall(path) + logger.debug("Extract zip file") + wx.PostEvent(parent, ResetGaugeEvent()) + file_info = zf.infolist()[0] + file_size = file_info.file_size + with zf.open(file_info) as source, open( + os.path.join(path, file_info.filename), "wb" + ) as target: + for chunk in iter(lambda: source.read(1024 * 1024), b""): + target.write(chunk) + progress = target.tell() / file_size * 100 + wx.PostEvent(parent, UpdateGaugeEvent(value=progress)) os.unlink(db_zip_file) From 3f4605ed3accfe7456ec84ded8a6bf614c50b6cf Mon Sep 17 00:00:00 2001 From: bouni Date: Fri, 13 Dec 2024 15:56:13 +0100 Subject: [PATCH 2/2] uncomment dev leftover --- library.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.py b/library.py index caed9799..ed419685 100644 --- a/library.py +++ b/library.py @@ -518,8 +518,8 @@ def download(self): return # Delete progress file to indicate the download is complete - # if os.path.exists(progress_file): - # os.remove(progress_file) + if os.path.exists(progress_file): + os.remove(progress_file) # Combine and extract downloaded files self.logger.debug("Combining and extracting zip part files...")