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: 719 lines (+213/-191)
1 file modified
openlp/core/ui/thememanager.py (+213/-191)
To merge this branch: bzr merge lp:~phill-ridout/openlp/overwrite
Reviewer Review Type Date Requested Status
Jonathan Corwin (community) Needs Fixing
Tim Bentley Needs Fixing
Raoul Snyman Pending
Review via email: mp+96872@code.launchpad.net

This proposal supersedes a proposal from 2012-03-09.

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 : Posted in a previous version of this proposal

 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
Revision history for this message
Tim Bentley (trb143) wrote :

unzipVersion122 returns abort_import always a false.

It would be better to not change that API.
Default abort_import to false and only set it to true if necessary.

review: Needs Fixing
Revision history for this message
Jonathan Corwin (j-corwin) wrote :

real_theme_name rather than realtheme_name
old_theme_name rather than oldtheme_name

161: Remove the exclamation!, it's not that exciting that it already exists ;-)
164: Remove the leading "The", so it starts: Theme %s ...
287: You've got two spaces after each comma instead of one.

review: Needs Fixing
lp:~phill-ridout/openlp/overwrite updated
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 16:17:20 +0000
4@@ -52,9 +52,9 @@
5 """
6 Manages the orders of Theme.
7 """
8- def __init__(self, mainwindow, parent=None):
9+ def __init__(self, mainWindow, parent=None):
10 QtGui.QWidget.__init__(self, parent)
11- self.mainwindow = mainwindow
12+ self.mainWindow = mainWindow
13 self.settingsSection = u'themes'
14 self.themeForm = ThemeForm(self)
15 self.fileRenameForm = FileRenameForm(self)
16@@ -140,13 +140,13 @@
17 QtCore.QObject.connect(Receiver.get_receiver(),
18 QtCore.SIGNAL(u'config_updated'), self.configUpdated)
19 # Variables
20- self.themelist = []
21+ self.theme_list = []
22 self.path = AppLocation.get_section_data_path(self.settingsSection)
23 check_directory_exists(self.path)
24- self.thumbPath = os.path.join(self.path, u'thumbnails')
25- check_directory_exists(self.thumbPath)
26+ self.thumb_path = os.path.join(self.path, u'thumbnails')
27+ check_directory_exists(self.thumb_path)
28 self.themeForm.path = self.path
29- self.oldBackgroundImage = None
30+ self.old_background_image = None
31 self.bad_v1_name_chars = re.compile(r'[%+\[\]]')
32 # Last little bits of setting up
33 self.configUpdated()
34@@ -178,10 +178,10 @@
35 """
36 if item is None:
37 return
38- realThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
39- themeName = unicode(item.text())
40+ real_theme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
41+ theme_name = unicode(item.text())
42 # If default theme restrict actions
43- if realThemeName == themeName:
44+ if real_theme_name == theme_name:
45 self.deleteToolbarAction.setVisible(True)
46 else:
47 self.deleteToolbarAction.setVisible(False)
48@@ -194,35 +194,35 @@
49 item = self.themeListWidget.itemAt(point)
50 if item is None:
51 return
52- realThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
53- themeName = unicode(item.text())
54+ real_theme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
55+ theme_name = unicode(item.text())
56 self.deleteAction.setVisible(False)
57 self.renameAction.setVisible(False)
58 self.globalAction.setVisible(False)
59 # If default theme restrict actions
60- if realThemeName == themeName:
61+ if real_theme_name == theme_name:
62 self.deleteAction.setVisible(True)
63 self.renameAction.setVisible(True)
64 self.globalAction.setVisible(True)
65 self.menu.exec_(self.themeListWidget.mapToGlobal(point))
66
67- def changeGlobalFromTab(self, themeName):
68+ def changeGlobalFromTab(self, theme_name):
69 """
70 Change the global theme when it is changed through the Themes settings
71 tab
72 """
73- log.debug(u'changeGlobalFromTab %s', themeName)
74+ log.debug(u'changeGlobalFromTab %s', theme_name)
75 for count in range (0, self.themeListWidget.count()):
76 # reset the old name
77 item = self.themeListWidget.item(count)
78- oldName = item.text()
79- newName = unicode(item.data(QtCore.Qt.UserRole).toString())
80- if oldName != newName:
81- self.themeListWidget.item(count).setText(newName)
82+ old_name = item.text()
83+ new_name = unicode(item.data(QtCore.Qt.UserRole).toString())
84+ if old_name != new_name:
85+ self.themeListWidget.item(count).setText(new_name)
86 # Set the new name
87- if themeName == newName:
88+ if theme_name == new_name:
89 name = unicode(translate('OpenLP.ThemeManager',
90- '%s (default)')) % newName
91+ '%s (default)')) % new_name
92 self.themeListWidget.item(count).setText(name)
93
94 def changeGlobalFromScreen(self, index=-1):
95@@ -234,9 +234,9 @@
96 selected_row = self.themeListWidget.currentRow()
97 for count in range (0, self.themeListWidget.count()):
98 item = self.themeListWidget.item(count)
99- oldName = item.text()
100+ old_name = item.text()
101 # reset the old name
102- if oldName != unicode(item.data(QtCore.Qt.UserRole).toString()):
103+ if old_name != unicode(item.data(QtCore.Qt.UserRole).toString()):
104 self.themeListWidget.item(count).setText(
105 unicode(item.data(QtCore.Qt.UserRole).toString()))
106 # Set the new name
107@@ -272,19 +272,19 @@
108 unicode(translate('OpenLP.ThemeManager', 'Rename %s theme?')),
109 False, False):
110 item = self.themeListWidget.currentItem()
111- oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
112- self.fileRenameForm.fileNameEdit.setText(oldThemeName)
113+ old_theme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
114+ self.fileRenameForm.fileNameEdit.setText(old_theme_name)
115 if self.fileRenameForm.exec_():
116- newThemeName = unicode(self.fileRenameForm.fileNameEdit.text())
117- if oldThemeName == newThemeName:
118+ new_theme_name = unicode(self.fileRenameForm.fileNameEdit.text())
119+ if old_theme_name == new_theme_name:
120 return
121- if self.checkIfThemeExists(newThemeName):
122- oldThemeData = self.getThemeData(oldThemeName)
123- self.cloneThemeData(oldThemeData, newThemeName)
124- self.deleteTheme(oldThemeName)
125- for plugin in self.mainwindow.pluginManager.plugins:
126- if plugin.usesTheme(oldThemeName):
127- plugin.renameTheme(oldThemeName, newThemeName)
128+ if self.checkIfThemeExists(new_theme_name):
129+ old_theme_data = self.getThemeData(old_theme_name)
130+ self.cloneThemeData(old_theme_data, new_theme_name)
131+ self.deleteTheme(old_theme_name)
132+ for plugin in self.mainWindow.pluginManager.plugins:
133+ if plugin.usesTheme(old_theme_name):
134+ plugin.renameTheme(old_theme_name, new_theme_name)
135 self.loadThemes()
136
137 def onCopyTheme(self):
138@@ -292,30 +292,30 @@
139 Copies an existing theme to a new name
140 """
141 item = self.themeListWidget.currentItem()
142- oldThemeName = unicode(item.data(QtCore.Qt.UserRole).toString())
143+ old_theme_name = unicode(item.data(QtCore.Qt.UserRole).toString())
144 self.fileRenameForm.fileNameEdit.setText(
145 unicode(translate('OpenLP.ThemeManager',
146- 'Copy of %s','Copy of <theme name>')) % oldThemeName)
147+ 'Copy of %s','Copy of <theme name>')) % old_theme_name)
148 if self.fileRenameForm.exec_(True):
149- newThemeName = unicode(self.fileRenameForm.fileNameEdit.text())
150- if self.checkIfThemeExists(newThemeName):
151- themeData = self.getThemeData(oldThemeName)
152- self.cloneThemeData(themeData, newThemeName)
153+ new_theme_name = unicode(self.fileRenameForm.fileNameEdit.text())
154+ if self.checkIfThemeExists(new_theme_name):
155+ theme_data = self.getThemeData(old_theme_name)
156+ self.cloneThemeData(theme_data, new_theme_name)
157
158- def cloneThemeData(self, themeData, newThemeName):
159+ def cloneThemeData(self, theme_data, new_theme_name):
160 """
161 Takes a theme and makes a new copy of it as well as saving it.
162 """
163 log.debug(u'cloneThemeData')
164- saveTo = None
165- saveFrom = None
166- if themeData.background_type == u'image':
167- saveTo = os.path.join(self.path, newThemeName,
168- os.path.split(unicode(themeData.background_filename))[1])
169- saveFrom = themeData.background_filename
170- themeData.theme_name = newThemeName
171- themeData.extend_image_filename(self.path)
172- self.saveTheme(themeData, saveFrom, saveTo)
173+ save_to = None
174+ save_from = None
175+ if theme_data.background_type == u'image':
176+ save_to = os.path.join(self.path, new_theme_name,
177+ os.path.split(unicode(theme_data.background_filename))[1])
178+ save_from = theme_data.background_filename
179+ theme_data.theme_name = new_theme_name
180+ theme_data.extend_image_filename(self.path)
181+ self.saveTheme(theme_data, save_from, save_to)
182
183 def onEditTheme(self):
184 """
185@@ -329,10 +329,10 @@
186 theme = self.getThemeData(
187 unicode(item.data(QtCore.Qt.UserRole).toString()))
188 if theme.background_type == u'image':
189- self.oldBackgroundImage = theme.background_filename
190+ self.old_background_image = theme.background_filename
191 self.themeForm.theme = theme
192 self.themeForm.exec_(True)
193- self.oldBackgroundImage = None
194+ self.old_background_image = None
195
196 def onDeleteTheme(self):
197 """
198@@ -358,10 +358,10 @@
199 ``theme``
200 The theme to delete.
201 """
202- self.themelist.remove(theme)
203+ self.theme_list.remove(theme)
204 thumb = u'%s.png' % theme
205 delete_file(os.path.join(self.path, thumb))
206- delete_file(os.path.join(self.thumbPath, thumb))
207+ delete_file(os.path.join(self.thumb_path, thumb))
208 try:
209 encoding = get_filesystem_encoding()
210 shutil.rmtree(os.path.join(self.path, theme).encode(encoding))
211@@ -386,10 +386,10 @@
212 Receiver.send_message(u'cursor_busy')
213 if path:
214 SettingsManager.set_last_dir(self.settingsSection, path, 1)
215- themePath = os.path.join(path, theme + u'.otz')
216+ theme_path = os.path.join(path, theme + u'.otz')
217 zip = None
218 try:
219- zip = zipfile.ZipFile(themePath, u'w')
220+ zip = zipfile.ZipFile(theme_path, u'w')
221 source = os.path.join(self.path, theme)
222 for files in os.walk(source):
223 for name in files[2]:
224@@ -439,9 +439,8 @@
225 The plugins will call back in to get the real list if they want it.
226 """
227 log.debug(u'Load themes from dir')
228- self.themelist = []
229+ self.theme_list = []
230 self.themeListWidget.clear()
231- dirList = os.listdir(self.path)
232 files = SettingsManager.get_files(self.settingsSection, u'.png')
233 if firstTime:
234 self.firstTime()
235@@ -458,29 +457,29 @@
236 files = SettingsManager.get_files(self.settingsSection, u'.png')
237 # Sort the themes by its name considering language specific characters.
238 # lower() is needed for windows!
239- files.sort(key=lambda filename: unicode(filename).lower(),
240+ files.sort(key=lambda file_name: unicode(file_name).lower(),
241 cmp=locale.strcoll)
242 # now process the file list of png files
243 for name in files:
244 # check to see file is in theme root directory
245 theme = os.path.join(self.path, name)
246 if os.path.exists(theme):
247- textName = os.path.splitext(name)[0]
248- if textName == self.global_theme:
249+ text_name = os.path.splitext(name)[0]
250+ if text_name == self.global_theme:
251 name = unicode(translate('OpenLP.ThemeManager',
252- '%s (default)')) % textName
253+ '%s (default)')) % text_name
254 else:
255- name = textName
256- thumb = os.path.join(self.thumbPath, u'%s.png' % textName)
257+ name = text_name
258+ thumb = os.path.join(self.thumb_path, u'%s.png' % text_name)
259 item_name = QtGui.QListWidgetItem(name)
260 if validate_thumb(theme, thumb):
261 icon = build_icon(thumb)
262 else:
263 icon = create_thumb(theme, thumb)
264 item_name.setIcon(icon)
265- item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(textName))
266+ item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(text_name))
267 self.themeListWidget.addItem(item_name)
268- self.themelist.append(textName)
269+ self.theme_list.append(text_name)
270 self._pushThemes()
271
272 def _pushThemes(self):
273@@ -493,50 +492,68 @@
274 """
275 Return the list of loaded themes
276 """
277- return self.themelist
278+ return self.theme_list
279
280- def getThemeData(self, themeName):
281+ def getThemeData(self, theme_name):
282 """
283 Returns a theme object from an XML file
284
285- ``themeName``
286+ ``theme_name``
287 Name of the theme to load from file
288 """
289- log.debug(u'getthemedata for theme %s', themeName)
290- xmlFile = os.path.join(self.path, unicode(themeName),
291- unicode(themeName) + u'.xml')
292- xml = get_text_file_string(xmlFile)
293+ log.debug(u'getthemedata for theme %s', theme_name)
294+ xml_file = os.path.join(self.path, unicode(theme_name),
295+ unicode(theme_name) + u'.xml')
296+ xml = get_text_file_string(xml_file)
297 if not xml:
298 log.debug("No theme data - using default theme")
299 return ThemeXML()
300 else:
301 return self._createThemeFromXml(xml, self.path)
302+
303+ def overWriteMessageBox(self, theme_name):
304+ ret = QtGui.QMessageBox.question(self,
305+ translate('OpenLP.ThemeManager', 'Theme Already Exists.'),
306+ translate('OpenLP.ThemeManager',
307+ 'Theme %s already exists. Do you want to replace it?'
308+ % theme_name),
309+ QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
310+ QtGui.QMessageBox.No),
311+ QtGui.QMessageBox.No)
312+ return ret == QtGui.QMessageBox.Yes
313
314- def unzipTheme(self, filename, dir):
315+ def unzipTheme(self, file_name, dir):
316 """
317 Unzip the theme, remove the preview file if stored
318 Generate a new preview file. Check the XML theme version and upgrade if
319 necessary.
320 """
321- log.debug(u'Unzipping theme %s', filename)
322- filename = unicode(filename)
323+ log.debug(u'Unzipping theme %s', file_name)
324+ file_name = unicode(file_name)
325 zip = None
326- outfile = None
327- filexml = None
328+ out_file = None
329+ file_xml = None
330 try:
331- zip = zipfile.ZipFile(filename)
332- xmlfile = filter(lambda name:
333+ zip = zipfile.ZipFile(file_name)
334+ xml_file = filter(lambda name:
335 os.path.splitext(name)[1].lower() == u'.xml', zip.namelist())
336- if len(xmlfile) != 1:
337- log.exception(u'Theme contains "%s" XML files' % len(xmlfile))
338+ if len(xml_file) != 1:
339+ log.exception(u'Theme contains "%s" XML files' % len(xml_file))
340 raise Exception(u'validation')
341- xml_tree = ElementTree(element=XML(zip.read(xmlfile[0]))).getroot()
342+ xml_tree = ElementTree(element=XML(zip.read(xml_file[0]))).getroot()
343 v1_background = xml_tree.find(u'BackgroundType')
344 if v1_background is not None:
345- (themename, filexml, outfile) = self.unzipVersion122(dir, zip,
346- xmlfile[0], xml_tree, v1_background, outfile)
347+ theme_name, file_xml, out_file, abort_import = self.unzipVersion122(dir, zip,
348+ xml_file[0], xml_tree, v1_background, out_file)
349 else:
350- themename = xml_tree.find(u'name').text.strip()
351+ theme_name = xml_tree.find(u'name').text.strip()
352+ theme_folder = os.path.join(dir, theme_name)
353+ theme_exists = os.path.exists(theme_folder)
354+ if theme_exists and not self.overWriteMessageBox(theme_name):
355+ abort_import = True
356+ return
357+ else:
358+ abort_import = False
359 for name in zip.namelist():
360 try:
361 uname = unicode(name, u'utf-8')
362@@ -545,22 +562,22 @@
363 u' "%s"' % name.decode(u'utf-8', u'replace'))
364 raise Exception(u'validation')
365 uname = unicode(QtCore.QDir.toNativeSeparators(uname))
366- splitname = uname.split(os.path.sep)
367- if splitname[-1] == u'' or len(splitname) == 1:
368+ spli_tname = uname.split(os.path.sep)
369+ if spli_tname[-1] == u'' or len(spli_tname) == 1:
370 # is directory or preview file
371 continue
372- fullname = os.path.join(dir, uname)
373- check_directory_exists(os.path.dirname(fullname))
374+ full_name = os.path.join(dir, uname)
375+ check_directory_exists(os.path.dirname(full_name))
376 if os.path.splitext(uname)[1].lower() == u'.xml':
377- filexml = unicode(zip.read(name), u'utf-8')
378- outfile = open(fullname, u'w')
379- outfile.write(filexml.encode(u'utf-8'))
380+ file_xml = unicode(zip.read(name), u'utf-8')
381+ out_file = open(full_name, u'w')
382+ out_file.write(file_xml.encode(u'utf-8'))
383 else:
384- outfile = open(fullname, u'wb')
385- outfile.write(zip.read(name))
386- outfile.close()
387+ out_file = open(full_name, u'wb')
388+ out_file.write(zip.read(name))
389+ out_file.close()
390 except (IOError, zipfile.BadZipfile):
391- log.exception(u'Importing theme from zip failed %s' % filename)
392+ log.exception(u'Importing theme from zip failed %s' % file_name)
393 raise Exception(u'validation')
394 except Exception as info:
395 if unicode(info) == u'validation':
396@@ -573,60 +590,65 @@
397 # Close the files, to be able to continue creating the theme.
398 if zip:
399 zip.close()
400- if outfile:
401- outfile.close()
402- # As all files are closed, we can create the Theme.
403- if filexml:
404- theme = self._createThemeFromXml(filexml, self.path)
405- self.generateAndSaveImage(dir, themename, theme)
406- # Only show the error message, when IOError was not raised (in this
407- # case the error message has already been shown).
408- elif zip is not None:
409- critical_error_message_box(
410- translate('OpenLP.ThemeManager', 'Validation Error'),
411- translate('OpenLP.ThemeManager',
412- 'File is not a valid theme.'))
413- log.exception(u'Theme file does not contain XML data %s' %
414- filename)
415+ if out_file:
416+ out_file.close()
417+ if not abort_import:
418+ # As all files are closed, we can create the Theme.
419+ if file_xml:
420+ theme = self._createThemeFromXml(file_xml, self.path)
421+ self.generateAndSaveImage(dir, theme_name, theme)
422+ # Only show the error message, when IOError was not raised (in this
423+ # case the error message has already been shown).
424+ elif zip is not None:
425+ critical_error_message_box(
426+ translate('OpenLP.ThemeManager', 'Validation Error'),
427+ translate('OpenLP.ThemeManager',
428+ 'File is not a valid theme.'))
429+ log.exception(u'Theme file does not contain XML data %s' %
430+ file_name)
431
432- def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, outfile):
433+ def unzipVersion122(self, dir, zip, xml_file, xml_tree, background, out_file):
434 """
435 Unzip openlp.org 1.2x theme file and upgrade the theme xml. When calling
436 this method, please keep in mind, that some parameters are redundant.
437 """
438- themename = xml_tree.find(u'Name').text.strip()
439- themename = self.bad_v1_name_chars.sub(u'', themename)
440- themedir = os.path.join(dir, themename)
441+ theme_name = xml_tree.find(u'Name').text.strip()
442+ theme_name = self.bad_v1_name_chars.sub(u'', theme_name)
443+ theme_folder = os.path.join(dir, theme_name)
444+ theme_exists = os.path.exists(theme_folder)
445+ if theme_exists and not self.overWriteMessageBox(theme_name):
446+ return '', '', '', True
447+ themedir = os.path.join(dir, theme_name)
448 check_directory_exists(themedir)
449- filexml = unicode(zip.read(xmlfile), u'utf-8')
450- filexml = self._migrateVersion122(filexml)
451- outfile = open(os.path.join(themedir, themename + u'.xml'), u'w')
452- outfile.write(filexml.encode(u'utf-8'))
453- outfile.close()
454+ file_xml = unicode(zip.read(xml_file), u'utf-8')
455+ file_xml = self._migrateVersion122(file_xml)
456+ out_file = open(os.path.join(themedir, theme_name + u'.xml'), u'w')
457+ out_file.write(file_xml.encode(u'utf-8'))
458+ out_file.close()
459 if background.text.strip() == u'2':
460- imagename = xml_tree.find(u'BackgroundParameter1').text.strip()
461+ image_name = xml_tree.find(u'BackgroundParameter1').text.strip()
462 # image file has same extension and is in subfolder
463 imagefile = filter(lambda name: os.path.splitext(name)[1].lower()
464- == os.path.splitext(imagename)[1].lower() and name.find(r'/'),
465+ == os.path.splitext(image_name)[1].lower() and name.find(r'/'),
466 zip.namelist())
467 if len(imagefile) >= 1:
468- outfile = open(os.path.join(themedir, imagename), u'wb')
469- outfile.write(zip.read(imagefile[0]))
470- outfile.close()
471+ out_file = open(os.path.join(themedir, image_name), u'wb')
472+ out_file.write(zip.read(imagefile[0]))
473+ out_file.close()
474 else:
475 log.exception(u'Theme file does not contain image file "%s"' %
476- imagename.decode(u'utf-8', u'replace'))
477+ image_name.decode(u'utf-8', u'replace'))
478 raise Exception(u'validation')
479- return (themename, filexml, outfile)
480+ return theme_name, file_xml, out_file, False
481
482- def checkIfThemeExists(self, themeName):
483+ def checkIfThemeExists(self, theme_name):
484 """
485 Check if theme already exists and displays error message
486
487- ``themeName``
488+ ``theme_name``
489 Name of the Theme to test
490 """
491- theme_dir = os.path.join(self.path, themeName)
492+ theme_dir = os.path.join(self.path, theme_name)
493 if os.path.exists(theme_dir):
494 critical_error_message_box(
495 translate('OpenLP.ThemeManager', 'Validation Error'),
496@@ -635,20 +657,20 @@
497 return False
498 return True
499
500- def saveTheme(self, theme, imageFrom, imageTo):
501+ def saveTheme(self, theme, image_from, image_to):
502 """
503 Called by thememaintenance Dialog to save the theme
504 and to trigger the reload of the theme list
505 """
506- self._writeTheme(theme, imageFrom, imageTo)
507+ self._writeTheme(theme, image_from, image_to)
508 if theme.background_type == \
509 BackgroundType.to_string(BackgroundType.Image):
510- self.mainwindow.imageManager.update_image(theme.theme_name,
511+ self.mainWindow.imageManager.update_image(theme.theme_name,
512 u'theme', QtGui.QColor(theme.background_border_color))
513- self.mainwindow.imageManager.process_updates()
514+ self.mainWindow.imageManager.process_updates()
515 self.loadThemes()
516
517- def _writeTheme(self, theme, imageFrom, imageTo):
518+ def _writeTheme(self, theme, image_from, image_to):
519 """
520 Writes the theme to the disk and handles the background image if
521 necessary
522@@ -659,24 +681,24 @@
523 theme_dir = os.path.join(self.path, name)
524 check_directory_exists(theme_dir)
525 theme_file = os.path.join(theme_dir, name + u'.xml')
526- if self.oldBackgroundImage and \
527- imageTo != self.oldBackgroundImage:
528- delete_file(self.oldBackgroundImage)
529- outfile = None
530+ if self.old_background_image and \
531+ image_to != self.old_background_image:
532+ delete_file(self.old_background_image)
533+ out_file = None
534 try:
535- outfile = open(theme_file, u'w')
536- outfile.write(theme_pretty_xml)
537+ out_file = open(theme_file, u'w')
538+ out_file.write(theme_pretty_xml)
539 except IOError:
540 log.exception(u'Saving theme to file failed')
541 finally:
542- if outfile:
543- outfile.close()
544- if imageFrom and imageFrom != imageTo:
545+ if out_file:
546+ out_file.close()
547+ if image_from and image_from != image_to:
548 try:
549 encoding = get_filesystem_encoding()
550 shutil.copyfile(
551- unicode(imageFrom).encode(encoding),
552- unicode(imageTo).encode(encoding))
553+ unicode(image_from).encode(encoding),
554+ unicode(image_to).encode(encoding))
555 except IOError:
556 log.exception(u'Failed to save theme image')
557 self.generateAndSaveImage(self.path, name, theme)
558@@ -684,39 +706,39 @@
559 def generateAndSaveImage(self, dir, name, theme):
560 log.debug(u'generateAndSaveImage %s %s', dir, name)
561 frame = self.generateImage(theme)
562- samplepathname = os.path.join(self.path, name + u'.png')
563- if os.path.exists(samplepathname):
564- os.unlink(samplepathname)
565- frame.save(samplepathname, u'png')
566- thumb = os.path.join(self.thumbPath, u'%s.png' % name)
567- create_thumb(samplepathname, thumb, False)
568- log.debug(u'Theme image written to %s', samplepathname)
569+ sample_path_name = os.path.join(self.path, name + u'.png')
570+ if os.path.exists(sample_path_name):
571+ os.unlink(sample_path_name)
572+ frame.save(sample_path_name, u'png')
573+ thumb = os.path.join(self.thumb_path, u'%s.png' % name)
574+ create_thumb(sample_path_name, thumb, False)
575+ log.debug(u'Theme image written to %s', sample_path_name)
576
577 def updatePreviewImages(self):
578 """
579 Called to update the themes' preview images.
580 """
581- self.mainwindow.displayProgressBar(len(self.themelist))
582- for theme in self.themelist:
583- self.mainwindow.incrementProgressBar()
584+ self.mainWindow.displayProgressBar(len(self.theme_list))
585+ for theme in self.theme_list:
586+ self.mainWindow.incrementProgressBar()
587 self.generateAndSaveImage(
588 self.path, theme, self.getThemeData(theme))
589- self.mainwindow.finishedProgressBar()
590+ self.mainWindow.finishedProgressBar()
591 self.loadThemes()
592
593- def generateImage(self, themeData, forcePage=False):
594+ def generateImage(self, theme_data, forcePage=False):
595 """
596 Call the renderer to build a Sample Image
597
598- ``themeData``
599+ ``theme_data``
600 The theme to generated a preview for.
601
602 ``forcePage``
603 Flag to tell message lines per page need to be generated.
604 """
605- log.debug(u'generateImage \n%s ', themeData)
606- return self.mainwindow.renderer.generate_preview(
607- themeData, forcePage)
608+ log.debug(u'generateImage \n%s ', theme_data)
609+ return self.mainWindow.renderer.generate_preview(
610+ theme_data, forcePage)
611
612 def getPreviewImage(self, theme):
613 """
614@@ -729,15 +751,15 @@
615 image = os.path.join(self.path, theme + u'.png')
616 return image
617
618- def _createThemeFromXml(self, themeXml, path):
619+ def _createThemeFromXml(self, theme_xml, path):
620 """
621 Return a theme object using information parsed from XML
622
623- ``themeXml``
624+ ``theme_xml``
625 The XML data to load into the theme
626 """
627 theme = ThemeXML()
628- theme.parse(themeXml)
629+ theme.parse(theme_xml)
630 theme.extend_image_filename(path)
631 return theme
632
633@@ -769,7 +791,7 @@
634 return False
635 # check for use in the system else where.
636 if testPlugin:
637- for plugin in self.mainwindow.pluginManager.plugins:
638+ for plugin in self.mainWindow.pluginManager.plugins:
639 if plugin.usesTheme(theme):
640 critical_error_message_box(
641 translate('OpenLP.ThemeManager',
642@@ -792,53 +814,53 @@
643 Version 1 theme to convert
644 """
645 theme = Theme(xml_data)
646- newtheme = ThemeXML()
647- newtheme.theme_name = self.bad_v1_name_chars.sub(u'', theme.Name)
648+ new_theme = ThemeXML()
649+ new_theme.theme_name = self.bad_v1_name_chars.sub(u'', theme.Name)
650 if theme.BackgroundType == 0:
651- newtheme.background_type = \
652+ new_theme.background_type = \
653 BackgroundType.to_string(BackgroundType.Solid)
654- newtheme.background_color = \
655+ new_theme.background_color = \
656 unicode(theme.BackgroundParameter1.name())
657 elif theme.BackgroundType == 1:
658- newtheme.background_type = \
659+ new_theme.background_type = \
660 BackgroundType.to_string(BackgroundType.Gradient)
661- newtheme.background_direction = \
662+ new_theme.background_direction = \
663 BackgroundGradientType. \
664 to_string(BackgroundGradientType.Horizontal)
665 if theme.BackgroundParameter3.name() == 1:
666- newtheme.background_direction = \
667+ new_theme.background_direction = \
668 BackgroundGradientType. \
669 to_string(BackgroundGradientType.Horizontal)
670- newtheme.background_start_color = \
671+ new_theme.background_start_color = \
672 unicode(theme.BackgroundParameter1.name())
673- newtheme.background_end_color = \
674+ new_theme.background_end_color = \
675 unicode(theme.BackgroundParameter2.name())
676 elif theme.BackgroundType == 2:
677- newtheme.background_type = \
678+ new_theme.background_type = \
679 BackgroundType.to_string(BackgroundType.Image)
680- newtheme.background_filename = unicode(theme.BackgroundParameter1)
681+ new_theme.background_filename = unicode(theme.BackgroundParameter1)
682 elif theme.BackgroundType == 3:
683- newtheme.background_type = \
684+ new_theme.background_type = \
685 BackgroundType.to_string(BackgroundType.Transparent)
686- newtheme.font_main_name = theme.FontName
687- newtheme.font_main_color = unicode(theme.FontColor.name())
688- newtheme.font_main_size = theme.FontProportion * 3
689- newtheme.font_footer_name = theme.FontName
690- newtheme.font_footer_color = unicode(theme.FontColor.name())
691- newtheme.font_main_shadow = False
692+ new_theme.font_main_name = theme.FontName
693+ new_theme.font_main_color = unicode(theme.FontColor.name())
694+ new_theme.font_main_size = theme.FontProportion * 3
695+ new_theme.font_footer_name = theme.FontName
696+ new_theme.font_footer_color = unicode(theme.FontColor.name())
697+ new_theme.font_main_shadow = False
698 if theme.Shadow == 1:
699- newtheme.font_main_shadow = True
700- newtheme.font_main_shadow_color = unicode(theme.ShadowColor.name())
701+ new_theme.font_main_shadow = True
702+ new_theme.font_main_shadow_color = unicode(theme.ShadowColor.name())
703 if theme.Outline == 1:
704- newtheme.font_main_outline = True
705- newtheme.font_main_outline_color = \
706+ new_theme.font_main_outline = True
707+ new_theme.font_main_outline_color = \
708 unicode(theme.OutlineColor.name())
709 vAlignCorrection = VerticalType.Top
710 if theme.VerticalAlign == 2:
711 vAlignCorrection = VerticalType.Middle
712 elif theme.VerticalAlign == 1:
713 vAlignCorrection = VerticalType.Bottom
714- newtheme.display_horizontal_align = theme.HorizontalAlign
715- newtheme.display_vertical_align = vAlignCorrection
716- return newtheme.extract_xml()
717+ new_theme.display_horizontal_align = theme.HorizontalAlign
718+ new_theme.display_vertical_align = vAlignCorrection
719+ return new_theme.extract_xml()
720