clean up of get_os_path and its tests

work with @Zsailer
Paul Ivanov 13 years ago committed by MinRK
parent 34b9840786
commit b8318708eb

@ -131,28 +131,29 @@ class FileNotebookManager(NotebookManager):
path = self.get_os_path(name, path)
return os.path.isfile(path)
def get_os_path(self, fname, path=None):
"""Return a full path to a notebook with the os.sep as the separator.
def get_os_path(self, fname, path='/'):
"""Given a notebook name and a server URL path, return its file system
path.
Parameters
----------
fname : string
The name of a notebook file with the .ipynb extension
path : string
The relative path (with '/' as separator) to the named notebook.
The relative URL path (with '/' as separator) to the named
notebook.
Returns
-------
path :
A path that combines notebook_dir (location where server started),
the relative path, and the filename with the current operating
system's url.
path : string
A file system path that combines notebook_dir (location where
server started), the relative path, and the filename with the
current operating system's url.
"""
if path is None:
path = '/'
parts = path.split('/')
parts = [p for p in parts if p != ''] # remove duplicate splits
path = os.sep.join([self.notebook_dir] + parts + [fname])
parts += [fname]
path = os.path.join(self.notebook_dir, *parts)
return path
def read_notebook_object_from_path(self, path):

@ -14,14 +14,14 @@ class TestFileNotebookManager(TestCase):
def test_nb_dir(self):
with TemporaryDirectory() as td:
km = FileNotebookManager(notebook_dir=td)
self.assertEqual(km.notebook_dir, td)
fm = FileNotebookManager(notebook_dir=td)
self.assertEqual(fm.notebook_dir, td)
def test_create_nb_dir(self):
with TemporaryDirectory() as td:
nbdir = os.path.join(td, 'notebooks')
km = FileNotebookManager(notebook_dir=nbdir)
self.assertEqual(km.notebook_dir, nbdir)
fm = FileNotebookManager(notebook_dir=nbdir)
self.assertEqual(fm.notebook_dir, nbdir)
def test_missing_nb_dir(self):
with TemporaryDirectory() as td:
@ -37,22 +37,21 @@ class TestFileNotebookManager(TestCase):
# separators.
with TemporaryDirectory() as td:
nbdir = os.path.join(td, 'notebooks')
km = FileNotebookManager(notebook_dir=nbdir)
path = km.get_os_path('test.ipynb', '/path/to/notebook/')
self.assertEqual(path, km.notebook_dir+os.sep+'path'+os.sep+'to'+os.sep+
'notebook'+os.sep+'test.ipynb')
with TemporaryDirectory() as td:
nbdir = os.path.join(td, 'notebooks')
km = FileNotebookManager(notebook_dir=nbdir)
path = km.get_os_path('test.ipynb', None)
self.assertEqual(path, km.notebook_dir+os.sep+'test.ipynb')
with TemporaryDirectory() as td:
nbdir = os.path.join(td, 'notebooks')
km = FileNotebookManager(notebook_dir=nbdir)
path = km.get_os_path('test.ipynb', '////')
self.assertEqual(path, km.notebook_dir+os.sep+'test.ipynb')
fm = FileNotebookManager(notebook_dir=nbdir)
path = fm.get_os_path('test.ipynb', '/path/to/notebook/')
rel_path_list = '/path/to/notebook/test.ipynb'.split('/')
fs_path = os.path.join(fm.notebook_dir, *rel_path_list)
self.assertEqual(path, fs_path)
fm = FileNotebookManager(notebook_dir=nbdir)
path = fm.get_os_path('test.ipynb')
fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
self.assertEqual(path, fs_path)
fm = FileNotebookManager(notebook_dir=nbdir)
path = fm.get_os_path('test.ipynb', '////')
fs_path = os.path.join(fm.notebook_dir, 'test.ipynb')
self.assertEqual(path, fs_path)
class TestNotebookManager(TestCase):
def test_named_notebook_path(self):

Loading…
Cancel
Save