From b7ec09cf8720346c86fb4c16292821b62a7c646f Mon Sep 17 00:00:00 2001 From: M2j Date: Sat, 19 Jun 2010 12:41:13 +0200 Subject: [PATCH] Unicode support for theme XML --- openlp/core/lib/__init__.py | 12 +++++++----- openlp/core/lib/themexmlhandler.py | 9 +++++---- openlp/core/theme/theme.py | 2 +- openlp/core/ui/mainwindow.py | 4 +--- openlp/core/ui/thememanager.py | 12 +++++++++--- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 40557d446..949aea3da 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -52,9 +52,10 @@ def translate(context, text, comment=None): def get_text_file_string(text_file): """ - Open a file and return the contents of the file. If the supplied file name - is not a file then the function returns False. If there is an error - loading the file then the function will return None. + Open a file and return its content as unicode string. If the supplied file + name is not a file then the function returns False. If there is an error + loading the file or the content can't be decoded then the function will + return None. ``textfile`` The name of the file. @@ -65,8 +66,9 @@ def get_text_file_string(text_file): content_string = None try: file_handle = open(text_file, u'r') - content_string = file_handle.read() - except IOError: + content = file_handle.read() + content_string = content.decode(u'utf-8') + except (IOError, UnicodeError): log.exception(u'Failed to open text file %s' % text_file) finally: if file_handle: diff --git a/openlp/core/lib/themexmlhandler.py b/openlp/core/lib/themexmlhandler.py index a6d1ec186..3303b8d77 100644 --- a/openlp/core/lib/themexmlhandler.py +++ b/openlp/core/lib/themexmlhandler.py @@ -334,13 +334,13 @@ class ThemeXML(object): Pull out the XML string. """ # Print our newly created XML - return self.theme_xml.toxml() + return self.theme_xml.toxml(u'utf-8').decode(u'utf-8') def extract_formatted_xml(self): """ Pull out the XML string formatted for human consumption """ - return self.theme_xml.toprettyxml(indent=u' ', newl=u'\n') + return self.theme_xml.toprettyxml(indent=u' ', newl=u'\n', encoding=u'utf-8') def parse(self, xml): """ @@ -365,11 +365,12 @@ class ThemeXML(object): ``xml`` The XML string to parse. """ - theme_xml = ElementTree(element=XML(xml)) + theme_xml = ElementTree(element=XML(xml.encode(u'ascii', u'xmlcharrefreplace'))) xml_iter = theme_xml.getiterator() master = u'' for element in xml_iter: - element.text = unicode(element.text).decode('unicode-escape') + if not isinstance(element.text, unicode): + element.text = unicode(str(element.text), u'utf-8') if element.getchildren(): master = element.tag + u'_' else: diff --git a/openlp/core/theme/theme.py b/openlp/core/theme/theme.py index 75a2ddd85..b75b55a2e 100644 --- a/openlp/core/theme/theme.py +++ b/openlp/core/theme/theme.py @@ -175,7 +175,7 @@ class Theme(object): ``xml`` The data to apply to the theme """ - root = ElementTree(element=XML(xml)) + root = ElementTree(element=XML(xml.encode(u'ascii', u'xmlcharrefreplace'))) xml_iter = root.getiterator() for element in xml_iter: delphi_color_change = False diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 9efadddd8..9de790854 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -356,8 +356,6 @@ class Ui_MainWindow(object): """ MainWindow.mainTitle = translate(u'MainWindow', u'OpenLP 2.0') MainWindow.language = translate(u'MainWindow', u'English') - MainWindow.defaultThemeText = translate(u'MainWindow', - u'Default Theme: ') MainWindow.setWindowTitle(MainWindow.mainTitle) self.FileMenu.setTitle(translate(u'MainWindow', u'&File')) self.FileImportMenu.setTitle(translate(u'MainWindow', u'&Import')) @@ -774,7 +772,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow): def defaultThemeChanged(self, theme): self.DefaultThemeLabel.setText( - u'%s %s' % (self.defaultThemeText, theme)) + unicode(translate('MainWindow', 'Default Theme: %s')) % theme) def toggleMediaManager(self, visible): if self.MediaManagerDock.isVisible() != visible: diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 5b9f78f75..431b1d450 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -418,15 +418,21 @@ class ThemeManager(QtGui.QWidget): theme_dir = os.path.join(dir, names[0]) if not os.path.exists(theme_dir): os.mkdir(os.path.join(dir, names[0])) - xml_data = zip.read(file) if os.path.splitext(ucsfile)[1].lower() in [u'.xml']: + xml_data = zip.read(file) + try: + xml_data = xml_data.decode(u'utf-8') + except UnicodeDecodeError: + log.exception(u'Theme XML is not UTF-8 ' + 'encoded.') + break; if self.checkVersion1(xml_data): # upgrade theme xml filexml = self.migrateVersion122(xml_data) else: filexml = xml_data outfile = open(fullpath, u'w') - outfile.write(filexml) + outfile.write(filexml.encode(u'utf-8')) else: outfile = open(fullpath, u'wb') outfile.write(zip.read(file)) @@ -457,7 +463,7 @@ class ThemeManager(QtGui.QWidget): Theme XML to check the version of """ log.debug(u'checkVersion1 ') - theme = xmlfile + theme = xmlfile.encode(u'ascii', u'xmlcharrefreplace') tree = ElementTree(element=XML(theme)).getroot() if tree.find(u'BackgroundType') is None: return False