From 87a99254ce57318ee9b4e6137015f7610d85e529 2013-10-17 21:09:13 From: MinRK Date: 2013-10-17 21:09:13 Subject: [PATCH] move url_[un]escape to utils from nbm --- diff --git a/IPython/html/notebook/handlers.py b/IPython/html/notebook/handlers.py index c3777ec..fd0e3fa 100644 --- a/IPython/html/notebook/handlers.py +++ b/IPython/html/notebook/handlers.py @@ -24,7 +24,7 @@ from zmq.utils import jsonapi from ..base.handlers import IPythonHandler from ..services.notebooks.handlers import _notebook_path_regex, _path_regex -from ..utils import url_path_join +from ..utils import url_path_join, url_escape, url_unescape from urllib import quote #----------------------------------------------------------------------------- @@ -64,8 +64,8 @@ class NamedNotebookHandler(IPythonHandler): # a .ipynb filename was given if not nbm.notebook_exists(name, path): raise web.HTTPError(404, u'Notebook does not exist: %s/%s' % (path, name)) - name = nbm.url_encode(name) - path = nbm.url_encode(path) + name = url_escape(name) + path = url_escape(path) self.write(self.render_template('notebook.html', project=self.project_dir, notebook_path=path, diff --git a/IPython/html/services/notebooks/nbmanager.py b/IPython/html/services/notebooks/nbmanager.py index c17f420..0b4e5b9 100644 --- a/IPython/html/services/notebooks/nbmanager.py +++ b/IPython/html/services/notebooks/nbmanager.py @@ -91,18 +91,6 @@ class NotebookManager(LoggingConfigurable): path = os.path.join(self.notebook_dir, *parts) return path - def url_encode(self, path): - """Takes a URL path with special characters and returns - the path with all these characters URL encoded""" - parts = path.split('/') - return '/'.join([quote(p) for p in parts]) - - def url_decode(self, path): - """Takes a URL path with encoded special characters and - returns the URL with special characters decoded""" - parts = path.split('/') - return '/'.join([unquote(p) for p in parts]) - def _notebook_dir_changed(self, name, old, new): """Do a bit of validation of the notebook dir.""" if not os.path.isabs(new): diff --git a/IPython/html/services/notebooks/tests/test_nbmanager.py b/IPython/html/services/notebooks/tests/test_nbmanager.py index cf65fbf..6a52496 100644 --- a/IPython/html/services/notebooks/tests/test_nbmanager.py +++ b/IPython/html/services/notebooks/tests/test_nbmanager.py @@ -67,42 +67,6 @@ class TestNotebookManager(TestCase): except OSError: print "Directory already exists." - def test_url_encode(self): - nm = NotebookManager() - - # changes path or notebook name with special characters to url encoding - # these tests specifically encode paths with spaces - path = nm.url_encode('/this is a test/for spaces/') - self.assertEqual(path, '/this%20is%20a%20test/for%20spaces/') - - path = nm.url_encode('notebook with space.ipynb') - self.assertEqual(path, 'notebook%20with%20space.ipynb') - - path = nm.url_encode('/path with a/notebook and space.ipynb') - self.assertEqual(path, '/path%20with%20a/notebook%20and%20space.ipynb') - - path = nm.url_encode('/ !@$#%^&* / test %^ notebook @#$ name.ipynb') - self.assertEqual(path, - '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') - - def test_url_decode(self): - nm = NotebookManager() - - # decodes a url string to a plain string - # these tests decode paths with spaces - path = nm.url_decode('/this%20is%20a%20test/for%20spaces/') - self.assertEqual(path, '/this is a test/for spaces/') - - path = nm.url_decode('notebook%20with%20space.ipynb') - self.assertEqual(path, 'notebook with space.ipynb') - - path = nm.url_decode('/path%20with%20a/notebook%20and%20space.ipynb') - self.assertEqual(path, '/path with a/notebook and space.ipynb') - - path = nm.url_decode( - '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') - self.assertEqual(path, '/ !@$#%^&* / test %^ notebook @#$ name.ipynb') - def test_create_notebook_model(self): with TemporaryDirectory() as td: # Test in root directory diff --git a/IPython/html/tests/test_utils.py b/IPython/html/tests/test_utils.py new file mode 100644 index 0000000..6ceff59 --- /dev/null +++ b/IPython/html/tests/test_utils.py @@ -0,0 +1,61 @@ +"""Test HTML utils""" + +#----------------------------------------------------------------------------- +# Copyright (C) 2013 The IPython Development Team +# +# Distributed under the terms of the BSD License. The full license is in +# the file COPYING, distributed as part of this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + +import nose.tools as nt + +import IPython.testing.tools as tt +from IPython.html.utils import url_escape, url_unescape + +#----------------------------------------------------------------------------- +# Test functions +#----------------------------------------------------------------------------- + +def test_help_output(): + """ipython notebook --help-all works""" + tt.help_all_output_test('notebook') + + +def test_url_escape(): + + # changes path or notebook name with special characters to url encoding + # these tests specifically encode paths with spaces + path = url_escape('/this is a test/for spaces/') + nt.assert_equal(path, '/this%20is%20a%20test/for%20spaces/') + + path = url_escape('notebook with space.ipynb') + nt.assert_equal(path, 'notebook%20with%20space.ipynb') + + path = url_escape('/path with a/notebook and space.ipynb') + nt.assert_equal(path, '/path%20with%20a/notebook%20and%20space.ipynb') + + path = url_escape('/ !@$#%^&* / test %^ notebook @#$ name.ipynb') + nt.assert_equal(path, + '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') + +def test_url_unescape(): + + # decodes a url string to a plain string + # these tests decode paths with spaces + path = url_unescape('/this%20is%20a%20test/for%20spaces/') + nt.assert_equal(path, '/this is a test/for spaces/') + + path = url_unescape('notebook%20with%20space.ipynb') + nt.assert_equal(path, 'notebook with space.ipynb') + + path = url_unescape('/path%20with%20a/notebook%20and%20space.ipynb') + nt.assert_equal(path, '/path with a/notebook and space.ipynb') + + path = url_unescape( + '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb') + nt.assert_equal(path, '/ !@$#%^&* / test %^ notebook @#$ name.ipynb') + diff --git a/IPython/html/utils.py b/IPython/html/utils.py index 500d152..c34c776 100644 --- a/IPython/html/utils.py +++ b/IPython/html/utils.py @@ -49,3 +49,18 @@ def url2path(url): path = os.path.join(*pieces) return path +def url_escape(path): + """Escape special characters in a URL path + + Turns '/foo bar/' into '/foo%20bar/' + """ + parts = path.split('/') + return '/'.join([quote(p) for p in parts]) + +def url_unescape(path): + """Unescape special characters in a URL path + + Turns '/foo%20bar/' into '/foo bar/' + """ + return '/'.join([unquote(p) for p in path.split('/')]) +