# Path: nbx/nbmanager/nbxmanager.py
# class NBXContentsManager(DispatcherMixin, ContentsManager):
#     def __init__(self, *args, **kwargs):
#         super(NBXContentsManager, self).__init__(*args, **kwargs)
# 
#     def is_dir(self, path):
#         raise NotImplementedError('must be implemented in a subclass')
# 
#     def is_notebook(self, path):
#         return path.endswith('.ipynb')
# 
#     def _base_model(self, path=''):
#         """Build the common base of a contents model"""
#         # Create the base model.
#         model = {}
#         model['name'] = path.rsplit('/', 1)[-1]
#         model['path'] = path
#         model['created'] = datetime.datetime.now()
#         model['last_modified'] = datetime.datetime.now()
#         model['content'] = None
#         model['format'] = None
#         model['writable'] = None
#         model['mimetype'] = None
#         return model
# 
#     def get(self, path='', content=True, **kwargs):
#         """
#         backwards compat with get_model rename. fml.
#         Putting here instead of creating another mixin
#         """
#         return super().get(path=path, content=content, **kwargs)
# 
#     # shims to bridge Content service and older notebook apis
#     def get_dir(self, path='', content=True, **kwargs):
#         """
#         retrofit to use old list_dirs. No notebooks
#         note that this requires the dispatcher mixin
#         """
#         model = self._base_model(path)
#         fullpath = path
# 
#         model['type'] = 'directory'
#         model['format'] = 'json'
#         dirs = self.list_dirs(fullpath)
#         notebooks = self.list_notebooks(fullpath)
# 
#         files = []
#         if hasattr(self, 'list_files'):
#             files = self.list_files(fullpath)
# 
#         entries = list(dirs) + list(notebooks) + list(files)
#         model['content'] = entries
#         return model
# 
# Path: nbx/nbmanager/tagged_gist/notebook_gisthub.py
# def parse_tags(desc):
#     # real tags and not system-like tags
#     tags = _hashtags(desc)
#     if '#notebook' in tags:
#         tags.remove('#notebook')
#     if '#inactive' in tags:
#         tags.remove('#inactive')
#     return tags


import itertools
import nbformat
from tornado import web
from ..nbxmanager import NBXContentsManager
from .notebook_gisthub import parse_tags
        path : unicode
            The URL path of the notebooks directory

        Returns
        -------
        name : unicode
            A notebook name (with the .ipynb extension) that starts
            with basename and does not refer to any existing notebook.
        """
        path = path.strip('/')
        for i in itertools.count():
            name = u'{basename}{i}'.format(basename=basename, i=i)
            if not self.basename_exists(name, path):
                break
        return name

    def save_notebook(self, model, name='', path=''):
        """Save the notebook model and return the model with no content."""
        path = path.strip('/')

        if 'content' not in model:
            raise web.HTTPError(400, u'No notebook JSON data provided')

        if not path:
            raise web.HTTPError(400, u'We require path for saving.')

        nb = nbformat.from_dict(model['content'])

        gist = self._get_gist(name, path)
        if gist is None:

# Path: nbx/nbmanager/nbxmanager.py
# class NBXContentsManager(DispatcherMixin, ContentsManager):
#     def __init__(self, *args, **kwargs):
#         super(NBXContentsManager, self).__init__(*args, **kwargs)
# 
#     def is_dir(self, path):
#         raise NotImplementedError('must be implemented in a subclass')
# 
#     def is_notebook(self, path):
#         return path.endswith('.ipynb')
# 
#     def _base_model(self, path=''):
#         """Build the common base of a contents model"""
#         # Create the base model.
#         model = {}
#         model['name'] = path.rsplit('/', 1)[-1]
#         model['path'] = path
#         model['created'] = datetime.datetime.now()
#         model['last_modified'] = datetime.datetime.now()
#         model['content'] = None
#         model['format'] = None
#         model['writable'] = None
#         model['mimetype'] = None
#         return model
# 
#     def get(self, path='', content=True, **kwargs):
#         """
#         backwards compat with get_model rename. fml.
#         Putting here instead of creating another mixin
#         """
#         return super().get(path=path, content=content, **kwargs)
# 
#     # shims to bridge Content service and older notebook apis
#     def get_dir(self, path='', content=True, **kwargs):
#         """
#         retrofit to use old list_dirs. No notebooks
#         note that this requires the dispatcher mixin
#         """
#         model = self._base_model(path)
#         fullpath = path
# 
#         model['type'] = 'directory'
#         model['format'] = 'json'
#         dirs = self.list_dirs(fullpath)
#         notebooks = self.list_notebooks(fullpath)
# 
#         files = []
#         if hasattr(self, 'list_files'):
#             files = self.list_files(fullpath)
# 
#         entries = list(dirs) + list(notebooks) + list(files)
#         model['content'] = entries
#         return model
# 
# Path: nbx/nbmanager/tagged_gist/notebook_gisthub.py
# def parse_tags(desc):
#     # real tags and not system-like tags
#     tags = _hashtags(desc)
#     if '#notebook' in tags:
#         tags.remove('#notebook')
#     if '#inactive' in tags:
#         tags.remove('#inactive')
#     return tags


# Path: nbx/nbmanager/tagged_gist/gistnbmanager.py
import itertools
import nbformat
from tornado import web
from ..nbxmanager import NBXContentsManager
from .notebook_gisthub import parse_tags

        path : unicode
            The URL path of the notebooks directory

        Returns
        -------
        name : unicode
            A notebook name (with the .ipynb extension) that starts
            with basename and does not refer to any existing notebook.
        """
        path = path.strip('/')
        for i in itertools.count():
            name = u'{basename}{i}'.format(basename=basename, i=i)
            if not self.basename_exists(name, path):
                break
        return name

    def save_notebook(self, model, name='', path=''):
        """Save the notebook model and return the model with no content."""
        path = path.strip('/')

        if 'content' not in model:
            raise web.HTTPError(400, u'No notebook JSON data provided')

        if not path:
            raise web.HTTPError(400, u'We require path for saving.')

        nb = nbformat.from_dict(model['content'])

        gist = self._get_gist(name, path)
        if gist is None:

            tags = parse_tags(name)