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 """This module provides various dialog-based Output classes"""
38 from image_creator.output import Output
43 class GaugeOutput(Output):
44 """Output class implemented using dialog's gauge widget"""
45 def __init__(self, dialog, title, msg=''):
49 self.d.gauge_start(self.msg, title=title)
51 # Open pipe workaround. A fork will dublicate the open file descriptor.
52 # The FD_CLOEXEC flag makes sure that the gauge internal fd will be
53 # closed if execve is executed. This is needed because libguestfs will
54 # fork/exec the kvm process. If this fd stays open in the kvm process,
55 # then doing a gauge_stop will make this process wait forever for
56 # a dialog process that is blocked waiting for input from the kvm
57 # process's open file descriptor.
58 fd = self.d._gauge_process['stdin'].fileno()
59 flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
60 fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
62 def output(self, msg='', new_line=True):
63 """Print msg as normal output"""
66 self.d.gauge_update(self.percent, self.msg, update_text=True)
69 def success(self, result, new_line=True):
70 """Print result after a successfull action"""
72 self.d.gauge_update(self.percent, "%s %s" % (self.msg, result),
76 def warn(self, msg, new_line=True):
78 self.d.gauge_update(self.percent, "%s Warning: %s" % (self.msg, msg),
83 """Cleanup the GaugeOutput instance"""
86 class _Progress(Output._Progress):
87 """Progress class for dialog's gauge widget"""
89 'default': '%(index)d/%(size)d',
91 'b': "%(index)d/%(size)d B",
92 'kb': "%(index)d/%(size)d KB",
93 'mb': "%(index)d/%(size)d MB"
96 def __init__(self, size, title, bar_type='default'):
97 self.output.size = size
98 self.bar_type = bar_type
99 self.output.msg = "%s ..." % title
103 return self.template[self.bar_type] % self.output.__dict__
105 def goto(self, dest):
106 """Move progress bar to a specific position"""
107 self.output.index = dest
108 self.output.percent = self.output.index * 100 // self.output.size
109 msg = "%s %s" % (self.output.msg, self._postfix())
110 self.output.d.gauge_update(self.output.percent, msg,
114 """Move progress bar one step forward"""
115 self.goto(self.output.index + 1)
118 class InfoBoxOutput(Output):
119 """Output class implemented using dialog's infobox widget"""
120 def __init__(self, dialog, title, msg='', height=20, width=70):
126 self.d.infobox(self.msg, title=self.title)
128 def output(self, msg='', new_line=True):
129 """Print msg as normal output"""
130 nl = '\n' if new_line else ''
131 self.msg += "%s%s" % (msg, nl)
132 # If output is long, only output the last lines that fit in the box
133 lines = self.msg.splitlines()
135 display = self.msg if len(lines) <= h else "\n".join(lines[-h:])
136 self.d.infobox(display, title=self.title, height=self.height,
139 def success(self, result, new_line=True):
140 """Print result after an action is completed successfully"""
141 self.output(result, new_line)
143 def warn(self, msg, new_line=True):
144 """Print a warning message"""
145 self.output("Warning: %s" % msg, new_line)
148 """Finalize the output. After this is called, the InfoboxOutput
149 instance should be destroyed
151 self.d.msgbox(self.msg, title=self.title, height=(self.height + 2),
154 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :