From cb7585067fd69e443089ed8fda895dfa25f06697 2014-02-05 20:01:11 From: Brian E. Granger Date: 2014-02-05 20:01:11 Subject: [PATCH] Addressing review comments. * list_dirs and get_dir_model added to base NBM class. * Calling of list_dirs moved to handler. * type=notebook added to notebook model. --- diff --git a/IPython/html/services/notebooks/filenbmanager.py b/IPython/html/services/notebooks/filenbmanager.py index fd4d70e..28aeb99 100644 --- a/IPython/html/services/notebooks/filenbmanager.py +++ b/IPython/html/services/notebooks/filenbmanager.py @@ -159,12 +159,17 @@ class FileNotebookManager(NotebookManager): """List the directories for a given API style path.""" path = path.strip('/') os_path = self.get_os_path('', path) + if not os.path.isdir(os_path): + raise web.HTTPError(404, u'diretory does not exist: %r' % os_path) dir_names = os.listdir(os_path) dirs = [] for name in dir_names: os_path = self.get_os_path(name, path) if os.path.isdir(os_path) and not name.startswith('.'): - model = self.get_dir_model(name, path) + try: + model = self.get_dir_model(name, path) + except IOError: + pass dirs.append(model) dirs = sorted(dirs, key=lambda item: item['name']) return dirs @@ -206,16 +211,8 @@ class FileNotebookManager(NotebookManager): """ path = path.strip('/') notebook_names = self.get_notebook_names(path) - index = [] - notebooks = [] - for name in notebook_names: - model = self.get_notebook_model(name, path, content=False) - if name.lower() == 'index.ipynb': - index.append(model) - else: - notebooks.append(model) + notebooks = [self.get_notebook_model(name, path, content=False) for name in notebook_names] notebooks = sorted(notebooks, key=lambda item: item['name']) - notebooks = index + self.list_dirs(path) + notebooks return notebooks def get_notebook_model(self, name, path='', content=True): @@ -248,6 +245,7 @@ class FileNotebookManager(NotebookManager): model['path'] = path model['last_modified'] = last_modified model['created'] = created + model['type'] = 'notebook' if content: with io.open(os_path, 'r', encoding='utf-8') as f: try: @@ -264,7 +262,7 @@ class FileNotebookManager(NotebookManager): if 'content' not in model: raise web.HTTPError(400, u'No notebook JSON data provided') - + # One checkpoint should always exist if self.notebook_exists(name, path) and not self.list_checkpoints(name, path): self.create_checkpoint(name, path) diff --git a/IPython/html/services/notebooks/handlers.py b/IPython/html/services/notebooks/handlers.py index 27e74d7..ac44dd6 100644 --- a/IPython/html/services/notebooks/handlers.py +++ b/IPython/html/services/notebooks/handlers.py @@ -69,8 +69,18 @@ class NotebookHandler(IPythonHandler): nbm = self.notebook_manager # Check to see if a notebook name was given if name is None: - # List notebooks in 'path' - notebooks = nbm.list_notebooks(path) + # TODO: Remove this after we create the contents web service and directories are + # no longer listed by the notebook web service. This should only handle notebooks + # and not directories. + dirs = nbm.list_dirs(path) + notebooks = [] + index = [] + for nb in nbm.list_notebooks(path): + if nb['name'].lower() == 'index.ipynb': + index.append(nb) + else: + notebooks.append(nb) + notebooks = index + dirs + notebooks self.finish(json.dumps(notebooks, default=date_default)) return # get and return notebook representation diff --git a/IPython/html/services/notebooks/nbmanager.py b/IPython/html/services/notebooks/nbmanager.py index f86e914..2a9771c 100644 --- a/IPython/html/services/notebooks/nbmanager.py +++ b/IPython/html/services/notebooks/nbmanager.py @@ -112,6 +112,26 @@ class NotebookManager(LoggingConfigurable): """ return basename + # TODO: Remove this after we create the contents web service and directories are + # no longer listed by the notebook web service. + def list_dirs(self, path): + """List the directory models for a given API style path.""" + raise NotImplementedError('must be implemented in a subclass') + + # TODO: Remove this after we create the contents web service and directories are + # no longer listed by the notebook web service. + def get_dir_model(self, name, path=''): + """Get the directory model given a directory name and its API style path. + + The keys in the model should be: + * name + * path + * last_modified + * created + * type='directory' + """ + raise NotImplementedError('must be implemented in a subclass') + def list_notebooks(self, path=''): """Return a list of notebook dicts without content. diff --git a/IPython/html/services/notebooks/tests/test_notebooks_api.py b/IPython/html/services/notebooks/tests/test_notebooks_api.py index e9e17fa..5ac0cb1 100644 --- a/IPython/html/services/notebooks/tests/test_notebooks_api.py +++ b/IPython/html/services/notebooks/tests/test_notebooks_api.py @@ -24,7 +24,7 @@ from IPython.utils.data import uniq_stable # TODO: Remove this after we create the contents web service and directories are # no longer listed by the notebook web service. def notebooks_only(nb_list): - return [nb for nb in nb_list if 'type' not in nb] + return [nb for nb in nb_list if nb['type']=='notebook'] class NBAPI(object):