diff --git a/openlp/core/lib/imagemanager.py b/openlp/core/lib/imagemanager.py index dcfc50f0f..02d7010be 100644 --- a/openlp/core/lib/imagemanager.py +++ b/openlp/core/lib/imagemanager.py @@ -30,7 +30,6 @@ A Thread is used to convert the image to a byte array so the user does not need to wait for the conversion to happen. """ import logging -import os import time from PyQt4 import QtCore diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index 2d85e4b7f..ffc2bee25 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -37,7 +37,7 @@ from openlp.core.lib import OpenLPToolbar, ServiceItem, context_menu_action, \ Receiver, build_icon, ItemCapabilities, SettingsManager, translate, \ ThemeLevel from openlp.core.ui import ServiceNoteForm, ServiceItemEditForm -from openlp.core.utils import AppLocation, split_filename +from openlp.core.utils import AppLocation, file_is_unicode, split_filename class ServiceManagerList(QtGui.QTreeWidget): """ @@ -484,16 +484,13 @@ class ServiceManager(QtGui.QWidget): try: zip = zipfile.ZipFile(fileName) for file in zip.namelist(): - try: - ucsfile = file.decode(u'utf-8') - except UnicodeDecodeError: + ucsfile = file_is_unicode(file) + if not ucsfile: QtGui.QMessageBox.critical( self, translate('OpenLP.ServiceManager', 'Error'), translate('OpenLP.ServiceManager', 'File is not a valid service.\n' 'The content encoding is not UTF-8.')) - log.exception(u'Filename "%s" is not valid UTF-8' % - file.decode(u'utf-8', u'replace')) continue osfile = unicode(QtCore.QDir.toNativeSeparators(ucsfile)) filePath = os.path.join(self.servicePath, @@ -515,8 +512,7 @@ class ServiceManager(QtGui.QWidget): serviceItem.set_from_service(item, self.servicePath) self.validateItem(serviceItem) self.addServiceItem(serviceItem) - if serviceItem.is_capable( - ItemCapabilities.OnLoadUpdate): + if serviceItem.is_capable(ItemCapabilities.OnLoadUpdate): Receiver.send_message(u'%s_service_load' % serviceItem.name.lower(), serviceItem) try: diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 73f1a6d8b..81e191396 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -37,7 +37,8 @@ from openlp.core.theme import Theme from openlp.core.lib import OpenLPToolbar, ThemeXML, get_text_file_string, \ build_icon, Receiver, SettingsManager, translate, check_item_selected, \ BackgroundType, BackgroundGradientType, check_directory_exists -from openlp.core.utils import AppLocation, get_filesystem_encoding +from openlp.core.utils import AppLocation, file_is_unicode, \ + get_filesystem_encoding log = logging.getLogger(__name__) @@ -475,7 +476,8 @@ class ThemeManager(QtGui.QWidget): unicode(themeName) + u'.xml') xml = get_text_file_string(xmlFile) if not xml: - return self._baseTheme() + log.debug("No theme data - using default theme") + return ThemeXML() else: return self._createThemeFromXml(xml, self.path) @@ -494,16 +496,13 @@ class ThemeManager(QtGui.QWidget): filexml = None themename = None for file in zip.namelist(): - try: - ucsfile = file.decode(u'utf-8') - except UnicodeDecodeError: + ucsfile = file_is_unicode(file) + if not ucsfile: QtGui.QMessageBox.critical( self, translate('OpenLP.ThemeManager', 'Error'), translate('OpenLP.ThemeManager', 'File is not a valid theme.\n' 'The content encoding is not UTF-8.')) - log.exception(u'Filename "%s" is not valid UTF-8' % - file.decode(u'utf-8', u'replace')) continue osfile = unicode(QtCore.QDir.toNativeSeparators(ucsfile)) theme_dir = None @@ -668,14 +667,6 @@ class ThemeManager(QtGui.QWidget): image = os.path.join(self.path, theme + u'.png') return image - def _baseTheme(self): - """ - Provide a base theme with sensible defaults - """ - log.debug(u'base theme created') - newtheme = ThemeXML() - return newtheme - def _createThemeFromXml(self, themeXml, path): """ Return a theme object using information parsed from XML diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index ccdb2afa1..75f00f298 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -317,9 +317,29 @@ def get_web_page(url, header=None, update_openlp=False): Receiver.send_message(u'openlp_process_events') return page +def file_is_unicode(filename): + """ + Checks if a file is valid unicode and returns the unicode decoded file or + None. + + ``filename`` + File to check is valid unicode. + """ + if not filename: + return None + ucsfile = None + try: + ucsfile = filename.decode(u'utf-8') + except UnicodeDecodeError: + log.exception(u'Filename "%s" is not valid UTF-8' % + filename.decode(u'utf-8', u'replace')) + if not ucsfile: + return None + return ucsfile + from languagemanager import LanguageManager from actions import ActionList __all__ = [u'AppLocation', u'check_latest_version', u'add_actions', u'get_filesystem_encoding', u'LanguageManager', u'ActionList', - u'get_web_page'] + u'get_web_page', u'file_is_unicode'] diff --git a/openlp/plugins/bibles/lib/csvbible.py b/openlp/plugins/bibles/lib/csvbible.py index 8b1d70128..2e9e9523b 100644 --- a/openlp/plugins/bibles/lib/csvbible.py +++ b/openlp/plugins/bibles/lib/csvbible.py @@ -72,7 +72,7 @@ class CSVBible(BibleDB): self.create_book(unicode(line[1], details['encoding']), line[2], int(line[0])) Receiver.send_message(u'openlp_process_events') - except IOError, IndexError: + except (IOError, IndexError): log.exception(u'Loading books from file failed') success = False finally: diff --git a/openlp/plugins/bibles/lib/opensong.py b/openlp/plugins/bibles/lib/opensong.py index c0b60f911..03d243390 100644 --- a/openlp/plugins/bibles/lib/opensong.py +++ b/openlp/plugins/bibles/lib/opensong.py @@ -89,7 +89,7 @@ class OpenSongBible(BibleDB): 'Importing ...')) % (db_book.name, int(chapter.attrib[u'n']))) self.session.commit() - except IOError, AttributeError: + except (IOError, AttributeError): log.exception(u'Loading bible from OpenSong file failed') success = False finally: