ui: use sys.stdout and sys.stderr all the time (#5991)

We used to allow changing default output and error stream
but that was problematic for the testing. Now it just uses
sys, so capsys should work for the testing.
The proper way to change sys.stdout and sys.stderr is
by using 'redirect_stdout' now.
This commit is contained in:
Saugat Pachhai
2021-05-11 11:33:22 +00:00
committed by GitHub
parent e7571ab325
commit eab8146b76
+9 -24
View File
@@ -11,6 +11,8 @@ from typing import (
Union,
)
from funcy import cached_property
from dvc.progress import Tqdm
from dvc.utils import colorize
@@ -33,25 +35,11 @@ class Formatter:
class Console:
def __init__(
self,
formatter: Formatter = None,
output: TextIO = None,
error: TextIO = None,
enable: bool = False,
self, formatter: Formatter = None, enable: bool = False,
) -> None:
self._output: Optional[TextIO] = output
self._error: Optional[TextIO] = error
self.formatter: Formatter = formatter or Formatter()
self._enabled: bool = enable
@property
def output(self) -> TextIO:
return self._output or sys.stdout
@property
def error_output(self) -> TextIO:
return self._error or sys.stderr
def enable(self) -> None:
self._enabled = True
@@ -77,7 +65,7 @@ class Console:
style=style,
sep=sep,
end=end,
file=self.error_output,
file=sys.stderr,
flush=flush,
)
@@ -94,12 +82,12 @@ class Console:
if not self._enabled and not force:
return
file = file or self.output
file = file or sys.stdout
values = (self.formatter.format(obj, style=style) for obj in objects)
return print(*values, sep=sep, end=end, file=file, flush=flush)
def progress(self, *args, **kwargs) -> Tqdm:
kwargs.setdefault("file", self.error_output)
@staticmethod
def progress(*args, **kwargs) -> Tqdm:
return Tqdm(*args, **kwargs)
def prompt(
@@ -137,15 +125,12 @@ class Console:
return False
return answer.startswith("y")
@property
@cached_property
def rich_console(self):
"""rich_console is only set to stdout for now."""
from rich import console
# FIXME: Getting IO Operation on closed file error
# when testing with capsys, therefore we are creating
# one instance each time as a temporary workaround.
return console.Console(file=self.output)
return console.Console()
def table(
self,