Refactor file deleting, cleanups

This commit is contained in:
Jon Tibble 2011-01-14 18:58:47 +00:00
parent 6beac0d027
commit ecb97881a1
9 changed files with 39 additions and 52 deletions

View File

@ -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:

View File

@ -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):
"""

View File

@ -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

View File

@ -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):
"""

View File

@ -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')

View File

@ -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.

View File

@ -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())

View File

@ -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)

View File

@ -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='):