From a84f19caadc976b1884285491fb980346686a04a Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Wed, 27 Sep 2017 09:31:06 +0200 Subject: [PATCH 1/2] fix #75 : catch exceptions when saving or loading files and display a QMessageBox instead --- larray_editor/editor.py | 44 +++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/larray_editor/editor.py b/larray_editor/editor.py index 6129381..3c14deb 100644 --- a/larray_editor/editor.py +++ b/larray_editor/editor.py @@ -539,23 +539,34 @@ def new(self): self.statusBar().showMessage("Viewer has been reset", 4000) def _open_file(self, filepath): - self._reset() + def _update_arrays(current_file_name, session): + self._reset() + self.set_current_file(current_file_name) + self._add_arrays(session) + self._listwidget.setCurrentRow(0) + self.unsaved_modifications = False + session = Session() if '.csv' in filepath: filepath = [filepath] if isinstance(filepath, (list, tuple)): - session.load(None, filepath) dirname = os.path.dirname(filepath[0]) basenames = [os.path.basename(fpath) for fpath in filepath] - self.set_current_file(dirname) - self.statusBar().showMessage("CSV files {} loaded".format(' ,'.join(basenames)), 4000) + try: + session.load(None, filepath) + _update_arrays(dirname, session) + self.statusBar().showMessage("CSV files {} loaded".format(' ,'.join(basenames)), 4000) + except Exception as e: + QMessageBox.critical(self, "Error", "Something went wrong during load of CSV files {}:\n{}" + .format(' ,'.join(basenames), e)) else: - session.load(filepath) - self.set_current_file(filepath) - self.statusBar().showMessage("File {} loaded".format(os.path.basename(filepath)), 4000) - self._add_arrays(session) - self._listwidget.setCurrentRow(0) - self.unsaved_modifications = False + try: + session.load(filepath) + _update_arrays(filepath, session) + self.statusBar().showMessage("File {} loaded".format(os.path.basename(filepath), 4000)) + except Exception as e: + QMessageBox.critical(self, "Error", "Something went wrong during load of file {}:\n{}" + .format(filepath, e)) def open(self): if self._ask_to_save_if_unsaved_modifications(): @@ -583,11 +594,14 @@ def open_recent_file(self): QMessageBox.warning(self, "Warning", "File {} could not be found".format(filepath)) def _save_data(self, filepath): - session = Session({k: v for k, v in self.data.items() if self._display_in_grid(k, v)}) - session.save(filepath) - self.set_current_file(filepath) - self.unsaved_modifications = False - self.statusBar().showMessage("Arrays saved in file {}".format(filepath), 4000) + try: + session = Session({k: v for k, v in self.data.items() if self._display_in_grid(k, v)}) + session.save(filepath) + self.set_current_file(filepath) + self.unsaved_modifications = False + self.statusBar().showMessage("Arrays saved in file {}".format(filepath), 4000) + except Exception as e: + QMessageBox.critical(self, "Error", "Something went wrong during save in file {}:\n{}".format(filepath, e)) def save(self): """ From 4148a7c95364c86752fd67946c51d31efd79418a Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Wed, 27 Sep 2017 11:20:20 +0200 Subject: [PATCH 2/2] updated _open_file() --- larray_editor/editor.py | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/larray_editor/editor.py b/larray_editor/editor.py index 3c14deb..a876ee2 100644 --- a/larray_editor/editor.py +++ b/larray_editor/editor.py @@ -539,34 +539,29 @@ def new(self): self.statusBar().showMessage("Viewer has been reset", 4000) def _open_file(self, filepath): - def _update_arrays(current_file_name, session): - self._reset() - self.set_current_file(current_file_name) - self._add_arrays(session) - self._listwidget.setCurrentRow(0) - self.unsaved_modifications = False - session = Session() if '.csv' in filepath: filepath = [filepath] if isinstance(filepath, (list, tuple)): - dirname = os.path.dirname(filepath[0]) - basenames = [os.path.basename(fpath) for fpath in filepath] - try: - session.load(None, filepath) - _update_arrays(dirname, session) - self.statusBar().showMessage("CSV files {} loaded".format(' ,'.join(basenames)), 4000) - except Exception as e: - QMessageBox.critical(self, "Error", "Something went wrong during load of CSV files {}:\n{}" - .format(' ,'.join(basenames), e)) + current_file_name = os.path.dirname(filepath[0]) + display_name = ','.join(os.path.basename(fpath) for fpath in filepath) + names = filepath + filepath = None else: - try: - session.load(filepath) - _update_arrays(filepath, session) - self.statusBar().showMessage("File {} loaded".format(os.path.basename(filepath), 4000)) - except Exception as e: - QMessageBox.critical(self, "Error", "Something went wrong during load of file {}:\n{}" - .format(filepath, e)) + names = None + current_file_name = filepath + display_name = os.path.basename(filepath) + try: + session.load(filepath, names) + self._reset() + self.set_current_file(current_file_name) + self._add_arrays(session) + self._listwidget.setCurrentRow(0) + self.unsaved_modifications = False + self.statusBar().showMessage("Loaded: {}".format(display_name), 4000) + except Exception as e: + QMessageBox.critical(self, "Error", "Something went wrong during load of file(s) {}:\n{}" + .format(display_name, e)) def open(self): if self._ask_to_save_if_unsaved_modifications():