Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress bar and logg messages to zip deflation #558

Merged
merged 2 commits into from
Dec 13, 2024
Merged
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
4 changes: 2 additions & 2 deletions library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -524,7 +524,7 @@ def download(self):
# 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,
Expand Down
27 changes: 24 additions & 3 deletions unzip_parts.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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:
Expand All @@ -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)
Loading