当前位置: 首页>>代码示例>>Python>>正文


Python ChunkObject.prepare_chunk方法代码示例

本文整理汇总了Python中chunkobject.ChunkObject.prepare_chunk方法的典型用法代码示例。如果您正苦于以下问题:Python ChunkObject.prepare_chunk方法的具体用法?Python ChunkObject.prepare_chunk怎么用?Python ChunkObject.prepare_chunk使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chunkobject.ChunkObject的用法示例。


在下文中一共展示了ChunkObject.prepare_chunk方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CloudFilesFD

# 需要导入模块: from chunkobject import ChunkObject [as 别名]
# 或者: from chunkobject.ChunkObject import prepare_chunk [as 别名]
class CloudFilesFD(object):
    '''Acts like a file() object, but attached to a cloud files object'''

    def __init__(self, cffs, container, obj, mode):
        self.cffs = cffs
        self.container = container
        self.name = obj
        self.mode = mode
        self.closed = False
        self.total_size = 0
        self.stream = None

        if not all([container, obj]):
            self.closed = True
            raise IOSError(EPERM, 'Container and object requred')

        self.container = self.cffs._get_container(self.container)

        if 'r' in self.mode:
            self.obj = self.container.get_object(self.name)
            logging.debug("read fd obj.name=%r obj.size=%r" % (self.obj.name, self.obj.size))
        else: #write
            self.obj = ChunkObject(self.container, obj)
            self.obj.content_type = mimetypes.guess_type(obj)[0]
            self.obj.prepare_chunk()

    def write(self, data):
        '''Write data to the object'''
        if 'r' in self.mode:
            raise IOSError(EPERM, "Can't write to stream opened for read")
        self.obj.send_chunk(data)

    def close(self):
        '''Close the object and finish the data transfer'''
        if 'r' in self.mode:
            return
        self.obj.finish_chunk()

    def read(self, size=65536):
        '''Read data from the object.

        We can use just one request because 'seek' is not supported.
        
        NB: It uses the size passed into the first call for all subsequent calls'''
        if not self.stream:
            self.stream = self.obj.stream(size)

        logging.debug("read size=%r, total_size=%r, obj.size=%r" % (size, self.total_size, self.obj.size))
        try:
            buff = self.stream.next()
            self.total_size += len(buff)
        except StopIteration:
            return ""
        else:
            return buff

    def seek(self, *kargs, **kwargs):
        '''Seek in the object: FIXME doesn't work and raises an error'''
        logging.debug("seek args=%s, kargs=%s" % (str(kargs), str(kwargs)))
        raise IOSError(EPERM, "Seek not implemented")
开发者ID:dblundell,项目名称:ftp-cloudfs,代码行数:62,代码来源:fs.py

示例2: CloudFilesFD

# 需要导入模块: from chunkobject import ChunkObject [as 别名]
# 或者: from chunkobject.ChunkObject import prepare_chunk [as 别名]
class CloudFilesFD(object):
    """Acts like a file() object, but attached to a cloud files object"""

    def __init__(self, cffs, container, obj, mode):
        self.cffs = cffs
        self.container = container
        self.name = obj
        self.mode = mode
        self.closed = False
        self.total_size = 0

        if not all([container, obj]):
            self.closed = True
            raise IOSError(EPERM, "Container and object requred")

        self.container = self.cffs._get_container(self.container)

        if "r" in self.mode:
            self.obj = self.container.get_object(self.name)
            logging.debug("read fd obj.name=%r obj.size=%r" % (self.obj.name, self.obj.size))
        else:  # write
            self.obj = ChunkObject(self.container, obj)
            self.obj.content_type = mimetypes.guess_type(obj)[0]
            self.obj.prepare_chunk()

    def write(self, data):
        """Write data to the object"""
        if "r" in self.mode:
            raise IOSError(EPERM, "Can't write to stream opened for read")
        self.obj.send_chunk(data)

    def close(self):
        """Close the object and finish the data transfer"""
        if "r" in self.mode:
            return
        self.obj.finish_chunk()

    def read(self, size=65536):
        """Read data from the object"""
        logging.debug("read size=%r, total_size=%r, obj.size=%r" % (size, self.total_size, self.obj.size))
        readsize = size
        if (self.total_size + size) > self.obj.size:
            readsize = self.obj.size - self.total_size
        if self.total_size >= self.obj.size:
            return ""
        else:
            offset = self.total_size
            self.total_size += size
            return self.obj.read(size=readsize, offset=offset)

    def seek(self, *kargs, **kwargs):
        """Seek in the object: FIXME doesn't work and raises an error"""
        logging.debug("seek args=%s, kargs=%s" % (str(kargs), str(kwargs)))
        raise IOSError(EPERM, "Seek not implemented")
