I’m creating a pre_save_hook to check the amount of disk space in use, and if over a certain threshold I’d like to pop a warning message and cancel the save. I’ve got everything working except cancelling the save (I’ve tried ‘return’, ‘exit()’, ‘quit()’, ‘sys.exit()’, and ‘os.exit()’). Is this possible? Any ideas appreciated, TIA.
Currently, all errors in pre_save_hooks are caught and logged, so they cannot cancel the save in progress. This patch will allow you to raise HTTPErrors to cancel saves with a message that will be shown to the user.
Until you can require that, though, you need to implement the quota enforcement by overriding ContentsManager.save
instead of a hook:
from tornado.web import HTTPError
from jupyter_server.services.contents.largefilemanager import LargeFileManager
class QuotaFileManager(LargeFileManager):
def _check_quota(self):
return True # whatever you want here
def save(self, model, path):
# this bit can move to a hook following https://github.com/jupyter-server/jupyter_server/pull/456
if not self._check_quota():
# raising here means cancelling the save
# the user will see this message in a dialog
raise HTTPError(400, "Quota exceeded")
return super().save(model, path)
# use our contents manager subclass
c.ServerApp.contents_manager_class = QuotaFileManager