Statistics
| Branch: | Tag: | Revision:

root / image_creator / output / cli.py @ 12c97404

History | View | Annotate | Download (4.6 kB)

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