开发者ID:noodley,项目名称:ftp-cloudfs,代码行数:56,代码来源:fs.py

示例3: CloudFilesFD

# 需要导入模块: from chunkobject import ChunkObject [as 别名]
# 或者: from chunkobject.ChunkObject import prepare_chunk [as 别名]
class CloudFilesFD(object):
    '''Acts like a file() object, but attached to a cloud files object'''

    def __init__(self, cffs, container, obj, mode):
        self.cffs = cffs
        self.container = container
        self.name = obj
        self.mode = mode
        self.closed = False
        self.total_size = 0
        self.stream = None
        self.headers = dict()

        if not all([container, obj]):
            self.closed = True
            raise IOSError(EPERM, 'Container and object required')

        self.container = self.cffs._get_container(self.container)

        if 'r' in self.mode:
            self.obj = self.container.get_object(self.name)
            logging.debug("read fd obj.name=%r obj.size=%r" % (self.obj.name, self.obj.size))
        else: #write
            self.obj = ChunkObject(self.container, obj)
            self.obj.content_type = mimetypes.guess_type(obj)[0]
            self.obj.prepare_chunk()

    def write(self, data):
        '''Write data to the object'''
        if 'r' in self.mode:
            raise IOSError(EPERM, "Can't write to stream opened for read")
        self.obj.send_chunk(data)

    def close(self):
        '''Close the object and finish the data transfer'''
        if 'r' in self.mode:
            return
        self.obj.finish_chunk()

    def read(self, size=65536):
        '''Read data from the object.

        We can use just one request because 'seek' is not fully supported.

        NB: It uses the size passed into the first call for all subsequent calls'''
        if not self.stream:
            self.stream = self.obj.stream(size, hdrs=self.headers)

        logging.debug("read size=%r, total_size=%r, obj.size=%r" % (size, self.total_size, self.obj.size))
        try:
            buff = self.stream.next()
            self.total_size += len(buff)
        except StopIteration:
            return ""
        else:
            return buff

    def seek(self, offset, whence=None):
        '''Seek in the object.

        It's supported only for read operations because of the object storage limitations.'''
        logging.debug("seek offset=%s, whence=%s" % (str(offset), str(whence)))
        if 'r' in self.mode:
            if not whence:
                offs = offset
            elif whence == 1:
                offs = self.total_size + offset
            elif whence == 2:
                offs = self.obj.size - offset
            else:
                raise IOSError(EPERM, "Invalid file offset")

            if offs < 0 or offs > self.obj.size:
                raise IOSError(EPERM, "Invalid file offset")

            # we need to start over after a seek call
            if self.stream:
                self.stream = None
                self.obj = self.container.get_object(self.name)
            self.headers['Range'] = "bytes=%s-" % offs
            self.total_size = offs
        else:
            raise IOSError(EPERM, "Seek not available for write operations")
开发者ID:avp-ukr,项目名称:ftp-cloudfs,代码行数:85,代码来源:fs.py


注:本文中的chunkobject.ChunkObject.prepare_chunk方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。