Merge lp:~phill-ridout/openlp/overwrite into lp:openlp

Proposed by Phill
Status: Superseded
Proposed branch: lp:~phill-ridout/openlp/overwrite
Merge into: lp:openlp
Diff against target: 354 lines (+109/-86)
1 file modified
openlp/core/ui/thememanager.py (+109/-86)
To merge this branch: bzr merge lp:~phill-ridout/openlp/overwrite
Reviewer Review Type Date Requested Status
Raoul Snyman Needs Fixing
Review via email: mp+96844@code.launchpad.net

This proposal has been superseded by a proposal from 2012-03-10.

Description of the change

Adds a dialouge asking if the user wants to replace an existing theme when importing.
It works, but I dont know, it just feels like my code is a little bit "rough 'n' ready" let me know what you think!

To post a comment you must log in.
Revision history for this message
Raoul Snyman (raoul-snyman) wrote :

 if ret == QtGui.QMessageBox.Yes:
     return True
 elif ret == QtGui.QMessageBox.No:
     return False

Can be replaced with:

 return ret == QtGui.QMessageBox.Yes

What changed between lines 29 and 30? Please make sure you're using UNIX/Linux line endings.

There's actually no need for brackets around the returned params on line 39.

Please use words_separated_by_underscores, as per PEP8. Wordsthatarechainedtogetherarehardtoreadandunderstand.

You also don't need brackets around all the things you are returning.

review: Needs Fixing
lp:~phill-ridout/openlp/overwrite updated
1894. By Phill

Fixed up to Raouls guidelines

1895. By Phill

renaming variables, if your going to open a can of worms, might as well eat them all!

1896. By Phill

More minor fixes

1897. By Phill

Few more minor fixes

1898. By Phill

fixed mainWindow this time!

1899. By Phill

