From d8e6f35694d152aeabb68ea4992ae9a8ad04527c Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Fri, 6 Feb 2015 22:17:24 +0000 Subject: [PATCH] Handle OSError exception when creating files in various places. Fixes bug 1416888. Fixes: https://launchpad.net/bugs/1416888 --- openlp/core/ui/mainwindow.py | 24 ++++++++++++------- openlp/core/ui/servicemanager.py | 6 +++++ openlp/core/ui/thememanager.py | 21 ++++++++-------- openlp/plugins/songs/forms/songexportform.py | 16 ++++++++----- .../songusage/forms/songusagedetailform.py | 6 ++++- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 2feab127e..b4413552e 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -989,15 +989,21 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow, RegistryProperties): # Read the temp file and output the user's CONF file with blanks to # make it more readable. temp_conf = open(temp_file, 'r') - export_conf = open(export_file_name, 'w') - for file_record in temp_conf: - # Get rid of any invalid entries. - if file_record.find('@Invalid()') == -1: - file_record = file_record.replace('%20', ' ') - export_conf.write(file_record) - temp_conf.close() - export_conf.close() - os.remove(temp_file) + try: + export_conf = open(export_file_name, 'w') + for file_record in temp_conf: + # Get rid of any invalid entries. + if file_record.find('@Invalid()') == -1: + file_record = file_record.replace('%20', ' ') + export_conf.write(file_record) + temp_conf.close() + export_conf.close() + os.remove(temp_file) + except OSError as ose: + QtGui.QMessageBox.critical(self, translate('OpenLP.MainWindow', 'Export setting error'), + translate('OpenLP.MainWindow', 'While writing the export file this error ' + 'occurred:\n%s') % str(ose), + QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok)) def on_mode_default_item_clicked(self): """ diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index 8bdf9c383..61299d548 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -601,6 +601,12 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtGui.QWidget, Ui_ServiceManage shutil.copy(temp_file_name, path_file_name) except shutil.Error: return self.save_file_as() + except OSError as ose: + QtGui.QMessageBox.critical(self, translate('OpenLP.ServiceManager', 'Error Saving File'), + translate('OpenLP.ServiceManager', 'While writing the service file this ' + 'error occurred:\n%s') % str(ose), + QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok)) + success = False self.main_window.add_recent_file(path_file_name) self.set_modified(False) delete_file(temp_file_name) diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 5ced57ea7..e3df2f4fe 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -377,17 +377,11 @@ class ThemeManager(OpenLPMixin, RegistryMixin, QtGui.QWidget, Ui_ThemeManager, R self.application.set_busy_cursor() if path: Settings().setValue(self.settings_section + '/last directory export', path) - try: - self._export_theme(path, theme) + if self._export_theme(path, theme): QtGui.QMessageBox.information(self, translate('OpenLP.ThemeManager', 'Theme Exported'), translate('OpenLP.ThemeManager', 'Your theme has been successfully exported.')) - except (IOError, OSError): - self.log_exception('Export Theme Failed') - critical_error_message_box(translate('OpenLP.ThemeManager', 'Theme Export Failed'), - translate('OpenLP.ThemeManager', - 'Your theme could not be exported due to an error.')) self.application.set_normal_cursor() def _export_theme(self, path, theme): @@ -397,19 +391,24 @@ class ThemeManager(OpenLPMixin, RegistryMixin, QtGui.QWidget, Ui_ThemeManager, R :param theme: The name of the theme to be exported """ theme_path = os.path.join(path, theme + '.otz') + theme_zip = None try: theme_zip = zipfile.ZipFile(theme_path, 'w') source = os.path.join(self.path, theme) for files in os.walk(source): for name in files[2]: theme_zip.write(os.path.join(source, name), os.path.join(theme, name)) - except (IOError, OSError): + theme_zip.close() + return True + except OSError as ose: + self.log_exception('Export Theme Failed') + critical_error_message_box(translate('OpenLP.ThemeManager', 'Theme Export Failed'), + translate('OpenLP.ThemeManager', + 'Your theme could not be exported due to this error:\n%s' % str(ose))) if theme_zip: theme_zip.close() shutil.rmtree(theme_path, True) - raise - else: - theme_zip.close() + return False def on_import_theme(self, field=None): """ diff --git a/openlp/plugins/songs/forms/songexportform.py b/openlp/plugins/songs/forms/songexportform.py index 9caf7a446..a136ccee1 100644 --- a/openlp/plugins/songs/forms/songexportform.py +++ b/openlp/plugins/songs/forms/songexportform.py @@ -244,12 +244,16 @@ class SongExportForm(OpenLPWizard): for song in self._find_list_widget_items(self.selected_list_widget) ] exporter = OpenLyricsExport(self, songs, self.directory_line_edit.text()) - if exporter.do_export(): - self.progress_label.setText( - translate('SongsPlugin.SongExportForm', - 'Finished export. To import these files use the OpenLyrics importer.')) - else: - self.progress_label.setText(translate('SongsPlugin.SongExportForm', 'Your song export failed.')) + try: + if exporter.do_export(): + self.progress_label.setText( + translate('SongsPlugin.SongExportForm', + 'Finished export. To import these files use the OpenLyrics importer.')) + else: + self.progress_label.setText(translate('SongsPlugin.SongExportForm', 'Your song export failed.')) + except OSError as ose: + self.progress_label.setText(translate('SongsPlugin.SongExportForm', 'Your song export failed due to this ' + 'error:\n%s') % str(ose)) def _find_list_widget_items(self, list_widget, text=''): """ diff --git a/openlp/plugins/songusage/forms/songusagedetailform.py b/openlp/plugins/songusage/forms/songusagedetailform.py index ba65e21df..19da1f731 100644 --- a/openlp/plugins/songusage/forms/songusagedetailform.py +++ b/openlp/plugins/songusage/forms/songusagedetailform.py @@ -27,6 +27,7 @@ from PyQt4 import QtGui from sqlalchemy.sql import and_ from openlp.core.common import RegistryProperties, Settings, check_directory_exists, translate +from openlp.core.lib.ui import critical_error_message_box from openlp.plugins.songusage.lib.db import SongUsageItem from .songusagedetaildialog import Ui_SongUsageDetailDialog @@ -104,8 +105,11 @@ class SongUsageDetailForm(QtGui.QDialog, Ui_SongUsageDetailDialog, RegistryPrope translate('SongUsagePlugin.SongUsageDetailForm', 'Report \n%s \nhas been successfully created. ') % report_file_name ) - except IOError: + except OSError as ose: log.exception('Failed to write out song usage records') + critical_error_message_box(translate('SongUsagePlugin.SongUsageDetailForm', 'Report Creation Failed'), + translate('SongUsagePlugin.SongUsageDetailForm', + 'The report could not be created due to this error:\n%s' % str(ose))) finally: if file_handle: file_handle.close()