Given a generator which yields strings and a splitter function, joins all input, splits on the separator and yields each chunk. Unlike string.split(), each chunk includes the trailing separator, except for the last one if none was found on the end of the input.
(stream, splitter=None, decoder=lambda a: a)
| 48 | |
| 49 | |
| 50 | def split_buffer(stream, splitter=None, decoder=lambda a: a): |
| 51 | """Given a generator which yields strings and a splitter function, |
| 52 | joins all input, splits on the separator and yields each chunk. |
| 53 | Unlike string.split(), each chunk includes the trailing |
| 54 | separator, except for the last one if none was found on the end |
| 55 | of the input. |
| 56 | """ |
| 57 | splitter = splitter or line_splitter |
| 58 | buffered = '' |
| 59 | |
| 60 | for data in stream_as_text(stream): |
| 61 | buffered += data |
| 62 | while True: |
| 63 | buffer_split = splitter(buffered) |
| 64 | if buffer_split is None: |
| 65 | break |
| 66 | |
| 67 | item, buffered = buffer_split |
| 68 | yield item |
| 69 | |
| 70 | if buffered: |
| 71 | try: |
| 72 | yield decoder(buffered) |
| 73 | except Exception as e: |
| 74 | raise StreamParseError(e) from e |
no test coverage detected