Skip to content

Commit aa461d9

Browse files
authored
Merge pull request #1968 from takluyver/delete-to-trash
Send files to OS trash mechanism on delete
2 parents 8a15950 + 1849b80 commit aa461d9

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
@@ -14,6 +14,7 @@
1414
import mimetypes
1515
import nbformat
1616

17+
from send2trash import send2trash
1718
from tornado import web
1819

1920
from .filecheckpoints import FileCheckpoints
@@ -149,6 +150,11 @@ def _validate_root_dir(self, proposal):
149150
def _checkpoints_class_default(self):
150151
return FileCheckpoints
151152

153+
delete_to_trash = Bool(True, config=True,
154+
help="""If True (default), deleting files will send them to the
155+
platform's trash/recycle bin, where they can be recovered. If False,
156+
deleting files really deletes them.""")
157+
152158
@default('files_handler_class')
153159
def _files_handler_class_default(self):
154160
return AuthenticatedFileHandler
@@ -493,6 +499,14 @@ def delete_file(self, path):
493499
elif not os.path.isfile(os_path):
494500
raise web.HTTPError(404, u'File does not exist: %s' % os_path)
495501

502+
if self.delete_to_trash:
503+
self.log.debug("Sending %s to trash", os_path)
504+
# Looking at the code in send2trash, I don't think the errors it
505+
# raises let us distinguish permission errors from other errors in
506+
# code. So for now, just let them all get logged as server errors.
507+
send2trash(os_path)
508+
return
509+
496510
if os.path.isdir(os_path):
497511
self.log.debug("Removing directory %s", os_path)
498512
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)