dosage/dosagelib/output.py

32 lines
993 B
Python
Raw Normal View History

# -*- coding: iso-8859-1 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012 Bastian Kleineidam
2012-06-20 19:58:13 +00:00
import time
class Output(object):
2012-09-26 14:47:39 +00:00
"""Print output with context, indentation and optional timestamps."""
2012-06-20 19:58:13 +00:00
def __init__(self):
2012-09-26 14:47:39 +00:00
"""Initialize context and indentation."""
2012-06-20 19:58:13 +00:00
self.context = ''
self.level = 0
self.timestamps = False
def write(self, s, level=0):
2012-09-26 14:47:39 +00:00
"""Write message with indentation, context and optional timestamp."""
2012-06-20 19:58:13 +00:00
if level > self.level:
return
if self.level > 1 or self.timestamps:
timestamp = time.strftime('%H:%M:%S ')
else:
timestamp = ''
print '%s%s> %s' % (timestamp, self.context, s)
def writelines(self, lines, level=0):
2012-09-26 14:47:39 +00:00
"""Write multiple messages."""
2012-06-20 19:58:13 +00:00
for line in lines:
for line in line.rstrip('\n').split('\n'):
self.write(line.rstrip('\n'), level=level)
out = Output()