|
12 | 12 | import imp |
13 | 13 | import importlib |
14 | 14 | import io |
15 | | -import itertools |
16 | 15 | import json |
17 | 16 | import logging |
18 | 17 | import os |
@@ -127,36 +126,6 @@ def load_handlers(name): |
127 | 126 | name = 'jupyter_notebook.' + name |
128 | 127 | mod = __import__(name, fromlist=['default_handlers']) |
129 | 128 | 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}) |
160 | 129 |
|
161 | 130 | #----------------------------------------------------------------------------- |
162 | 131 | # The Tornado web application |
@@ -309,13 +278,24 @@ def _create_widget_shim_handlers(self): |
309 | 278 | ipythonwidgets = imp.find_module('ipython_widgets')[1] |
310 | 279 | widget_js = os.path.join(ipythonwidgets, 'static', 'widgets') |
311 | 280 |
|
| 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') |
312 | 296 | return [( |
313 | 297 | 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 |
319 | 299 | )] |
320 | 300 | except: # ipython_widgets is not installed |
321 | 301 | class ErrorHandler(IPythonHandler): |
|
0 commit comments