|
| 1 | + |
| 2 | +import argparse |
| 3 | +import asyncio |
| 4 | +import io |
| 5 | +import sys |
| 6 | +from functools import cached_property |
| 7 | +from typing import Any, Callable, TextIO, Tuple |
| 8 | + |
| 9 | +import abstracts |
| 10 | + |
| 11 | +from aio.core.functional import async_property |
| 12 | +from aio.core.pipe import interface |
| 13 | + |
| 14 | + |
| 15 | +@abstracts.implementer(interface.IProcessProtocol) |
| 16 | +class AProcessProtocol(metaclass=abstracts.Abstraction): |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + processor: interface.IProcessor, |
| 21 | + args: argparse.Namespace) -> None: |
| 22 | + self.processor = processor |
| 23 | + self.args = args |
| 24 | + |
| 25 | + async def __call__(self, request: Any) -> Any: |
| 26 | + return await self.process(request) |
| 27 | + |
| 28 | + @abstracts.interfacemethod |
| 29 | + async def process(self, request: Any) -> Any: |
| 30 | + raise NotImplementedError |
| 31 | + |
| 32 | + |
| 33 | +@abstracts.implementer(interface.IStdinStdoutProcessor) |
| 34 | +class AStdinStdoutProcessor(metaclass=abstracts.Abstraction): |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + protocol: interface.IProcessProtocol, |
| 39 | + stdin: TextIO = sys.stdin, |
| 40 | + stdout: TextIO = sys.stdout, |
| 41 | + log: Callable[[str], None] = None) -> None: |
| 42 | + self._protocol = protocol |
| 43 | + self.stdin = stdin |
| 44 | + self.stdout = stdout |
| 45 | + self._log = log |
| 46 | + |
| 47 | + async def __call__(self) -> None: |
| 48 | + await self.start() |
| 49 | + |
| 50 | + @cached_property |
| 51 | + def connecting(self) -> asyncio.Lock: |
| 52 | + return asyncio.Lock() |
| 53 | + |
| 54 | + @async_property(cache=True) |
| 55 | + async def connection( |
| 56 | + self) -> Tuple[ |
| 57 | + asyncio.StreamReader, |
| 58 | + asyncio.StreamWriter]: |
| 59 | + await self.loop.connect_read_pipe( |
| 60 | + lambda: self.stream_protocol, |
| 61 | + self.stdin) |
| 62 | + return self.stream_reader, await self.stream_writer |
| 63 | + |
| 64 | + @cached_property |
| 65 | + def in_q(self) -> asyncio.Queue: |
| 66 | + return asyncio.Queue() |
| 67 | + |
| 68 | + @async_property |
| 69 | + async def listener(self) -> None: |
| 70 | + async with self.connecting: |
| 71 | + reader = await self.reader |
| 72 | + self.log(f"START LISTENING {reader}") |
| 73 | + while True: |
| 74 | + line = await reader.readline() |
| 75 | + if not line: |
| 76 | + self.log(f"CLOSING") |
| 77 | + break |
| 78 | + if not line.strip(): |
| 79 | + continue |
| 80 | + await self.in_q.put(line.decode()) |
| 81 | + self.log("STOP LISTENING") |
| 82 | + await self.in_q.put("") |
| 83 | + |
| 84 | + @cached_property |
| 85 | + def loop(self) -> asyncio.AbstractEventLoop: |
| 86 | + return asyncio.get_event_loop() |
| 87 | + |
| 88 | + @cached_property |
| 89 | + def out_q(self) -> asyncio.Queue: |
| 90 | + return asyncio.Queue() |
| 91 | + |
| 92 | + @async_property |
| 93 | + async def processor(self) -> None: |
| 94 | + async with self.connecting: |
| 95 | + await self.protocol |
| 96 | + self.log("START PROCESSING") |
| 97 | + while True: |
| 98 | + recv = await self.recv() |
| 99 | + if not recv: |
| 100 | + break |
| 101 | + await self.send(await self.process(recv)) |
| 102 | + self.complete() |
| 103 | + self.log("STOP PROCESSING") |
| 104 | + await self.send("") |
| 105 | + |
| 106 | + @async_property(cache=True) |
| 107 | + async def protocol(self) -> interface.IProcessProtocol: |
| 108 | + return await self._protocol(self) |
| 109 | + |
| 110 | + @async_property |
| 111 | + async def reader(self) -> asyncio.StreamReader: |
| 112 | + return (await self.connection)[0] |
| 113 | + |
| 114 | + @async_property |
| 115 | + async def sender(self) -> None: |
| 116 | + async with self.connecting: |
| 117 | + writer = await self.writer |
| 118 | + self.log(f"START SEND {writer}") |
| 119 | + while True: |
| 120 | + outgoing = await self.out_q.get() |
| 121 | + if not outgoing: |
| 122 | + break |
| 123 | + self.out_q.task_done() |
| 124 | + writer.write(outgoing.encode("utf-8")) |
| 125 | + self.log("STOP SEND") |
| 126 | + |
| 127 | + @cached_property |
| 128 | + def stream_protocol(self) -> asyncio.StreamReaderProtocol: |
| 129 | + return asyncio.StreamReaderProtocol(self.stream_reader) |
| 130 | + |
| 131 | + @cached_property |
| 132 | + def stream_reader(self) -> asyncio.StreamReader: |
| 133 | + return asyncio.StreamReader() |
| 134 | + |
| 135 | + @async_property(cache=True) |
| 136 | + async def stream_transport( |
| 137 | + self) -> Tuple[ |
| 138 | + asyncio.WriteTransport, |
| 139 | + asyncio.streams.FlowControlMixin]: |
| 140 | + return await self.loop.connect_write_pipe( |
| 141 | + asyncio.streams.FlowControlMixin, |
| 142 | + self.stdout) |
| 143 | + |
| 144 | + @async_property(cache=True) |
| 145 | + async def stream_writer(self) -> asyncio.StreamWriter: |
| 146 | + transport, flow = await self.stream_transport |
| 147 | + return asyncio.StreamWriter( |
| 148 | + transport, |
| 149 | + flow, |
| 150 | + self.stream_reader, |
| 151 | + self.loop) |
| 152 | + |
| 153 | + @async_property |
| 154 | + async def writer(self) -> asyncio.StreamWriter: |
| 155 | + return (await self.connection)[1] |
| 156 | + |
| 157 | + def complete(self) -> None: |
| 158 | + self.in_q.task_done() |
| 159 | + |
| 160 | + def log(self, message: str) -> None: |
| 161 | + if self._log: |
| 162 | + self._log(f"{message}\n") |
| 163 | + |
| 164 | + async def process(self, data: Any) -> Any: |
| 165 | + protocol = await self.protocol |
| 166 | + self.log(f"PROCESS: {protocol} {data}") |
| 167 | + return await protocol(data) |
| 168 | + |
| 169 | + async def recv(self) -> Any: |
| 170 | + recv = await self.in_q.get() |
| 171 | + self.log(f"RECV: {recv}") |
| 172 | + return recv |
| 173 | + |
| 174 | + async def send(self, msg: Any) -> Any: |
| 175 | + self.log(f"SEND: {msg}") |
| 176 | + return await self.out_q.put(msg) |
| 177 | + |
| 178 | + async def start(self) -> None: |
| 179 | + self.log("PROCESSOR START") |
| 180 | + await asyncio.gather( |
| 181 | + asyncio.create_task(self.listener), |
| 182 | + asyncio.create_task(self.sender), |
| 183 | + asyncio.create_task(self.processor)) |
| 184 | + self.log("PROCESSOR SHUTDOWN") |
0 commit comments