Newer
Older
# -*- python -*-
# -*- coding: utf-8 -*-
# Installsystems is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Installsystems 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 Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with Installsystems. If not, see <http://www.gnu.org/licenses/>.
'''
Install Systems Printer module
'''
from locale import getpreferredencoding
from os import linesep, _exit
from re import sub
from sys import stdout, stderr, exc_info
from traceback import print_exc
from warnings import filterwarnings
VERBOSITY = 1 # 0: quiet, 1: normal, 2: debug
# arrow_level is between 1 and 3
# is the level of indentation of arrow
def out(message="", fd=stdout, endl=linesep, flush=True):
'''
Print message colorised in fd ended by endl
'''
if message is None:
return
color_pattern = "#(%s)#" % "|".join(COLOR)
if not fd.isatty() or NOCOLOR:
f = lambda obj: ""
else:
f = lambda obj: COLOR[obj.group(1)]
# convert unicode into str before write
# this can cause issue on python 2.6
if type(message) == unicode:
message = message.encode(getpreferredencoding(), "replace")
# printing
fd.write("%s%s" % (message, endl))
if flush:
fd.flush()
'''
Print a message on stderr
'''
def fatal(message, quit=True, fd=stderr, endl=linesep):
out(u"#light##red#Fatal:#reset# #red#%s#reset#" % message, fd, endl)
def error(message=None, exception=None, quit=True, fd=stderr, endl=linesep):
# create error message
pmesg = u""
if message is not None:
pmesg += unicode(message)
if exception is not None:
if pmesg == "":
pmesg += unicode(exception)
else:
pmesg += u": %s" % unicode(exception)
# print error message
if pmesg != "":
out(u"#light##red#Error:#reset# #red#%s#reset#" % pmesg, fd, endl)
# print traceback in debug mode
if VERBOSITY > 1 and isinstance(exception, ISException):
out("#l##B#", fd=fd, endl="")
out("#R#", fd=fd, endl="")
out(u"#light##yellow#Warning:#reset# #yellow#%s#reset#" % message, fd, endl)
def info(message, fd=stderr, endl=linesep):
if VERBOSITY > 0:
out(u"#light#Info:#reset# %s" % message, fd, endl)
def debug(message, fd=stderr, endl=linesep):
'''
Print debug information
'''
if VERBOSITY > 1:
out(u"#light##black#%s#reset#" % message, fd, endl)
'''
Modify the current arrow level
'''
global _ARROW_LEVEL
old_level = _ARROW_LEVEL
_ARROW_LEVEL = max(1, min(4, _ARROW_LEVEL + inc))
def arrow(message, inclevel=None, level=None, fd=stdout, endl=linesep):
'''
Print a message prefixed by an arrow
Arrows have indentation levels
'''
if VERBOSITY == 0:
# define new level
old_level = arrowlevel(inc=inclevel, level=level)
out(u"#light##red#=>#reset# %s" % message, fd=fd, endl=endl)
out(u" #light##yellow#=>#reset# %s" % message, fd=fd, endl=endl)
out(u" #light##blue#=>#reset# %s" % message, fd=fd, endl=endl)
out(u" #light##green#=>#reset# %s" % message, fd=fd, endl=endl)
'''
Ask a question on stdin
'''
out(message, fd=fd, endl=endl, flush=True)
return raw_input()
def confirm(message=None, ans=None, fd=stdout, endl=""):
'''
Ask a question on stdin
'''
if ans is None:
ans = "yes"
if message is None:
message = u"#u##l##w#Are you sure?#R# (%s) " % ans
def setmode(verbosity=None, nocolor=None):
'''
Set printer mode
This is done to allow write access to global variables
'''
global VERBOSITY, NOCOLOR
if verbosity is not None:
# no warning if we are not in debug mode
if verbosity < 2:
filterwarnings("ignore")
VERBOSITY = verbosity
if nocolor is not None:
NOCOLOR = nocolor