Skip to content

Commit d17caf9

Browse files
authored
Merge pull request #4453 from minrk/maybe-future
- Finish transition to our own maybe_future - Bump minimum Python version to 3.5
2 parents 43622e7 + 3d94b51 commit d17caf9

File tree

14 files changed

+112
-150
lines changed

14 files changed

+112
-150
lines changed

.travis.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,13 @@ matrix:
9393
- MOZ_HEADLESS=1
9494
addons:
9595
firefox: 57.0
96-
97-
- python: 3.4
98-
env: GROUP=python
9996
- python: 3.5
10097
env: GROUP=python
10198
- python: 3.7
10299
dist: xenial
103100
env: GROUP=python
104101
- python: 3.6
105102
env: GROUP=docs
106-
- python: 3.6
107-
env:
108-
- GROUP=python
109-
- EXTRA_PIP="tornado<5"
110103

111104
after_success:
112105
- codecov

notebook/base/zmqhandlers.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
# Copyright (c) Jupyter Development Team.
55
# Distributed under the terms of the Modified BSD License.
66

7-
import os
87
import json
98
import struct
10-
import warnings
119
import sys
12-
13-
try:
14-
from urllib.parse import urlparse # Py 3
15-
except ImportError:
16-
from urlparse import urlparse # Py 2
10+
from urllib.parse import urlparse
1711

1812
import tornado
1913
from tornado import gen, ioloop, web
@@ -23,8 +17,10 @@
2317
from jupyter_client.jsonutil import date_default, extract_dates
2418
from ipython_genutils.py3compat import cast_unicode
2519

20+
from notebook.utils import maybe_future
2621
from .handlers import IPythonHandler
2722

