~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/pack.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Container format for Bazaar data.
18
18
 
34
34
 
35
35
def _check_name(name):
36
36
    """Do some basic checking of 'name'.
37
 
 
 
37
    
38
38
    At the moment, this just checks that there are no whitespace characters in a
39
39
    name.
40
40
 
47
47
 
48
48
def _check_name_encoding(name):
49
49
    """Check that 'name' is valid UTF-8.
50
 
 
 
50
    
51
51
    This is separate from _check_name because UTF-8 decoding is relatively
52
52
    expensive, and we usually want to avoid it.
53
53
 
61
61
 
62
62
class ContainerSerialiser(object):
63
63
    """A helper class for serialising containers.
64
 
 
 
64
    
65
65
    It simply returns bytes from method calls to 'begin', 'end' and
66
66
    'bytes_record'.  You may find ContainerWriter to be a more convenient
67
67
    interface.
138
138
 
139
139
    def add_bytes_record(self, bytes, names):
140
140
        """Add a Bytes record with the given names.
141
 
 
 
141
        
142
142
        :param bytes: The bytes to insert.
143
143
        :param names: The names to give the inserted bytes. Each name is
144
144
            a tuple of bytestrings. The bytestrings may not contain
234
234
        is a ``list`` and bytes is a function that takes one argument,
235
235
        ``max_length``.
236
236
 
237
 
        You **must not** call the callable after advancing the iterator to the
 
237
        You **must not** call the callable after advancing the interator to the
238
238
        next record.  That is, this code is invalid::
239
239
 
240
240
            record_iter = container.iter_records()
241
241
            names1, callable1 = record_iter.next()
242
242
            names2, callable2 = record_iter.next()
243
243
            bytes1 = callable1(None)
244
 
 
 
244
        
245
245
        As it will give incorrect results and invalidate the state of the
246
246
        ContainerReader.
247
247
 
248
 
        :raises ContainerError: if any sort of container corruption is
 
248
        :raises ContainerError: if any sort of containter corruption is
249
249
            detected, e.g. UnknownContainerFormatError is the format of the
250
250
            container is unrecognised.
251
251
        :seealso: ContainerReader.read
252
252
        """
253
253
        self._read_format()
254
254
        return self._iter_records()
255
 
 
 
255
    
256
256
    def iter_record_objects(self):
257
257
        """Iterate over the container, yielding each record as it is read.
258
258
 
260
260
        methods.  Like with iter_records, it is not safe to use a record object
261
261
        after advancing the iterator to yield next record.
262
262
 
263
 
        :raises ContainerError: if any sort of container corruption is
 
263
        :raises ContainerError: if any sort of containter corruption is
264
264
            detected, e.g. UnknownContainerFormatError is the format of the
265
265
            container is unrecognised.
266
266
        :seealso: iter_records
267
267
        """
268
268
        self._read_format()
269
269
        return self._iter_record_objects()
270
 
 
 
270
    
271
271
    def _iter_records(self):
272
272
        for record in self._iter_record_objects():
273
273
            yield record.read()
342
342
        except ValueError:
343
343
            raise errors.InvalidRecordError(
344
344
                "%r is not a valid length." % (length_line,))
345
 
 
 
345
        
346
346
        # Read the list of names.
347
347
        names = []
348
348
        while True:
411
411
            self._state_handler()
412
412
            cur_buffer_length = len(self._buffer)
413
413
 
414
 
    def read_pending_records(self, max=None):
415
 
        if max:
416
 
            records = self._parsed_records[:max]
417
 
            del self._parsed_records[:max]
418
 
            return records
419
 
        else:
420
 
            records = self._parsed_records
421
 
            self._parsed_records = []
422
 
            return records
423
 
 
 
414
    def read_pending_records(self):
 
415
        records = self._parsed_records
 
416
        self._parsed_records = []
 
417
        return records
 
418
    
424
419
    def _consume_line(self):
425
420
        """Take a line out of the buffer, and return the line.
426
421
 
473
468
            for name_part in name_parts:
474
469
                _check_name(name_part)
475
470
            self._current_record_names.append(name_parts)
476
 
 
 
471
            
477
472
    def _state_expecting_body(self):
478
473
        if len(self._buffer) >= self._current_record_length:
479
474
            body_bytes = self._buffer[:self._current_record_length]