~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: John Arbash Meinel
  • Date: 2005-07-11 22:24:03 UTC
  • mto: (1185.11.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050711222403-5dc86e254b69f7ab
Remote functionality work.
Added lock_read/write() to Transport.
Turns out that both urllib2 and urlgrabber don't let you
seek on files that are returned, and GzipFile wants to
seek on the file when it is read. So instead,
we check, and wrap in a StringIO when required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
class Storage(object):
36
36
    """This class represents the abstract storage layout for saving information.
37
37
    """
 
38
    _transport = None
 
39
    _max_buffered_requests = 10
 
40
 
38
41
    def __init__(self, transport):
 
42
        from transport import Transport
 
43
        assert isinstance(transport, Transport)
39
44
        self._transport = transport
40
 
        self._max_buffered_requests = 10
41
45
 
42
46
    def __repr__(self):
43
 
        return "%s(%r)" % (self.__class__.__name__, self._transport.base)
 
47
        if self._transport is None:
 
48
            return "%s(None)" % (self.__class__.__name__)
 
49
        else:
 
50
            return "%s(%r)" % (self.__class__.__name__, self._transport.base)
44
51
 
45
52
    __str__ = __repr__
46
53
 
167
174
        self._check_fileid(fileid)
168
175
        return fileid + '.gz'
169
176
 
170
 
    def __repr__(self):
171
 
        return "%s(%r)" % (self.__class__.__name__, self._transport.base)
172
 
 
173
177
    def add(self, fileid, f):
174
178
        """Add contents of a file into the store.
175
179
 
196
200
        gf = gzip.GzipFile(mode='wb', fileobj=sio)
197
201
        # if pumpfile handles files that don't fit in ram,
198
202
        # so will this function
199
 
        pumpfile(f, gf)
 
203
        if isinstance(f, basestring):
 
204
            gf.write(f)
 
205
        else:
 
206
            pumpfile(f, gf)
200
207
        gf.close()
201
208
        sio.seek(0)
202
209
        self._transport.put(fn, sio)
241
248
        """Returns a file reading from a particular entry."""
242
249
        fn = self._relpath(fileid)
243
250
        f = self._transport.get(fn)
244
 
        return gzip.GzipFile(mode='rb', fileobj=f)
 
251
 
 
252
        # gzip.GzipFile.read() requires a tell() function
 
253
        # but some transports return objects that cannot seek
 
254
        # so buffer them in a StringIO instead
 
255
        if hasattr(f, 'tell'):
 
256
            return gzip.GzipFile(mode='rb', fileobj=f)
 
257
        else:
 
258
            from cStringIO import StringIO
 
259
            sio = StringIO(f.read())
 
260
            return gzip.GzipFile(mode='rb', fileobj=sio)
 
261
            
245
262
 
246
263
    def total_size(self):
247
264
        """Return (count, bytes)