260
class FileStream(object):
261
"""Base class for FileStreams."""
263
def __init__(self, transport, relpath):
264
"""Create a FileStream for relpath on transport."""
265
self.transport = transport
266
self.relpath = relpath
269
"""A hook point for subclasses that need to take action on close."""
273
del _file_streams[self.transport.abspath(self.relpath)]
276
class FileFileStream(FileStream):
277
"""A file stream object returned by open_write_stream.
279
This version uses a file like object to perform writes.
282
def __init__(self, transport, relpath, file_handle):
283
FileStream.__init__(self, transport, relpath)
284
self.file_handle = file_handle
287
self.file_handle.close()
289
def write(self, bytes):
290
self.file_handle.write(bytes)
293
class AppendBasedFileStream(FileStream):
294
"""A file stream object returned by open_write_stream.
296
This version uses append on a transport to perform writes.
299
def write(self, bytes):
300
self.transport.append_bytes(self.relpath, bytes)
260
303
class Transport(object):
261
304
"""This class encapsulates methods for retrieving or putting a file
262
305
from/to a storage location.
811
854
self.mkdir(path, mode=mode)
812
855
return len(self._iterate_over(relpaths, mkdir, pb, 'mkdir', expand=False))
814
def open_file_stream(self, relpath, mode=None):
815
"""Open a file stream at relpath.
817
A file stream is a callback which adds data to the file. Buffering
818
may occur internally until the stream is closed with close_file_stream.
819
Calls to readv or the get_* methods will be synchronised with any
820
internal buffering that may be present.
822
:seealso: close_file_stream.
857
def open_write_stream(self, relpath, mode=None):
858
"""Open a writable file stream at relpath.
860
A file stream is a file like object with a write() method that accepts
861
bytes to write.. Buffering may occur internally until the stream is
862
closed with stream.close(). Calls to readv or the get_* methods will
863
be synchronised with any internal buffering that may be present.
823
865
:param relpath: The relative path to the file.
824
866
:param mode: The mode for the newly created file,
825
867
None means just use the default
826
:return: A write callback to add data to the file.
868
:return: A FileStream. FileStream objects have two methods, write() and
869
close(). There is no guarantee that data is committed to the file
870
if close() has not been called (even if get() is called on the same
828
raise NotImplementedError(self.open_file_stream)
873
raise NotImplementedError(self.open_write_stream)
830
875
def append_file(self, relpath, f, mode=None):
831
876
"""Append bytes from a file-like object to a file at relpath.