Exemplo n.º 1
0
 def open(self, path, mode='r', **kwargs):
     self._log(INFO, 'Opening file %s in mode %s' % (path, mode))        
     newfile = False
     if not self.exists(path):
         if 'w' in mode or 'a' in mode:
             newfile = True
         else:
             self._log(DEBUG, "File %s not found while opening for reads" % path)
             raise errors.ResourceNotFoundError(path)
     elif self.isdir(path):
         self._log(DEBUG, "Path %s is directory, not a file" % path)
         raise errors.ResourceInvalidError(path)
     elif 'w' in mode:
         newfile = True
     
     if newfile:
         self._log(DEBUG, 'Creating empty file %s' % path)
         if self.getmeta("read_only"):
             raise errors.UnsupportedError('read only filesystem')
         self.setcontents(path, b(''))
         handler = NullFile()
     else:
         self._log(DEBUG, 'Opening existing file %s for reading' % path)
         handler = self.getrange(path,0)
     
     return RemoteFileBuffer(self, path, mode, handler,
                 write_on_flush=False)
Exemplo n.º 2
0
    def open(self,
             path,
             mode='r',
             buffering=-1,
             encoding=None,
             errors=None,
             newline=None,
             line_buffering=False,
             **kwargs):
        if self.isdir(path):
            raise ResourceInvalidError(path)
        if 'w' in mode and not self.isdir(dirname(path)):
            raise ParentDirectoryMissingError(path)
        if 'r' in mode and not self.isfile(path):
            raise ResourceNotFoundError(path)
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
        if 'w' in mode and '+' not in mode and self.isfile(path):
            self.remove(path)

        data = ''
        if 'r' in mode:
            data = self.getcontents(path,
                                    mode=mode,
                                    encoding=encoding,
                                    errors=errors,
                                    newline=newline)
        rfile = StringIO(data=data, mode=mode)
        return RemoteFileBuffer(self, path, mode=mode, rfile=rfile)
Exemplo n.º 3
0
    def open(self, path, mode='r', buffering=-1, encoding=None,
             errors=None, newline=None, line_buffering=False, **kwargs):
        """Open the named file in the given mode.

        This method downloads the file contents into a local temporary file so
        that it can be worked on efficiently.  Any changes made to the file are
        only sent back to cloud storage when the file is flushed or closed.

        :param path: Id of the file to be opened
        :param mode: In which mode to open the file
        :raises ResourceNotFoundError: If given path doesn't exist and
            'w' is not in mode
        :return: RemoteFileBuffer object
        """
        path = self._normpath(path)
        spooled_file = SpooledTemporaryFile(mode=mode, bufsize=MAX_BUFFER)

        # Truncate the file if requested
        if "w" in mode:
            try:
                self._update(path, "")
            except:
                path = self.createfile(path, True)
        else:
            try:
                spooled_file.write(self.client.get_file(path))
                spooled_file.seek(0, 0)
            except Exception as e:
                if "w" not in mode and "a" not in mode:
                    raise ResourceNotFoundError("%r" % e)
                else:
                    path = self.createfile(path, True)
        return RemoteFileBuffer(self, path, mode, spooled_file)
Exemplo n.º 4
0
    def open(self, path, mode="rb", **kwargs):
        """Open the named file in the given mode.

        This method downloads the file contents into a local temporary
        file so that it can be worked on efficiently.  Any changes
        made to the file are only sent back to cloud storage when
        the file is flushed or closed.

        :param path: Path to the file to be opened
        :param mode: In which mode to open the file
        :raise ResourceNotFoundError: If given path doesn't exist and
            'w' is not in mode
        :return: RemoteFileBuffer object
        """
        path = abspath(normpath(path))
        spooled_file = SpooledTemporaryFile(mode=mode, bufsize=MAX_BUFFER)

        if "w" in mode:
            # Truncate the file if requested
            self.client.put_file(path, "", True)
        else:
            # Try to write to the spooled file, if path doesn't exist create it
            # if 'w' is in mode
            try:
                spooled_file.write(self.client.get_file(path).read())
                spooled_file.seek(0, 0)
            except:
                if "w" not in mode:
                    raise ResourceNotFoundError(path)
                else:
                    self.createfile(path, True)
        #  This will take care of closing the socket when it's done.
        return RemoteFileBuffer(self, path, mode, spooled_file)
Exemplo n.º 5
0
    def open(self, path, mode='r', **kwargs):
        if self.isdir(path):
            raise ResourceInvalidError(path)

        # Erase the contents of a file upon write.
        if 'w' in mode:
            file_obj = None
            self.setcontents(path, StringIO())
        else:
            file_obj = SpooledTemporaryFile()
            self._retrieveFile(path, file_obj)
        return RemoteFileBuffer(self, path, mode, file_obj)
Exemplo n.º 6
0
 def open(self, path, mode="r", **kwargs):
     mode = mode.replace("b", "").replace("t", "")
     # Truncate the file if requested
     contents = b("")
     if "w" in mode:
         self.setcontents(path, contents)
     else:
         contents = self._request(path, "GET")
         if contents.status == 404:
             # Create the file if it's missing in append mode.
             if "a" not in mode:
                 contents.close()
                 raise ResourceNotFoundError(path)
             contents = b("")
             self.setcontents(path, contents)
         elif contents.status in (401, 403):
             contents.close()
             raise PermissionDeniedError("open")
         elif contents.status != 200:
             contents.close()
             raise_generic_error(contents, "open", path)
         elif self.isdir(path):
             contents.close()
             raise ResourceInvalidError(path)
     #  For streaming reads, return the socket contents directly.
     if mode == "r-":
         contents.size = contents.getheader("Content-Length", None)
         if contents.size is not None:
             try:
                 contents.size = int(contents.size)
             except ValueError:
                 contents.size = None
         if not hasattr(contents, "__exit__"):
             contents.__enter__ = lambda *a: contents
             contents.__exit__ = lambda *a: contents.close()
         return contents
     #  For everything else, use a RemoteFileBuffer.
     #  This will take care of closing the socket when it's done.
     return RemoteFileBuffer(self, path, mode, contents)