From eab8146b76c1626cd565621125425fccf1ef288b Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Tue, 11 May 2021 17:18:22 +0545 Subject: [PATCH] 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. --- dvc/ui/__init__.py | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/dvc/ui/__init__.py b/dvc/ui/__init__.py index a02a7e50e..80727d5f3 100644 --- a/dvc/ui/__init__.py +++ b/dvc/ui/__init__.py @@ -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,