355
355
_check_name_encoding(name)
359
class ContainerPushParser(object):
363
self._state_handler = self._state_expecting_format_line
364
self._parsed_records = []
365
self._reset_current_record()
367
def _reset_current_record(self):
368
self._current_record_length = None
369
self._current_record_names = []
371
def accept_bytes(self, bytes):
372
self._buffer += bytes
373
# Keep iterating the state machine until it stops consuming bytes from
376
from bzrlib.trace import mutter
377
while len(self._buffer) != buffer_length:
378
mutter('state: %r, buffer: %r', self._buffer, self._state_handler)
379
buffer_length = len(self._buffer)
380
self._state_handler()
382
def read_pending_records(self):
383
records = self._parsed_records
384
self._parsed_records = []
387
def _consume_until_byte(self, byte):
388
"""Take all bytes up to the given out of the buffer, and return it.
390
If the specified byte is not found in the buffer, the buffer is
391
unchanged and this returns None instead.
393
newline_pos = self._buffer.find('\n')
394
if newline_pos != -1:
395
line = self._buffer[:newline_pos]
396
self._buffer = self._buffer[newline_pos+1:]
401
def _consume_line(self):
402
return self._consume_until_byte('\n')
404
def _state_expecting_format_line(self):
405
line = self._consume_line()
407
if line != FORMAT_ONE:
408
raise errors.UnknownContainerFormatError(line)
409
self._state_handler = self._state_expecting_record_type
411
def _state_expecting_record_type(self):
412
if len(self._buffer) >= 1:
413
record_type = self._buffer[0]
414
self._buffer = self._buffer[1:]
415
if record_type != 'B':
416
raise NotImplementedError('XXX')
417
self._state_handler = self._state_expecting_length
419
def _state_expecting_length(self):
420
line = self._consume_line()
423
self._current_record_length = int(line)
425
raise errors.InvalidRecordError(
426
"%r is not a valid length." % (line,))
427
self._state_handler = self._state_expecting_name
429
def _state_expecting_name(self):
430
encoded_name_parts = self._consume_line()
431
if encoded_name_parts is not None:
432
if encoded_name_parts == '':
433
self._state_handler = self._state_expecting_body
435
name_parts = tuple(encoded_name_parts.split('\x00'))
436
for name_part in name_parts:
437
_check_name(name_part)
438
self._current_record_names.append(name_parts)
440
def _state_expecting_body(self):
441
if len(self._buffer) >= self._current_record_length:
442
body_bytes = self._buffer[:self._current_record_length]
443
self._buffer = self._buffer[self._current_record_length:]
444
record = (self._current_record_names, body_bytes)
445
self._parsed_records.append(record)
446
self._reset_current_record()
447
self._state_handler = self._state_expecting_record_type