Cleanup and refactor file unicode check

This commit is contained in:
Jon Tibble 2011-01-13 01:14:38 +00:00
parent 658645cfc9
commit 7f5293c4c8
4 changed files with 31 additions and 25 deletions

View File

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

View File

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

View File

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

View File

@ -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']