lorax/src/pylorax/output.py

142 lines
3.6 KiB
Python
Raw Normal View History

#
# output.py
2010-01-12 11:45:54 +00:00
#
# 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
2010-02-23 13:20:05 +00:00
import re
2017-10-12 11:34:58 +00:00
import pylorax.decorators as decorators
2010-10-12 16:23:29 +00:00
# output levels
CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
NOTSET = 0
2010-02-23 13:20:05 +00:00
# color codes
C_DEFAULT = "\x1b[39m"
C_RESET = "\x1b[0m"
2010-02-23 13:20:05 +00:00
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"
2010-02-23 13:20:05 +00:00
C_BOLD = "\x1b[1m"
C_UNDERLINE = "\x1b[4m"
2010-02-23 13:20:05 +00:00
# format tags
TAGS = [(re.compile(r"<b>"), C_BOLD),
(re.compile(r"<u>"), C_UNDERLINE),
(re.compile(r"<red>"), C_RED),
(re.compile(r"<green>"), C_GREEN),
(re.compile(r"<blue>"), C_BLUE),
2010-10-12 16:23:29 +00:00
(re.compile(r"</(b|u|red|green|blue)>"), C_RESET)]
2010-02-23 13:20:05 +00:00
2010-10-12 16:23:29 +00:00
@decorators.singleton
class LinuxTerminalOutput(object):
def __init__(self):
2010-10-12 16:23:29 +00:00
self._output_level = INFO
2010-02-23 13:20:05 +00:00
self._colors = True
self._encoding = "utf-8"
2010-10-12 16:23:29 +00:00
self._ignored_messages = set()
2010-02-23 13:20:05 +00:00
self._indent_level = 0
2010-10-12 16:23:29 +00:00
self.width = 79
2010-10-12 16:23:29 +00:00
def basic_config(self, output_level=None, colors=None, encoding=None):
self._output_level = output_level or self._output_level
self._colors = colors or self._colors
self._encoding = encoding or self._encoding
2010-08-17 12:14:36 +00:00
2010-10-12 16:23:29 +00:00
def ignore(self, message):
self._ignored_messages.add(message)
def indent(self):
2010-02-23 13:20:05 +00:00
self._indent_level += 1
def unindent(self):
2010-02-23 13:20:05 +00:00
if self._indent_level > 0:
self._indent_level -= 1
2017-10-12 11:44:20 +00:00
def write(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
if self._colors:
s = self.__format(s)
else:
s = self.__raw(s)
2017-10-12 11:44:20 +00:00
fp.write(s)
fp.flush()
2017-10-12 11:44:20 +00:00
def writeline(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
s = "{0}{1}\n".format(" " * self._indent_level, s)
2017-10-12 11:44:20 +00:00
self.write(s, fp=fp)
2017-10-12 11:44:20 +00:00
def critical(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
s = "** critical: {0}".format(s)
if (self._output_level <= CRITICAL and
2010-10-12 16:23:29 +00:00
self.__raw(s) not in self._ignored_messages):
2017-10-12 11:44:20 +00:00
self.writeline(s, fp=fp)
2017-10-12 11:44:20 +00:00
def error(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
s = "** error: {0}".format(s)
if (self._output_level <= ERROR and
2010-10-12 16:23:29 +00:00
self.__raw(s) not in self._ignored_messages):
2017-10-12 11:44:20 +00:00
self.writeline(s, fp=fp)
2017-10-12 11:44:20 +00:00
def warning(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
s = "** warning: {0}".format(s)
if (self._output_level <= WARNING and
2010-10-12 16:23:29 +00:00
self.__raw(s) not in self._ignored_messages):
2017-10-12 11:44:20 +00:00
self.writeline(s, fp=fp)
2017-10-12 11:44:20 +00:00
def info(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
if self._output_level <= INFO:
2017-10-12 11:44:20 +00:00
self.writeline(s, fp=fp)
2017-10-12 11:44:20 +00:00
def debug(self, s, fp=sys.stdout):
2010-02-23 13:20:05 +00:00
if self._output_level <= DEBUG:
2017-10-12 11:44:20 +00:00
self.writeline(s, fp=fp)
2010-02-23 13:20:05 +00:00
2010-08-17 12:14:36 +00:00
def __format(self, s):
2010-02-23 13:20:05 +00:00
for tag, ccode in TAGS:
2010-08-17 12:14:36 +00:00
s = tag.sub(ccode, s)
2010-02-23 13:20:05 +00:00
return s
2010-08-17 12:14:36 +00:00
def __raw(self, s):
2010-12-02 12:24:15 +00:00
for tag, _ in TAGS:
2010-08-17 12:14:36 +00:00
s = tag.sub("", s)
2010-02-23 13:20:05 +00:00
return s
2010-10-12 16:23:29 +00:00
# set up the output type to be used by lorax
LoraxOutput = LinuxTerminalOutput