2015-07-23 06:09:57 +03:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from datetime import datetime
|
2015-04-22 06:03:06 +02:00
|
|
|
import sys
|
|
|
|
from traceback import format_exception
|
|
|
|
import colorama
|
|
|
|
|
|
|
|
|
|
|
|
def color(color_, settings):
|
|
|
|
"""Utility for ability to disabling colored output."""
|
|
|
|
if settings.no_colors:
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return color_
|
|
|
|
|
|
|
|
|
2015-04-22 20:18:53 +02:00
|
|
|
def exception(title, exc_info, settings):
|
2015-04-22 06:03:06 +02:00
|
|
|
sys.stderr.write(
|
2015-04-22 20:18:53 +02:00
|
|
|
u'{warn}[WARN] {title}:{reset}\n{trace}'
|
2015-04-22 06:03:06 +02:00
|
|
|
u'{warn}----------------------------{reset}\n\n'.format(
|
|
|
|
warn=color(colorama.Back.RED + colorama.Fore.WHITE
|
|
|
|
+ colorama.Style.BRIGHT, settings),
|
|
|
|
reset=color(colorama.Style.RESET_ALL, settings),
|
2015-04-22 20:18:53 +02:00
|
|
|
title=title,
|
2015-04-22 06:03:06 +02:00
|
|
|
trace=''.join(format_exception(*exc_info))))
|
|
|
|
|
|
|
|
|
2015-04-22 20:18:53 +02:00
|
|
|
def rule_failed(rule, exc_info, settings):
|
|
|
|
exception('Rule {}'.format(rule.name), exc_info, settings)
|
|
|
|
|
|
|
|
|
2015-05-01 04:39:37 +02:00
|
|
|
def show_command(new_command, side_effect, settings):
|
2015-07-25 23:10:21 +02:00
|
|
|
sys.stderr.write('{bold}{command}{reset}{side_effect}\n'.format(
|
2015-04-22 06:03:06 +02:00
|
|
|
command=new_command,
|
2015-07-25 23:10:21 +02:00
|
|
|
side_effect=' (+side effect)' if side_effect else '',
|
2015-04-22 06:03:06 +02:00
|
|
|
bold=color(colorama.Style.BRIGHT, settings),
|
|
|
|
reset=color(colorama.Style.RESET_ALL, settings)))
|
|
|
|
|
|
|
|
|
2015-05-01 04:39:37 +02:00
|
|
|
def confirm_command(new_command, side_effect, settings):
|
2015-04-22 06:03:06 +02:00
|
|
|
sys.stderr.write(
|
2015-07-25 23:10:21 +02:00
|
|
|
'{bold}{command}{reset}{side_effect} '
|
2015-05-01 04:39:37 +02:00
|
|
|
'[{green}enter{reset}/{red}ctrl+c{reset}]'.format(
|
2015-04-22 06:03:06 +02:00
|
|
|
command=new_command,
|
2015-07-25 23:10:21 +02:00
|
|
|
side_effect=' (+side effect)' if side_effect else '',
|
2015-04-22 06:03:06 +02:00
|
|
|
bold=color(colorama.Style.BRIGHT, settings),
|
|
|
|
green=color(colorama.Fore.GREEN, settings),
|
|
|
|
red=color(colorama.Fore.RED, settings),
|
|
|
|
reset=color(colorama.Style.RESET_ALL, settings)))
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
|
|
|
|
def failed(msg, settings):
|
|
|
|
sys.stderr.write('{red}{msg}{reset}\n'.format(
|
|
|
|
msg=msg,
|
|
|
|
red=color(colorama.Fore.RED, settings),
|
|
|
|
reset=color(colorama.Style.RESET_ALL, settings)))
|
2015-07-15 07:47:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
def debug(msg, settings):
|
|
|
|
if settings.debug:
|
|
|
|
sys.stderr.write(u'{blue}{bold}DEBUG:{reset} {msg}\n'.format(
|
|
|
|
msg=msg,
|
|
|
|
reset=color(colorama.Style.RESET_ALL, settings),
|
|
|
|
blue=color(colorama.Fore.BLUE, settings),
|
|
|
|
bold=color(colorama.Style.BRIGHT, settings)))
|
2015-07-23 06:09:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def debug_time(msg, settings):
|
|
|
|
started = datetime.now()
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
debug('{} took: {}'.format(msg, datetime.now() - started), settings)
|