Skip to content

Commit ad3ab9b

Browse files
committed
Send files to OS trash mechanism on delete
This adds a config option, defaulting to use trash, and adds a dependency on the pure-Python 'send2trash' package. Linux, Mac and Windows should all be supported. Alternatively, we could make it default to hard delete (the current behaviour), and let users opt in to trash behaviour. Then Send2Trash could be a soft dependency. This doesn't touch the UI yet, so you still get a confirmation dialog which inaccurately says it will 'permanently delete' the file. If we want to do this, we'll need some way for the contents manager to pass the UI a hint about whether deleting is permanent or not. Closes gh-165
1 parent c05138a commit ad3ab9b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

notebook/services/contents/filemanager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import mimetypes
1414
import nbformat
1515

16+
from send2trash import send2trash
1617
from tornado import web
1718

1819
from .filecheckpoints import FileCheckpoints
@@ -144,6 +145,11 @@ def _validate_root_dir(self, proposal):
144145
def _checkpoints_class_default(self):
145146
return FileCheckpoints
146147

148+
delete_to_trash = Bool(True, config=True,
149+
help="""If True (default), deleting files will send them to the
150+
platform's trash/recycle bin, where they can be recovered. If False,
151+
deleting files really deletes them.""")
152+
147153
def is_hidden(self, path):
148154
"""Does the API style path correspond to a hidden directory or file?
149155
@@ -464,6 +470,14 @@ def delete_file(self, path):
464470
elif not os.path.isfile(os_path):
465471
raise web.HTTPError(404, u'File does not exist: %s' % os_path)
466472

473+
if self.delete_to_trash:
474+
self.log.debug("Sending %s to trash", os_path)
475+
# Looking at the code in send2trash, I don't think the errors it
476+
# raises let us distinguish permission errors from other errors in
477+
# code. So for now, just let them all get logged as server errors.
478+
send2trash(os_path)
479+
return
480+
467481
if os.path.isdir(os_path):
468482
self.log.debug("Removing directory %s", os_path)
469483
with self.perm_to_403():

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
'nbformat',
153153
'nbconvert',
154154
'ipykernel', # bless IPython kernel for now
155+
'Send2Trash',
155156
]
156157
extras_require = {
157158
':sys_platform != "win32"': ['terminado>=0.3.3'],

0 commit comments

Comments
 (0)