2016-03-20 22:55:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-20 20:41:04 +00:00
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2016-04-10 01:45:00 +00:00
|
|
|
# Copyright (C) 2015-2016 Tobias Gruetzmacher
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
2016-03-20 22:55:32 +00:00
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
import time
|
2012-12-07 23:45:18 +00:00
|
|
|
import sys
|
2012-12-12 16:41:29 +00:00
|
|
|
import os
|
2012-12-09 17:12:41 +00:00
|
|
|
import threading
|
2013-03-25 18:39:37 +00:00
|
|
|
import traceback
|
2013-04-29 18:25:05 +00:00
|
|
|
import codecs
|
2016-04-10 01:45:00 +00:00
|
|
|
import contextlib
|
|
|
|
import pydoc
|
|
|
|
import io
|
|
|
|
|
|
|
|
try:
|
|
|
|
import curses
|
|
|
|
except ImportError:
|
|
|
|
curses = None
|
|
|
|
|
|
|
|
import colorama
|
|
|
|
from colorama import Fore, Style, win32
|
2012-12-09 17:12:41 +00:00
|
|
|
|
2016-03-20 22:55:32 +00:00
|
|
|
|
2012-12-09 17:12:41 +00:00
|
|
|
lock = threading.Lock()
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2016-03-20 22:55:32 +00:00
|
|
|
|
2014-01-05 15:17:34 +00:00
|
|
|
def get_threadname():
|
|
|
|
"""Return name of current thread."""
|
|
|
|
return threading.current_thread().getName()
|
|
|
|
|
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
class Output(object):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Print output with context, indentation and optional timestamps."""
|
|
|
|
|
2016-04-10 01:45:00 +00:00
|
|
|
DEFAULT_WIDTH = 80
|
|
|
|
|
2013-04-30 18:26:36 +00:00
|
|
|
def __init__(self, stream=None):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Initialize context and indentation."""
|
2014-01-05 16:37:13 +00:00
|
|
|
self.context = None
|
2012-06-20 19:58:13 +00:00
|
|
|
self.level = 0
|
|
|
|
self.timestamps = False
|
2013-04-30 18:26:36 +00:00
|
|
|
if stream is None:
|
2016-04-10 01:45:00 +00:00
|
|
|
colorama.init(wrap=False)
|
2013-04-30 18:26:36 +00:00
|
|
|
if hasattr(sys.stdout, "encoding") and sys.stdout.encoding:
|
|
|
|
self.encoding = sys.stdout.encoding
|
2013-04-29 18:25:05 +00:00
|
|
|
else:
|
|
|
|
self.encoding = 'utf-8'
|
2016-03-20 22:55:32 +00:00
|
|
|
if hasattr(sys.stdout, 'buffer'):
|
2013-04-30 18:26:36 +00:00
|
|
|
stream = sys.stdout.buffer
|
|
|
|
else:
|
|
|
|
stream = sys.stdout
|
|
|
|
stream = codecs.getwriter(self.encoding)(stream, 'replace')
|
2016-04-10 01:45:00 +00:00
|
|
|
if os.name == 'nt':
|
|
|
|
stream = colorama.AnsiToWin32(stream).stream
|
|
|
|
self.stream = stream
|
|
|
|
|
|
|
|
@property
|
|
|
|
def stream(self):
|
|
|
|
"""The underlaying stream."""
|
|
|
|
return self._stream
|
|
|
|
|
|
|
|
@stream.setter
|
|
|
|
def stream(self, attr):
|
|
|
|
"""Change stream and base stream. base_stream is used for terminal
|
|
|
|
interaction when _stream is redirected to a pager."""
|
|
|
|
self._stream = attr
|
|
|
|
self._base_stream = attr
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2012-12-07 23:45:18 +00:00
|
|
|
def info(self, s, level=0):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write an informational message."""
|
2012-12-07 23:45:18 +00:00
|
|
|
self.write(s, level=level)
|
|
|
|
|
2013-02-07 17:28:40 +00:00
|
|
|
def debug(self, s, level=2):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write a debug message."""
|
2016-04-10 01:45:00 +00:00
|
|
|
# "white" is the default color for most terminals...
|
|
|
|
self.write(s, level=level, color=Fore.WHITE)
|
2012-12-07 23:45:18 +00:00
|
|
|
|
|
|
|
def warn(self, s):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write a warning message."""
|
2016-04-10 01:45:00 +00:00
|
|
|
self.write(u"WARN: %s" % s, color=Style.BRIGHT + Fore.YELLOW)
|
2012-12-07 23:45:18 +00:00
|
|
|
|
2013-03-25 18:39:37 +00:00
|
|
|
def error(self, s, tb=None):
|
2012-12-12 16:41:29 +00:00
|
|
|
"""Write an error message."""
|
2016-04-10 01:45:00 +00:00
|
|
|
self.write(u"ERROR: %s" % s, color=Style.DIM + Fore.RED)
|
2013-03-25 18:39:37 +00:00
|
|
|
|
|
|
|
def exception(self, s):
|
|
|
|
"""Write error message with traceback info."""
|
|
|
|
self.error(s)
|
|
|
|
type, value, tb = sys.exc_info()
|
|
|
|
self.writelines(traceback.format_stack(), 1)
|
|
|
|
self.writelines(traceback.format_tb(tb)[1:], 1)
|
|
|
|
self.writelines(traceback.format_exception_only(type, value), 1)
|
2012-12-07 23:45:18 +00:00
|
|
|
|
2012-12-12 16:41:29 +00:00
|
|
|
def write(self, s, level=0, color=None):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Write message with indentation, context and optional timestamp."""
|
2012-06-20 19:58:13 +00:00
|
|
|
if level > self.level:
|
|
|
|
return
|
2014-01-05 15:17:34 +00:00
|
|
|
if self.timestamps:
|
2013-04-12 19:02:31 +00:00
|
|
|
timestamp = time.strftime(u'%H:%M:%S ')
|
2012-06-20 19:58:13 +00:00
|
|
|
else:
|
2013-04-12 19:02:31 +00:00
|
|
|
timestamp = u''
|
2012-12-09 17:12:41 +00:00
|
|
|
with lock:
|
2014-01-05 15:17:34 +00:00
|
|
|
if self.context:
|
2013-04-12 19:02:31 +00:00
|
|
|
self.stream.write(u'%s%s> ' % (timestamp, self.context))
|
2014-01-05 16:37:13 +00:00
|
|
|
elif self.context is None:
|
2014-01-05 15:17:34 +00:00
|
|
|
self.stream.write(u'%s%s> ' % (timestamp, get_threadname()))
|
2016-04-10 01:45:00 +00:00
|
|
|
if color and self.has_color:
|
|
|
|
s = u'%s%s%s' % (color, s, Style.RESET_ALL)
|
2013-04-12 19:02:31 +00:00
|
|
|
try:
|
|
|
|
text_type = unicode
|
|
|
|
except NameError:
|
|
|
|
text_type = str
|
2016-04-10 01:45:00 +00:00
|
|
|
self.stream.write(text_type(s))
|
2013-04-12 19:02:31 +00:00
|
|
|
self.stream.write(text_type(os.linesep))
|
2012-12-12 16:41:29 +00:00
|
|
|
self.stream.flush()
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
def writelines(self, lines, level=0):
|
2012-09-26 14:47:39 +00:00
|
|
|
"""Write multiple messages."""
|
2012-06-20 19:58:13 +00:00
|
|
|
for line in lines:
|
2013-04-12 19:02:31 +00:00
|
|
|
for line in line.rstrip(u'\n').split(u'\n'):
|
|
|
|
self.write(line.rstrip(u'\n'), level=level)
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2016-04-10 01:45:00 +00:00
|
|
|
@property
|
|
|
|
def has_color(self):
|
|
|
|
if not self.is_tty:
|
|
|
|
return False
|
|
|
|
elif os.name == 'nt':
|
|
|
|
return True
|
|
|
|
elif curses:
|
|
|
|
try:
|
|
|
|
curses.setupterm(os.environ.get("TERM"),
|
|
|
|
self._base_stream.fileno())
|
|
|
|
# More than 8 colors are good enough.
|
|
|
|
return curses.tigetnum("colors") >= 8
|
|
|
|
except curses.error:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def width(self):
|
|
|
|
"""Get width of this output."""
|
|
|
|
if not self.is_tty:
|
|
|
|
return self.DEFAULT_WIDTH
|
|
|
|
elif os.name == 'nt':
|
|
|
|
csbi = win32.GetConsoleScreenBufferInfo(win32.STDOUT)
|
|
|
|
return csbi.dwSize.X
|
|
|
|
elif curses:
|
|
|
|
try:
|
|
|
|
curses.setupterm(os.environ.get("TERM"),
|
|
|
|
self._base_stream.fileno())
|
|
|
|
return curses.tigetnum("cols")
|
|
|
|
except curses.error:
|
|
|
|
pass
|
|
|
|
return self.DEFAULT_WIDTH
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_tty(self):
|
|
|
|
"""Is this output stream a terminal?"""
|
|
|
|
return (hasattr(self._base_stream, "isatty") and
|
|
|
|
self._base_stream.isatty())
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def temporary_context(self, context):
|
|
|
|
"""Run a block with a temporary output context"""
|
|
|
|
orig_context = self.context
|
|
|
|
self.context = context
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.context = orig_context
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def pager(self):
|
|
|
|
"""Run the output of a block through a pager."""
|
|
|
|
try:
|
|
|
|
if self.is_tty:
|
|
|
|
fd = io.StringIO()
|
|
|
|
self._stream = fd
|
|
|
|
with self.temporary_context(u''):
|
|
|
|
yield
|
|
|
|
if self.is_tty:
|
|
|
|
pydoc.pager(fd.getvalue())
|
|
|
|
finally:
|
|
|
|
self._stream = self._base_stream
|
|
|
|
|
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
out = Output()
|