mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-07 23:17:32 -06:00
Fix downloadPresenter and initial LicensePresenter.py code
CURA-6983
This commit is contained in:
parent
028aece644
commit
dda3d0b4eb
5 changed files with 196 additions and 43 deletions
|
@ -1,7 +1,7 @@
|
|||
import os
|
||||
import tempfile
|
||||
from functools import reduce
|
||||
from typing import Dict, List, Optional
|
||||
from typing import Dict, List, Optional, Any
|
||||
|
||||
from PyQt5.QtNetwork import QNetworkReply
|
||||
|
||||
|
@ -30,7 +30,7 @@ class DownloadPresenter:
|
|||
|
||||
self._started = False
|
||||
self._progress_message = None # type: Optional[Message]
|
||||
self._progress = {} # type: Dict[str, Dict[str, int]] # package_id, Dict
|
||||
self._progress = {} # type: Dict[str, Dict[str, Any]] # package_id, Dict
|
||||
self._error = [] # type: List[str] # package_id
|
||||
|
||||
def download(self, model: SubscribedPackagesModel):
|
||||
|
@ -41,26 +41,41 @@ class DownloadPresenter:
|
|||
manager = HttpRequestManager.getInstance()
|
||||
for item in model.items:
|
||||
package_id = item["package_id"]
|
||||
|
||||
request_data = manager.get(
|
||||
item["download_url"],
|
||||
callback = lambda reply, pid = package_id: self._onFinished(pid, reply),
|
||||
download_progress_callback = lambda rx, rt, pid = package_id: self._onProgress(pid, rx, rt),
|
||||
error_callback = lambda rx, rt, pid = package_id: self._onProgress(pid, rx, rt),
|
||||
scope = self._scope)
|
||||
|
||||
self._progress[package_id] = {
|
||||
"received": 0,
|
||||
"total": 1 # make sure this is not considered done yet. Also divByZero-safe
|
||||
"total": 1, # make sure this is not considered done yet. Also divByZero-safe
|
||||
"file_written": None,
|
||||
"request_data": request_data
|
||||
}
|
||||
|
||||
manager.get(
|
||||
item["download_url"],
|
||||
callback = lambda reply: self._onFinished(package_id, reply),
|
||||
download_progress_callback = lambda rx, rt: self._onProgress(package_id, rx, rt),
|
||||
error_callback = lambda rx, rt: self._onProgress(package_id, rx, rt),
|
||||
scope = self._scope)
|
||||
|
||||
self._started = True
|
||||
self._showProgressMessage()
|
||||
|
||||
def abort(self):
|
||||
manager = HttpRequestManager.getInstance()
|
||||
for item in self._progress.values():
|
||||
manager.abortRequest(item["request_data"])
|
||||
|
||||
# Aborts all current operations and returns a copy with the same settings such as app and scope
|
||||
def resetCopy(self):
|
||||
self.abort()
|
||||
self.done.disconnectAll()
|
||||
return DownloadPresenter(self._app)
|
||||
|
||||
def _showProgressMessage(self):
|
||||
self._progress_message = Message(i18n_catalog.i18nc(
|
||||
"@info:generic",
|
||||
"\nSyncing..."),
|
||||
lifetime = 0,
|
||||
use_inactivity_timer=False,
|
||||
progress = 0.0,
|
||||
title = i18n_catalog.i18nc("@info:title", "Changes detected from your Ultimaker account", ))
|
||||
self._progress_message.show()
|
||||
|
@ -68,13 +83,21 @@ class DownloadPresenter:
|
|||
def _onFinished(self, package_id: str, reply: QNetworkReply):
|
||||
self._progress[package_id]["received"] = self._progress[package_id]["total"]
|
||||
|
||||
file_path = self._getTempFile(package_id)
|
||||
file_fd, file_path = tempfile.mkstemp()
|
||||
os.close(file_fd) # close the file so we can open it from python
|
||||
|
||||
try:
|
||||
with open(file_path) as temp_file:
|
||||
# todo buffer this
|
||||
temp_file.write(reply.readAll())
|
||||
except IOError:
|
||||
with open(file_path, "wb+") as temp_file:
|
||||
bytes_read = reply.read(256 * 1024)
|
||||
while bytes_read:
|
||||
temp_file.write(bytes_read)
|
||||
bytes_read = reply.read(256 * 1024)
|
||||
self._app.processEvents()
|
||||
self._progress[package_id]["file_written"] = file_path
|
||||
except IOError as e:
|
||||
Logger.logException("e", "Failed to write downloaded package to temp file", e)
|
||||
self._onError(package_id)
|
||||
temp_file.close()
|
||||
|
||||
self._checkDone()
|
||||
|
||||
|
@ -90,8 +113,6 @@ class DownloadPresenter:
|
|||
|
||||
self._progress_message.setProgress(100.0 * (received / total)) # [0 .. 100] %
|
||||
|
||||
self._checkDone()
|
||||
|
||||
def _onError(self, package_id: str):
|
||||
self._progress.pop(package_id)
|
||||
self._error.append(package_id)
|
||||
|
@ -99,16 +120,11 @@ class DownloadPresenter:
|
|||
|
||||
def _checkDone(self) -> bool:
|
||||
for item in self._progress.values():
|
||||
if item["received"] != item["total"] or item["total"] == -1:
|
||||
if not item["file_written"]:
|
||||
return False
|
||||
|
||||
success_items = {package_id : self._getTempFile(package_id) for package_id in self._progress.keys()}
|
||||
success_items = {package_id : value["file_written"] for package_id, value in self._progress.items()}
|
||||
error_items = [package_id for package_id in self._error]
|
||||
|
||||
self._progress_message.hide()
|
||||
self.done.emit(success_items, error_items)
|
||||
|
||||
def _getTempFile(self, package_id: str) -> str:
|
||||
temp_dir = tempfile.gettempdir()
|
||||
return os.path.join(temp_dir, package_id)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue