|
| 1 | +import typing |
| 2 | + |
| 3 | +from streamson.streamson import Convert, PythonConverter |
| 4 | + |
| 5 | +from .matcher import Matcher |
| 6 | + |
| 7 | + |
| 8 | +def convert_iter( |
| 9 | + input_gen: typing.Generator[bytes, None, None], |
| 10 | + handlers: typing.List[typing.Callable[[typing.Optional[str], int, typing.Optional[bytes]], typing.Optional[bytes]]], |
| 11 | + matcher: Matcher, |
| 12 | + require_path: bool = True, |
| 13 | +) -> typing.Generator[str, None, None]: |
| 14 | + """Converts handlers on matched data from a file description |
| 15 | + :param input_gen: input generator |
| 16 | + :param handlers: list of handlers which should be called on match |
| 17 | + :param matcher: used matcher |
| 18 | + :param: require_path: is path required for handlers |
| 19 | +
|
| 20 | + :yields: converted data |
| 21 | + """ |
| 22 | + convert = Convert() |
| 23 | + convert.add_matcher(matcher.inner, [PythonConverter(handler, require_path) for handler in handlers]) |
| 24 | + for item in input_gen: |
| 25 | + for output in convert.process(item): |
| 26 | + yield output |
| 27 | + |
| 28 | + |
| 29 | +def convert_fd( |
| 30 | + input_fd: typing.IO[bytes], |
| 31 | + handlers: typing.List[typing.Callable[[typing.Optional[str], int, typing.Optional[bytes]], typing.Optional[bytes]]], |
| 32 | + matcher: Matcher, |
| 33 | + buffer_size: int = 1024 * 1024, |
| 34 | + require_path: bool = True, |
| 35 | +) -> typing.Generator[str, None, None]: |
| 36 | + """Converts handlers on matched data from a file description |
| 37 | + :param input_fd: input generator |
| 38 | + :param handlers: list of handlers which should be called on match |
| 39 | + :param matcher: used matcher |
| 40 | + :param: buffer_size: how many bytes can be read from a file at once |
| 41 | + :param: require_path: is path required for handlers |
| 42 | +
|
| 43 | + :yields: converted data |
| 44 | + """ |
| 45 | + convert = Convert() |
| 46 | + convert.add_matcher(matcher.inner, [PythonConverter(handler, require_path) for handler in handlers]) |
| 47 | + |
| 48 | + input_data = input_fd.read(buffer_size) |
| 49 | + |
| 50 | + while input_data: |
| 51 | + for item in convert.process(input_data): |
| 52 | + yield item |
| 53 | + input_data = input_fd.read(buffer_size) |
| 54 | + |
| 55 | + |
| 56 | +async def convert_async( |
| 57 | + input_gen: typing.AsyncGenerator[bytes, None], |
| 58 | + handlers: typing.List[typing.Callable[[typing.Optional[str], int, typing.Optional[bytes]], typing.Optional[bytes]]], |
| 59 | + matcher: Matcher, |
| 60 | + require_path: bool = True, |
| 61 | +) -> typing.AsyncGenerator[str, None]: |
| 62 | + """Convert handlers on matched data from async generator |
| 63 | + :param: input_gen: input generator |
| 64 | + :param handlers: list of handlers which should be called on match |
| 65 | + :param: matcher: used matcher |
| 66 | + :param: require_path: is path required for handlers |
| 67 | +
|
| 68 | + :yields: input data |
| 69 | + """ |
| 70 | + convert = Convert() |
| 71 | + convert.add_matcher(matcher.inner, [PythonConverter(handler, require_path) for handler in handlers]) |
| 72 | + |
| 73 | + async for input_data in input_gen: |
| 74 | + for item in convert.process(input_data): |
| 75 | + yield item |
0 commit comments