Unicode support for theme XML

This commit is contained in:
M2j 2010-06-19 12:41:13 +02:00
parent b13f9eb74f
commit b7ec09cf87
5 changed files with 23 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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

View File

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