diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index c4d11beb58..6d18a2d663 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -36,33 +36,8 @@ from calibre.utils.config import prefs, tweaks from calibre.utils.search_query_parser import saved_searches, set_saved_searches from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format from calibre.utils.magick.draw import save_cover_data_to +from calibre.utils.recycle_bin import delete_file, delete_tree -if iswindows: - import calibre.utils.winshell as winshell - -def delete_file(path): - try: - winshell.delete_file(path, silent=True, no_confirm=True) - except: - os.remove(path) - -def delete_tree(path, permanent=False): - if permanent: - try: - # For completely mysterious reasons, sometimes a file is left open - # leading to access errors. If we get an exception, wait and hope - # that whatever has the file (the O/S?) lets go of it. - shutil.rmtree(path) - except: - traceback.print_exc() - time.sleep(1) - shutil.rmtree(path) - else: - try: - if not permanent: - winshell.delete_file(path, silent=True, no_confirm=True) - except: - delete_tree(path, permanent=True) copyfile = os.link if hasattr(os, 'link') else shutil.copyfile @@ -983,10 +958,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): path = None self.data.remove(id) if path and os.path.exists(path): - try: - winshell.delete_file(path, no_confirm=True, silent=True) - except: - self.rmtree(path) + self.rmtree(path) parent = os.path.dirname(path) if len(os.listdir(parent)) == 0: self.rmtree(parent) diff --git a/src/calibre/utils/recycle_bin.py b/src/calibre/utils/recycle_bin.py new file mode 100644 index 0000000000..df6016d796 --- /dev/null +++ b/src/calibre/utils/recycle_bin.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai + +__license__ = 'GPL v3' +__copyright__ = '2010, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +import os, shutil, time +from functools import partial + +from calibre import isbytestring +from calibre.constants import iswindows, isosx, plugins, filesystem_encoding + +recycle = None + +if iswindows: + import calibre.utils.winshell as winshell + recycle = partial(winshell.delete_file, silent=True, no_confirm=True) +elif isosx: + u = plugins['usbobserver'][0] + if hasattr(u, 'send2trash'): + def recycle(path): + if isbytestring(path): + path = path.decode(filesystem_encoding) + u.send2trash(path) + + +def delete_file(path): + if callable(recycle): + try: + recycle(path) + return + except: + import traceback + traceback.print_exc() + os.remove(path) + +def delete_tree(path, permanent=False): + if permanent: + try: + # For completely mysterious reasons, sometimes a file is left open + # leading to access errors. If we get an exception, wait and hope + # that whatever has the file (the O/S?) lets go of it. + shutil.rmtree(path) + except: + import traceback + traceback.print_exc() + time.sleep(1) + shutil.rmtree(path) + else: + if callable(recycle): + try: + recycle(path) + return + except: + import traceback + traceback.print_exc() + delete_tree(path, permanent=True) +