Reading packages from archive now uses With context

This commit is contained in:
Jaime van Kessel 2018-05-03 20:22:38 +02:00
parent defb22dc07
commit 7665f8a7cb
2 changed files with 31 additions and 35 deletions

View file

@ -190,6 +190,8 @@ class CuraPackageManager(QObject):
try:
# Get package information
package_info = self.getPackageInfo(filename)
if not package_info:
return
package_id = package_info["package_id"]
# Check the delayed installation and removal lists first
@ -282,7 +284,7 @@ class CuraPackageManager(QObject):
self._purgePackage(package_id)
# Install the package
archive = zipfile.ZipFile(filename, "r")
with zipfile.ZipFile(filename, "r") as archive:
temp_dir = tempfile.TemporaryDirectory()
archive.extractall(temp_dir.name)
@ -305,8 +307,6 @@ class CuraPackageManager(QObject):
to_rename_files = sub_dir_name not in ("plugins",)
self.__installPackageFiles(package_id, src_dir_path, dst_dir_path, need_to_rename_files= to_rename_files)
archive.close()
# Remove the file
os.remove(filename)
@ -325,16 +325,15 @@ class CuraPackageManager(QObject):
# Gets package information from the given file.
def getPackageInfo(self, filename: str) -> dict:
archive = zipfile.ZipFile(filename, "r")
with zipfile.ZipFile(filename, "r") as archive:
try:
# All information is in package.json
with archive.open("package.json", "r") as f:
package_info_dict = json.loads(f.read().decode("utf-8"))
return package_info_dict
except Exception as e:
raise RuntimeError("Could not get package information from file '%s': %s" % (filename, e))
finally:
archive.close()
Logger.logException("Could not get package information from file '%s': %s" % (filename, e))
return {}
# Gets the license file content if present in the given package file.
# Returns None if there is no license file found.

View file

@ -420,10 +420,9 @@ class Toolbox(QObject, Extension):
def _onDownloadComplete(self, file_path: str):
Logger.log("i", "Toolbox: Download complete.")
try:
package_info = self._package_manager.getPackageInfo(file_path)
except:
Logger.logException("w", "Toolbox: Package file [%s] was not a valid CuraPackage.", file_path)
if not package_info:
Logger.log("w", "Toolbox: Package file [%s] was not a valid CuraPackage.", file_path)
return
license_content = self._package_manager.getPackageLicense(file_path)
@ -434,8 +433,6 @@ class Toolbox(QObject, Extension):
self.install(file_path)
return
# Getter & Setters for Properties:
# --------------------------------------------------------------------------
def setDownloadProgress(self, progress: int):