本文整理汇总了Python中chunkobject.ChunkObject.next方法的典型用法代码示例。如果您正苦于以下问题:Python ChunkObject.next方法的具体用法?Python ChunkObject.next怎么用?Python ChunkObject.next使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chunkobject.ChunkObject
的用法示例。
在下文中一共展示了ChunkObject.next方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ObjectStorageFD
# 需要导入模块: from chunkobject import ChunkObject [as 别名]
# 或者: from chunkobject.ChunkObject import next [as 别名]
class ObjectStorageFD(object):
"""File alike object attached to the Object Storage."""
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.headers = dict()
self.obj = None
# this is only used by `seek`, so we delay the HEAD request until is required
self.size = None
if not all([container, obj]):
self.closed = True
raise IOSError(EPERM, 'Container and object required')
logging.debug("ObjectStorageFD object: %r (mode: %r)" % (obj, mode))
if 'r' in self.mode:
logging.debug("read fd %r" % self.name)
else: # write
logging.debug("write fd %r" % self.name)
self.obj = ChunkObject(self.conn, self.container, self.name, content_type=mimetypes.guess_type(self.name)[0])
@property
def conn(self):
"""Connection to the storage."""
return self.cffs.conn
def write(self, data):
"""Write data to the object."""
if 'r' in self.mode:
raise IOSError(EPERM, "File is 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 self.obj is None:
if self.total_size > 0:
self.conn.range_from = self.total_size
# we need to open a new connection to inject the `Range` header
if self.conn.http_conn:
self.conn.http_conn[1].close()
self.conn.http_conn = None
_, self.obj = self.conn.get_object(self.container, self.name, resp_chunk_size=size)
logging.debug("read size=%r, total_size=%r (range_from: %s)" % (size,
self.total_size, self.conn.range_from))
try:
buff = self.obj.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 object storage limitations.
"""
logging.debug("seek offset=%s, whence=%s" % (str(offset), str(whence)))
if 'r' in self.mode:
if self.size is None:
meta = self.conn.head_object(self.container, self.name)
try:
self.size = int(meta["content-length"])
except ValueError:
raise IOSError(EPERM, "Invalid file size")
if not whence:
offs = offset
elif whence == 1:
offs = self.total_size + offset
elif whence == 2:
offs = self.size - offset
else:
raise IOSError(EPERM, "Invalid file offset")
if offs < 0 or offs > self.size:
#.........这里部分代码省略.........
示例2: ObjectStorageFD
# 需要导入模块: from chunkobject import ChunkObject [as 别名]
# 或者: from chunkobject.ChunkObject import next [as 别名]
#.........这里部分代码省略.........
current_size = self.split_size-self.part_size
logging.debug("data is to large (%r), using %s" % (len(data), current_size))
else:
current_size = len(data)-offs
self.part_size += current_size
if not self.obj:
self.obj = ChunkObject(self.conn, self.container, self.part_name, content_type=self.content_type)
self.obj.send_chunk(data[offs:offs+current_size])
offs += current_size
if self.part_size == self.split_size:
logging.debug("current size is %r, split_file is %r" % (self.part_size, self.split_size))
self.obj.finish_chunk()
# this obj is not valid anymore, will create a new one if a new part is required
self.obj = None
# make it the first part
if self.part == 0:
self._start_copy_task()
self.part_size = 0
self.part += 1
else:
self.obj.send_chunk(data)
@translate_objectstorage_error
def close(self):
"""Close the object and finish the data transfer."""
if 'r' in self.mode:
return
if self.pending_copy_task:
logging.debug("waiting for a pending copy task...")
self.pending_copy_task.join()
logging.debug("wait is over")
if self.pending_copy_task.exitcode != 0:
raise IOSError(EIO, 'Failed to store the file')
if self.obj is not None:
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 self.obj is None:
if self.total_size > 0:
self.conn.range_from = self.total_size
# we need to open a new connection to inject the `Range` header
if self.conn.http_conn:
self.conn.http_conn[1].close()
self.conn.http_conn = None
_, self.obj = self.conn.get_object(self.container, self.name, resp_chunk_size=size)
logging.debug("read size=%r, total_size=%r (range_from: %s)" % (size,
self.total_size, self.conn.range_from))
try:
buff = self.obj.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 object storage limitations.
"""
logging.debug("seek offset=%s, whence=%s" % (str(offset), str(whence)))
if 'r' in self.mode:
if self.size is None:
meta = self.conn.head_object(self.container, self.name)
try:
self.size = int(meta["content-length"])
except ValueError:
raise IOSError(EPERM, "Invalid file size")
if not whence:
offs = offset
elif whence == 1:
offs = self.total_size + offset
elif whence == 2:
offs = self.size - offset
else:
raise IOSError(EPERM, "Invalid file offset")
if offs < 0 or offs > self.size:
raise IOSError(EPERM, "Invalid file offset")
# we need to start over after a seek call
if self.obj is not None:
del self.obj # GC the generator
self.obj = None
self.total_size = offs
else:
raise IOSError(EPERM, "Seek not available for write operations")