예제 #1
0
    def __init__(self, **kwargs):
        self.resources = []

        self._default_cm = ('', LargeFileManager(**kwargs))

        self._managers = dict([self._default_cm])

        # remove kwargs not relevant to pyfs
        kwargs.pop('parent')
        kwargs.pop('log')
        self._pyfs_kw = kwargs
    def save(self, model, path=''):
        """ Save the file model and return the model with no content """

        chunk = model.get('chunk', None)
        if chunk is not None:
            return LargeFileManager.save(self, model, path)

        if 'type' not in model:
            raise web.HTTPError(400, u'No file type provided')
        if 'content' not in model and model['type'] != 'directory' and model[
                'type'] != 'project':
            raise web.HTTPError(400, u'No file content provided')

        path = path.strip('/')
        os_path = self._get_os_path(path)

        if self._contains_swan_folder_name(os_path):
            raise web.HTTPError(
                400, "The name %s is restricted" % self.swan_default_folder)

        self.log.debug("Saving %s", os_path)
        self.run_pre_save_hook(model=model, path=path)

        try:
            if model['type'] == 'project':
                if not self._is_swan_root_folder(os_path):
                    raise web.HTTPError(
                        400,
                        "You can only create projects inside Swan Projects")
                self._save_project(os_path, model, path)

            else:
                if model['type'] == 'notebook':
                    nb = nbformat.from_dict(model['content'])
                    self.check_and_sign(nb, path)
                    self._save_notebook(os_path, nb)
                    # One checkpoint should always exist for notebooks.
                    if not self.checkpoints.list_checkpoints(path):
                        self.create_checkpoint(path)

                elif model['type'] == 'file':
                    # Missing format will be handled internally by _save_file.
                    self._save_file(os_path, model['content'],
                                    model.get('format'))

                elif model['type'] == 'directory':
                    self._save_directory(os_path, model, path)

                else:
                    raise web.HTTPError(
                        400, "Unhandled contents type: %s" % model['type'])

        except web.HTTPError:
            raise

        except Exception as e:
            self.log.error(u'Error while saving file: %s %s',
                           path,
                           e,
                           exc_info=True)
            raise web.HTTPError(
                500, u'Unexpected error while saving file: %s %s' % (path, e))

        validation_message = None
        if model['type'] == 'notebook':
            self.validate_notebook_model(model)
            validation_message = model.get('message', None)

        model = self.get(path, content=False)
        if validation_message:
            model['message'] = validation_message

        self.run_post_save_hook(model=model, os_path=os_path)

        return model
예제 #3
0
 def __init__(self, **kwargs):
     self._contents_managers = {'': LargeFileManager(**kwargs)}
     self._kwargs = kwargs
     self._inited = False