From 1ed94a0c5466fd3d3f39f67e291363761f1eab9f 2013-12-19 21:01:00 From: Thomas Kluyver Date: 2013-12-19 21:01:00 Subject: [PATCH] Move notebook URL fragment regexen into IPython.html.base.handlers --- diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py index f0ca195..f63438a 100644 --- a/IPython/html/base/handlers.py +++ b/IPython/html/base/handlers.py @@ -355,6 +355,14 @@ class TrailingSlashHandler(web.RequestHandler): self.redirect(self.request.uri.rstrip('/')) #----------------------------------------------------------------------------- +# URL pattern fragments for re-use +#----------------------------------------------------------------------------- + +path_regex = r"(?P(?:/.*)*)" +notebook_name_regex = r"(?P[^/]+\.ipynb)" +notebook_path_regex = "%s/%s" % (path_regex, notebook_name_regex) + +#----------------------------------------------------------------------------- # URL to handler mappings #----------------------------------------------------------------------------- diff --git a/IPython/html/nbconvert/handlers.py b/IPython/html/nbconvert/handlers.py index 6e51bb6..0d30c30 100644 --- a/IPython/html/nbconvert/handlers.py +++ b/IPython/html/nbconvert/handlers.py @@ -2,7 +2,7 @@ import os from tornado import web -from ..base.handlers import IPythonHandler +from ..base.handlers import IPythonHandler, notebook_path_regex from IPython.nbformat.current import to_notebook_json from IPython.nbconvert.exporters.export import exporter_map from IPython.utils import tz @@ -74,12 +74,10 @@ class NbconvertPostHandler(IPythonHandler): #----------------------------------------------------------------------------- _format_regex = r"(?P\w+)" -_path_regex = r"(?P(?:/.*)*)" -_notebook_name_regex = r"(?P[^/]+\.ipynb)" -_notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex) + default_handlers = [ - (r"/nbconvert/%s%s" % (_format_regex, _notebook_path_regex), + (r"/nbconvert/%s%s" % (_format_regex, notebook_path_regex), NbconvertFileHandler), (r"/nbconvert/%s" % _format_regex, NbconvertPostHandler), ] \ No newline at end of file diff --git a/IPython/html/notebook/handlers.py b/IPython/html/notebook/handlers.py index f3174b7..e442d00 100644 --- a/IPython/html/notebook/handlers.py +++ b/IPython/html/notebook/handlers.py @@ -20,8 +20,7 @@ import os from tornado import web HTTPError = web.HTTPError -from ..base.handlers import IPythonHandler -from ..services.notebooks.handlers import _notebook_path_regex, _path_regex +from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex from ..utils import url_path_join, url_escape #----------------------------------------------------------------------------- @@ -85,7 +84,7 @@ class NotebookRedirectHandler(IPythonHandler): default_handlers = [ - (r"/notebooks%s" % _notebook_path_regex, NotebookHandler), - (r"/notebooks%s" % _path_regex, NotebookRedirectHandler), + (r"/notebooks%s" % notebook_path_regex, NotebookHandler), + (r"/notebooks%s" % path_regex, NotebookRedirectHandler), ] diff --git a/IPython/html/services/notebooks/handlers.py b/IPython/html/services/notebooks/handlers.py index 69d0ac4..27e74d7 100644 --- a/IPython/html/services/notebooks/handlers.py +++ b/IPython/html/services/notebooks/handlers.py @@ -23,7 +23,9 @@ from tornado import web from IPython.html.utils import url_path_join, url_escape from IPython.utils.jsonutil import date_default -from IPython.html.base.handlers import IPythonHandler, json_errors +from IPython.html.base.handlers import (IPythonHandler, json_errors, + notebook_path_regex, path_regex, + notebook_name_regex) #----------------------------------------------------------------------------- # Notebook web service handlers @@ -264,17 +266,14 @@ class ModifyNotebookCheckpointsHandler(IPythonHandler): #----------------------------------------------------------------------------- -_path_regex = r"(?P(?:/.*)*)" _checkpoint_id_regex = r"(?P[\w-]+)" -_notebook_name_regex = r"(?P[^/]+\.ipynb)" -_notebook_path_regex = "%s/%s" % (_path_regex, _notebook_name_regex) default_handlers = [ - (r"/api/notebooks%s/checkpoints" % _notebook_path_regex, NotebookCheckpointsHandler), - (r"/api/notebooks%s/checkpoints/%s" % (_notebook_path_regex, _checkpoint_id_regex), + (r"/api/notebooks%s/checkpoints" % notebook_path_regex, NotebookCheckpointsHandler), + (r"/api/notebooks%s/checkpoints/%s" % (notebook_path_regex, _checkpoint_id_regex), ModifyNotebookCheckpointsHandler), - (r"/api/notebooks%s" % _notebook_path_regex, NotebookHandler), - (r"/api/notebooks%s" % _path_regex, NotebookHandler), + (r"/api/notebooks%s" % notebook_path_regex, NotebookHandler), + (r"/api/notebooks%s" % path_regex, NotebookHandler), ] diff --git a/IPython/html/tree/handlers.py b/IPython/html/tree/handlers.py index 1ec33d5..03820ca 100644 --- a/IPython/html/tree/handlers.py +++ b/IPython/html/tree/handlers.py @@ -18,9 +18,8 @@ Authors: import os from tornado import web -from ..base.handlers import IPythonHandler +from ..base.handlers import IPythonHandler, notebook_path_regex, path_regex from ..utils import url_path_join, path2url, url2path, url_escape -from ..services.notebooks.handlers import _notebook_path_regex, _path_regex #----------------------------------------------------------------------------- # Handlers @@ -70,8 +69,8 @@ class TreeRedirectHandler(IPythonHandler): default_handlers = [ - (r"/tree%s" % _notebook_path_regex, TreeHandler), - (r"/tree%s" % _path_regex, TreeHandler), + (r"/tree%s" % notebook_path_regex, TreeHandler), + (r"/tree%s" % path_regex, TreeHandler), (r"/tree", TreeHandler), (r"/", TreeRedirectHandler), ]