Check if stdout is a tty
[snf-image-creator] / image_creator / output.py
1 # Copyright 2012 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 import sys
35 from progress.bar import Bar
36 from colors import red, green, yellow
37
38
39 def error(msg, new_line=True, color=True):
40     nl = "\n" if new_line else ''
41     if color:
42         sys.stderr.write(red('Error: %s' % msg) + nl)
43     else:
44         sys.stderr.write('Error: %s' % msg + nl)
45
46
47 def warn(msg, new_line=True, color=True):
48     nl = "\n" if new_line else ''
49     if color:
50         sys.stderr.write(yellow("Warning: %s" % msg) + nl)
51     else:
52         sys.stderr.write("Warning: %s" % msg + nl)
53
54
55 def success(msg, new_line=True, color=True):
56     nl = "\n" if new_line else ''
57     if color:
58         sys.stdout.write(green(msg) + nl)
59     else:
60         sys.stdout.write(msg + nl)
61     if not nl:
62         sys.stdout.flush()
63
64
65 def output(msg='', new_line=True):
66     nl = "\n" if new_line else ''
67     sys.stdout.write(msg + nl)
68     if not nl:
69         sys.stdout.flush()
70
71
72 class Output(object):
73     def error(self, msg, new_line=True):
74         error(msg, new_line, False)
75
76     def warn(self, msg, new_line=True):
77         warn(msg, new_line, False)
78
79     def success(self, msg, new_line=True):
80         success(msg, new_line, False)
81
82     def output(self, msg='', new_line=True):
83         output(msg, new_line)
84
85     class Progress(object):
86         def __init__(self, title, bar_type='default'):
87             output("%s..." % title, False)
88
89         def goto(self, dest):
90             pass
91
92         def next(self):
93             pass
94
95         def success(self, result):
96             sucess(result)
97
98     def progress_generator(self, message):
99         def generator(n):
100             progressbar = self.Progress(message, 'default')
101             progressbar.max = n
102
103             for _ in range(n):
104                 yield
105                 progressbar.next()
106
107             progressbar.success('done')
108             yield
109         return generator
110
111
112 class Output_wth_colors(Output):
113     def error(self, msg, new_line=True):
114         error(msg, new_line)
115
116     def warn(self, msg, new_line=True):
117         warn(msg, new_line)
118
119     def success(self, msg, new_line=True):
120         success(msg, new_line)
121
122
123 class Output_wth_progress(Output_wth_colors):
124     class Progress(Bar):
125         MESSAGE_LENGTH = 30
126
127         template = {
128             'default': '%(index)d/%(max)d',
129             'percent': '%(percent)d%%',
130             'b': '%(index)d/%(max)d B',
131             'kb': '%(index)d/%(max)d KB',
132             'mb': '%(index)d/%(max)d MB'
133         }
134
135         def __init__(self, title, bar_type='default'):
136             super(Output_wth_progress.Progress, self).__init__()
137             self.title = title
138             self.fill = '#'
139             self.bar_prefix = ' ['
140             self.bar_suffix = '] '
141             self.message = ("%s:" % self.title).ljust(self.MESSAGE_LENGTH)
142             self.suffix = self.template[bar_type]
143
144         def success(self, result):
145             output("\r%s... \033[K" % self.title, False)
146             success(result)
147
148
149 class Silent(Output):
150     def warn(self, msg, new_line=True):
151         pass
152
153     def success(self, msg, new_line=True):
154         pass
155
156     def output(self, msg='', new_line=True):
157         pass
158
159     class Progress(Output.Progress):
160         def __init__(self, title, bar_type='default'):
161             pass
162
163         def success(self, result):
164             pass
165
166 class Silent_wth_colors(Silent):
167     def error(self, msg, new_line=True):
168         error(msg, new_line)
169
170 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :