Fixed up to Raouls guidelines

This commit is contained in:
Philip Ridout 2012-03-10 08:22:52 +00:00
parent e9caa194f5
commit a40717c16f
1 changed files with 90 additions and 93 deletions

View File

@ -178,10 +178,10 @@ class ThemeManager(QtGui.QWidget):
""" """
if item is None: if item is None:
return return
realThemeName = unicode(item.data(QtCore.Qt.UserRole).toString()) realtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
themeName = unicode(item.text()) theme_name = unicode(item.text())
# If default theme restrict actions # If default theme restrict actions
if realThemeName == themeName: if realtheme_name == theme_name:
self.deleteToolbarAction.setVisible(True) self.deleteToolbarAction.setVisible(True)
else: else:
self.deleteToolbarAction.setVisible(False) self.deleteToolbarAction.setVisible(False)
@ -194,24 +194,24 @@ class ThemeManager(QtGui.QWidget):
item = self.themeListWidget.itemAt(point) item = self.themeListWidget.itemAt(point)
if item is None: if item is None:
return return
realThemeName = unicode(item.data(QtCore.Qt.UserRole).toString()) realtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
themeName = unicode(item.text()) theme_name = unicode(item.text())
self.deleteAction.setVisible(False) self.deleteAction.setVisible(False)
self.renameAction.setVisible(False) self.renameAction.setVisible(False)
self.globalAction.setVisible(False) self.globalAction.setVisible(False)
# If default theme restrict actions # If default theme restrict actions
if realThemeName == themeName: if realtheme_name == theme_name:
self.deleteAction.setVisible(True) self.deleteAction.setVisible(True)
self.renameAction.setVisible(True) self.renameAction.setVisible(True)
self.globalAction.setVisible(True) self.globalAction.setVisible(True)
self.menu.exec_(self.themeListWidget.mapToGlobal(point)) self.menu.exec_(self.themeListWidget.mapToGlobal(point))
def changeGlobalFromTab(self, themeName): def changeGlobalFromTab(self, theme_name):
""" """
Change the global theme when it is changed through the Themes settings Change the global theme when it is changed through the Themes settings
tab tab
""" """
log.debug(u'changeGlobalFromTab %s', themeName) log.debug(u'changeGlobalFromTab %s', theme_name)
for count in range (0, self.themeListWidget.count()): for count in range (0, self.themeListWidget.count()):
# reset the old name # reset the old name
item = self.themeListWidget.item(count) item = self.themeListWidget.item(count)
@ -220,7 +220,7 @@ class ThemeManager(QtGui.QWidget):
if oldName != newName: if oldName != newName:
self.themeListWidget.item(count).setText(newName) self.themeListWidget.item(count).setText(newName)
# Set the new name # Set the new name
if themeName == newName: if theme_name == newName:
name = unicode(translate('OpenLP.ThemeManager', name = unicode(translate('OpenLP.ThemeManager',
'%s (default)')) % newName '%s (default)')) % newName
self.themeListWidget.item(count).setText(name) self.themeListWidget.item(count).setText(name)
@ -272,19 +272,19 @@ class ThemeManager(QtGui.QWidget):
unicode(translate('OpenLP.ThemeManager', 'Rename %s theme?')), unicode(translate('OpenLP.ThemeManager', 'Rename %s theme?')),
False, False): False, False):
item = self.themeListWidget.currentItem() item = self.themeListWidget.currentItem()
oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString()) oldtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
self.fileRenameForm.fileNameEdit.setText(oldThemeName) self.fileRenameForm.fileNameEdit.setText(oldtheme_name)
if self.fileRenameForm.exec_(): if self.fileRenameForm.exec_():
newThemeName = unicode(self.fileRenameForm.fileNameEdit.text()) newtheme_name = unicode(self.fileRenameForm.fileNameEdit.text())
if oldThemeName == newThemeName: if oldtheme_name == newtheme_name:
return return
if self.checkIfThemeExists(newThemeName): if self.checkIfThemeExists(newtheme_name):
oldThemeData = self.getThemeData(oldThemeName) oldThemeData = self.getThemeData(oldtheme_name)
self.cloneThemeData(oldThemeData, newThemeName) self.cloneThemeData(oldThemeData, newtheme_name)
self.deleteTheme(oldThemeName) self.deleteTheme(oldtheme_name)
for plugin in self.mainwindow.pluginManager.plugins: for plugin in self.mainwindow.pluginManager.plugins:
if plugin.usesTheme(oldThemeName): if plugin.usesTheme(oldtheme_name):
plugin.renameTheme(oldThemeName, newThemeName) plugin.renameTheme(oldtheme_name, newtheme_name)
self.loadThemes() self.loadThemes()
def onCopyTheme(self): def onCopyTheme(self):
@ -292,17 +292,17 @@ class ThemeManager(QtGui.QWidget):
Copies an existing theme to a new name Copies an existing theme to a new name
""" """
item = self.themeListWidget.currentItem() item = self.themeListWidget.currentItem()
oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString()) oldtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
self.fileRenameForm.fileNameEdit.setText( self.fileRenameForm.fileNameEdit.setText(
unicode(translate('OpenLP.ThemeManager', unicode(translate('OpenLP.ThemeManager',
'Copy of %s','Copy of <theme name>')) % oldThemeName) 'Copy of %s','Copy of <theme name>')) % oldtheme_name)
if self.fileRenameForm.exec_(True): if self.fileRenameForm.exec_(True):
newThemeName = unicode(self.fileRenameForm.fileNameEdit.text()) newtheme_name = unicode(self.fileRenameForm.fileNameEdit.text())
if self.checkIfThemeExists(newThemeName): if self.checkIfThemeExists(newtheme_name):
themeData = self.getThemeData(oldThemeName) themeData = self.getThemeData(oldtheme_name)
self.cloneThemeData(themeData, newThemeName) self.cloneThemeData(themeData, newtheme_name)
def cloneThemeData(self, themeData, newThemeName): def cloneThemeData(self, themeData, newtheme_name):
""" """
Takes a theme and makes a new copy of it as well as saving it. Takes a theme and makes a new copy of it as well as saving it.
""" """
@ -310,10 +310,10 @@ class ThemeManager(QtGui.QWidget):
saveTo = None saveTo = None
saveFrom = None saveFrom = None
if themeData.background_type == u'image': if themeData.background_type == u'image':
saveTo = os.path.join(self.path, newThemeName, saveTo = os.path.join(self.path, newtheme_name,
os.path.split(unicode(themeData.background_filename))[1]) os.path.split(unicode(themeData.background_filename))[1])
saveFrom = themeData.background_filename saveFrom = themeData.background_filename
themeData.theme_name = newThemeName themeData.theme_name = newtheme_name
themeData.extend_image_filename(self.path) themeData.extend_image_filename(self.path)
self.saveTheme(themeData, saveFrom, saveTo) self.saveTheme(themeData, saveFrom, saveTo)
@ -458,7 +458,7 @@ class ThemeManager(QtGui.QWidget):
files = SettingsManager.get_files(self.settingsSection, u'.png') files = SettingsManager.get_files(self.settingsSection, u'.png')
# Sort the themes by its name considering language specific characters. # Sort the themes by its name considering language specific characters.
# lower() is needed for windows! # lower() is needed for windows!
files.sort(key=lambda filename: unicode(filename).lower(), files.sort(key=lambda file_name: unicode(file_name).lower(),
cmp=locale.strcoll) cmp=locale.strcoll)
# now process the file list of png files # now process the file list of png files
for name in files: for name in files:
@ -495,16 +495,16 @@ class ThemeManager(QtGui.QWidget):
""" """
return self.themelist return self.themelist
def getThemeData(self, themeName): def getThemeData(self, theme_name):
""" """
Returns a theme object from an XML file Returns a theme object from an XML file
``themeName`` ``theme_name``
Name of the theme to load from file Name of the theme to load from file
""" """
log.debug(u'getthemedata for theme %s', themeName) log.debug(u'getthemedata for theme %s', theme_name)
xmlFile = os.path.join(self.path, unicode(themeName), xmlFile = os.path.join(self.path, unicode(theme_name),
unicode(themeName) + u'.xml') unicode(theme_name) + u'.xml')
xml = get_text_file_string(xmlFile) xml = get_text_file_string(xmlFile)
if not xml: if not xml:
log.debug("No theme data - using default theme") log.debug("No theme data - using default theme")
@ -512,33 +512,30 @@ class ThemeManager(QtGui.QWidget):
else: else:
return self._createThemeFromXml(xml, self.path) return self._createThemeFromXml(xml, self.path)
def overWriteMessageBox(self, themeName): def overWriteMessageBox(self, theme_name):
ret = QtGui.QMessageBox.question(self, ret = QtGui.QMessageBox.question(self,
translate('OpenLP.ThemeManager', 'Theme Already Exists!'), translate('OpenLP.ThemeManager', 'Theme Already Exists!'),
translate('OpenLP.ThemeManager', translate('OpenLP.ThemeManager',
'The theme %s already exists. Do you want to replace it?' 'The theme %s already exists. Do you want to replace it?'
% themeName), % theme_name),
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes | QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No), QtGui.QMessageBox.No),
QtGui.QMessageBox.No) QtGui.QMessageBox.No)
if ret == QtGui.QMessageBox.Yes: return ret == QtGui.QMessageBox.Yes
return True
elif ret == QtGui.QMessageBox.No:
return False
def unzipTheme(self, filename, dir): def unzipTheme(self, file_name, dir):
""" """
Unzip the theme, remove the preview file if stored Unzip the theme, remove the preview file if stored
Generate a new preview file. Check the XML theme version and upgrade if Generate a new preview file. Check the XML theme version and upgrade if
necessary. necessary.
""" """
log.debug(u'Unzipping theme %s', filename) log.debug(u'Unzipping theme %s', file_name)
filename = unicode(filename) file_name = unicode(file_name)
zip = None zip = None
outfile = None out_file = None
filexml = None file_xml = None
try: try:
zip = zipfile.ZipFile(filename) zip = zipfile.ZipFile(file_name)
xmlfile = filter(lambda name: xmlfile = filter(lambda name:
os.path.splitext(name)[1].lower() == u'.xml', zip.namelist()) os.path.splitext(name)[1].lower() == u'.xml', zip.namelist())
if len(xmlfile) != 1: if len(xmlfile) != 1:
@ -547,17 +544,17 @@ class ThemeManager(QtGui.QWidget):
xml_tree = ElementTree(element=XML(zip.read(xmlfile[0]))).getroot() xml_tree = ElementTree(element=XML(zip.read(xmlfile[0]))).getroot()
v1_background = xml_tree.find(u'BackgroundType') v1_background = xml_tree.find(u'BackgroundType')
if v1_background is not None: if v1_background is not None:
(themename, filexml, outfile, abortimport) = self.unzipVersion122(dir, zip, theme_name, file_xml, out_file, abort_import = self.unzipVersion122(dir, zip,
xmlfile[0], xml_tree, v1_background, outfile) xmlfile[0], xml_tree, v1_background, out_file)
else: else:
themename = xml_tree.find(u'name').text.strip() theme_name = xml_tree.find(u'name').text.strip()
themefolder = os.path.join(dir, themename) theme_folder = os.path.join(dir, theme_name)
themeexists = os.path.exists(themefolder) theme_exists = os.path.exists(theme_folder)
if themeexists and not self.overWriteMessageBox(themename): if theme_exists and not self.overWriteMessageBox(theme_name):
abortimport = True abort_import = True
return return
else: else:
abortimport = False abort_import = False
for name in zip.namelist(): for name in zip.namelist():
try: try:
uname = unicode(name, u'utf-8') uname = unicode(name, u'utf-8')
@ -573,15 +570,15 @@ class ThemeManager(QtGui.QWidget):
fullname = os.path.join(dir, uname) fullname = os.path.join(dir, uname)
check_directory_exists(os.path.dirname(fullname)) check_directory_exists(os.path.dirname(fullname))
if os.path.splitext(uname)[1].lower() == u'.xml': if os.path.splitext(uname)[1].lower() == u'.xml':
filexml = unicode(zip.read(name), u'utf-8') file_xml = unicode(zip.read(name), u'utf-8')
outfile = open(fullname, u'w') out_file = open(fullname, u'w')
outfile.write(filexml.encode(u'utf-8')) out_file.write(file_xml.encode(u'utf-8'))
else: else:
outfile = open(fullname, u'wb') out_file = open(fullname, u'wb')
outfile.write(zip.read(name)) out_file.write(zip.read(name))
outfile.close() out_file.close()
except (IOError, zipfile.BadZipfile): except (IOError, zipfile.BadZipfile):
log.exception(u'Importing theme from zip failed %s' % filename) log.exception(u'Importing theme from zip failed %s' % file_name)
raise Exception(u'validation') raise Exception(u'validation')
except Exception as info: except Exception as info:
if unicode(info) == u'validation': if unicode(info) == u'validation':
@ -594,13 +591,13 @@ class ThemeManager(QtGui.QWidget):
# Close the files, to be able to continue creating the theme. # Close the files, to be able to continue creating the theme.
if zip: if zip:
zip.close() zip.close()
if outfile: if out_file:
outfile.close() out_file.close()
if not abortimport: if not abort_import:
# As all files are closed, we can create the Theme. # As all files are closed, we can create the Theme.
if filexml: if file_xml:
theme = self._createThemeFromXml(filexml, self.path) theme = self._createThemeFromXml(file_xml, self.path)
self.generateAndSaveImage(dir, themename, theme) self.generateAndSaveImage(dir, theme_name, theme)
# Only show the error message, when IOError was not raised (in this # Only show the error message, when IOError was not raised (in this
# case the error message has already been shown). # case the error message has already been shown).
elif zip is not None: elif zip is not None:
@ -609,26 +606,26 @@ class ThemeManager(QtGui.QWidget):
translate('OpenLP.ThemeManager', translate('OpenLP.ThemeManager',
'File is not a valid theme.')) 'File is not a valid theme.'))
log.exception(u'Theme file does not contain XML data %s' % log.exception(u'Theme file does not contain XML data %s' %
filename) file_name)
def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, outfile): def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, out_file):
""" """
Unzip openlp.org 1.2x theme file and upgrade the theme xml. When calling Unzip openlp.org 1.2x theme file and upgrade the theme xml. When calling
this method, please keep in mind, that some parameters are redundant. this method, please keep in mind, that some parameters are redundant.
""" """
themename = xml_tree.find(u'Name').text.strip() theme_name = xml_tree.find(u'Name').text.strip()
themename = self.bad_v1_name_chars.sub(u'', themename) theme_name = self.bad_v1_name_chars.sub(u'', theme_name)
themefolder = os.path.join(dir, themename) theme_folder = os.path.join(dir, theme_name)
themeexists = os.path.exists(themefolder) theme_exists = os.path.exists(theme_folder)
if themeexists and not self.overWriteMessageBox(themename): if theme_exists and not self.overWriteMessageBox(theme_name):
return ( '', '', '', True) return '', '', '', True
themedir = os.path.join(dir, themename) themedir = os.path.join(dir, theme_name)
check_directory_exists(themedir) check_directory_exists(themedir)
filexml = unicode(zip.read(xmlfile), u'utf-8') file_xml = unicode(zip.read(xmlfile), u'utf-8')
filexml = self._migrateVersion122(filexml) file_xml = self._migrateVersion122(file_xml)
outfile = open(os.path.join(themedir, themename + u'.xml'), u'w') out_file = open(os.path.join(themedir, theme_name + u'.xml'), u'w')
outfile.write(filexml.encode(u'utf-8')) out_file.write(file_xml.encode(u'utf-8'))
outfile.close() out_file.close()
if background.text.strip() == u'2': if background.text.strip() == u'2':
imagename = xml_tree.find(u'BackgroundParameter1').text.strip() imagename = xml_tree.find(u'BackgroundParameter1').text.strip()
# image file has same extension and is in subfolder # image file has same extension and is in subfolder
@ -636,23 +633,23 @@ class ThemeManager(QtGui.QWidget):
== os.path.splitext(imagename)[1].lower() and name.find(r'/'), == os.path.splitext(imagename)[1].lower() and name.find(r'/'),
zip.namelist()) zip.namelist())
if len(imagefile) >= 1: if len(imagefile) >= 1:
outfile = open(os.path.join(themedir, imagename), u'wb') out_file = open(os.path.join(themedir, imagename), u'wb')
outfile.write(zip.read(imagefile[0])) out_file.write(zip.read(imagefile[0]))
outfile.close() out_file.close()
else: else:
log.exception(u'Theme file does not contain image file "%s"' % log.exception(u'Theme file does not contain image file "%s"' %
imagename.decode(u'utf-8', u'replace')) imagename.decode(u'utf-8', u'replace'))
raise Exception(u'validation') raise Exception(u'validation')
return (themename, filexml, outfile, False) return theme_name, file_xml, out_file, False
def checkIfThemeExists(self, themeName): def checkIfThemeExists(self, theme_name):
""" """
Check if theme already exists and displays error message Check if theme already exists and displays error message
``themeName`` ``theme_name``
Name of the Theme to test Name of the Theme to test
""" """
theme_dir = os.path.join(self.path, themeName) theme_dir = os.path.join(self.path, theme_name)
if os.path.exists(theme_dir): if os.path.exists(theme_dir):
critical_error_message_box( critical_error_message_box(
translate('OpenLP.ThemeManager', 'Validation Error'), translate('OpenLP.ThemeManager', 'Validation Error'),
@ -688,15 +685,15 @@ class ThemeManager(QtGui.QWidget):
if self.oldBackgroundImage and \ if self.oldBackgroundImage and \
imageTo != self.oldBackgroundImage: imageTo != self.oldBackgroundImage:
delete_file(self.oldBackgroundImage) delete_file(self.oldBackgroundImage)
outfile = None out_file = None
try: try:
outfile = open(theme_file, u'w') out_file = open(theme_file, u'w')
outfile.write(theme_pretty_xml) out_file.write(theme_pretty_xml)
except IOError: except IOError:
log.exception(u'Saving theme to file failed') log.exception(u'Saving theme to file failed')
finally: finally:
if outfile: if out_file:
outfile.close() out_file.close()
if imageFrom and imageFrom != imageTo: if imageFrom and imageFrom != imageTo:
try: try:
encoding = get_filesystem_encoding() encoding = get_filesystem_encoding()