1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
34 from image_creator.output import Output
39 class GaugeOutput(Output):
40 """Output class implemented using dialog's gauge widget"""
41 def __init__(self, dialog, title, msg=''):
45 self.d.gauge_start(self.msg, title=title)
47 # Open pipe workaround. A fork will dublicate the open file descriptor.
48 # The FD_CLOEXEC flag makes sure that the gauge internal fd will be
49 # closed if execve is executed. This is needed because libguestfs will
50 # fork/exec the kvm process. If this fd stays open in the kvm process,
51 # then doing a gauge_stop will make this process wait forever for
52 # a dialog process that is blocked waiting for input from the kvm
53 # process's open file descriptor.
54 fd = self.d._gauge_process['stdin'].fileno()
55 flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
56 fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
58 def output(self, msg='', new_line=True):
59 """Print msg as normal output"""
62 self.d.gauge_update(self.percent, self.msg, update_text=True)
65 def success(self, result, new_line=True):
66 """Print result after a successfull action"""
68 self.d.gauge_update(self.percent, "%s %s" % (self.msg, result),
72 def warn(self, msg, new_line=True):
74 self.d.gauge_update(self.percent, "%s Warning: %s" % (self.msg, msg),
79 """Cleanup the GaugeOutput instance"""
82 class _Progress(Output._Progress):
83 """Progress class for dialog's gauge widget"""
85 'default': '%(index)d/%(size)d',
87 'b': "%(index)d/%(size)d B",
88 'kb': "%(index)d/%(size)d KB",
89 'mb': "%(index)d/%(size)d MB"
92 def __init__(self, size, title, bar_type='default'):
93 self.output.size = size
94 self.bar_type = bar_type
95 self.output.msg = "%s ..." % title
99 return self.template[self.bar_type] % self.output.__dict__
101 def goto(self, dest):
102 """Move progress bar to a specific position"""
103 self.output.index = dest
104 self.output.percent = self.output.index * 100 // self.output.size
105 msg = "%s %s" % (self.output.msg, self._postfix())
106 self.output.d.gauge_update(self.output.percent, msg,
110 """Move progress bar one step forward"""
111 self.goto(self.output.index + 1)
114 class InfoBoxOutput(Output):
115 """Output class implemented using dialog's infobox widget"""
116 def __init__(self, dialog, title, msg='', height=20, width=70):
122 self.d.infobox(self.msg, title=self.title)
124 def output(self, msg='', new_line=True):
125 """Print msg as normal output"""
126 nl = '\n' if new_line else ''
127 self.msg += "%s%s" % (msg, nl)
128 # If output is long, only output the last lines that fit in the box
129 lines = self.msg.splitlines()
131 display = self.msg if len(lines) <= h else "\n".join(lines[-h:])
132 self.d.infobox(display, title=self.title, height=self.height,
135 def success(self, result, new_line=True):
136 """Print result after an action is completed successfully"""
137 self.output(result, new_line)
139 def warn(self, msg, new_line=True):
140 """Print a warning message"""
141 self.output("Warning: %s" % msg, new_line)
144 """Finalize the output. After this is called, the InfoboxOutput
145 instance should be destroyed
147 self.d.msgbox(self.msg, title=self.title, height=(self.height + 2),
150 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :