diff --git a/openlp.pyw b/openlp.pyw
index fffed8726..4eaf8ee22 100755
--- a/openlp.pyw
+++ b/openlp.pyw
@@ -74,6 +74,7 @@ class OpenLP(QtGui.QApplication):
#Load and store current Application Version
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(filepath, u'version.txt'))
+ fversion = None
try:
fversion = open(filepath, 'r')
for line in fversion:
@@ -83,6 +84,9 @@ class OpenLP(QtGui.QApplication):
except:
applicationVersion = {u'Full':u'1.9.0-000',
u'version':u'1.9.0', u'build':u'000'}
+ finally:
+ if fversion:
+ fversion.close()
#set the default string encoding
try:
sys.setappdefaultencoding(u'utf-8')
diff --git a/openlp/core/lib/settingsmanager.py b/openlp/core/lib/settingsmanager.py
index 43f148ffd..148fbd40b 100644
--- a/openlp/core/lib/settingsmanager.py
+++ b/openlp/core/lib/settingsmanager.py
@@ -60,7 +60,7 @@ class SettingsManager(object):
u'user interface', u'display previewpanel', True))
def setUIItemVisibility(self, item=u'', isVisible=True):
- if item != u'':
+ if item:
if item == u'ThemeManagerDock':
ConfigHelper.set_config(u'user interface',
u'display thememanager', isVisible)
diff --git a/openlp/core/ui/amendthemeform.py b/openlp/core/ui/amendthemeform.py
index c66eb5199..27197c5af 100644
--- a/openlp/core/ui/amendthemeform.py
+++ b/openlp/core/ui/amendthemeform.py
@@ -194,7 +194,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
def onImageToolButtonClicked(self):
filename = QtGui.QFileDialog.getOpenFileName(
self, self.trUtf8('Open file'))
- if filename != u'':
+ if filename:
self.ImageLineEdit.setText(filename)
self.theme.background_filename = filename
self.previewTheme(self.theme)
diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py
index e8abed32c..245140e31 100644
--- a/openlp/core/ui/servicemanager.py
+++ b/openlp/core/ui/servicemanager.py
@@ -35,7 +35,7 @@ from openlp.core.lib import PluginConfig, OpenLPToolbar, ServiceItem, \
class ServiceManagerList(QtGui.QTreeWidget):
- def __init__(self,parent=None,name=None):
+ def __init__(self, parent=None, name=None):
QtGui.QTreeWidget.__init__(self,parent)
self.parent = parent
@@ -418,7 +418,7 @@ class ServiceManager(QtGui.QWidget):
u'Save Service', self.config.get_last_dir())
else:
filename = self.config.get_last_dir()
- if filename != u'':
+ if filename:
splittedFile = filename.split(u'.')
if splittedFile[-1] != u'osz':
filename = filename + u'.osz'
@@ -427,21 +427,30 @@ class ServiceManager(QtGui.QWidget):
self.config.set_last_dir(filename)
service = []
servicefile = filename + u'.osd'
- zip = zipfile.ZipFile(unicode(filename), 'w')
- for item in self.serviceItems:
- service.append(
- {u'serviceitem':item[u'data'].get_service_repr()})
- if item[u'data'].service_item_type == ServiceItemType.Image or \
- item[u'data'].service_item_type == ServiceItemType.Command:
- for frame in item[u'data'].frames:
- path_from = unicode(os.path.join(
- item[u'data'].service_item_path, frame[u'title']))
- zip.write(path_from)
- file = open(servicefile, u'wb')
- cPickle.dump(service, file)
- file.close()
- zip.write(servicefile)
- zip.close()
+ zip = None
+ file = None
+ try:
+ zip = zipfile.ZipFile(unicode(filename), 'w')
+ for item in self.serviceItems:
+ service.append(
+ {u'serviceitem':item[u'data'].get_service_repr()})
+ if item[u'data'].service_item_type == ServiceItemType.Image or \
+ item[u'data'].service_item_type == ServiceItemType.Command:
+ for frame in item[u'data'].frames:
+ path_from = unicode(os.path.join(
+ item[u'data'].service_item_path, frame[u'title']))
+ zip.write(path_from)
+ file = open(servicefile, u'wb')
+ cPickle.dump(service, file)
+ file.close()
+ zip.write(servicefile)
+ except:
+ log.exception(u'Failed to save service to disk')
+ finally:
+ if file:
+ file.close()
+ if zip:
+ zip.close()
try:
os.remove(servicefile)
except:
@@ -467,8 +476,10 @@ class ServiceManager(QtGui.QWidget):
self.config.get_last_dir(), u'Services (*.osz)')
filename = unicode(filename)
name = filename.split(os.path.sep)
- if filename != u'':
+ if filename:
self.config.set_last_dir(filename)
+ zip = None
+ f = None
try:
zip = zipfile.ZipFile(unicode(filename))
for file in zip.namelist():
@@ -501,6 +512,11 @@ class ServiceManager(QtGui.QWidget):
log.exception(u'Failed to remove osd file')
except:
log.exception(u'Problem loading a service file')
+ finally:
+ if f:
+ f.close()
+ if zip:
+ zip.close()
self.isNew = False
self.serviceName = name[len(name) - 1]
self.parent.serviceChanged(True, self.serviceName)
diff --git a/openlp/core/ui/slidecontroller.py b/openlp/core/ui/slidecontroller.py
index 029a4b1f9..19d469638 100644
--- a/openlp/core/ui/slidecontroller.py
+++ b/openlp/core/ui/slidecontroller.py
@@ -426,7 +426,7 @@ class SlideController(QtGui.QWidget):
self.onSlideSelected()
self.PreviewListWidget.setFocus()
log.info(u'Display Rendering took %4s' % (time.time() - before))
- if self.serviceitem.audit != u'' and self.isLive:
+ if self.serviceitem.audit and self.isLive:
Receiver().send_message(u'songusage_live', self.serviceitem.audit)
log.debug(u'displayServiceManagerItems End')
diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py
index 3c9d930ce..8850d880a 100644
--- a/openlp/core/ui/thememanager.py
+++ b/openlp/core/ui/thememanager.py
@@ -182,10 +182,6 @@ class ThemeManager(QtGui.QWidget):
self.ThemeListWidget.takeItem(row)
try:
os.remove(os.path.join(self.path, th))
- except:
- #if not present do not worry
- pass
- try:
shutil.rmtree(os.path.join(self.path, theme))
except:
#if not present do not worry
@@ -210,16 +206,22 @@ class ThemeManager(QtGui.QWidget):
unicode(self.trUtf8(u'Save Theme - (%s)')) % theme,
self.config.get_last_dir(1) )
path = unicode(path)
- if path != u'':
+ if path:
self.config.set_last_dir(path, 1)
themePath = os.path.join(path, theme + u'.theme')
- zip = zipfile.ZipFile(themePath, u'w')
- source = os.path.join(self.path, theme)
- for root, dirs, files in os.walk(source):
- for name in files:
- zip.write(
- os.path.join(source, name), os.path.join(theme, name))
- zip.close()
+ zip = None
+ try:
+ zip = zipfile.ZipFile(themePath, u'w')
+ source = os.path.join(self.path, theme)
+ for root, dirs, files in os.walk(source):
+ for name in files:
+ zip.write(
+ os.path.join(source, name), os.path.join(theme, name))
+ except:
+ log.exception(u'Export Theme Failed')
+ finally:
+ if zip:
+ zip.close()
def onImportTheme(self):
files = QtGui.QFileDialog.getOpenFileNames(
@@ -291,44 +293,49 @@ class ThemeManager(QtGui.QWidget):
"""
log.debug(u'Unzipping theme %s', filename)
filename = unicode(filename)
+ zip = None
+ outfile = None
try:
zip = zipfile.ZipFile(filename)
+ filexml = None
+ themename = None
+ for file in zip.namelist():
+ if file.endswith(os.path.sep):
+ theme_dir = os.path.join(dir, file)
+ if not os.path.exists(theme_dir):
+ os.mkdir(os.path.join(dir, file))
+ else:
+ fullpath = os.path.join(dir, file)
+ names = file.split(os.path.sep)
+ if len(names) > 1:
+ # not preview file
+ if themename is None:
+ themename = names[0]
+ xml_data = zip.read(file)
+ if os.path.splitext(file)[1].lower() in [u'.xml']:
+ if self.checkVersion1(xml_data):
+ # upgrade theme xml
+ filexml = self.migrateVersion122(filename,
+ fullpath, xml_data)
+ else:
+ filexml = xml_data
+ outfile = open(fullpath, u'w')
+ outfile.write(filexml)
+ else:
+ outfile = open(fullpath, u'w')
+ outfile.write(zip.read(file))
+ self.generateAndSaveImage(dir, themename, filexml)
except:
QtGui.QMessageBox.critical(
self, self.trUtf8(u'Error'),
self.trUtf8(u'File is not a valid theme!'),
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok))
- return
- filexml = None
- themename = None
- for file in zip.namelist():
- if file.endswith(os.path.sep):
- theme_dir = os.path.join(dir, file)
- if not os.path.exists(theme_dir):
- os.mkdir(os.path.join(dir, file))
- else:
- fullpath = os.path.join(dir, file)
- names = file.split(os.path.sep)
- if len(names) > 1:
- # not preview file
- if themename is None:
- themename = names[0]
- xml_data = zip.read(file)
- if os.path.splitext(file)[1].lower() in [u'.xml']:
- if self.checkVersion1(xml_data):
- # upgrade theme xml
- filexml = self.migrateVersion122(filename,
- fullpath, xml_data)
- else:
- filexml = xml_data
- outfile = open(fullpath, u'w')
- outfile.write(filexml)
- outfile.close()
- else:
- outfile = open(fullpath, u'w')
- outfile.write(zip.read(file))
- outfile.close()
- self.generateAndSaveImage(dir, themename, filexml)
+ log.exception(u'Importing theme from zip file failed')
+ finally:
+ if zip:
+ zip.close()
+ if outfile:
+ outfile.close()
def checkVersion1(self, xmlfile):
"""
@@ -408,13 +415,22 @@ class ThemeManager(QtGui.QWidget):
result == QtGui.QMessageBox.Yes
if result == QtGui.QMessageBox.Yes:
# Save the theme, overwriting the existing theme if necessary.
- outfile = open(theme_file, u'w')
- outfile.write(theme_pretty_xml)
- outfile.close()
+ outfile = None
+ try:
+ outfile = open(theme_file, u'w')
+ outfile.write(theme_pretty_xml)
+ except:
+ log.exception(u'Saving theme to file failed')
+ finally:
+ if outfile:
+ outfile.close()
if image_from and image_from != image_to:
print "if", image_from
print "it", image_to
- shutil.copyfile(image_from, image_to)
+ try:
+ shutil.copyfile(image_from, image_to)
+ except:
+ log.exception(u'Failed to save theme image')
self.generateAndSaveImage(self.path, name, theme_xml)
self.loadThemes()
else:
diff --git a/openlp/core/utils/registry.py b/openlp/core/utils/registry.py
index 3a1ceb520..8fa0bb6a8 100644
--- a/openlp/core/utils/registry.py
+++ b/openlp/core/utils/registry.py
@@ -101,23 +101,29 @@ class Registry(object):
return False
def _load(self):
+ file_handle = None
try:
if not os.path.isfile(self.file_name):
return False
file_handle = open(self.file_name, u'r')
self.config.readfp(file_handle)
- file_handle.close()
return True
except:
return False
+ finally:
+ if file_handle:
+ file_handle.close()
def _save(self):
+ file_handle = None
try:
if not os.path.exists(os.path.dirname(self.file_name)):
os.makedirs(os.path.dirname(self.file_name))
file_handle = open(self.file_name, u'w')
self.config.write(file_handle)
- file_handle.close()
return self._load()
except:
return False
+ finally:
+ if file_handle:
+ file_handle.close()
diff --git a/openlp/plugins/bibles/forms/bibleimportform.py b/openlp/plugins/bibles/forms/bibleimportform.py
index de7397355..62210d44f 100644
--- a/openlp/plugins/bibles/forms/bibleimportform.py
+++ b/openlp/plugins/bibles/forms/bibleimportform.py
@@ -59,6 +59,7 @@ class BibleImportForm(QtGui.QDialog, Ui_BibleImportDialog):
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(filepath, u'..',
u'resources', u'crosswalkbooks.csv'))
+ fbibles = None
try:
fbibles = open(filepath, 'r')
for line in fbibles:
@@ -66,6 +67,9 @@ class BibleImportForm(QtGui.QDialog, Ui_BibleImportDialog):
self.cwBibleVersions[p[0]] = p[1].replace(u'\n', u'')
except:
log.exception(u'Crosswalk resources missing')
+ finally:
+ if fbibles:
+ fbibles.close()
#Load and store BibleGateway Bibles
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(filepath, u'..',
@@ -77,6 +81,9 @@ class BibleImportForm(QtGui.QDialog, Ui_BibleImportDialog):
self.bgBibleVersions[p[0]] = p[1].replace(u'\n', u'')
except:
log.exception(u'Biblegateway resources missing')
+ finally:
+ if fbibles:
+ fbibles.close()
self.loadBibleCombo(self.cwBibleVersions)
self.cwActive = True
@@ -125,7 +132,7 @@ class BibleImportForm(QtGui.QDialog, Ui_BibleImportDialog):
filename = QtGui.QFileDialog.getOpenFileName(
self, self.trUtf8(u'Open Bible Verses file'),
self.config.get_last_dir(1))
- if filename != u'':
+ if filename:
self.VerseLocationEdit.setText(filename)
self.config.set_last_dir(filename, 1)
self.setCsv()
@@ -134,7 +141,7 @@ class BibleImportForm(QtGui.QDialog, Ui_BibleImportDialog):
filename = QtGui.QFileDialog.getOpenFileName(
self, self.trUtf8(u'Open Bible Books file'),
self.config.get_last_dir(2))
- if filename != u'':
+ if filename:
self.BooksLocationEdit.setText(filename)
self.config.set_last_dir(filename, 2)
self.setCsv()
@@ -143,7 +150,7 @@ class BibleImportForm(QtGui.QDialog, Ui_BibleImportDialog):
filename = QtGui.QFileDialog.getOpenFileName(
self, self.trUtf8(u'Open OSIS import file'),
self.config.get_last_dir(3))
- if filename != u'':
+ if filename:
self.OSISLocationEdit.setText(filename)
self.config.set_last_dir(filename, 3)
self.setOsis()
diff --git a/openlp/plugins/bibles/lib/bibleCSVimpl.py b/openlp/plugins/bibles/lib/bibleCSVimpl.py
index a2fed1ce5..15a29f900 100644
--- a/openlp/plugins/bibles/lib/bibleCSVimpl.py
+++ b/openlp/plugins/bibles/lib/bibleCSVimpl.py
@@ -48,46 +48,59 @@ class BibleCSVImpl(BibleCommon):
def load_data(self, booksfile, versesfile, dialogobject):
#Populate the Tables
- fbooks = open(booksfile, 'r')
- fverse = open(versesfile, 'r')
+ fbooks = None
+ try:
+ fbooks = open(booksfile, 'r')
+ count = 0
+ for line in fbooks:
+ # cancel pressed
+ if not self.loadbible:
+ break
+ details = chardet.detect(line)
+ line = unicode(line, details['encoding'])
+ p = line.split(u',')
+ p1 = p[1].replace(u'"', u'')
+ p2 = p[2].replace(u'"', u'')
+ p3 = p[3].replace(u'"', u'')
+ self.bibledb.create_book(p2, p3, int(p1))
+ count += 1
+ #Flush the screen events
+ if count % 3 == 0:
+ Receiver().send_message(u'process_events')
+ count = 0
+ except:
+ log.exception(u'Loading books from file failed')
+ finally:
+ if fbooks:
+ fbooks.close()
- count = 0
- for line in fbooks:
- # cancel pressed
- if not self.loadbible:
- break
- details = chardet.detect(line)
- line = unicode(line, details['encoding'])
- p = line.split(u',')
- p1 = p[1].replace(u'"', u'')
- p2 = p[2].replace(u'"', u'')
- p3 = p[3].replace(u'"', u'')
- self.bibledb.create_book(p2, p3, int(p1))
- count += 1
- #Flush the screen events
- if count % 3 == 0:
- Receiver().send_message(u'process_events')
- count = 0
-
- count = 0
- book_ptr = None
- for line in fverse:
- if not self.loadbible: # cancel pressed
- break
- details = chardet.detect(line)
- line = unicode(line, details['encoding'])
- # split into 3 units and leave the rest as a single field
- p = line.split(u',', 3)
- p0 = p[0].replace(u'"', u'')
- p3 = p[3].replace(u'"',u'')
- if book_ptr is not p0:
- book = self.bibledb.get_bible_book(p0)
- book_ptr = book.name
- # increament the progress bar
- dialogobject.incrementProgressBar(book.name)
- self.bibledb.add_verse(book.id, p[1], p[2], p3)
- count += 1
- #Every x verses repaint the screen
- if count % 3 == 0:
- Receiver().send_message(u'process_events')
- count = 0
+ fverse = None
+ try:
+ fverse = open(versesfile, 'r')
+ count = 0
+ book_ptr = None
+ for line in fverse:
+ if not self.loadbible: # cancel pressed
+ break
+ details = chardet.detect(line)
+ line = unicode(line, details['encoding'])
+ # split into 3 units and leave the rest as a single field
+ p = line.split(u',', 3)
+ p0 = p[0].replace(u'"', u'')
+ p3 = p[3].replace(u'"',u'')
+ if book_ptr is not p0:
+ book = self.bibledb.get_bible_book(p0)
+ book_ptr = book.name
+ # increament the progress bar
+ dialogobject.incrementProgressBar(book.name)
+ self.bibledb.add_verse(book.id, p[1], p[2], p3)
+ count += 1
+ #Every x verses repaint the screen
+ if count % 3 == 0:
+ Receiver().send_message(u'process_events')
+ count = 0
+ except:
+ log.exception(u'Loading verses from file failed')
+ finally:
+ if fverse:
+ fverse.close()
diff --git a/openlp/plugins/bibles/lib/bibleOSISimpl.py b/openlp/plugins/bibles/lib/bibleOSISimpl.py
index 04609e262..d4f94d6c3 100644
--- a/openlp/plugins/bibles/lib/bibleOSISimpl.py
+++ b/openlp/plugins/bibles/lib/bibleOSISimpl.py
@@ -60,12 +60,20 @@ class BibleOSISImpl():
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(
filepath, u'..', u'resources',u'osisbooks.csv'))
- fbibles = open(filepath, u'r')
- for line in fbibles:
- p = line.split(u',')
- self.booksOfBible[p[0]] = p[1].replace(u'\n', u'')
- self.abbrevOfBible[p[0]] = p[2].replace(u'\n', u'')
- self.loadbible = True
+ fbibles = None
+ try:
+ fbibles = open(filepath, u'r')
+ for line in fbibles:
+ p = line.split(u',')
+ self.booksOfBible[p[0]] = p[1].replace(u'\n', u'')
+ self.abbrevOfBible[p[0]] = p[2].replace(u'\n', u'')
+ self.loadbible = True
+ except:
+ log.exception(u'OSIS bible import failed')
+ finally:
+ self.loadbible = False
+ if fbibles:
+ fbibles.close()
QtCore.QObject.connect(Receiver().get_receiver(),
QtCore.SIGNAL(u'openlpstopimport'), self.stop_import)
@@ -86,82 +94,98 @@ class BibleOSISImpl():
The Import dialog, so that we can increase the counter on
the progress bar.
"""
- detect_file = open(osisfile_record, u'r')
- details = chardet.detect(detect_file.read(2048))
- detect_file.close()
- osis = codecs.open(osisfile_record, u'r', details['encoding'])
- book_ptr = None
- count = 0
- verseText = u'