Revision 789a3763 image_creator/output/cli.py

b/image_creator/output/cli.py
38 38
from progress.bar import Bar
39 39

  
40 40

  
41
def output(msg='', new_line=True, decorate=lambda x: x):
41
def output(msg, new_line, decorate, stream):
42 42
    nl = "\n" if new_line else ' '
43
    sys.stderr.write(decorate(msg) + nl)
43
    stream.write(decorate(msg) + nl)
44 44

  
45 45

  
46
def error(msg, new_line=True, colored=True):
46
def error(msg, new_line, colored, stream):
47 47
    color = red if colored else lambda x: x
48
    output("Error: %s" % msg, new_line, color)
48
    output("Error: %s" % msg, new_line, color, stream)
49 49

  
50 50

  
51
def warn(msg, new_line=True, colored=True):
51
def warn(msg, new_line, colored, stream):
52 52
    color = yellow if colored else lambda x: x
53
    output("Warning: %s" % msg, new_line, color)
53
    output("Warning: %s" % msg, new_line, color, stream)
54 54

  
55 55

  
56
def success(msg, new_line=True, colored=True):
56
def success(msg, new_line, colored, stream):
57 57
    color = green if colored else lambda x: x
58
    output(msg, new_line, color)
58
    output(msg, new_line, color, stream)
59 59

  
60 60

  
61
def clear():
61
def clear(stream):
62 62
    #clear the page
63
    if sys.stderr.isatty():
64
        sys.stderr.write('\033[H\033[2J')
63
    if stream.isatty():
64
        stream.write('\033[H\033[2J')
65 65

  
66 66

  
67 67
class SilentOutput(Output):
......
69 69

  
70 70

  
71 71
class SimpleOutput(Output):
72
    def __init__(self, colored=True):
72
    def __init__(self, colored=True, stream=None):
73 73
        self.colored = colored
74
        self.stream = sys.stderr if stream is None else stream
74 75

  
75 76
    def error(self, msg, new_line=True):
76
        error(msg, new_line, self.colored)
77
        error(msg, new_line, self.colored, self.stream)
77 78

  
78 79
    def warn(self, msg, new_line=True):
79
        warn(msg, new_line, self.colored)
80
        warn(msg, new_line, self.colored, self.stream)
80 81

  
81 82
    def success(self, msg, new_line=True):
82
        success(msg, new_line, self.colored)
83
        success(msg, new_line, self.colored, self.stream)
83 84

  
84 85
    def output(self, msg='', new_line=True):
85
        output(msg, new_line)
86
        output(msg, new_line, lambda x: x, self.stream)
86 87

  
87 88
    def clear(self):
88
        clear()
89
        clear(self.stream)
89 90

  
90 91

  
91 92
class OutputWthProgress(SimpleOutput):
......
117 118
            self.output.output("\r%s...\033[K" % self.title, False)
118 119
            self.output.success(result)
119 120

  
120

  
121 121
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :

Also available in: Unified diff