Skip to content

Commit 188fc3c

Browse files
Mariko WakabayashiZsailer
authored andcommitted
Make handlers compatible with async CMs
1 parent 564325e commit 188fc3c

File tree

8 files changed

+19
-20
lines changed

8 files changed

+19
-20
lines changed

jupyter_server/base/handlers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import jupyter_server
3232
from jupyter_server._tz import utcnow
3333
from jupyter_server.i18n import combine_translations
34-
from jupyter_server.utils import is_hidden, url_path_join, url_is_absolute, url_escape
34+
from jupyter_server.utils import ensure_async, is_hidden, url_path_join, url_is_absolute, url_escape
3535
from jupyter_server.services.security import csp_report_uri
3636

3737
#-----------------------------------------------------------------------------
@@ -800,28 +800,28 @@ class FilesRedirectHandler(JupyterHandler):
800800
"""Handler for redirecting relative URLs to the /files/ handler"""
801801

802802
@staticmethod
803-
def redirect_to_files(self, path):
803+
async def redirect_to_files(self, path):
804804
"""make redirect logic a reusable static method
805805
806806
so it can be called from other handlers.
807807
"""
808808
cm = self.contents_manager
809-
if cm.dir_exists(path):
809+
if await ensure_async(cm.dir_exists(path)):
810810
# it's a *directory*, redirect to /tree
811811
url = url_path_join(self.base_url, 'tree', url_escape(path))
812812
else:
813813
orig_path = path
814814
# otherwise, redirect to /files
815815
parts = path.split('/')
816816

817-
if not cm.file_exists(path=path) and 'files' in parts:
817+
if not await ensure_async(cm.file_exists(path=path)) and 'files' in parts:
818818
# redirect without files/ iff it would 404
819819
# this preserves pre-2.0-style 'files/' links
820820
self.log.warning("Deprecated files/ URL: %s", orig_path)
821821
parts.remove('files')
822822
path = '/'.join(parts)
823823

824-
if not cm.file_exists(path=path):
824+
if not await ensure_async(cm.file_exists(path=path)):
825825
raise web.HTTPError(404)
826826

827827
url = url_path_join(self.base_url, 'files', url_escape(path))

jupyter_server/files/handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from base64 import decodebytes
99
from tornado import web
1010
from jupyter_server.base.handlers import JupyterHandler
11-
11+
from jupyter_server.utils import ensure_async
1212

1313
class FilesHandler(JupyterHandler):
1414
"""serve files via ContentsManager
@@ -34,7 +34,7 @@ def head(self, path):
3434
async def get(self, path, include_body=True):
3535
cm = self.contents_manager
3636

37-
if cm.is_hidden(path) and not cm.allow_hidden:
37+
if await ensure_async(cm.is_hidden(path)) and not cm.allow_hidden:
3838
self.log.info("Refusing to serve hidden file, via 404 Error")
3939
raise web.HTTPError(404)
4040

@@ -44,7 +44,7 @@ async def get(self, path, include_body=True):
4444
else:
4545
name = path
4646

47-
model = await cm.get(path, type='file', content=include_body)
47+
model = await ensure_async(cm.get(path, type='file', content=include_body))
4848

4949
if self.get_argument("download", False):
5050
self.set_attachment_header(name)

jupyter_server/nbconvert/handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
JupyterHandler, FilesRedirectHandler,
1515
path_regex,
1616
)
17+
from jupyter_server.utils import ensure_async
1718
from nbformat import from_dict
1819

1920
from ipython_genutils.py3compat import cast_bytes
@@ -80,7 +81,7 @@ class NbconvertFileHandler(JupyterHandler):
8081
SUPPORTED_METHODS = ('GET',)
8182

8283
@web.authenticated
83-
def get(self, format, path):
84+
async def get(self, format, path):
8485

8586
exporter = get_exporter(format, config=self.config, log=self.log)
8687

@@ -93,7 +94,7 @@ def get(self, format, path):
9394
else:
9495
ext_resources_dir = None
9596

96-
model = self.contents_manager.get(path=path)
97+
model = await ensure_async(self.contents_manager.get(path=path))
9798
name = model['name']
9899
if model['type'] != 'notebook':
99100
# not a notebook, redirect to files

jupyter_server/services/contents/filemanager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from datetime import datetime
77
import errno
8-
import io
98
import os
109
import shutil
1110
import stat

jupyter_server/services/contents/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ async def post(self, path=''):
176176

177177
cm = self.contents_manager
178178

179-
file_exists = cm.file_exists(path)
179+
file_exists = await ensure_async(cm.file_exists(path))
180180
if file_exists:
181181
raise web.HTTPError(400, "Cannot POST to files, use PUT instead.")
182182

@@ -213,7 +213,7 @@ async def put(self, path=''):
213213
if model:
214214
if model.get('copy_from'):
215215
raise web.HTTPError(400, "Cannot copy with PUT, only POST")
216-
exists = self.contents_manager.file_exists(path)
216+
exists = await ensure_async(self.contents_manager.file_exists(path))
217217
if exists:
218218
await self._save(model, path)
219219
else:

jupyter_server/services/contents/largefilemanager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from anyio import run_sync_in_worker_thread
2-
from contextlib import contextmanager
32
from tornado import web
4-
import nbformat
53
import base64
64
import os, io
75

@@ -139,3 +137,4 @@ async def _save_large_file(self, os_path, content, format):
139137
with io.open(os_path, 'ab') as f:
140138
await run_sync_in_worker_thread(f.write, bcontent)
141139

140+

jupyter_server/services/contents/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ async def exists(self, path):
623623
exists : bool
624624
Whether the target exists.
625625
"""
626-
return await (self.file_exists(path) or self.dir_exists(path))
626+
return await (ensure_async(self.file_exists(path)) or ensure_async(self.dir_exists(path)))
627627

628628
async def get(self, path, content=True, type=None, format=None):
629629
"""Get a file or directory model."""
@@ -807,7 +807,7 @@ async def copy(self, from_path, to_path=None):
807807
raise HTTPError(400, "Can't copy directories")
808808
if to_path is None:
809809
to_path = from_dir
810-
if self.dir_exists(to_path):
810+
if await ensure_async(self.dir_exists(to_path)):
811811
name = copy_pat.sub(u'.', from_name)
812812
to_name = await self.increment_filename(name, to_path, insert='-Copy')
813813
to_path = u'{0}/{1}'.format(to_path, to_name)

jupyter_server/view/handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
from tornado import web
88
from ..base.handlers import JupyterHandler, path_regex
9-
from ..utils import url_escape, url_path_join
9+
from ..utils import ensure_async, url_escape, url_path_join
1010

1111

1212
class ViewHandler(JupyterHandler):
1313
"""Render HTML files within an iframe."""
1414
@web.authenticated
15-
def get(self, path):
15+
async def get(self, path):
1616
path = path.strip('/')
17-
if not self.contents_manager.file_exists(path):
17+
if not await ensure_async(self.contents_manager.file_exists(path)):
1818
raise web.HTTPError(404, u'File does not exist: %s' % path)
1919

2020
basename = path.rsplit('/', 1)[-1]

0 commit comments

Comments
 (0)