2009-09-09 13:20:17 +00:00
|
|
|
#
|
|
|
|
# output.py
|
|
|
|
# output control
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Red Hat Author(s): Martin Gracik <mgracik@redhat.com>
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
# color codes
|
|
|
|
C_DEFAULT = "\x1b[39m"
|
|
|
|
C_RESET = "\x1b[0m"
|
|
|
|
|
|
|
|
C_BOLD = "\x1b[1m"
|
|
|
|
C_UNDERLINE = "\x1b[4m"
|
|
|
|
|
|
|
|
C_BLACK = "\x1b[0;30m"
|
|
|
|
C_WHITE = "\x1b[1;37m"
|
|
|
|
C_RED = "\x1b[0;31m"
|
|
|
|
C_GREEN = "\x1b[0;32m"
|
|
|
|
C_BLUE = "\x1b[0;34m"
|
|
|
|
C_LIGHTRED = "\x1b[1;31m"
|
|
|
|
C_LIGHTGREEN = "\x1b[1;32m"
|
|
|
|
C_LIGHTBLUE = "\x1b[1;34m"
|
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
# font types
|
|
|
|
BOLD = 0b01
|
|
|
|
UNDERLINE = 0b10
|
2009-09-09 13:20:17 +00:00
|
|
|
|
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
class OutputError(Exception):
|
|
|
|
pass
|
|
|
|
|
2009-09-09 13:20:17 +00:00
|
|
|
class Output(object):
|
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
def __init__(self, output=sys.stdout, colors=True, encoding="utf-8",
|
|
|
|
verbose=False):
|
|
|
|
|
2009-09-09 13:20:17 +00:00
|
|
|
self.output = output
|
2009-09-23 10:21:33 +00:00
|
|
|
if not hasattr(self.output, "write"):
|
|
|
|
raise OutputError, "output does not support write()"
|
|
|
|
|
2009-09-09 13:20:17 +00:00
|
|
|
self.is_flushable = hasattr(self.output, "flush")
|
|
|
|
|
|
|
|
self.colors = colors
|
|
|
|
self.encoding = encoding
|
|
|
|
self.verbose = verbose
|
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
self.__indent_level = 0
|
|
|
|
|
|
|
|
def write(self, s, color=C_RESET, type=None):
|
|
|
|
s = self.format(s, color=color, type=type)
|
|
|
|
self.output.write(s)
|
2009-09-09 13:20:17 +00:00
|
|
|
|
|
|
|
if self.is_flushable:
|
|
|
|
self.output.flush()
|
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
def format(self, s, color=C_RESET, type=None):
|
2009-09-09 13:20:17 +00:00
|
|
|
s = s.encode(self.encoding)
|
|
|
|
|
|
|
|
if self.colors:
|
2009-09-23 10:21:33 +00:00
|
|
|
if type is not None and (type & BOLD):
|
2009-09-09 13:20:17 +00:00
|
|
|
s = "%s%s" % (C_BOLD, s)
|
2009-09-23 10:21:33 +00:00
|
|
|
if type is not None and (type & UNDERLINE):
|
2009-09-09 13:20:17 +00:00
|
|
|
s = "%s%s" % (C_UNDERLINE, s)
|
|
|
|
s = "%s%s%s" % (color, s, C_RESET)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
def writeline(self, s, color=C_RESET, type=None):
|
|
|
|
s = "%s%s\n" % (" " * self.__indent_level, s)
|
|
|
|
self.write(s, color=color, type=type)
|
2009-09-09 13:20:17 +00:00
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
def indent(self):
|
|
|
|
self.__indent_level += 1
|
2009-09-09 13:20:17 +00:00
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
def unindent(self):
|
|
|
|
if self.__indent_level > 0:
|
|
|
|
self.__indent_level -= 1
|
2009-09-09 13:20:17 +00:00
|
|
|
|
2009-09-23 10:21:33 +00:00
|
|
|
def newline(self):
|
|
|
|
self.output.write("\n")
|
|
|
|
|
2009-09-29 12:41:39 +00:00
|
|
|
def banner(self, s):
|
2009-09-23 10:21:33 +00:00
|
|
|
self.writeline(s, color=C_GREEN, type=BOLD)
|
|
|
|
|
2009-09-29 12:41:39 +00:00
|
|
|
def header(self, s):
|
2009-09-23 10:21:33 +00:00
|
|
|
self.writeline(s, type=BOLD)
|
|
|
|
|
2009-09-29 12:41:39 +00:00
|
|
|
def info(self, s):
|
2009-09-09 13:20:17 +00:00
|
|
|
self.writeline(s)
|
|
|
|
|
2009-09-29 12:41:39 +00:00
|
|
|
def error(self, s):
|
2009-09-23 10:21:33 +00:00
|
|
|
self.writeline(s, color=C_RED, type=BOLD)
|
|
|
|
|
2009-09-29 12:41:39 +00:00
|
|
|
def warning(self, s):
|
2009-09-23 10:21:33 +00:00
|
|
|
self.writeline(s, color=C_RED)
|
2009-09-09 13:20:17 +00:00
|
|
|
|
2009-09-29 12:41:39 +00:00
|
|
|
def debug(self, s):
|
2009-09-09 13:20:17 +00:00
|
|
|
if self.verbose:
|
|
|
|
self.writeline(s)
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(verbose=False):
|
|
|
|
stdout = Output(output=sys.stdout, verbose=verbose)
|
|
|
|
stderr = Output(output=sys.stderr, verbose=verbose)
|
|
|
|
|
|
|
|
return stdout, stderr
|