From ecb97881a1b15a6de48520ff6621c4395936fc41 Mon Sep 17 00:00:00 2001 From: Jon Tibble Date: Fri, 14 Jan 2011 18:58:47 +0000 Subject: [PATCH] Refactor file deleting, cleanups --- documentation/api/source/plugins/songs.rst | 3 --- openlp/core/lib/db.py | 10 +++------ openlp/core/ui/maindisplay.py | 2 ++ openlp/core/ui/servicemanager.py | 21 +++++-------------- openlp/core/ui/thememanager.py | 18 ++++++---------- openlp/core/utils/__init__.py | 17 +++++++++++++++ openlp/plugins/images/lib/mediaitem.py | 12 ++++------- .../presentations/lib/impresscontroller.py | 4 ++-- openlp/plugins/songs/lib/cclifileimport.py | 4 ---- 9 files changed, 39 insertions(+), 52 deletions(-) diff --git a/documentation/api/source/plugins/songs.rst b/documentation/api/source/plugins/songs.rst index fed9907a2..1e86ce020 100644 --- a/documentation/api/source/plugins/songs.rst +++ b/documentation/api/source/plugins/songs.rst @@ -72,9 +72,6 @@ Song Importers .. automodule:: openlp.plugins.songs.lib.cclifileimport :members: -.. autoclass:: openlp.plugins.songs.lib.cclifileimport.CCLIFileImportError - :members: - .. automodule:: openlp.plugins.songs.lib.ewimport :members: diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index c2e1243ce..3171730ea 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -34,7 +34,7 @@ from sqlalchemy import create_engine, MetaData from sqlalchemy.exceptions import InvalidRequestError from sqlalchemy.orm import scoped_session, sessionmaker -from openlp.core.utils import AppLocation +from openlp.core.utils import AppLocation, delete_file log = logging.getLogger(__name__) @@ -75,11 +75,7 @@ def delete_database(plugin_name, db_file_name=None): else: db_file_path = os.path.join( AppLocation.get_section_data_path(plugin_name), plugin_name) - try: - os.remove(db_file_path) - return True - except OSError: - return False + return delete_file(db_file_path) class BaseModel(object): """ @@ -295,4 +291,4 @@ class Manager(object): if self.is_dirty: engine = create_engine(self.db_url) if self.db_url.startswith(u'sqlite'): - engine.execute("vacuum") \ No newline at end of file + engine.execute("vacuum") diff --git a/openlp/core/ui/maindisplay.py b/openlp/core/ui/maindisplay.py index ab69cd1e9..273a3c4f0 100644 --- a/openlp/core/ui/maindisplay.py +++ b/openlp/core/ui/maindisplay.py @@ -24,6 +24,8 @@ # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### """ +The :mod:`maindisplay` module provides the functionality to display screens +and play multimedia within OpenLP. """ import logging import os diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index ffc2bee25..0b5b25e06 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -37,7 +37,8 @@ from openlp.core.lib import OpenLPToolbar, ServiceItem, context_menu_action, \ Receiver, build_icon, ItemCapabilities, SettingsManager, translate, \ ThemeLevel from openlp.core.ui import ServiceNoteForm, ServiceItemEditForm -from openlp.core.utils import AppLocation, file_is_unicode, split_filename +from openlp.core.utils import AppLocation, delete_file, file_is_unicode, \ + split_filename class ServiceManagerList(QtGui.QTreeWidget): """ @@ -445,11 +446,7 @@ class ServiceManager(QtGui.QWidget): file.close() if zip: zip.close() - try: - os.remove(serviceFileName) - except (IOError, OSError): - # if not present do not worry - pass + delete_file(serviceFileName) self.mainwindow.addRecentFile(fileName) self.setModified(False) return True @@ -515,11 +512,7 @@ class ServiceManager(QtGui.QWidget): if serviceItem.is_capable(ItemCapabilities.OnLoadUpdate): Receiver.send_message(u'%s_service_load' % serviceItem.name.lower(), serviceItem) - try: - if os.path.isfile(p_file): - os.remove(p_file) - except (IOError, OSError): - log.exception(u'Failed to remove osd file') + delete_file(p_file) else: QtGui.QMessageBox.critical( self, translate('OpenLP.ServiceManager', 'Error'), @@ -873,11 +866,7 @@ class ServiceManager(QtGui.QWidget): """ for file in os.listdir(self.servicePath): file_path = os.path.join(self.servicePath, file) - try: - if os.path.isfile(file_path): - os.remove(file_path) - except OSError: - log.exception(u'Failed to clean up servicePath') + delete_file(file_path) def onThemeComboBoxSelected(self, currentIndex): """ diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 81e191396..966306eb1 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -37,7 +37,7 @@ from openlp.core.theme import Theme from openlp.core.lib import OpenLPToolbar, ThemeXML, get_text_file_string, \ build_icon, Receiver, SettingsManager, translate, check_item_selected, \ BackgroundType, BackgroundGradientType, check_directory_exists -from openlp.core.utils import AppLocation, file_is_unicode, \ +from openlp.core.utils import AppLocation, delete_file, file_is_unicode, \ get_filesystem_encoding log = logging.getLogger(__name__) @@ -341,9 +341,9 @@ class ThemeManager(QtGui.QWidget): """ self.themelist.remove(theme) thumb = theme + u'.png' + delete_file(os.path.join(self.path, thumb)) + delete_file(os.path.join(self.thumbPath, thumb)) try: - os.remove(os.path.join(self.path, thumb)) - os.remove(os.path.join(self.thumbPath, thumb)) encoding = get_filesystem_encoding() shutil.rmtree(os.path.join(self.path, theme).encode(encoding)) except OSError: @@ -521,11 +521,8 @@ class ThemeManager(QtGui.QWidget): check_directory_exists(theme_dir) 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 ' - u'encoded.') + xml_data = file_is_unicode(xml_data) + if not xml_data: break filexml = self.checkVersionAndConvert(xml_data) outfile = open(fullpath, u'w') @@ -603,10 +600,7 @@ class ThemeManager(QtGui.QWidget): theme_file = os.path.join(theme_dir, name + u'.xml') if imageTo and self.oldBackgroundImage and \ imageTo != self.oldBackgroundImage: - try: - os.remove(self.oldBackgroundImage) - except OSError: - log.exception(u'Unable to remove old theme background') + delete_file(self.oldBackgroundImage) outfile = None try: outfile = open(theme_file, u'w') diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index d8012f433..b7d95a46d 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -282,6 +282,23 @@ def split_filename(path): else: return os.path.split(path) +def delete_file(file_path_name): + """ + Deletes a file from the system. + + ``file_path_name`` + The file, including path, to delete. + """ + if not file_path_name: + return False + try: + if os.path.exists(file_path_name): + os.remove(file_path_name) + return True + except (IOError, OSError): + log.exception("Unable to delete file %s" % file_path_name) + return False + def get_web_page(url, header=None, update_openlp=False): """ Attempts to download the webpage at url and returns that page or None. diff --git a/openlp/plugins/images/lib/mediaitem.py b/openlp/plugins/images/lib/mediaitem.py index 063a80d02..e9f41abcd 100644 --- a/openlp/plugins/images/lib/mediaitem.py +++ b/openlp/plugins/images/lib/mediaitem.py @@ -32,7 +32,7 @@ from PyQt4 import QtCore, QtGui from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \ ItemCapabilities, SettingsManager, translate, check_item_selected, \ Receiver, check_directory_exists -from openlp.core.utils import AppLocation, get_images_filter +from openlp.core.utils import AppLocation, delete_file, get_images_filter log = logging.getLogger(__name__) @@ -115,12 +115,8 @@ class ImageMediaItem(MediaManagerItem): for row in row_list: text = self.listView.item(row) if text: - try: - os.remove(os.path.join(self.servicePath, - unicode(text.text()))) - except OSError: - # if not present do not worry - pass + delete_file(os.path.join(self.servicePath, + unicode(text.text()))) self.listView.takeItem(row) SettingsManager.set_list(self.settingsSection, self.settingsSection, self.getFileList()) @@ -216,4 +212,4 @@ class ImageMediaItem(MediaManagerItem): 'the image file "%s" no longer exists.')) % filename}) def onPreviewClick(self): - MediaManagerItem.onPreviewClick(self) \ No newline at end of file + MediaManagerItem.onPreviewClick(self) diff --git a/openlp/plugins/presentations/lib/impresscontroller.py b/openlp/plugins/presentations/lib/impresscontroller.py index 7c8cf593d..516c595c7 100644 --- a/openlp/plugins/presentations/lib/impresscontroller.py +++ b/openlp/plugins/presentations/lib/impresscontroller.py @@ -51,6 +51,7 @@ else: from PyQt4 import QtCore +from openlp.core.utils import delete_file from presentationcontroller import PresentationController, PresentationDocument log = logging.getLogger(__name__) @@ -292,8 +293,7 @@ class ImpressDocument(PresentationDocument): try: doc.storeToURL(urlpath, props) self.convert_thumbnail(path, idx + 1) - if os.path.exists(path): - os.remove(path) + delete_file(path) except: log.exception(u'%s - Unable to store openoffice preview' % path) diff --git a/openlp/plugins/songs/lib/cclifileimport.py b/openlp/plugins/songs/lib/cclifileimport.py index 2234a81b7..441391d02 100644 --- a/openlp/plugins/songs/lib/cclifileimport.py +++ b/openlp/plugins/songs/lib/cclifileimport.py @@ -34,9 +34,6 @@ from songimport import SongImport log = logging.getLogger(__name__) -class CCLIFileImportError(Exception): - pass - class CCLIFileImport(SongImport): """ The :class:`CCLIFileImport` class provides OpenLP with the ability to @@ -152,7 +149,6 @@ class CCLIFileImport(SongImport): """ log.debug(u'USR file text: %s', textList) - lyrics = [] self.set_defaults() for line in textList: if line.startswith(u'Title='):