Handle OSError exception when creating files in various places. Fixes bug 1416888.

Fixes: https://launchpad.net/bugs/1416888
This commit is contained in:
Tomas Groth 2015-02-06 22:17:24 +00:00
parent a5b92f1e87
commit d8e6f35694
5 changed files with 46 additions and 27 deletions

View File

@ -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):
"""

View File

@ -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)

View File

@ -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):
"""

View File

@ -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 <strong>OpenLyrics</strong> 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 <strong>OpenLyrics</strong> 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=''):
"""

View File

@ -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()