|
| 1 | +import typing |
| 2 | + |
| 3 | +from streamson.streamson import Filter |
| 4 | + |
| 5 | +from .matcher import Matcher |
| 6 | + |
| 7 | + |
| 8 | +def filter_iter( |
| 9 | + input_gen: typing.Generator[bytes, None, None], |
| 10 | + matcher: Matcher, |
| 11 | +) -> typing.Generator[typing.Tuple[str, typing.Any], None, None]: |
| 12 | + """Filters json parts from generator specified by given matcher |
| 13 | + :param: input_gen: input generator |
| 14 | + :param: matcher: used matcher |
| 15 | +
|
| 16 | + :yields: filtered data |
| 17 | + """ |
| 18 | + filter_strategy = Filter() |
| 19 | + filter_strategy.add_matcher(matcher.inner) |
| 20 | + for item in input_gen: |
| 21 | + yield filter_strategy.process(item) |
| 22 | + |
| 23 | + |
| 24 | +def filter_fd( |
| 25 | + input_fd: typing.IO[bytes], |
| 26 | + matcher: Matcher, |
| 27 | + buffer_size: int = 1024 * 1024, |
| 28 | +) -> typing.Generator[typing.Tuple[str, typing.Any], None, None]: |
| 29 | + """Filters json parts from input file specified by given matcher |
| 30 | + :param: input_fd: input fd |
| 31 | + :param: matcher: used matcher |
| 32 | + :param: buffer_size: how many bytes can be read from a file at once |
| 33 | +
|
| 34 | + :yields: filtered data |
| 35 | + """ |
| 36 | + filter_strategy = Filter() |
| 37 | + filter_strategy.add_matcher(matcher.inner) |
| 38 | + |
| 39 | + input_data = input_fd.read(buffer_size) |
| 40 | + |
| 41 | + while input_data: |
| 42 | + yield filter_strategy.process(input_data) |
| 43 | + input_data = input_fd.read(buffer_size) |
| 44 | + |
| 45 | + |
| 46 | +async def filter_async( |
| 47 | + input_gen: typing.AsyncGenerator[bytes, None], |
| 48 | + matcher: Matcher, |
| 49 | +): |
| 50 | + """Filters json parts from given async generator specified by given matcher |
| 51 | + :param: input_gen: input generator |
| 52 | + :param: matcher: used matcher |
| 53 | +
|
| 54 | + :yields: filtered data |
| 55 | + """ |
| 56 | + filter_strategy = Filter() |
| 57 | + filter_strategy.add_matcher(matcher.inner) |
| 58 | + |
| 59 | + async for input_data in input_gen: |
| 60 | + yield filter_strategy.process(input_data) |
0 commit comments