From 6f526acb4d01395abe81025351c5957b691727ec Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Fri, 9 Aug 2019 22:39:13 +0300 Subject: [PATCH] - added Exception handing for the case when the user is trying to save & overwrite a file already opened in another file --- FlatCAMApp.py | 75 ++++++++++++++++++++++++--------- FlatCAMObj.py | 5 ++- README.md | 6 ++- flatcamTools/ToolPaint.py | 22 ++++++++-- flatcamTools/ToolSolderPaste.py | 4 ++ 5 files changed, 87 insertions(+), 25 deletions(-) diff --git a/FlatCAMApp.py b/FlatCAMApp.py index e2cf4d83..ee6dcf03 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -4669,10 +4669,13 @@ class App(QtCore.QObject): with open(filename, 'w') as f: for line in my_gcode: f.write(line) - except FileNotFoundError: self.inform.emit(_("[WARNING] No such file or directory")) return + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return # Just for adding it to the recent files list. if self.defaults["global_open_style"] is False: @@ -7109,8 +7112,14 @@ class App(QtCore.QObject): # Parse the xml through a xml parser just to add line feeds # and to make it look more pretty for the output svgcode = parse_xml_string(svg_elem) - with open(filename, 'w') as fp: - fp.write(svgcode.toprettyxml()) + try: + with open(filename, 'w') as fp: + fp.write(svgcode.toprettyxml()) + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' + if self.defaults["global_open_style"] is False: self.file_opened.emit("SVG", filename) self.file_saved.emit("SVG", filename) @@ -7213,8 +7222,13 @@ class App(QtCore.QObject): # Parse the xml through a xml parser just to add line feeds # and to make it look more pretty for the output doc = parse_xml_string(svg_elem) - with open(filename, 'w') as fp: - fp.write(doc.toprettyxml()) + try: + with open(filename, 'w') as fp: + fp.write(doc.toprettyxml()) + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' self.progress.emit(100) if self.defaults["global_open_style"] is False: @@ -7329,8 +7343,14 @@ class App(QtCore.QObject): # Parse the xml through a xml parser just to add line feeds # and to make it look more pretty for the output doc = parse_xml_string(svg_elem) - with open(filename, 'w') as fp: - fp.write(doc.toprettyxml()) + try: + with open(filename, 'w') as fp: + fp.write(doc.toprettyxml()) + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' + self.progress.emit(100) if self.defaults["global_open_style"] is False: self.file_opened.emit("SVG", filename) @@ -7371,15 +7391,20 @@ class App(QtCore.QObject): file_string = StringIO(obj.source_file) time_string = "{:%A, %d %B %Y at %H:%M}".format(datetime.now()) - with open(filename, 'w') as file: - file.writelines('G04*\n') - file.writelines('G04 %s (RE)GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s*\n' % - (obj.kind.upper(), str(self.version), str(self.version_date))) - file.writelines('G04 Filename: %s*\n' % str(obj_name)) - file.writelines('G04 Created on : %s*\n' % time_string) + try: + with open(filename, 'w') as file: + file.writelines('G04*\n') + file.writelines('G04 %s (RE)GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s*\n' % + (obj.kind.upper(), str(self.version), str(self.version_date))) + file.writelines('G04 Filename: %s*\n' % str(obj_name)) + file.writelines('G04 Created on : %s*\n' % time_string) - for line in file_string: - file.writelines(line) + for line in file_string: + file.writelines(line) + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' def export_excellon(self, obj_name, filename, use_thread=True): """ @@ -7481,8 +7506,14 @@ class App(QtCore.QObject): exported_excellon += excellon_code exported_excellon += footer - with open(filename, 'w') as fp: - fp.write(exported_excellon) + try: + with open(filename, 'w') as fp: + fp.write(exported_excellon) + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' + if self.defaults["global_open_style"] is False: self.file_opened.emit("Excellon", filename) self.file_saved.emit("Excellon", filename) @@ -7598,8 +7629,14 @@ class App(QtCore.QObject): exported_gerber += gerber_code exported_gerber += footer - with open(filename, 'w') as fp: - fp.write(exported_gerber) + try: + with open(filename, 'w') as fp: + fp.write(exported_gerber) + except PermissionError: + self.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' + if self.defaults["global_open_style"] is False: self.file_opened.emit("Gerber", filename) self.file_saved.emit("Gerber", filename) diff --git a/FlatCAMObj.py b/FlatCAMObj.py index cb56201e..1c831fc1 100644 --- a/FlatCAMObj.py +++ b/FlatCAMObj.py @@ -5887,12 +5887,15 @@ class FlatCAMCNCjob(FlatCAMObj, CNCjob): with open(filename, 'w') as f: for line in lines: f.write(line) - except FileNotFoundError: self.app.inform.emit(_( "[WARNING_NOTCL] No such file or directory" )) return + except PermissionError: + self.app.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' elif to_file is False: # Just for adding it to the recent files list. if self.app.defaults["global_open_style"] is False: diff --git a/README.md b/README.md index 7579abc9..67f7ae51 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ CAD program, and create G-Code for Isolation routing. ================================================= +9.09.2018 + +- added Exception handing for the case when the user is trying to save & overwrite a file already opened in another file + 7.09.2019 - replaced setFixedWidth calls with setMinimumWidth @@ -26,7 +30,7 @@ CAD program, and create G-Code for Isolation routing. 5.08.2019 - made sure that if using an negative Gerber isolation diameter, the resulting Geometry object will use a tool with positive diameter -- fixed bug that when isolating a Gerber file made out of a single polygon, an Recurrsion Exception was issued together with inability to create tbe isolation +- fixed bug that when isolating a Gerber file made out of a single polygon, an RecursionException was issued together with inability to create tbe isolation - when applying a new language if there are any changes in the current project, the app will offer to save the project before the reboot 3.08.2019 diff --git a/flatcamTools/ToolPaint.py b/flatcamTools/ToolPaint.py index 24026d41..3eb64b64 100644 --- a/flatcamTools/ToolPaint.py +++ b/flatcamTools/ToolPaint.py @@ -825,10 +825,10 @@ class ToolPaint(FlatCAMTool, Gerber): tooldia = float('%.4f' % float(self.tools_table.item(0, 1).text())) # To be called after clicking on the plot. - def doit(event): + def on_mouse_press(event): # do paint single only for left mouse clicks if event.button == 1: - self.app.inform.emit(_("Painting polygon...")) + self.app.inform.emit(_("Painting selected area...")) self.app.plotcanvas.vis_disconnect('mouse_press', doit) pos = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos) @@ -841,10 +841,24 @@ class ToolPaint(FlatCAMTool, Gerber): overlap=overlap, connect=connect, contour=contour) - self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot) + + # to be called after second click on plot + def on_mouse_click_release(event): + self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot) + self.app.plotcanvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot) + self.app.plotcanvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot) + + # called on mouse move + def on_mouse_move(event): + pass self.app.plotcanvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot) - self.app.plotcanvas.vis_connect('mouse_press', doit) + self.app.plotcanvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot) + self.app.plotcanvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot) + + self.app.plotcanvas.vis_connect('mouse_press', on_mouse_press) + self.app.plotcanvas.vis_connect('mouse_move', on_mouse_move) + self.app.plotcanvas.vis_connect('mouse_release', on_mouse_click_release) def paint_poly(self, obj, inside_pt, tooldia, overlap, outname=None, connect=True, contour=True): """ diff --git a/flatcamTools/ToolSolderPaste.py b/flatcamTools/ToolSolderPaste.py index 0bac7e1d..0ebfb665 100644 --- a/flatcamTools/ToolSolderPaste.py +++ b/flatcamTools/ToolSolderPaste.py @@ -1396,6 +1396,10 @@ class SolderPaste(FlatCAMTool): except FileNotFoundError: self.app.inform.emit(_("[WARNING_NOTCL] No such file or directory")) return + except PermissionError: + self.app.inform.emit(_("[WARNING] Permission denied, saving not possible.\n" + "Most likely another app is holding the file open and not accessible.")) + return 'fail' if self.app.defaults["global_open_style"] is False: self.app.file_opened.emit("gcode", filename)