/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/__init__.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
from bzrlib import registry
67
67
 
68
68
 
 
69
# a dictionary of open file streams. Keys are absolute paths, values are
 
70
# transport defined.
 
71
_file_streams = {}
 
72
 
 
73
 
69
74
def _get_protocol_handlers():
70
75
    """Return a dictionary of {urlprefix: [factory]}"""
71
76
    return transport_list_registry
252
257
        self._fail()
253
258
 
254
259
 
 
260
class FileStream(object):
 
261
    """Base class for FileStreams."""
 
262
 
 
263
    def __init__(self, transport, relpath):
 
264
        """Create a FileStream for relpath on transport."""
 
265
        self.transport = transport
 
266
        self.relpath = relpath
 
267
 
 
268
    def _close(self):
 
269
        """A hook point for subclasses that need to take action on close."""
 
270
 
 
271
    def close(self):
 
272
        self._close()
 
273
        del _file_streams[self.transport.abspath(self.relpath)]
 
274
 
 
275
 
 
276
class FileFileStream(FileStream):
 
277
    """A file stream object returned by open_write_stream.
 
278
    
 
279
    This version uses a file like object to perform writes.
 
280
    """
 
281
 
 
282
    def __init__(self, transport, relpath, file_handle):
 
283
        FileStream.__init__(self, transport, relpath)
 
284
        self.file_handle = file_handle
 
285
 
 
286
    def _close(self):
 
287
        self.file_handle.close()
 
288
 
 
289
    def write(self, bytes):
 
290
        self.file_handle.write(bytes)
 
291
 
 
292
 
 
293
class AppendBasedFileStream(FileStream):
 
294
    """A file stream object returned by open_write_stream.
 
295
    
 
296
    This version uses append on a transport to perform writes.
 
297
    """
 
298
 
 
299
    def write(self, bytes):
 
300
        self.transport.append_bytes(self.relpath, bytes)
 
301
 
 
302
 
255
303
class Transport(object):
256
304
    """This class encapsulates methods for retrieving or putting a file
257
305
    from/to a storage location.
455
503
            path = '/' + path
456
504
        return path
457
505
 
 
506
    def recommended_page_size(self):
 
507
        """Return the recommended page size for this transport.
 
508
 
 
509
        This is potentially different for every path in a given namespace.
 
510
        For example, local transports might use an operating system call to 
 
511
        get the block size for a given path, which can vary due to mount
 
512
        points.
 
513
 
 
514
        :return: The page size in bytes.
 
515
        """
 
516
        return 4 * 1024
 
517
 
458
518
    def relpath(self, abspath):
459
519
        """Return the local path portion from a given absolute path.
460
520
 
785
845
            self.mkdir(path, mode=mode)
786
846
        return len(self._iterate_over(relpaths, mkdir, pb, 'mkdir', expand=False))
787
847
 
 
848
    def open_write_stream(self, relpath, mode=None):
 
849
        """Open a writable file stream at relpath.
 
850
 
 
851
        A file stream is a file like object with a write() method that accepts
 
852
        bytes to write.. Buffering may occur internally until the stream is
 
853
        closed with stream.close().  Calls to readv or the get_* methods will
 
854
        be synchronised with any internal buffering that may be present.
 
855
 
 
856
        :param relpath: The relative path to the file.
 
857
        :param mode: The mode for the newly created file, 
 
858
                     None means just use the default
 
859
        :return: A FileStream. FileStream objects have two methods, write() and
 
860
            close(). There is no guarantee that data is committed to the file
 
861
            if close() has not been called (even if get() is called on the same
 
862
            path).
 
863
        """
 
864
        raise NotImplementedError(self.open_write_stream)
 
865
 
788
866
    def append_file(self, relpath, f, mode=None):
789
867
        """Append bytes from a file-like object to a file at relpath.
790
868