forked from openlp/openlp
Cleanups and refactor unicode file check
bzr-revno: 1213
This commit is contained in:
commit
56d3712ded
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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']
|
||||
|
@ -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:
|
||||
|
@ -89,7 +89,7 @@ class OpenSongBible(BibleDB):
|
||||
'Importing <book name> <chapter>...')) %
|
||||
(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:
|
||||
|
Loading…
Reference in New Issue
Block a user