root / image_creator / output / cli.py @ b6765b7e
History | View | Annotate | Download (4.6 kB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
#
|
3 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
4 |
#
|
5 |
# Redistribution and use in source and binary forms, with or
|
6 |
# without modification, are permitted provided that the following
|
7 |
# conditions are met:
|
8 |
#
|
9 |
# 1. Redistributions of source code must retain the above
|
10 |
# copyright notice, this list of conditions and the following
|
11 |
# disclaimer.
|
12 |
#
|
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.
|
17 |
#
|
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.
|
30 |
#
|
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.
|
35 |
|
36 |
"""Normal Command-line interface output"""
|
37 |
|
38 |
from image_creator.output import Output |
39 |
|
40 |
import sys |
41 |
from colors import red, green, yellow |
42 |
from progress.bar import Bar |
43 |
|
44 |
|
45 |
def output(msg, new_line, decorate, stream): |
46 |
nl = "\n" if new_line else ' ' |
47 |
stream.write(decorate(msg) + nl) |
48 |
|
49 |
|
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)
|
53 |
|
54 |
|
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)
|
58 |
|
59 |
|
60 |
def success(msg, new_line, colored, stream): |
61 |
color = green if colored else lambda x: x |
62 |
output(msg, new_line, color, stream) |
63 |
|
64 |
|
65 |
def clear(stream): |
66 |
"""Clears the terminal screen."""
|
67 |
if stream.isatty():
|
68 |
stream.write('\033[H\033[2J')
|
69 |
|
70 |
|
71 |
class SilentOutput(Output): |
72 |
"""Silent Output class. Only Errors are printed"""
|
73 |
pass
|
74 |
|
75 |
|
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 |
83 |
|
84 |
def error(self, msg, new_line=True): |
85 |
"""Print an error"""
|
86 |
error(msg, new_line, self.colored, self.stream) |
87 |
|
88 |
def warn(self, msg, new_line=True): |
89 |
"""Print a warning"""
|
90 |
warn(msg, new_line, self.colored, self.stream) |
91 |
|
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) |
95 |
|
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) |
99 |
|
100 |
def clear(self): |
101 |
"""Clear the screen"""
|
102 |
clear(self.stream)
|
103 |
|
104 |
|
105 |
class OutputWthProgress(SimpleOutput): |
106 |
"""Output class with progress."""
|
107 |
class _Progress(Bar): |
108 |
MESSAGE_LENGTH = 30
|
109 |
|
110 |
template = { |
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' |
116 |
} |
117 |
|
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__() |
122 |
self.title = title
|
123 |
self.fill = '#' |
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] |
128 |
self.max = size
|
129 |
|
130 |
# print empty progress bar
|
131 |
self.start()
|
132 |
|
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)
|
137 |
|
138 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|