23+
2824
def serialize_binary_message(msg):
2925
"""serialize a message as a binary blob
3026
@@ -251,39 +247,39 @@ def _on_zmq_reply(self, stream, msg_list):
251247

252248

253249
class AuthenticatedZMQStreamHandler(ZMQStreamHandler, IPythonHandler):
254-
250+
255251
def set_default_headers(self):
256252
"""Undo the set_default_headers in IPythonHandler
257-
253+
258254
which doesn't make sense for websockets
259255
"""
260256
pass
261-
257+
262258
def pre_get(self):
263259
"""Run before finishing the GET request
264-
260+
265261
Extend this method to add logic that should fire before
266262
the websocket finishes completing.
267263
"""
268264
# authenticate the request before opening the websocket
269265
if self.get_current_user() is None:
270266
self.log.warning("Couldn't authenticate WebSocket connection")
271267
raise web.HTTPError(403)
272-
268+
273269
if self.get_argument('session_id', False):
274270
self.session.session = cast_unicode(self.get_argument('session_id'))
275271
else:
276272
self.log.warning("No session ID specified")
277-
273+
278274
@gen.coroutine
279275
def get(self, *args, **kwargs):
280276
# pre_get can be a coroutine in subclasses
281277
# assign and yield in two step to avoid tornado 3 issues
282278
res = self.pre_get()
283-
yield gen.maybe_future(res)
279+
yield maybe_future(res)
284280
res = super(AuthenticatedZMQStreamHandler, self).get(*args, **kwargs)
285-
yield gen.maybe_future(res)
286-
281+
yield maybe_future(res)
282+
287283
def initialize(self):
288284
self.log.debug("Initializing websocket connection %s", self.request.path)
289285
self.session = Session(config=self.config)

notebook/bundler/handlers.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
# Copyright (c) Jupyter Development Team.
44
# Distributed under the terms of the Modified BSD License.
5-
from . import tools
6-
from notebook.utils import url2path
7-
from notebook.base.handlers import IPythonHandler
8-
from notebook.services.config import ConfigManager
5+
96
from ipython_genutils.importstring import import_item
107
from tornado import web, gen
118

9+
from notebook.utils import maybe_future, url2path
10+
from notebook.base.handlers import IPythonHandler
11+
from notebook.services.config import ConfigManager
12+
13+
from . import tools
14+
1215

1316
class BundlerHandler(IPythonHandler):
1417
def initialize(self):
@@ -74,7 +77,7 @@ def get(self, path):
7477

7578
# Let the bundler respond in any way it sees fit and assume it will
7679
# finish the request
77-
yield gen.maybe_future(bundler_mod.bundle(self, model))
80+
yield maybe_future(bundler_mod.bundle(self, model))
7881

7982
_bundler_id_regex = r'(?P<bundler_id>[A-Za-z0-9_]+)'
8083

notebook/files/handlers.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55

66
import mimetypes
77
import json
8+
from base64 import decodebytes
89

9-
try: #PY3
10-
from base64 import decodebytes
11-
except ImportError: #PY2
12-
from base64 import decodestring as decodebytes
13-
14-
15-
from tornado import gen, web
10+
from tornado import web
1611

1712
from notebook.base.handlers import IPythonHandler
13+
from notebook.utils import maybe_future
1814

1915

2016
class FilesHandler(IPythonHandler):
@@ -51,7 +47,7 @@ def get(self, path, include_body=True):
5147
else:
5248
name = path
5349

54-
model = yield gen.maybe_future(cm.get(path, type='file', content=include_body))
50+
model = yield maybe_future(cm.get(path, type='file', content=include_body))
5551

5652
if self.get_argument("download", False):
5753
self.set_attachment_header(name)

notebook/services/api/handlers.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
# Copyright (c) Jupyter Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6-
from itertools import chain
76
import json
7+
import os
88

99
from tornado import gen, web
1010

1111
from ...base.handlers import IPythonHandler, APIHandler
1212
from notebook._tz import utcfromtimestamp, isoformat
13+
from notebook.utils import maybe_future
1314

14-
import os
1515

1616
class APISpecHandler(web.StaticFileHandler, IPythonHandler):
1717

@@ -22,10 +22,11 @@ def initialize(self):
2222
def get(self):
2323
self.log.warning("Serving api spec (experimental, incomplete)")
2424
return web.StaticFileHandler.get(self, 'api.yaml')
25-
25+
2626
def get_content_type(self):
2727
return 'text/x-yaml'
2828

29+
2930
class APIStatusHandler(APIHandler):
3031

3132
_track_activity = False
@@ -37,7 +38,7 @@ def get(self):
3738
started = self.settings.get('started', utcfromtimestamp(0))
3839
started = isoformat(started)
3940

40-
kernels = yield gen.maybe_future(self.kernel_manager.list_kernels())
41+
kernels = yield maybe_future(self.kernel_manager.list_kernels())
4142
total_connections = sum(k['connections'] for k in kernels)
4243
last_activity = isoformat(self.application.last_activity())
4344
model = {
@@ -48,6 +49,7 @@ def get(self):
4849
}
4950
self.finish(json.dumps(model, sort_keys=True))
5051

52+
5153
default_handlers = [
5254
(r"/api/spec.yaml", APISpecHandler),
5355
(r"/api/status", APIStatusHandler),

notebook/services/contents/handlers.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from tornado import gen, web
1212

13-
from notebook.utils import url_path_join, url_escape
13+
from notebook.utils import maybe_future, url_path_join, url_escape
1414
from jupyter_client.jsonutil import date_default
1515

1616
from notebook.base.handlers import (
@@ -108,7 +108,7 @@ def get(self, path=''):
108108
raise web.HTTPError(400, u'Content %r is invalid' % content)
109109
content = int(content)
110110

111-
model = yield gen.maybe_future(self.contents_manager.get(
111+
model = yield maybe_future(self.contents_manager.get(
112112
path=path, type=type, format=format, content=content,
113113
))
114114
validate_model(model, expect_content=content)
@@ -122,7 +122,7 @@ def patch(self, path=''):
122122
model = self.get_json_body()
123123
if model is None:
124124
raise web.HTTPError(400, u'JSON body missing')
125-
model = yield gen.maybe_future(cm.update(model, path))
125+
model = yield maybe_future(cm.update(model, path))
126126
validate_model(model, expect_content=False)
127127
self._finish_model(model)
128128

@@ -133,7 +133,7 @@ def _copy(self, copy_from, copy_to=None):
133133
copy_from=copy_from,
134134
copy_to=copy_to or '',
135135
))
136-
model = yield gen.maybe_future(self.contents_manager.copy(copy_from, copy_to))
136+
model = yield maybe_future(self.contents_manager.copy(copy_from, copy_to))
137137
self.set_status(201)
138138
validate_model(model, expect_content=False)
139139
self._finish_model(model)
@@ -142,7 +142,7 @@ def _copy(self, copy_from, copy_to=None):
142142
def _upload(self, model, path):
143143
"""Handle upload of a new file to path"""
144144
self.log.info(u"Uploading file to %s", path)
145-
model = yield gen.maybe_future(self.contents_manager.new(model, path))
145+
model = yield maybe_future(self.contents_manager.new(model, path))
146146
self.set_status(201)
147147
validate_model(model, expect_content=False)
148148
self._finish_model(model)
@@ -151,7 +151,7 @@ def _upload(self, model, path):
151151
def _new_untitled(self, path, type='', ext=''):
152152
"""Create a new, empty untitled entity"""
153153
self.log.info(u"Creating new %s in %s", type or 'file', path)
154-
model = yield gen.maybe_future(self.contents_manager.new_untitled(path=path, type=type, ext=ext))
154+
model = yield maybe_future(self.contents_manager.new_untitled(path=path, type=type, ext=ext))
155155
self.set_status(201)
156156
validate_model(model, expect_content=False)
157157
self._finish_model(model)
@@ -162,7 +162,7 @@ def _save(self, model, path):
162162
chunk = model.get("chunk", None)
163163
if not chunk or chunk == -1: # Avoid tedious log information
164164
self.log.info(u"Saving file at %s", path)
165-
model = yield gen.maybe_future(self.contents_manager.save(model, path))
165+
model = yield maybe_future(self.contents_manager.save(model, path))
166166
validate_model(model, expect_content=False)
167167
self._finish_model(model)
168168

@@ -182,11 +182,11 @@ def post(self, path=''):
182182

183183
cm = self.contents_manager
184184

185-
file_exists = yield gen.maybe_future(cm.file_exists(path))
185+
file_exists = yield maybe_future(cm.file_exists(path))
186186
if file_exists:
187187
raise web.HTTPError(400, "Cannot POST to files, use PUT instead.")
188188

189-
dir_exists = yield gen.maybe_future(cm.dir_exists(path))
189+
dir_exists = yield maybe_future(cm.dir_exists(path))
190190
if not dir_exists:
191191
raise web.HTTPError(404, "No such directory: %s" % path)
192192

@@ -220,21 +220,21 @@ def put(self, path=''):
220220
if model:
221221
if model.get('copy_from'):
222222
raise web.HTTPError(400, "Cannot copy with PUT, only POST")
223-
exists = yield gen.maybe_future(self.contents_manager.file_exists(path))
223+
exists = yield maybe_future(self.contents_manager.file_exists(path))
224224
if exists:
225-
yield gen.maybe_future(self._save(model, path))
225+
yield maybe_future(self._save(model, path))
226226
else:
227-
yield gen.maybe_future(self._upload(model, path))
227+
yield maybe_future(self._upload(model, path))
228228
else:
229-
yield gen.maybe_future(self._new_untitled(path))
229+
yield maybe_future(self._new_untitled(path))
230230

231231
@web.authenticated
232232
@gen.coroutine
233233
def delete(self, path=''):
234234
"""delete a file in the given path"""
235235
cm = self.contents_manager
236236
self.log.warning('delete %s', path)
237-
yield gen.maybe_future(cm.delete(path))
237+
yield maybe_future(cm.delete(path))
238238
self.set_status(204)
239239
self.finish()
240240

@@ -246,7 +246,7 @@ class CheckpointsHandler(APIHandler):
246246
def get(self, path=''):
247247
"""get lists checkpoints for a file"""
248248
cm = self.contents_manager
249-
checkpoints = yield gen.maybe_future(cm.list_checkpoints(path))
249+
checkpoints = yield maybe_future(cm.list_checkpoints(path))
250250
data = json.dumps(checkpoints, default=date_default)
251251
self.finish(data)
252252

@@ -255,7 +255,7 @@ def get(self, path=''):
255255
def post(self, path=''):
256256
"""post creates a new checkpoint"""
257257
cm = self.contents_manager
258-
checkpoint = yield gen.maybe_future(cm.create_checkpoint(path))
258+
checkpoint = yield maybe_future(cm.create_checkpoint(path))
259259
data = json.dumps(checkpoint, default=date_default)
260260
location = url_path_join(self.base_url, 'api/contents',
261261
url_escape(path), 'checkpoints', url_escape(checkpoint['id']))
@@ -271,7 +271,7 @@ class ModifyCheckpointsHandler(APIHandler):
271271
def post(self, path, checkpoint_id):
272272
"""post restores a file from a checkpoint"""
273273
cm = self.contents_manager
274-
yield gen.maybe_future(cm.restore_checkpoint(checkpoint_id, path))
274+
yield maybe_future(cm.restore_checkpoint(checkpoint_id, path))
275275
self.set_status(204)
276276
self.finish()
277277

@@ -280,7 +280,7 @@ def post(self, path, checkpoint_id):
280280
def delete(self, path, checkpoint_id):
281281
"""delete clears a checkpoint for a given file"""
282282
cm = self.contents_manager
283-
yield gen.maybe_future(cm.delete_checkpoint(checkpoint_id, path))
283+
yield maybe_future(cm.delete_checkpoint(checkpoint_id, path))
284284
self.set_status(204)
285285
self.finish()
286286

@@ -307,7 +307,7 @@ class TrustNotebooksHandler(IPythonHandler):
307307
@gen.coroutine
308308
def post(self,path=''):
309309
cm = self.contents_manager
310-
yield gen.maybe_future(cm.trust_notebook(path))
310+
yield maybe_future(cm.trust_notebook(path))
311311
self.set_status(201)
312312
self.finish()
313313
#-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)