Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
-import sys
+import sys, textwrap
class MessagePrinter(object):
- def __init__(self):
+ def __init__(self, file = None):
class Output(object):
def __init__(self, write, flush):
self.write = write
self.at_start_of_line = False
def tagged_lines(self, tag, lines):
tag += ': '
+ width = 79 - 2*self.level - len(tag)
+ lines = [wl for line in lines
+ for wl in textwrap.wrap(line, width,
+ break_long_words = False)]
for line in lines:
self.single_line(tag + line)
tag = ' '*len(tag)
self.new_line()
self.write(string)
self.at_start_of_line = string.endswith('\n')
- self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
- if sys.stdout.isatty():
+ if file:
+ self.__stdout = self.__stderr = Output(file.write, file.flush)
+ else:
+ self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
+ self.__stderr = Output(sys.stdout.write, sys.stdout.flush)
+ if file or sys.stdout.isatty():
self.__out = self.__stdout
+ self.__err = self.__stdout
else:
self.__out = Output(lambda msg: None, lambda: None)
+ self.__err = self.__stderr
def stdout(self, line):
"""Write a line to stdout."""
self.__stdout.write_line(line)
def stdout_raw(self, string):
"""Write a string possibly containing newlines to stdout."""
self.__stdout.write_raw(string)
+ def err_raw(self, string):
+ """Write a string possibly containing newlines to the error
+ output."""
+ self.__err.write_raw(string)
def info(self, *msgs):
for msg in msgs:
self.__out.single_line(msg)
- def note(self, *msgs):
- self.__out.tagged_lines('Notice', msgs)
- def warn(self, *msgs):
- self.__out.tagged_lines('Warning', msgs)
- def error(self, *msgs):
- self.__out.tagged_lines('Error', msgs)
+ def note(self, *msgs, **kw):
+ self.__out.tagged_lines(kw.get('title', 'Notice'), msgs)
+ def warn(self, *msgs, **kw):
+ self.__err.tagged_lines(kw.get('title', 'Warning'), msgs)
+ def error(self, *msgs, **kw):
+ self.__err.tagged_lines(kw.get('title', 'Error'), msgs)
def start(self, msg):
"""Start a long-running operation."""
self.__out.single_line('%s ... ' % msg, print_newline = False)