Fixed mainwindow bug

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/core/ui/thememanager.py'
2--- openlp/core/ui/thememanager.py 2012-03-05 22:22:36 +0000
3+++ openlp/core/ui/thememanager.py 2012-03-10 08:27:21 +0000
4@@ -178,10 +178,10 @@
5 """
6 if item is None:
7 return
8- realThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
9- themeName = unicode(item.text())
10+ realtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
11+ theme_name = unicode(item.text())
12 # If default theme restrict actions
13- if realThemeName == themeName:
14+ if realtheme_name == theme_name:
15 self.deleteToolbarAction.setVisible(True)
16 else:
17 self.deleteToolbarAction.setVisible(False)
18@@ -194,24 +194,24 @@
19 item = self.themeListWidget.itemAt(point)
20 if item is None:
21 return
22- realThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
23- themeName = unicode(item.text())
24+ realtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
25+ theme_name = unicode(item.text())
26 self.deleteAction.setVisible(False)
27 self.renameAction.setVisible(False)
28 self.globalAction.setVisible(False)
29 # If default theme restrict actions
30- if realThemeName == themeName:
31+ if realtheme_name == theme_name:
32 self.deleteAction.setVisible(True)
33 self.renameAction.setVisible(True)
34 self.globalAction.setVisible(True)
35 self.menu.exec_(self.themeListWidget.mapToGlobal(point))
36
37- def changeGlobalFromTab(self, themeName):
38+ def changeGlobalFromTab(self, theme_name):
39 """
40 Change the global theme when it is changed through the Themes settings
41 tab
42 """
43- log.debug(u'changeGlobalFromTab %s', themeName)
44+ log.debug(u'changeGlobalFromTab %s', theme_name)
45 for count in range (0, self.themeListWidget.count()):
46 # reset the old name
47 item = self.themeListWidget.item(count)
48@@ -220,7 +220,7 @@
49 if oldName != newName:
50 self.themeListWidget.item(count).setText(newName)
51 # Set the new name
52- if themeName == newName:
53+ if theme_name == newName:
54 name = unicode(translate('OpenLP.ThemeManager',
55 '%s (default)')) % newName
56 self.themeListWidget.item(count).setText(name)
57@@ -272,19 +272,19 @@
58 unicode(translate('OpenLP.ThemeManager', 'Rename %s theme?')),
59 False, False):
60 item = self.themeListWidget.currentItem()
61- oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
62- self.fileRenameForm.fileNameEdit.setText(oldThemeName)
63+ oldtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
64+ self.fileRenameForm.fileNameEdit.setText(oldtheme_name)
65 if self.fileRenameForm.exec_():
66- newThemeName = unicode(self.fileRenameForm.fileNameEdit.text())
67- if oldThemeName == newThemeName:
68+ newtheme_name = unicode(self.fileRenameForm.fileNameEdit.text())
69+ if oldtheme_name == newtheme_name:
70 return
71- if self.checkIfThemeExists(newThemeName):
72- oldThemeData = self.getThemeData(oldThemeName)
73- self.cloneThemeData(oldThemeData, newThemeName)
74- self.deleteTheme(oldThemeName)
75+ if self.checkIfThemeExists(newtheme_name):
76+ oldThemeData = self.getThemeData(oldtheme_name)
77+ self.cloneThemeData(oldThemeData, newtheme_name)
78+ self.deleteTheme(oldtheme_name)
79 for plugin in self.mainwindow.pluginManager.plugins:
80- if plugin.usesTheme(oldThemeName):
81- plugin.renameTheme(oldThemeName, newThemeName)
82+ if plugin.usesTheme(oldtheme_name):
83+ plugin.renameTheme(oldtheme_name, newtheme_name)
84 self.loadThemes()
85
86 def onCopyTheme(self):
87@@ -292,17 +292,17 @@
88 Copies an existing theme to a new name
89 """
90 item = self.themeListWidget.currentItem()
91- oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
92+ oldtheme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
93 self.fileRenameForm.fileNameEdit.setText(
94 unicode(translate('OpenLP.ThemeManager',
95- 'Copy of %s','Copy of <theme name>')) % oldThemeName)
96+ 'Copy of %s','Copy of <theme name>')) % oldtheme_name)
97 if self.fileRenameForm.exec_(True):
98- newThemeName = unicode(self.fileRenameForm.fileNameEdit.text())
99- if self.checkIfThemeExists(newThemeName):
100- themeData = self.getThemeData(oldThemeName)
101- self.cloneThemeData(themeData, newThemeName)
102+ newtheme_name = unicode(self.fileRenameForm.fileNameEdit.text())
103+ if self.checkIfThemeExists(newtheme_name):
104+ themeData = self.getThemeData(oldtheme_name)
105+ self.cloneThemeData(themeData, newtheme_name)
106
107- def cloneThemeData(self, themeData, newThemeName):
108+ def cloneThemeData(self, themeData, newtheme_name):
109 """
110 Takes a theme and makes a new copy of it as well as saving it.
111 """
112@@ -310,10 +310,10 @@
113 saveTo = None
114 saveFrom = None
115 if themeData.background_type == u'image':
116- saveTo = os.path.join(self.path, newThemeName,
117+ saveTo = os.path.join(self.path, newtheme_name,
118 os.path.split(unicode(themeData.background_filename))[1])
119 saveFrom = themeData.background_filename
120- themeData.theme_name = newThemeName
121+ themeData.theme_name = newtheme_name
122 themeData.extend_image_filename(self.path)
123 self.saveTheme(themeData, saveFrom, saveTo)
124
125@@ -458,7 +458,7 @@
126 files = SettingsManager.get_files(self.settingsSection, u'.png')
127 # Sort the themes by its name considering language specific characters.
128 # lower() is needed for windows!
129- files.sort(key=lambda filename: unicode(filename).lower(),
130+ files.sort(key=lambda file_name: unicode(file_name).lower(),
131 cmp=locale.strcoll)
132 # now process the file list of png files
133 for name in files:
134@@ -495,36 +495,47 @@
135 """
136 return self.themelist
137
138- def getThemeData(self, themeName):
139+ def getThemeData(self, theme_name):
140 """
141 Returns a theme object from an XML file
142
143- ``themeName``
144+ ``theme_name``
145 Name of the theme to load from file
146 """
147- log.debug(u'getthemedata for theme %s', themeName)
148- xmlFile = os.path.join(self.path, unicode(themeName),
149- unicode(themeName) + u'.xml')
150+ log.debug(u'getthemedata for theme %s', theme_name)
151+ xmlFile = os.path.join(self.path, unicode(theme_name),
152+ unicode(theme_name) + u'.xml')
153 xml = get_text_file_string(xmlFile)
154 if not xml:
155 log.debug("No theme data - using default theme")
156 return ThemeXML()
157 else:
158 return self._createThemeFromXml(xml, self.path)
159+
160+ def overWriteMessageBox(self, theme_name):
161+ ret = QtGui.QMessageBox.question(self,
162+ translate('OpenLP.ThemeManager', 'Theme Already Exists!'),
163+ translate('OpenLP.ThemeManager',
164+ 'The theme %s already exists. Do you want to replace it?'
165+ % theme_name),
166+ QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
167+ QtGui.QMessageBox.No),
168+ QtGui.QMessageBox.No)
169+ return ret == QtGui.QMessageBox.Yes
170
171- def unzipTheme(self, filename, dir):
172+ def unzipTheme(self, file_name, dir):
173 """
174 Unzip the theme, remove the preview file if stored
175 Generate a new preview file. Check the XML theme version and upgrade if
176 necessary.
177 """
178- log.debug(u'Unzipping theme %s', filename)
179- filename = unicode(filename)
180+ log.debug(u'Unzipping theme %s', file_name)
181+ file_name = unicode(file_name)
182 zip = None
183- outfile = None
184- filexml = None
185+ out_file = None
186+ file_xml = None
187 try:
188- zip = zipfile.ZipFile(filename)
189+ zip = zipfile.ZipFile(file_name)
190 xmlfile = filter(lambda name:
191 os.path.splitext(name)[1].lower() == u'.xml', zip.namelist())
192 if len(xmlfile) != 1:
193@@ -533,10 +544,17 @@
194 xml_tree = ElementTree(element=XML(zip.read(xmlfile[0]))).getroot()
195 v1_background = xml_tree.find(u'BackgroundType')
196 if v1_background is not None:
197- (themename, filexml, outfile) = self.unzipVersion122(dir, zip,
198- xmlfile[0], xml_tree, v1_background, outfile)
199+ theme_name, file_xml, out_file, abort_import = self.unzipVersion122(dir, zip,
200+ xmlfile[0], xml_tree, v1_background, out_file)
201 else:
202- themename = xml_tree.find(u'name').text.strip()
203+ theme_name = xml_tree.find(u'name').text.strip()
204+ theme_folder = os.path.join(dir, theme_name)
205+ theme_exists = os.path.exists(theme_folder)
206+ if theme_exists and not self.overWriteMessageBox(theme_name):
207+ abort_import = True
208+ return
209+ else:
210+ abort_import = False
211 for name in zip.namelist():
212 try:
213 uname = unicode(name, u'utf-8')
214@@ -552,15 +570,15 @@
215 fullname = os.path.join(dir, uname)
216 check_directory_exists(os.path.dirname(fullname))
217 if os.path.splitext(uname)[1].lower() == u'.xml':
218- filexml = unicode(zip.read(name), u'utf-8')
219- outfile = open(fullname, u'w')
220- outfile.write(filexml.encode(u'utf-8'))
221+ file_xml = unicode(zip.read(name), u'utf-8')
222+ out_file = open(fullname, u'w')
223+ out_file.write(file_xml.encode(u'utf-8'))
224 else:
225- outfile = open(fullname, u'wb')
226- outfile.write(zip.read(name))
227- outfile.close()
228+ out_file = open(fullname, u'wb')
229+ out_file.write(zip.read(name))
230+ out_file.close()
231 except (IOError, zipfile.BadZipfile):
232- log.exception(u'Importing theme from zip failed %s' % filename)
233+ log.exception(u'Importing theme from zip failed %s' % file_name)
234 raise Exception(u'validation')
235 except Exception as info:
236 if unicode(info) == u'validation':
237@@ -573,36 +591,41 @@
238 # Close the files, to be able to continue creating the theme.
239 if zip:
240 zip.close()
241- if outfile:
242- outfile.close()
243- # As all files are closed, we can create the Theme.
244- if filexml:
245- theme = self._createThemeFromXml(filexml, self.path)
246- self.generateAndSaveImage(dir, themename, theme)
247- # Only show the error message, when IOError was not raised (in this
248- # case the error message has already been shown).
249- elif zip is not None:
250- critical_error_message_box(
251- translate('OpenLP.ThemeManager', 'Validation Error'),
252- translate('OpenLP.ThemeManager',
253- 'File is not a valid theme.'))
254- log.exception(u'Theme file does not contain XML data %s' %
255- filename)
256+ if out_file:
257+ out_file.close()
258+ if not abort_import:
259+ # As all files are closed, we can create the Theme.
260+ if file_xml:
261+ theme = self._createThemeFromXml(file_xml, self.path)
262+ self.generateAndSaveImage(dir, theme_name, theme)
263+ # Only show the error message, when IOError was not raised (in this
264+ # case the error message has already been shown).
265+ elif zip is not None:
266+ critical_error_message_box(
267+ translate('OpenLP.ThemeManager', 'Validation Error'),
268+ translate('OpenLP.ThemeManager',
269+ 'File is not a valid theme.'))
270+ log.exception(u'Theme file does not contain XML data %s' %
271+ file_name)
272
273- def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, outfile):
274+ def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, out_file):
275 """
276 Unzip openlp.org 1.2x theme file and upgrade the theme xml. When calling
277 this method, please keep in mind, that some parameters are redundant.
278 """
279- themename = xml_tree.find(u'Name').text.strip()
280- themename = self.bad_v1_name_chars.sub(u'', themename)
281- themedir = os.path.join(dir, themename)
282+ theme_name = xml_tree.find(u'Name').text.strip()
283+ theme_name = self.bad_v1_name_chars.sub(u'', theme_name)
284+ theme_folder = os.path.join(dir, theme_name)
285+ theme_exists = os.path.exists(theme_folder)
286+ if theme_exists and not self.overWriteMessageBox(theme_name):
287+ return '', '', '', True
288+ themedir = os.path.join(dir, theme_name)
289 check_directory_exists(themedir)
290- filexml = unicode(zip.read(xmlfile), u'utf-8')
291- filexml = self._migrateVersion122(filexml)
292- outfile = open(os.path.join(themedir, themename + u'.xml'), u'w')
293- outfile.write(filexml.encode(u'utf-8'))
294- outfile.close()
295+ file_xml = unicode(zip.read(xmlfile), u'utf-8')
296+ file_xml = self._migrateVersion122(file_xml)
297+ out_file = open(os.path.join(themedir, theme_name + u'.xml'), u'w')
298+ out_file.write(file_xml.encode(u'utf-8'))
299+ out_file.close()
300 if background.text.strip() == u'2':
301 imagename = xml_tree.find(u'BackgroundParameter1').text.strip()
302 # image file has same extension and is in subfolder
303@@ -610,23 +633,23 @@
304 == os.path.splitext(imagename)[1].lower() and name.find(r'/'),
305 zip.namelist())
306 if len(imagefile) >= 1:
307- outfile = open(os.path.join(themedir, imagename), u'wb')
308- outfile.write(zip.read(imagefile[0]))
309- outfile.close()
310+ out_file = open(os.path.join(themedir, imagename), u'wb')
311+ out_file.write(zip.read(imagefile[0]))
312+ out_file.close()
313 else:
314 log.exception(u'Theme file does not contain image file "%s"' %
315 imagename.decode(u'utf-8', u'replace'))
316 raise Exception(u'validation')
317- return (themename, filexml, outfile)
318+ return theme_name, file_xml, out_file, False
319
320- def checkIfThemeExists(self, themeName):
321+ def checkIfThemeExists(self, theme_name):
322 """
323 Check if theme already exists and displays error message
324
325- ``themeName``
326+ ``theme_name``
327 Name of the Theme to test
328 """
329- theme_dir = os.path.join(self.path, themeName)
330+ theme_dir = os.path.join(self.path, theme_name)
331 if os.path.exists(theme_dir):
332 critical_error_message_box(
333 translate('OpenLP.ThemeManager', 'Validation Error'),
334@@ -662,15 +685,15 @@
335 if self.oldBackgroundImage and \
336 imageTo != self.oldBackgroundImage:
337 delete_file(self.oldBackgroundImage)
338- outfile = None
339+ out_file = None
340 try:
341- outfile = open(theme_file, u'w')
342- outfile.write(theme_pretty_xml)
343+ out_file = open(theme_file, u'w')
344+ out_file.write(theme_pretty_xml)
345 except IOError:
346 log.exception(u'Saving theme to file failed')
347 finally:
348- if outfile:
349- outfile.close()
350+ if out_file:
351+ out_file.close()
352 if imageFrom and imageFrom != imageTo:
353 try:
354 encoding = get_filesystem_encoding()