1 # -*- coding: utf-8 -*-
3 # Copyright 2012 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials
16 # provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
31 # The views and conclusions contained in the software and
32 # documentation are those of the authors and should not be
33 # interpreted as representing official policies, either expressed
34 # or implied, of GRNET S.A.
36 """Normal Command-line interface output"""
38 from image_creator.output import Output
41 from colors import red, green, yellow
42 from progress.bar import Bar
45 def output(msg, new_line, decorate, stream):
46 nl = "\n" if new_line else ' '
47 stream.write(decorate(msg) + nl)
50 def error(msg, new_line, colored, stream):
51 color = red if colored else lambda x: x
52 output("Error: %s" % msg, new_line, color, stream)
55 def warn(msg, new_line, colored, stream):
56 color = yellow if colored else lambda x: x
57 output("Warning: %s" % msg, new_line, color, stream)
60 def success(msg, new_line, colored, stream):
61 color = green if colored else lambda x: x
62 output(msg, new_line, color, stream)
66 """Clears the terminal screen."""
68 stream.write('\033[H\033[2J')
71 class SilentOutput(Output):
72 """Silent Output class. Only Errors are printed"""
76 class SimpleOutput(Output):
77 """Print messages but not progress bars. Progress bars are treated as
78 output messages. The user gets informed when the action begins and when it
79 ends, but no progress is shown in between."""
80 def __init__(self, colored=True, stream=None):
81 self.colored = colored
82 self.stream = sys.stderr if stream is None else stream
84 def error(self, msg, new_line=True):
86 error(msg, new_line, self.colored, self.stream)
88 def warn(self, msg, new_line=True):
90 warn(msg, new_line, self.colored, self.stream)
92 def success(self, msg, new_line=True):
93 """Print msg after an action is completed"""
94 success(msg, new_line, self.colored, self.stream)
96 def output(self, msg='', new_line=True):
97 """Print msg as normal program output"""
98 output(msg, new_line, lambda x: x, self.stream)
101 """Clear the screen"""
105 class OutputWthProgress(SimpleOutput):
106 """Output class with progress."""
107 class _Progress(Bar):
111 'default': '%(index)d/%(max)d',
112 'percent': '%(percent)d%%',
113 'b': '%(index)d/%(max)d B',
114 'kb': '%(index)d/%(max)d KB',
115 'mb': '%(index)d/%(max)d MB'
118 def __init__(self, size, title, bar_type='default'):
119 """Create a Progress bar"""
120 self.hide_cursor = False
121 super(OutputWthProgress._Progress, self).__init__()
124 self.bar_prefix = ' ['
125 self.bar_suffix = '] '
126 self.message = ("%s:" % self.title).ljust(self.MESSAGE_LENGTH)
127 self.suffix = self.template[bar_type]
130 # print empty progress bar
133 def success(self, result):
134 """Print result after progress has finished"""
135 self.output.output("\r%s ...\033[K" % self.title, False)
136 self.output.success(result)
138 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :