mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
progress: Respect the --quiet option (#1377)
* progress: Respect the --quiet option Fix #1308 * Use a dynamic import to prevent circular dependency * test: add test for quiet mode Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
committed by
Ruslan Kuprieiev
parent
93cd9ced90
commit
44e67d4cdc
@@ -184,8 +184,15 @@ class Logger(object):
|
||||
def info(msg):
|
||||
Logger._with_progress(Logger.logger().info, msg)
|
||||
|
||||
@staticmethod
|
||||
def is_quiet():
|
||||
return Logger.logger().level == logging.CRITICAL
|
||||
|
||||
@classmethod
|
||||
def box(cls, msg, border_color=''):
|
||||
if cls.is_quiet():
|
||||
return
|
||||
|
||||
lines = msg.split('\n')
|
||||
max_width = max(visual_width(line) for line in lines)
|
||||
|
||||
|
||||
+11
-3
@@ -22,11 +22,11 @@ class Progress(object):
|
||||
return self._n_total == self._n_finished
|
||||
|
||||
def _clearln(self):
|
||||
print('\r\x1b[K', end='')
|
||||
self._print('\r\x1b[K', end='')
|
||||
|
||||
def _writeln(self, line):
|
||||
self._clearln()
|
||||
print(line, end='')
|
||||
self._print(line, end='')
|
||||
sys.stdout.flush()
|
||||
|
||||
def refresh(self, line=None):
|
||||
@@ -54,7 +54,7 @@ class Progress(object):
|
||||
if sys.stdout.isatty():
|
||||
self._clearln()
|
||||
|
||||
print(bar)
|
||||
self._print(bar)
|
||||
|
||||
self._n_finished += 1
|
||||
self._line = None
|
||||
@@ -85,6 +85,14 @@ class Progress(object):
|
||||
|
||||
return num + bar + percent + target_name
|
||||
|
||||
def _print(self, *args, **kwargs):
|
||||
from dvc.logger import Logger
|
||||
|
||||
if Logger.is_quiet():
|
||||
return
|
||||
|
||||
print(*args, **kwargs)
|
||||
|
||||
def __enter__(self):
|
||||
self._lock.acquire(True)
|
||||
if self._line is not None:
|
||||
|
||||
@@ -70,6 +70,30 @@ class TestLogger(TestDvc):
|
||||
mock_stdout.getvalue())
|
||||
|
||||
|
||||
class TestLoggerQuiet(TestDvc):
|
||||
def setUp(self):
|
||||
super(TestLoggerQuiet, self).setUp()
|
||||
class A(object):
|
||||
quiet = True
|
||||
verbose = False
|
||||
CmdBase._set_loglevel(A())
|
||||
|
||||
@patch('sys.stderr', new_callable=StringIO)
|
||||
@patch('sys.stdout', new_callable=StringIO)
|
||||
def test_progress(self, mock_stdout, mock_stderr):
|
||||
from dvc.progress import progress
|
||||
progress.update_target('target', 50, 100)
|
||||
self.assertEqual('', mock_stdout.getvalue())
|
||||
self.assertEqual('', mock_stderr.getvalue())
|
||||
|
||||
@patch('sys.stderr', new_callable=StringIO)
|
||||
@patch('sys.stdout', new_callable=StringIO)
|
||||
def test_box(self, mock_stdout, mock_stderr):
|
||||
Logger.box('message')
|
||||
self.assertEqual('', mock_stdout.getvalue())
|
||||
self.assertEqual('', mock_stderr.getvalue())
|
||||
|
||||
|
||||
class TestLoggerCLI(TestDvc):
|
||||
def test(self):
|
||||
class A(object):
|
||||
|
||||
Reference in New Issue
Block a user