Assert that the argument to Run is a sequence of strings
[stgit] / stgit / out.py
CommitLineData
5e888f30
KH
1# -*- coding: utf-8 -*-
2
3__copyright__ = """
4Copyright (C) 2007, Karl Hasselström <kha@treskal.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License version 2 as
8published by the Free Software Foundation.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18"""
19
20import sys
21
22class MessagePrinter(object):
23 def __init__(self):
24 class Output(object):
25 def __init__(self, write, flush):
26 self.write = write
27 self.flush = flush
28 self.at_start_of_line = True
29 self.level = 0
30 def new_line(self):
31 """Ensure that we're at the beginning of a line."""
32 if not self.at_start_of_line:
33 self.write('\n')
34 self.at_start_of_line = True
35 def single_line(self, msg, print_newline = True,
36 need_newline = True):
37 """Write a single line. Newline before and after are
38 separately configurable."""
39 if need_newline:
40 self.new_line()
41 if self.at_start_of_line:
42 self.write(' '*self.level)
43 self.write(msg)
44 if print_newline:
45 self.write('\n')
46 self.at_start_of_line = True
47 else:
48 self.flush()
49 self.at_start_of_line = False
50 def tagged_lines(self, tag, lines):
51 tag += ': '
52 for line in lines:
53 self.single_line(tag + line)
54 tag = ' '*len(tag)
55 def write_line(self, line):
56 """Write one line of text on a lines of its own, not
57 indented."""
58 self.new_line()
59 self.write('%s\n' % line)
60 self.at_start_of_line = True
61 def write_raw(self, string):
62 """Write an arbitrary string, possibly containing
63 newlines."""
64 self.new_line()
65 self.write(string)
66 self.at_start_of_line = string.endswith('\n')
67 self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
68 if sys.stdout.isatty():
69 self.__out = self.__stdout
70 else:
71 self.__out = Output(lambda msg: None, lambda: None)
72 def stdout(self, line):
73 """Write a line to stdout."""
74 self.__stdout.write_line(line)
75 def stdout_raw(self, string):
76 """Write a string possibly containing newlines to stdout."""
77 self.__stdout.write_raw(string)
78 def info(self, *msgs):
79 for msg in msgs:
80 self.__out.single_line(msg)
81 def note(self, *msgs):
82 self.__out.tagged_lines('Notice', msgs)
83 def warn(self, *msgs):
84 self.__out.tagged_lines('Warning', msgs)
85 def error(self, *msgs):
86 self.__out.tagged_lines('Error', msgs)
87 def start(self, msg):
88 """Start a long-running operation."""
89 self.__out.single_line('%s ... ' % msg, print_newline = False)
90 self.__out.level += 1
91 def done(self, extramsg = None):
92 """Finish long-running operation."""
93 self.__out.level -= 1
94 if extramsg:
95 msg = 'done (%s)' % extramsg
96 else:
97 msg = 'done'
98 self.__out.single_line(msg, need_newline = False)
99
100out = MessagePrinter()