summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2006-07-19 05:54:33 +0000
committerZac Medico <zmedico@gentoo.org>2006-07-19 05:54:33 +0000
commitd646b91b95808e3a977f41e7b9e82974235424a5 (patch)
tree5004cdd3374340ecb49dd0f7fa0e670705b488cc
parent1501ee33a7b3f38c736c0373d4b10d8baf544bb6 (diff)
downloadportage-d646b91b95808e3a977f41e7b9e82974235424a5.tar.gz
portage-d646b91b95808e3a977f41e7b9e82974235424a5.tar.bz2
portage-d646b91b95808e3a977f41e7b9e82974235424a5.zip
Add an EOutput class that provides a python implementation of the familar e* funtions from /sbin/functions.sh. Thanks to Alex Tarkovsky <alextarkovsky@gmail.com> for this patch.
svn path=/main/trunk/; revision=3924
-rw-r--r--pym/output.py142
1 files changed, 142 insertions, 0 deletions
diff --git a/pym/output.py b/pym/output.py
index 458cafa81..ee5a7bf62 100644
--- a/pym/output.py
+++ b/pym/output.py
@@ -2,6 +2,7 @@
# Distributed under the terms of the GNU General Public License v2
# $Id$
+__docformat__ = "epytext"
import commands,errno,os,re,shlex,sys
from portage_const import COLOR_MAP_FILE
@@ -228,3 +229,144 @@ def create_color_func(color_key):
for c in compat_functions_colors:
setattr(sys.modules[__name__], c, create_color_func(c))
+
+class EOutput:
+ """
+ Performs fancy terminal formatting for status and informational messages.
+
+ The provided methods produce identical terminal output to the eponymous
+ functions in the shell script C{/sbin/functions.sh} and also accept
+ identical parameters.
+
+ This is not currently a drop-in replacement however, as the output-related
+ functions in C{/sbin/functions.sh} are oriented for use mainly by system
+ init scripts and ebuilds and their output can be customized via certain
+ C{RC_*} environment variables (see C{/etc/conf.d/rc}). B{EOutput} is not
+ customizable in this manner since it's intended for more general uses.
+ Likewise, no logging is provided.
+
+ @ivar quiet: Specifies if output should be silenced.
+ @type quiet: BooleanType
+ @ivar term_columns: Width of terminal in characters. Defaults to the value
+ specified by the shell's C{COLUMNS} variable, else to the queried tty
+ size, else to C{80}.
+ @type term_columns: IntType
+ """
+
+ def __init__(self):
+ self.__last_e_cmd = ""
+ self.__last_e_len = 0
+ self.quiet = False
+ self.term_columns = int(os.getenv("COLUMNS", 0))
+ if self.term_columns == 0:
+ self.term_columns = int(commands.getoutput('set -- `stty size 2>/dev/null` ; echo "$2"'))
+ if self.term_columns == 0:
+ self.term_columns = 80
+
+ def __eend(self, caller, errno, msg):
+ if errno == 0:
+ status_brackets = colorize("BRACKET", "[ ") + colorize("GOOD", "ok") + colorize("BRACKET", " ]")
+ else:
+ status_brackets = colorize("BRACKET", "[ ") + colorize("BAD", "!!") + colorize("BRACKET", " ]")
+ if msg:
+ if caller == "eend":
+ self.eerror(msg[0])
+ elif caller == "ewend":
+ self.ewarn(msg[0])
+ if self.__last_e_cmd != "ebegin":
+ self.__last_e_len = 0
+ print "%*s%s" % ((self.term_columns - self.__last_e_len - 6), "", status_brackets)
+
+ def ebegin(self, msg):
+ """
+ Shows a message indicating the start of a process.
+
+ @param msg: A very brief (shorter than one line) description of the
+ starting process.
+ @type msg: StringType
+ """
+ msg += " ..."
+ if not self.quiet:
+ self.einfon(msg)
+ self.__last_e_len = len(msg) + 4
+ self.__last_e_cmd = "ebegin"
+
+ def eend(self, errno, *msg):
+ """
+ Indicates the completion of a process, optionally displaying a message
+ via L{eerror} if the process's exit status isn't C{0}.
+
+ @param errno: A standard UNIX C{errno} code returned by processes upon
+ exit.
+ @type errno: IntType
+ @param msg: I{(optional)} An error message, typically a standard UNIX
+ error string corresponding to C{errno}.
+ @type msg: StringType
+ """
+ if not self.quiet:
+ self.__eend("eend", errno, msg)
+ self.__last_e_cmd = "eend"
+
+ def eerror(self, msg):
+ """
+ Shows an error message.
+
+ @param msg: A very brief (shorter than one line) error message.
+ @type msg: StringType
+ """
+ if not self.quiet:
+ if self.__last_e_cmd == "ebegin": print
+ print colorize("BAD", " * ") + msg
+ self.__last_e_cmd = "eerror"
+
+ def einfo(self, msg):
+ """
+ Shows an informative message terminated with a newline.
+
+ @param msg: A very brief (shorter than one line) informative message.
+ @type msg: StringType
+ """
+ if not self.quiet:
+ if self.__last_e_cmd == "ebegin": print
+ print colorize("GOOD", " * ") + msg
+ self.__last_e_cmd = "einfo"
+
+ def einfon(self, msg):
+ """
+ Shows an informative message terminated without a newline.
+
+ @param msg: A very brief (shorter than one line) informative message.
+ @type msg: StringType
+ """
+ if not self.quiet:
+ if self.__last_e_cmd == "ebegin": print
+ print colorize("GOOD", " * ") + msg ,
+ self.__last_e_cmd = "einfon"
+
+ def ewarn(self, msg):
+ """
+ Shows a warning message.
+
+ @param msg: A very brief (shorter than one line) warning message.
+ @type msg: StringType
+ """
+ if not self.quiet:
+ if self.__last_e_cmd == "ebegin": print
+ print colorize("WARN", " * ") + msg
+ self.__last_e_cmd = "ewarn"
+
+ def ewend(self, errno, *msg):
+ """
+ Indicates the completion of a process, optionally displaying a message
+ via L{ewarn} if the process's exit status isn't C{0}.
+
+ @param errno: A standard UNIX C{errno} code returned by processes upon
+ exit.
+ @type errno: IntType
+ @param msg: I{(optional)} A warning message, typically a standard UNIX
+ error string corresponding to C{errno}.
+ @type msg: StringType
+ """
+ if not self.quiet:
+ self.__eend("ewend", errno, msg)
+ self.__last_e_cmd = "ewend"