Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Feb 14, 2022
1 parent 6feb09e commit f78c480
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/source/developers/savehooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ They are both called with keyword arguments::
pre_save_hook(model=model, path=path, contents_manager=cm)
post_save_hook(model=model, os_path=os_path, contents_manager=cm)

Assigning to ``pre_save_hook`` (or ``post_save_hook``) multiple times will
register hooks, not override them. They will then be called sequentially.

Examples
--------

Expand Down
49 changes: 38 additions & 11 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ def run_post_save_hook(self, model, os_path):
self.log.debug("Running post-save hook on %s", os_path)
post_save_hook(os_path=os_path, model=model, contents_manager=self)
except Exception as e:
self.log.error("Post-save %s hook failed on %s", post_save_hook.__name__, os_path, exc_info=True)
self.log.error(
"Post-save %s hook failed on %s",
post_save_hook.__name__,
os_path,
exc_info=True,
)
raise web.HTTPError(
500, "Unexpected error while running post hook save: %s" % e
) from e
Expand Down Expand Up @@ -294,7 +299,9 @@ def _dir_model(self, path, content=True):
if not os.path.isdir(os_path):
raise web.HTTPError(404, four_o_four)
elif is_hidden(os_path, self.root_dir) and not self.allow_hidden:
self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path)
self.log.info(
"Refusing to serve hidden directory %r, via 404 Error", os_path
)
raise web.HTTPError(404, four_o_four)

model = self._base_model(path)
Expand All @@ -316,7 +323,9 @@ def _dir_model(self, path, content=True):
# skip over broken symlinks in listing
if e.errno == errno.ENOENT:
self.log.warning("%s doesn't exist", os_path)
elif e.errno != errno.EACCES: # Don't provide clues about protected files
elif (
e.errno != errno.EACCES
): # Don't provide clues about protected files
self.log.warning("Error stat-ing %s: %s", os_path, e)
continue

Expand All @@ -330,8 +339,12 @@ def _dir_model(self, path, content=True):

try:
if self.should_list(name):
if self.allow_hidden or not is_file_hidden(os_path, stat_res=st):
contents.append(self.get(path="%s/%s" % (path, name), content=False))
if self.allow_hidden or not is_file_hidden(
os_path, stat_res=st
):
contents.append(
self.get(path="%s/%s" % (path, name), content=False)
)
except OSError as e:
# ELOOP: recursive symlink, also don't show failure due to permissions
if e.errno not in [errno.ELOOP, errno.EACCES]:
Expand Down Expand Up @@ -530,7 +543,11 @@ def is_non_empty_dir(os_path):
return False

if self.delete_to_trash:
if not self.always_delete_dir and sys.platform == "win32" and is_non_empty_dir(os_path):
if (
not self.always_delete_dir
and sys.platform == "win32"
and is_non_empty_dir(os_path)
):
# send2trash can really delete files on Windows, so disallow
# deleting non-empty files. See Github issue 3631.
raise web.HTTPError(400, "Directory %s not empty" % os_path)
Expand Down Expand Up @@ -598,7 +615,9 @@ def get_kernel_path(self, path, model=None):
return parent_dir


class AsyncFileContentsManager(FileContentsManager, AsyncFileManagerMixin, AsyncContentsManager):
class AsyncFileContentsManager(
FileContentsManager, AsyncFileManagerMixin, AsyncContentsManager
):
@default("checkpoints_class")
def _checkpoints_class_default(self):
return AsyncFileCheckpoints
Expand All @@ -615,7 +634,9 @@ async def _dir_model(self, path, content=True):
if not os.path.isdir(os_path):
raise web.HTTPError(404, four_o_four)
elif is_hidden(os_path, self.root_dir) and not self.allow_hidden:
self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path)
self.log.info(
"Refusing to serve hidden directory %r, via 404 Error", os_path
)
raise web.HTTPError(404, four_o_four)

model = self._base_model(path)
Expand All @@ -638,7 +659,9 @@ async def _dir_model(self, path, content=True):
# skip over broken symlinks in listing
if e.errno == errno.ENOENT:
self.log.warning("%s doesn't exist", os_path)
elif e.errno != errno.EACCES: # Don't provide clues about protected files
elif (
e.errno != errno.EACCES
): # Don't provide clues about protected files
self.log.warning("Error stat-ing %s: %s", os_path, e)
continue

Expand All @@ -652,9 +675,13 @@ async def _dir_model(self, path, content=True):

try:
if self.should_list(name):
if self.allow_hidden or not is_file_hidden(os_path, stat_res=st):
if self.allow_hidden or not is_file_hidden(
os_path, stat_res=st
):
contents.append(
await self.get(path="%s/%s" % (path, name), content=False)
await self.get(
path="%s/%s" % (path, name), content=False
)
)
except OSError as e:
# ELOOP: recursive symlink, also don't show failure due to permissions
Expand Down
11 changes: 9 additions & 2 deletions jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ def run_pre_save_hook(self, model, path, **kwargs):
except Exception:
# unhandled errors don't prevent saving,
# which could cause frustrating data loss
self.log.error("Pre-save hook %s failed on %s", pre_save_hook.__name__, path, exc_info=True)
self.log.error(
"Pre-save hook %s failed on %s",
pre_save_hook.__name__,
path,
exc_info=True,
)

checkpoints_class = Type(Checkpoints, config=True)
checkpoints = Instance(Checkpoints, config=True)
Expand Down Expand Up @@ -196,7 +201,9 @@ def get_extra_handlers(self):
"""
handlers = []
if self.files_handler_class:
handlers.append((r"/files/(.*)", self.files_handler_class, self.files_handler_params))
handlers.append(
(r"/files/(.*)", self.files_handler_class, self.files_handler_params)
)
return handlers

# ContentsManager API part 1: methods that must be
Expand Down

0 comments on commit f78c480

Please sign in to comment.