Beispiel #1
0
    def save(self, name, data, content_type=None):
        """Save and create new storage file.

        Args:
            name: relative filename
            data: string or file-like object with a
                  read(n) method.
            content_type: optional mime content type.
        Raises:
            StorageException
        """
        path = self.path(name)
        directory = os.path.dirname(path)

        if not os.path.exists(directory):
            try:
                os.makedirs(directory)
            except OSError as error:
                if error.errno != errno.EEXIST:
                    raise exception.FileOperationFailed(str(error))
            except Exception as error:
                raise exception.FileOperationFailed(str(error))

        if os.path.exists(path):
            raise exception.FileOperationFailed("'%s' already exists" % name)

        with FileSystemStorageFile(self.location, name, "w") as storage_file:
            storage_file.write(data)
Beispiel #2
0
    def write(self, data):
        """Write data to file.

        Args:
            data: string of file like object containing
                  a read(n) method.
        Raises:
            StorageException
        """
        if self.file:
            try:
                if isinstance(data, basestring):
                    self.file.write(data)
                elif hasattr(data, "read"):
                    while True:
                        chunk = data.read(4096)
                        if chunk:
                            self.file.write(chunk)
                        else:
                            break
                else:
                    raise RuntimeError("invalid data argument")
            except Exception as error:
                raise exception.FileOperationFailed(str(error))
        else:
            raise exception.FileNotOpen("file not open")
Beispiel #3
0
    def write(self, data):
        """Write data to file.

        Args:
            data: string of file like object containing
                  a read(n) method.
        Raises:
            StorageException
        """
        if self.object is None:
            raise exception.FileNotOpen("file not open")
        elif not self.file_mode.writable:
            raise exception.FileOperationNotAllowed(
                "file not opened in write mode")
        elif self.offset != 0:
            raise exception.FileOperationNotAllowed(
                "writes with non-zero offset not permitted")

        try:
            if self.content_type:
                self.object.content_type = self.content_type

            if isinstance(data, basestring):
                self.object.write(data)
            elif hasattr(data, "read"):
                self.object.write(data)
            else:
                raise RuntimeError("Invalid data argument")
            self.offset = self.object.size
        except Exception as error:
            logging.exception(error)
            raise exception.FileOperationFailed(str(error))
Beispiel #4
0
    def read(self, size=None):
        """Read size bytes from file.
        
        Args:
            size: optional number of bytes to read.
                  If not provided the entire file
                  will be read.
        Returns:
            file data as a string.
        Raises:
            StorageException
        """
        if self.object is None:
            raise exception.FileNotOpen("file not open")
        elif not self.file_mode.readable:
            raise exception.FileOperationNotAllowed(
                "file not opened in read mode")

        try:
            if self.offset >= self.size():
                result = ""
            else:
                result = self.object.read(size=size
                                          or self.size() - self.offset,
                                          offset=self.offset)
                self.offset += size or len(result)
        except Exception as error:
            logging.exception(error)
            raise exception.FileOperationFailed(str(error))
        return result
Beispiel #5
0
 def delete(self, name):
     """Delete storage file.
     Args:
         name: relative file name
     Raises:
         StorageException
     """
     try:
         location = self._name_to_location(name)
         self.container.delete_object(location)
     except Exception as error:
         logging.exception(error)
         raise exception.FileOperationFailed(str(error))
Beispiel #6
0
    def modified_time(self, name):
        """File modification utc datetime object.

        Args:
            name: relative filename
        Returns:
            file modification utc datetime object.
        Raises:
            StorageException
        """
        try:
            path = self.path(name)
            return datetime.datetime.utcfromtimestamp(os.path.getmtime(path))
        except Exception as error:
            raise exception.FileOperationFailed(str(error))
Beispiel #7
0
    def seek(self, offset, whence=0):
        """Move file pointer to specified offset.

        Args:
            offset: integer offset in bytes relative to whence
            whence: os.SEEK_SET (0) - relative to beginning of file
                    os.SEEK_CUR (1) - relative to current position
                    os.SEEK_END (2) - relative to end of file
        Raises:
            StorageException
        """
        if self.file:
            try:
                self.file.seek(offset, whence)
            except Exception as error:
                raise exception.FileOperationFailed(str(error))
        else:
            raise exception.FileNotOpen("file not open")
Beispiel #8
0
 def read(self, size=None):
     """Read size bytes from file.
     
     Args:
         size: optional number of bytes to read.
               If not provided the entire file
               will be read.
     Returns:
         file data as a string.
     Raises:
         StorageException
     """
     if self.file:
         try:
             size = size or self.size()
             return self.file.read(size)
         except Exception as error:
             raise exception.FileOperationFailed(str(error))
     else:
         raise exception.FileNotOpen("file not open")
Beispiel #9
0
    def open(self, mode=None):
        """Open/reopen file in the given mode.
        
        Args:
            mode: optional file mode, i.e. 'rb'
        Raises:
            StorageException

        Reopens the file in the specified mode if provided.
        Otherwise the file is reopened using the same mode.
        Reopening the file will reset the file pointer to
        the begining of the file.
        """
        self._mode = mode or self._mode

        if self.file:
            self.close()
        try:
            self.file = open(self.path, self._mode)
        except Exception as error:
            raise exception.FileOperationFailed(str(error))
Beispiel #10
0
    def save(self, name, data, content_type=None):
        """Save and create new storage file.

        Args:
            name: relative filename
            data: string or file-like object with a
                  read(n) method.
            content_type: optional mime content type.
        Raises:
            StorageException
        """
        if self.exists(name):
            raise exception.FileOperationFailed("'%s' already exists" % name)

        with CloudfilesStorageFile(
                container=self.container,
                name=name,
                mode="w",
                location_base=self.location_base) as storage_file:

            storage_file.write(data)