stream(source_file, [error_handling]) This method yield SubRipItem instances a soon as they have been parsed without storing them. It is a kind of SAX parser for .srt files. `source_file` -> Any iterable that yield unicode strings, like a file opened wi
(cls, source_file, error_handling=ERROR_PASS)
| 183 | |
| 184 | @classmethod |
| 185 | def stream(cls, source_file, error_handling=ERROR_PASS): |
| 186 | """ |
| 187 | stream(source_file, [error_handling]) |
| 188 | |
| 189 | This method yield SubRipItem instances a soon as they have been parsed |
| 190 | without storing them. It is a kind of SAX parser for .srt files. |
| 191 | |
| 192 | `source_file` -> Any iterable that yield unicode strings, like a file |
| 193 | opened with `codecs.open()` or an array of unicode. |
| 194 | |
| 195 | Example: |
| 196 | >>> import pysrt |
| 197 | >>> import codecs |
| 198 | >>> file = codecs.open('movie.srt', encoding='utf-8') |
| 199 | >>> for sub in pysrt.stream(file): |
| 200 | ... sub.text += "\nHello !" |
| 201 | ... print unicode(sub) |
| 202 | """ |
| 203 | string_buffer = [] |
| 204 | for index, line in enumerate(chain(source_file, '\n')): |
| 205 | if line.strip(): |
| 206 | string_buffer.append(line) |
| 207 | else: |
| 208 | source = string_buffer |
| 209 | string_buffer = [] |
| 210 | if source and all(source): |
| 211 | try: |
| 212 | yield SubRipItem.from_lines(source) |
| 213 | except Error as error: |
| 214 | error.args += (''.join(source), ) |
| 215 | cls._handle_error(error, error_handling, index) |
| 216 | |
| 217 | def save(self, path=None, encoding=None, eol=None): |
| 218 | """ |
no test coverage detected