Lines 2-7
Link Here
|
2 |
# Distributed under the terms of the GNU General Public License v2 |
2 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# $Id: output.py 3778 2006-07-03 17:35:45Z zmedico $ |
3 |
# $Id: output.py 3778 2006-07-03 17:35:45Z zmedico $ |
4 |
|
4 |
|
|
|
5 |
__docformat__ = "epytext" |
5 |
|
6 |
|
6 |
import commands,errno,os,re,shlex,sys |
7 |
import commands,errno,os,re,shlex,sys |
7 |
from portage_const import COLOR_MAP_FILE |
8 |
from portage_const import COLOR_MAP_FILE |
Lines 228-230
Link Here
|
228 |
|
229 |
|
229 |
for c in compat_functions_colors: |
230 |
for c in compat_functions_colors: |
230 |
setattr(sys.modules[__name__], c, create_color_func(c)) |
231 |
setattr(sys.modules[__name__], c, create_color_func(c)) |
|
|
232 |
|
233 |
class EOutput: |
234 |
""" |
235 |
Performs fancy terminal formatting for status and informational messages. |
236 |
|
237 |
The provided methods produce identical terminal output to the eponymous |
238 |
functions in the shell script C{/sbin/functions.sh} and also accept |
239 |
identical parameters. |
240 |
|
241 |
This is not currently a drop-in replacement however, as the output-related |
242 |
functions in C{/sbin/functions.sh} are oriented for use mainly by system |
243 |
init scripts and ebuilds and their output can be customized via certain |
244 |
C{RC_*} environment variables (see C{/etc/conf.d/rc}). B{EOutput} is not |
245 |
customizable in this manner since it's intended for more general uses. |
246 |
Likewise, no logging is provided. |
247 |
|
248 |
@ivar quiet: Specifies if output should be silenced. |
249 |
@type quiet: BooleanType |
250 |
@ivar term_columns: Width of terminal in characters. Defaults to the value |
251 |
specified by the shell's C{COLUMNS} variable, else to the queried tty |
252 |
size, else to C{80}. |
253 |
@type term_columns: IntegerType |
254 |
""" |
255 |
|
256 |
def __init__(self): |
257 |
self.__last_e_cmd = "" |
258 |
self.__last_e_len = 0 |
259 |
self.quiet = False |
260 |
self.term_columns = int(os.getenv("COLUMNS", 0)) |
261 |
if self.term_columns == 0: |
262 |
self.term_columns = int(commands.getoutput('set -- `stty size 2>/dev/null` ; echo "$2"')) |
263 |
if self.term_columns == 0: |
264 |
self.term_columns = 80 |
265 |
|
266 |
def __eend(self, caller, errno, msg): |
267 |
if errno == 0: |
268 |
status_brackets = colorize("BRACKET", "[ ") + colorize("GOOD", "ok") + colorize("BRACKET", " ]") |
269 |
else: |
270 |
status_brackets = colorize("BRACKET", "[ ") + colorize("BAD", "!!") + colorize("BRACKET", " ]") |
271 |
if msg: |
272 |
if caller == "eend": |
273 |
self.eerror(msg[0]) |
274 |
elif caller == "ewend": |
275 |
self.ewarn(msg[0]) |
276 |
if self.__last_e_cmd != "ebegin": |
277 |
self.__last_e_len = 0 |
278 |
print "%*s%s" % ((self.term_columns - self.__last_e_len - 6), "", status_brackets) |
279 |
|
280 |
def ebegin(self, msg): |
281 |
""" |
282 |
Shows a message indicating the start of a process. |
283 |
|
284 |
@param msg: A very brief (shorter than one line) description of the |
285 |
starting process. |
286 |
@type msg: StringType |
287 |
""" |
288 |
msg += " ..." |
289 |
if not self.quiet: |
290 |
self.einfon(msg) |
291 |
self.__last_e_len = len(msg) + 4 |
292 |
self.__last_e_cmd = "ebegin" |
293 |
|
294 |
def eend(self, errno, *msg): |
295 |
""" |
296 |
Indicates the completion of a process, optionally displaying a message |
297 |
via L{eerror} if the process's exit status isn't C{0}. |
298 |
|
299 |
@param errno: A standard UNIX C{errno} code returned by processes upon |
300 |
exit. |
301 |
@type errno: IntType |
302 |
@param msg: I{(optional)} An error message, typically a standard UNIX |
303 |
error string corresponding to C{errno}. |
304 |
@type msg: StringType |
305 |
""" |
306 |
if not self.quiet: |
307 |
self.__eend("eend", errno, msg) |
308 |
self.__last_e_cmd = "eend" |
309 |
|
310 |
def eerror(self, msg): |
311 |
""" |
312 |
Shows an error message. |
313 |
|
314 |
@param msg: A very brief (shorter than one line) error message. |
315 |
@type msg: StringType |
316 |
""" |
317 |
if not self.quiet: |
318 |
if self.__last_e_cmd == "ebegin": print |
319 |
print colorize("BAD", " * ") + msg |
320 |
self.__last_e_cmd = "eerror" |
321 |
|
322 |
def einfo(self, msg): |
323 |
""" |
324 |
Shows an informative message terminated with a newline. |
325 |
|
326 |
@param msg: A very brief (shorter than one line) informative message. |
327 |
@type msg: StringType |
328 |
""" |
329 |
if not self.quiet: |
330 |
if self.__last_e_cmd == "ebegin": print |
331 |
print colorize("GOOD", " * ") + msg |
332 |
self.__last_e_cmd = "einfo" |
333 |
|
334 |
def einfon(self, msg): |
335 |
""" |
336 |
Shows an informative message terminated without a newline. |
337 |
|
338 |
@param msg: A very brief (shorter than one line) informative message. |
339 |
@type msg: StringType |
340 |
""" |
341 |
if not self.quiet: |
342 |
if self.__last_e_cmd == "ebegin": print |
343 |
print colorize("GOOD", " * ") + msg , |
344 |
self.__last_e_cmd = "einfon" |
345 |
|
346 |
def ewarn(self, msg): |
347 |
""" |
348 |
Shows a warning message. |
349 |
|
350 |
@param msg: A very brief (shorter than one line) warning message. |
351 |
@type msg: StringType |
352 |
""" |
353 |
if not self.quiet: |
354 |
if self.__last_e_cmd == "ebegin": print |
355 |
print colorize("WARN", " * ") + msg |
356 |
self.__last_e_cmd = "ewarn" |
357 |
|
358 |
def ewend(self, errno, *msg): |
359 |
""" |
360 |
Indicates the completion of a process, optionally displaying a message |
361 |
via L{ewarn} if the process's exit status isn't C{0}. |
362 |
|
363 |
@param errno: A standard UNIX C{errno} code returned by processes upon |
364 |
exit. |
365 |
@type errno: IntType |
366 |
@param msg: I{(optional)} An error message, typically a standard UNIX |
367 |
error string corresponding to C{errno}. |
368 |
@type msg: StringType |
369 |
""" |
370 |
if not self.quiet: |
371 |
self.__eend("ewend", errno, msg) |
372 |
self.__last_e_cmd = "ewend" |