From 9ed395df99a2a7d9f40084df64183587929352a0 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Mon, 3 May 2021 00:12:24 +0300 Subject: [PATCH] - changed the save_project() method such that when the project is saved as compressed now the compression is first done in memory and only after that the app will attempt to write it to a file on disk --- CHANGELOG.md | 8 ++++++-- app_Main.py | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f03bfb..2b0b3e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ CHANGELOG for FlatCAM beta ================================================= +3.05.2021 + +- changed the save_project() method such that when the project is saved as compressed now the compression is first done in memory and only after that the app will attempt to write it to a file on disk + 29.04.2021 - in CNCJob object, for CNCJob objects created when importing a foreign Gcode file, fixed the updating of the tool diameter @@ -22,7 +26,7 @@ CHANGELOG for FlatCAM beta 5.04.2021 -- now every version of FlatCAM beta creates it's own set of Preferences files +- now every version of FlatCAM beta creates its own set of Preferences files 4.04.2021 @@ -30,7 +34,7 @@ CHANGELOG for FlatCAM beta 1.04.2021 -- fixed bug in the Drilling Tool when there are tools without drill points +- fixed a bug in the Drilling Tool when there are tools without drill points 29.03.2021 diff --git a/app_Main.py b/app_Main.py index a7b26b07..59b7dbf0 100644 --- a/app_Main.py +++ b/app_Main.py @@ -11277,11 +11277,30 @@ class MenuFileHandlers(QtCore.QObject): } if self.defaults["global_save_compressed"] is True: - with lzma.open(filename, "w", preset=int(self.defaults['global_compression_level'])) as f: - g = json.dumps(d, default=to_dict, indent=2, sort_keys=True).encode('utf-8') - # # Write - f.write(g) - self.inform.emit('[success] %s: %s' % (_("Project saved to"), filename)) + try: + project_as_json = json.dumps(d, default=to_dict, indent=2, sort_keys=True).encode('utf-8') + # with lzma.open(filename, "w", preset=int(self.defaults['global_compression_level'])) as f: + # # # Write + # f.write(project_as_json) + + compressor_obj = lzma.LZMACompressor(preset=int(self.defaults['global_compression_level'])) + out1 = compressor_obj.compress(project_as_json) + out2 = compressor_obj.flush() + project_zipped = b"".join([out1, out2]) + except Exception: + self.log.error("Failed to save file: %s", filename) + self.inform.emit('[ERROR_NOTCL] %s' % _("Failed.")) + return + + if project_zipped != b'': + with open(filename, "wb") as f_to_write: + f_to_write.write(project_zipped) + + self.inform.emit('[success] %s: %s' % (_("Project saved to"), filename)) + else: + self.log.error("Failed to save file: %s", filename) + self.inform.emit('[ERROR_NOTCL] %s' % _("Failed.")) + return else: # Open file try: @@ -11307,12 +11326,18 @@ class MenuFileHandlers(QtCore.QObject): try: saved_d = json.load(saved_f, object_hook=dict2obj) + if not saved_d: + self.inform.emit('[ERROR_NOTCL] %s: %s %s' % + (_("Failed to parse saved project file"), filename, _("Retry to save it."))) + f.close() + return except Exception: if silent is False: self.inform.emit('[ERROR_NOTCL] %s: %s %s' % (_("Failed to parse saved project file"), filename, _("Retry to save it."))) f.close() return + saved_f.close() if silent is False: