← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~meths/openlp/trivialfixes into lp:openlp

 

Jon Tibble has proposed merging lp:~meths/openlp/trivialfixes into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~meths/openlp/trivialfixes/+merge/46084

Cleanups and refactor file unicode check
-- 
https://code.launchpad.net/~meths/openlp/trivialfixes/+merge/46084
Your team OpenLP Core is requested to review the proposed merge of lp:~meths/openlp/trivialfixes into lp:openlp.
=== modified file 'openlp/core/lib/imagemanager.py'
--- openlp/core/lib/imagemanager.py	2011-01-12 18:52:16 +0000
+++ openlp/core/lib/imagemanager.py	2011-01-13 02:47:42 +0000
@@ -30,7 +30,6 @@
 to wait for the conversion to happen.
 """
 import logging
-import os
 import time
 
 from PyQt4 import QtCore

=== modified file 'openlp/core/ui/servicemanager.py'
--- openlp/core/ui/servicemanager.py	2011-01-10 20:19:27 +0000
+++ openlp/core/ui/servicemanager.py	2011-01-13 02:47:42 +0000
@@ -37,7 +37,7 @@
     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 @@
         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 @@
                     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:

=== modified file 'openlp/core/ui/thememanager.py'
--- openlp/core/ui/thememanager.py	2011-01-11 17:45:22 +0000
+++ openlp/core/ui/thememanager.py	2011-01-13 02:47:42 +0000
@@ -37,7 +37,8 @@
 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 @@
             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 @@
             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 @@
         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

=== modified file 'openlp/core/utils/__init__.py'
--- openlp/core/utils/__init__.py	2011-01-12 18:27:12 +0000
+++ openlp/core/utils/__init__.py	2011-01-13 02:47:42 +0000
@@ -317,9 +317,29 @@
         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']

=== modified file 'openlp/plugins/bibles/lib/csvbible.py'
--- openlp/plugins/bibles/lib/csvbible.py	2010-12-31 19:22:41 +0000
+++ openlp/plugins/bibles/lib/csvbible.py	2011-01-13 02:47:42 +0000
@@ -72,7 +72,7 @@
                 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:

=== modified file 'openlp/plugins/bibles/lib/opensong.py'
--- openlp/plugins/bibles/lib/opensong.py	2010-12-31 19:22:41 +0000
+++ openlp/plugins/bibles/lib/opensong.py	2011-01-13 02:47:42 +0000
@@ -89,7 +89,7 @@
                         '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:


Follow ups