From 8b6d655ebeecbe8cee09a00e8bd64c711898c303 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Wed, 24 Mar 2021 04:37:17 -0700 Subject: [PATCH] use global UI object for printing (#5653) --- dvc/command/status.py | 8 +++++--- dvc/main.py | 5 +++++ dvc/ui/__init__.py | 26 ++++++++++++++------------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/dvc/command/status.py b/dvc/command/status.py index 8d2496050..616d22376 100644 --- a/dvc/command/status.py +++ b/dvc/command/status.py @@ -44,6 +44,8 @@ class CmdDataStatus(CmdDataBase): self._show(value, indent + 1) def run(self): + from dvc.ui import ui + indent = 1 if self.args.cloud else 0 try: st = self.repo.status( @@ -68,14 +70,14 @@ class CmdDataStatus(CmdDataBase): elif st: self._show(st, indent) elif not self.repo.stages: - logger.info(self.EMPTY_PROJECT_MSG) + ui.write(self.EMPTY_PROJECT_MSG) elif self.args.cloud or self.args.remote: remote = self.args.remote or self.repo.config["core"].get( "remote" ) - logger.info(self.IN_SYNC_MSG.format(remote=remote)) + ui.write(self.IN_SYNC_MSG.format(remote=remote)) else: - logger.info(self.UP_TO_DATE_MSG) + ui.write(self.UP_TO_DATE_MSG) except DvcException: logger.exception("") diff --git a/dvc/main.py b/dvc/main.py index 89eb316f9..d0f1d3ecc 100644 --- a/dvc/main.py +++ b/dvc/main.py @@ -45,6 +45,11 @@ def main(argv=None): # noqa: C901 logger.trace(args) + if not args.quiet: + from dvc.ui import ui + + ui.enable() + with debugtools(args): cmd = args.func(args) ret = cmd.run() diff --git a/dvc/ui/__init__.py b/dvc/ui/__init__.py index afc464a26..1b6cd6c8b 100644 --- a/dvc/ui/__init__.py +++ b/dvc/ui/__init__.py @@ -7,9 +7,6 @@ from funcy import cached_property from dvc.progress import Tqdm from dvc.utils import colorize -NEWLINE = "\n" -SEP = " " - class Formatter: def __init__(self, theme: Dict = None, defaults: Dict = None) -> None: @@ -30,14 +27,17 @@ class Console: formatter: Formatter = None, output: TextIO = None, error: TextIO = None, - disable: bool = False, + enable: bool = False, ) -> None: self._input: TextIO = sys.stdin self._output: TextIO = output or sys.stdout self._error: TextIO = error or sys.stderr self.formatter: Formatter = formatter or Formatter() - self.disabled: bool = disable + self._enabled: bool = enable + + def enable(self): + self._enabled = True def success(self, message: str) -> None: self.write(message, style="success") @@ -52,8 +52,8 @@ class Console: self, *objects: Any, style: str = None, - sep: str = SEP, - end: str = NEWLINE, + sep: str = None, + end: str = None, flush: bool = False, ) -> None: return self.write( @@ -69,12 +69,12 @@ class Console: self, *objects: Any, style: str = None, - sep: str = SEP, - end: str = NEWLINE, + sep: str = None, + end: str = None, file: TextIO = None, flush: bool = False, ) -> None: - if self.disabled: + if not self._enabled: return file = file or self._output @@ -152,9 +152,11 @@ class Console: self.write(ret) -if __name__ == "__main__": - ui = Console() +ui = Console() + +if __name__ == "__main__": + ui.enable() ui.write("No default remote set") ui.success("Everything is up to date.") ui.warn("Run queued experiments will be removed.")