diff --git a/dvc/command/base.py b/dvc/command/base.py index 1cd76bd9f..c58a72b17 100644 --- a/dvc/command/base.py +++ b/dvc/command/base.py @@ -41,10 +41,13 @@ class CmdBase(object): self._settings = settings parser = argparse.ArgumentParser() + self.define_common_args(parser) self.define_args(parser) self._parsed_args, self._command_args = parser.parse_known_args(args=self.args) + self.process_common_args() + @property def settings(self): return self._settings @@ -88,6 +91,16 @@ class CmdBase(object): help='Lock data item - disable reproduction. ' + 'It can be enabled by `dvc lock` command or by forcing reproduction.') + def define_common_args(self, parser): + parser.add_argument('--quiet', '-q', action='store_true', default=False, help='Be quiet.') + parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Be verbose.') + + def process_common_args(self): + if self._parsed_args.quiet and not self._parsed_args.verbose: + Logger.be_quiet() + elif not self._parsed_args.quiet and self._parsed_args.verbose: + Logger.be_verbose() + @property def no_git_actions(self): return self.parsed_args.no_git_actions diff --git a/dvc/logger.py b/dvc/logger.py index 4d7b9ae20..1b36c7a0d 100644 --- a/dvc/logger.py +++ b/dvc/logger.py @@ -1,52 +1,63 @@ -class Logger(object): - DEFAULT_LEVEL = 2 +import sys +import logging - LEVEL = DEFAULT_LEVEL +import colorama + +class Logger(object): + DEFAULT_LEVEL = logging.INFO LEVEL_MAP = { - 'debug': 1, - 'info': 2, - 'warn': 3, - 'error': 4 + 'debug': logging.DEBUG, + 'info': logging.INFO, + 'warn': logging.WARNING, + 'error': logging.ERROR } + COLOR_MAP = { + 'debug': colorama.Fore.BLUE, + 'warn': colorama.Fore.YELLOW, + 'error': colorama.Fore.RED + } + + logging.basicConfig(stream=sys.stdout, format='%(message)s', level=DEFAULT_LEVEL) + + _logger = logging.getLogger('dvc') + @staticmethod def set_level(level): - Logger.LEVEL = Logger.LEVEL_MAP.get(level.lower(), 'debug') + Logger._logger.setLevel(Logger.LEVEL_MAP.get(level.lower(), 'debug')) @staticmethod - def is_debug(): - return Logger.LEVEL <= 1 + def be_quiet(): + Logger._logger.setLevel(logging.CRITICAL) @staticmethod - def is_info(): - return Logger.LEVEL <= 2 + def be_verbose(): + Logger._logger.setLevel(logging.DEBUG) @staticmethod - def is_warn(): - return Logger.LEVEL <= 3 + def colorize(msg, typ): + header = '' + footer = '' - @staticmethod - def is_error(): - return Logger.LEVEL <= 4 + if sys.stdout.isatty(): + header = Logger.COLOR_MAP.get(typ.lower(), '') + footer = colorama.Style.RESET_ALL - @staticmethod - def debug(msg): - if Logger.is_debug(): - print(u'Debug. {}'.format(msg)) - - @staticmethod - def info(msg): - if Logger.is_info(): - #print(u'Info. {}'.format(msg)) - print(msg) - - @staticmethod - def warn(msg): - if Logger.is_warn(): - print(u'Warning. {}'.format(msg)) + return u'{}{}{}'.format(header, msg, footer) @staticmethod def error(msg): - if Logger.is_error(): - print(u'Error. {}'.format(msg)) + return Logger._logger.error(Logger.colorize(msg, 'error')) + + @staticmethod + def warn(msg): + return Logger._logger.warn(Logger.colorize(msg, 'warn')) + + @staticmethod + def debug(msg): + return Logger._logger.debug(Logger.colorize(msg, 'debug')) + + @staticmethod + def info(msg): + return Logger._logger.info(Logger.colorize(msg, 'info')) diff --git a/dvc/runtime.py b/dvc/runtime.py index 92e9648dd..06cb18151 100644 --- a/dvc/runtime.py +++ b/dvc/runtime.py @@ -34,6 +34,9 @@ class Runtime(object): args = sys.argv[args_start_loc:] + # To make argparse print "usage: dvc cmd" instead of "usage: dvc" + sys.argv[0] = sys.argv[0] + " " + sys.argv[1] + instance = cmd_class(Settings(args, runtime_git, runtime_config)) sys.exit(instance.run()) except DvcException as e: diff --git a/requirements.txt b/requirements.txt index fac3b1245..a772e726c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -84,3 +84,4 @@ simplegeneric==0.8.1 six==1.10.0 traitlets==4.3.2 wcwidth==0.1.7 +colorama diff --git a/setup.py b/setup.py index 8e0bc1caa..f58e35eaa 100644 --- a/setup.py +++ b/setup.py @@ -90,6 +90,7 @@ install_requires = [ 'six==1.10.0', 'traitlets==4.3.2', 'wcwidth==0.1.7', + 'colorama', ] if platform.system() == 'Darwin':