~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revfile.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-09 02:59:08 UTC
  • Revision ID: mbp@sourcefrog.net-20050409025908-fa3e0ffaf69abbaacb5c71e0
revfile: fix up __getitem__ to allow simple iteration

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/env python
2
2
 
3
 
# (C) 2005 Matt Mackall
 
3
# (C) 2005 Canonical Ltd
4
4
 
 
5
# based on an idea by Matt Mackall
5
6
# modified to squish into bzr by Martin Pool
6
7
 
7
8
# This program is free software; you can redistribute it and/or modify
154
155
    def __len__(self):
155
156
        return int(self.last_idx())
156
157
 
 
158
 
157
159
    def __getitem__(self, idx):
158
160
        self.idxfile.seek((idx + 1) * _RECORDSIZE)
159
161
        rec = self.idxfile.read(_RECORDSIZE)
160
 
        if len(rec) != _RECORDSIZE:
 
162
        if not rec:
 
163
            raise IndexError("no record %d in index for %r" % (idx, self.basename))
 
164
        elif len(rec) != _RECORDSIZE:
161
165
            raise RevfileError("short read of %d bytes getting index %d from %r"
162
166
                               % (len(rec), idx, self.basename))
163
167
        return struct.unpack(">20sIIII12x", rec)
192
196
        f.write('-------- ---------------------------------------- ')
193
197
        f.write('-------- -------- -------- --------\n')
194
198
 
195
 
        for i in range(len(self)):
196
 
            rec = self[i]
 
199
        for i, rec in enumerate(self):
197
200
            f.write("#%-7d %40s " % (i, hexlify(rec[0])))
198
201
            if rec[1] == _NO_BASE:
199
202
                f.write("(none)   ")