Skip to content

Commit 04eadff

Browse files
committed
Simplify deprecated handler.
1 parent e228276 commit 04eadff

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

jupyter_notebook/notebookapp.py

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import imp
1313
import importlib
1414
import io
15-
import itertools
1615
import json
1716
import logging
1817
import os
@@ -127,36 +126,6 @@ def load_handlers(name):
127126
name = 'jupyter_notebook.' + name
128127
mod = __import__(name, fromlist=['default_handlers'])
129128
return mod.default_handlers
130-
131-
class ShimHandler(FileFindHandler):
132-
def validate_absolute_path(self, *args, **kwargs):
133-
return super(ShimHandler, self).validate_absolute_path(*args, **kwargs)
134-
135-
def get_content(self, *args, **kwargs):
136-
self.log.warn('Deprecated widget Javascript path /static/widgets/js/*.js was used')
137-
138-
# Get the content. get_content will return either a bytestring
139-
# or an iterator of bytestrings, depending on the file.
140-
content = super(ShimHandler, self).get_content(*args, **kwargs)
141-
142-
# Check if something should be prepended.
143-
if self.prepend is not None:
144-
self.set_header("Content-Length", str(int(self._headers["Content-Length"]) + len(self.prepend)))
145-
if isinstance(content, (bytes)): # Bytes
146-
content = self.prepend + content
147-
else: # Iterator
148-
content = itertools.chain([self.prepend], content)
149-
return content
150-
151-
def make_shim_handler(log, prepend=None, encoding='utf-8'):
152-
"""Makes a FileFindHandler capable of prepending content
153-
to the files."""
154-
155-
# If the caller wants something to be prepended, encode it as
156-
# the appropriate data type.
157-
if prepend is not None:
158-
prepend = bytes(prepend, encoding=encoding)
159-
return type('ShimHandler', (ShimHandler, ), {'log': log, 'prepend': prepend})
160129

161130
#-----------------------------------------------------------------------------
162131
# The Tornado web application
@@ -309,13 +278,24 @@ def _create_widget_shim_handlers(self):
309278
ipythonwidgets = imp.find_module('ipython_widgets')[1]
310279
widget_js = os.path.join(ipythonwidgets, 'static', 'widgets')
311280

281+
class DeprecationHandler(IPythonHandler):
282+
def get(self, url_path):
283+
self.set_header("Content-Type", 'text/javascript')
284+
285+
# Verify that the file requested exists.
286+
local_file = os.path.join(widget_js, *url_path.split('/'))
287+
if not os.path.isfile(local_file):
288+
self.set_status(404)
289+
self.finish("File doesn't exits")
290+
291+
# Read the file and append the warning.
292+
with open(local_file, 'r') as f:
293+
contents = f.read()
294+
self.finish(DEPRECATED_WIDGET_WARNING + contents)
295+
self.log.warn('Deprecated widget Javascript path /static/widgets/js/*.js was used')
312296
return [(
313297
r'/deprecatedwidgets/(.*)',
314-
make_shim_handler(self.log, prepend=DEPRECATED_WIDGET_WARNING),
315-
{
316-
'path': widget_js,
317-
'no_cache_paths': ['/'], # don't cache anything in widgets
318-
}
298+
DeprecationHandler
319299
)]
320300
except: # ipython_widgets is not installed
321301
class ErrorHandler(IPythonHandler):

0 commit comments

Comments
 (0)