-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from alphatwirl/dev
Clean code
- Loading branch information
Showing
10 changed files
with
162 additions
and
140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
__all__ = [ | ||
'OutputStream', | ||
'register_stream_queue', | ||
'StreamPickup', | ||
'StreamRedirection', | ||
'FD', | ||
'Queue', | ||
'StreamQueue', | ||
] | ||
|
||
from .output import OutputStream, register_stream_queue | ||
from .pickup import StreamPickup | ||
from .redirect import StreamRedirection | ||
from .type import FD, Queue, StreamQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import sys | ||
from io import TextIOBase | ||
|
||
from .type import FD, StreamQueue | ||
|
||
|
||
def register_stream_queue(queue: StreamQueue) -> None: | ||
if queue is None: | ||
return | ||
sys.stdout = OutputStream(queue, FD.STDOUT) # type: ignore | ||
sys.stderr = OutputStream(queue, FD.STDERR) # type: ignore | ||
|
||
|
||
class OutputStream(TextIOBase): | ||
def __init__(self, queue: StreamQueue, fd: FD) -> None: | ||
self.fd = fd | ||
self.queue = queue | ||
self.buffer = '' | ||
|
||
def write(self, s: str) -> int: | ||
if not isinstance(s, str): | ||
# The same error message as `sys.stdout.write()` | ||
raise TypeError(f'write() argument must be str, not {type(s).__name__}') | ||
|
||
self.buffer += s | ||
if s.endswith('\n'): | ||
self.flush() | ||
return len(s) | ||
|
||
def flush(self) -> None: | ||
if not self.buffer: | ||
return | ||
self.queue.put((self.buffer, self.fd)) | ||
self.buffer = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import threading | ||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
from .type import FD, StreamQueue | ||
|
||
|
||
class StreamPickup(threading.Thread): | ||
def __init__( | ||
self, | ||
queue: StreamQueue, | ||
stdout_write: Callable[[str], Any], | ||
stderr_write: Callable[[str], Any], | ||
) -> None: | ||
super().__init__(daemon=True) | ||
self.queue = queue | ||
self.stdout_write = stdout_write | ||
self.stderr_write = stderr_write | ||
|
||
def run(self) -> None: | ||
try: | ||
while True: | ||
m = self.queue.get() | ||
if m is None: | ||
break | ||
s, f = m | ||
if f == FD.STDOUT: | ||
self.stdout_write(s) | ||
elif f == FD.STDERR: | ||
self.stderr_write(s) | ||
else: | ||
raise ValueError('unknown fd: {!r}'.format(f)) | ||
|
||
except EOFError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import sys | ||
|
||
from atpbar.presentation import Presentation | ||
|
||
from .output import OutputStream | ||
from .pickup import StreamPickup | ||
from .type import FD, StreamQueue | ||
|
||
|
||
class StreamRedirection: | ||
def __init__(self, queue: StreamQueue, presentation: Presentation) -> None: | ||
self.disabled = not presentation.stdout_stderr_redirection | ||
if self.disabled: | ||
return | ||
|
||
self.queue = queue | ||
self.presentation = presentation | ||
|
||
self.stdout = OutputStream(self.queue, FD.STDOUT) | ||
self.stderr = OutputStream(self.queue, FD.STDERR) | ||
|
||
def start(self) -> None: | ||
if self.disabled: | ||
return | ||
|
||
self.pickup = StreamPickup( | ||
self.queue, self.presentation.stdout_write, self.presentation.stderr_write | ||
) | ||
self.pickup.start() | ||
|
||
self.stdout_org = sys.stdout | ||
sys.stdout = self.stdout # type: ignore | ||
|
||
self.stderr_org = sys.stderr | ||
sys.stderr = self.stderr # type: ignore | ||
|
||
def end(self) -> None: | ||
if self.disabled: | ||
return | ||
|
||
sys.stdout = self.stdout_org | ||
sys.stderr = self.stderr_org | ||
self.queue.put(None) | ||
self.pickup.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from enum import Enum | ||
from multiprocessing import Queue | ||
from typing import TypeAlias | ||
|
||
|
||
class FD(Enum): | ||
STDOUT = 1 | ||
STDERR = 2 | ||
|
||
|
||
StreamQueue: TypeAlias = 'Queue[tuple[str, FD] | None]' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters