Beispiel #1
0
class MixedContentsManager(ContentsManager):
    DRIVE_PATH_SENTINEL = 'gdrive'

    def __init__(self, **kwargs):
        self.file_contents_manager = FileContentsManager()
        self.client_side_contents_manager = ClientSideContentsManager()

    def is_drive_path(self, path):
        components = path.split('/')
        return components and components[0] == self.DRIVE_PATH_SENTINEL

    # ContentsManager API part 1: methods that must be
    # implemented in subclasses.

    def dir_exists(self, path):
        if self.is_drive_path(path):
            return self.client_side_contents_manager.dir_exists(path)
        return self.file_contents_manager.dir_exists(path)

    def is_hidden(self, path):
        if self.is_drive_path(path):
            return self.client_side_contents_manager.is_hidden(path)
        return self.file_contents_manager.is_hidden(path)

    def file_exists(self, path=''):
        if self.is_drive_path(path):
            return self.client_side_contents_manager.file_exists(path)
        return self.file_contents_manager.file_exists(path)

    def exists(self, path):
        return self.file_contents_manager.exists(path)

    def get(self, path, **kwargs):
        if self.is_drive_path(path):
            return self.client_side_contents_manager.get(path, **kwargs)
        return self.file_contents_manager.get(path, **kwargs)

    def save(self, model, path):
        return self.file_contents_manager.save(model, path)

    def update(self, model, path):
        return self.file_contents_manager.update(model, path)

    def delete(self, path):
        return self.file_contents_manager.delete(path)

    def create_checkpoint(self, path):
        return self.file_contents_manager.create_checkpoint(path)

    def list_checkpoints(self, path):
        return self.file_contents_manager.list_checkpoints(path)

    def restore_checkpoint(self, checkpoint_id, path):
        return self.file_contents_manager.restore_checkpoint(
            checkpoint_id, path)

    def delete_checkpoint(self, checkpoint_id, path):
        return self.file_contents_manager.delete_checkpoint(
            checkpoint_id, path)
Beispiel #2
0
class BackwardsFileContentsManager(BackwardsCompatMixin, NBXContentsManager):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.filemanager = FileContentsManager(*args, **kwargs)

    def get_notebook(self, name, path='', content=True, **kwargs):
        model = self.get(name, path, content=content, **kwargs)
        # note, since this is calling IPython's .get, the model
        # path will be the newer full path. reset it since we're
        # translating back in MetaManager.
        model['path'] = path
        return model

    def get_model_dir(self, name, path='', content=True, **kwargs):
        model = self.get(name, path, content=content, **kwargs)
        for nb in model['content']:
            # reset the model back to pre v4 name/path
            nb['name'] = nb['path'].rsplit('/')[-1]
            nb['path'] = path
        return model

    def notebook_exists(self, name, path=''):
        return self.file_exists(name, path) and name.endswith('.ipynb')

    # this is dumb, quite literally just translating back

    def is_hidden(self, path):
        return self.filemanager.is_hidden(path)

    def get(self, name, path, **kwargs):
        path = _fullpath(name, path)
        model =  self.filemanager.get(path, **kwargs)
        return model

    def save(self, model, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.save(model, path)

    def update(self, model, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.update(model, path)

    def delete(self, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.delete(path)

    def rename(self, old_name, old_path, new_name, new_path):
        path = _fullpath(name, path)
        return self.filemanager.rename(old_path, new_path)

    def file_exists(self, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.file_exists(path)

    def path_exists(self, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.exists(path)

    def exists(self, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.exists(path)

    # checkpoint stuff
    def get_checkpoint_path(self, checkpoint_id, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.get_checkpoint_path(checkpoint_id, path)

    def get_checkpoint_model(self, checkpoint_id, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.get_checkpoint_model(checkpoint_id, path)

    def create_checkpoint(self, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.create_checkpoint(path)

    def list_checkpoints(self, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.list_checkpoints(path)

    def restore_checkpoint(self, checkpoint_id, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.restore_checkpoint(checkpoint_id, path)

    def delete_checkpoint(self, checkpoint_id, name, path=''):
        path = _fullpath(name, path)
        return self.filemanager.delete_checkpoint(checkpoint_id, path)

    def get_kernel_path(self, name, path='', model=None):
        path = _fullpath(name, path)
        return self.filemanager.get_kernel_path(path, model=model)