Module libnova.common.filesystem.S3.FileStream

Expand source code
#!/usr/bin/env python
# coding: utf-8

import io


class FileStream(io.RawIOBase):
    """A class to provide a Stream based on an S3 Object

    Not used currently, but kept to provide alternatives to current streaming method

    Args:
        s3_object (S3.Object): An S3 Object object

    Returns:
        FileSTream: A seekable stream to read data from the object
    """

    def __init__(self, s3_object):
        self.s3_object = s3_object
        self.position  = 0

    def __repr__(self):
        return "<%s s3_object=%r>" % (type(self).__name__, self.s3_object)

    @property
    def size(self):
        return self.s3_object.content_length

    def tell(self):
        return self.position

    def seek(self, offset, whence=io.SEEK_SET):
        if whence == io.SEEK_SET:
            self.position = offset
        elif whence == io.SEEK_CUR:
            self.position += offset
        elif whence == io.SEEK_END:
            self.position = self.size + offset
        else:
            raise ValueError("invalid whence (%r, should be %d, %d, %d)" % (
                whence, io.SEEK_SET, io.SEEK_CUR, io.SEEK_END
            ))

        return self.position

    def seekable(self):
        return True

    def read(self, size=-1):
        try:
            if size == -1:
                # Read to the end of the file
                range_header = "bytes=%d-" % self.position
                self.seek(offset=0, whence=io.SEEK_END)
            else:
                new_position = self.position + size

                # If we're going to read beyond the end of the object, return
                # the entire object.
                if new_position >= self.size:
                    return self.read()

                range_header = "bytes=%d-%d" % (self.position, new_position - 1)
                self.seek(offset=size, whence=io.SEEK_CUR)

            return self.s3_object.get(Range=range_header)["Body"].read()
        except Exception as e:
            pass
        return False

    def readable(self):
        return True

Classes

class FileStream (s3_object)

A class to provide a Stream based on an S3 Object

Not used currently, but kept to provide alternatives to current streaming method

Args

s3_object : S3.Object
An S3 Object object

Returns

FileSTream
A seekable stream to read data from the object
Expand source code
class FileStream(io.RawIOBase):
    """A class to provide a Stream based on an S3 Object

    Not used currently, but kept to provide alternatives to current streaming method

    Args:
        s3_object (S3.Object): An S3 Object object

    Returns:
        FileSTream: A seekable stream to read data from the object
    """

    def __init__(self, s3_object):
        self.s3_object = s3_object
        self.position  = 0

    def __repr__(self):
        return "<%s s3_object=%r>" % (type(self).__name__, self.s3_object)

    @property
    def size(self):
        return self.s3_object.content_length

    def tell(self):
        return self.position

    def seek(self, offset, whence=io.SEEK_SET):
        if whence == io.SEEK_SET:
            self.position = offset
        elif whence == io.SEEK_CUR:
            self.position += offset
        elif whence == io.SEEK_END:
            self.position = self.size + offset
        else:
            raise ValueError("invalid whence (%r, should be %d, %d, %d)" % (
                whence, io.SEEK_SET, io.SEEK_CUR, io.SEEK_END
            ))

        return self.position

    def seekable(self):
        return True

    def read(self, size=-1):
        try:
            if size == -1:
                # Read to the end of the file
                range_header = "bytes=%d-" % self.position
                self.seek(offset=0, whence=io.SEEK_END)
            else:
                new_position = self.position + size

                # If we're going to read beyond the end of the object, return
                # the entire object.
                if new_position >= self.size:
                    return self.read()

                range_header = "bytes=%d-%d" % (self.position, new_position - 1)
                self.seek(offset=size, whence=io.SEEK_CUR)

            return self.s3_object.get(Range=range_header)["Body"].read()
        except Exception as e:
            pass
        return False

    def readable(self):
        return True

Ancestors

  • io.RawIOBase
  • _io._RawIOBase
  • io.IOBase
  • _io._IOBase

Instance variables

var size
Expand source code
@property
def size(self):
    return self.s3_object.content_length

Methods

def read(self, size=-1)
Expand source code
def read(self, size=-1):
    try:
        if size == -1:
            # Read to the end of the file
            range_header = "bytes=%d-" % self.position
            self.seek(offset=0, whence=io.SEEK_END)
        else:
            new_position = self.position + size

            # If we're going to read beyond the end of the object, return
            # the entire object.
            if new_position >= self.size:
                return self.read()

            range_header = "bytes=%d-%d" % (self.position, new_position - 1)
            self.seek(offset=size, whence=io.SEEK_CUR)

        return self.s3_object.get(Range=range_header)["Body"].read()
    except Exception as e:
        pass
    return False
def readable(self)

Return whether object was opened for reading.

If False, read() will raise OSError.

Expand source code
def readable(self):
    return True
def seek(self, offset, whence=0)

Change stream position.

Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence. Values for whence are:

  • 0 – start of stream (the default); offset should be zero or positive
  • 1 – current stream position; offset may be negative
  • 2 – end of stream; offset is usually negative

Return the new absolute position.

Expand source code
def seek(self, offset, whence=io.SEEK_SET):
    if whence == io.SEEK_SET:
        self.position = offset
    elif whence == io.SEEK_CUR:
        self.position += offset
    elif whence == io.SEEK_END:
        self.position = self.size + offset
    else:
        raise ValueError("invalid whence (%r, should be %d, %d, %d)" % (
            whence, io.SEEK_SET, io.SEEK_CUR, io.SEEK_END
        ))

    return self.position
def seekable(self)

Return whether object supports random access.

If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().

Expand source code
def seekable(self):
    return True
def tell(self)

Return current stream position.

Expand source code
def tell(self):
    return self.position