Update version.py and ChangeLog for 0.6.1
[snf-image-creator] / image_creator / output / dialog.py
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 """This module provides various dialog-based Output classes"""
37
38 from image_creator.output import Output
39 import time
40 import fcntl
41
42
43 class GaugeOutput(Output):
44     """Output class implemented using dialog's gauge widget"""
45     def __init__(self, dialog, title, msg=''):
46         self.d = dialog
47         self.msg = msg
48         self.percent = 0
49         self.d.gauge_start(self.msg, title=title)
50
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)
61
62     def output(self, msg='', new_line=True):
63         """Print msg as normal output"""
64         self.msg = msg
65         self.percent = 0
66         self.d.gauge_update(self.percent, self.msg, update_text=True)
67         time.sleep(0.4)
68
69     def success(self, result, new_line=True):
70         """Print result after a successfull action"""
71         self.percent = 100
72         self.d.gauge_update(self.percent, "%s %s" % (self.msg, result),
73                             update_text=True)
74         time.sleep(0.4)
75
76     def warn(self, msg, new_line=True):
77         """Print a warning"""
78         self.d.gauge_update(self.percent, "%s Warning: %s" % (self.msg, msg),
79                             update_text=True)
80         time.sleep(0.4)
81
82     def cleanup(self):
83         """Cleanup the GaugeOutput instance"""
84         self.d.gauge_stop()
85
86     class _Progress(Output._Progress):
87         """Progress class for dialog's gauge widget"""
88         template = {
89             'default': '%(index)d/%(size)d',
90             'percent': '',
91             'b': "%(index)d/%(size)d B",
92             'kb': "%(index)d/%(size)d KB",
93             'mb': "%(index)d/%(size)d MB"
94         }
95
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
100             self.goto(0)
101
102         def _postfix(self):
103             return self.template[self.bar_type] % self.output.__dict__
104
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,
111                                        update_text=True)
112
113         def next(self):
114             """Move progress bar one step forward"""
115             self.goto(self.output.index + 1)
116
117
118 class InfoBoxOutput(Output):
119     """Output class implemented using dialog's infobox widget"""
120     def __init__(self, dialog, title, msg='', height=20, width=70):
121         self.d = dialog
122         self.title = title
123         self.msg = msg
124         self.width = width
125         self.height = height
126         self.d.infobox(self.msg, title=self.title)
127
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()
134         # The height of the active region is 2 lines shorter that the height of
135         # the dialog
136         h = self.height - 2
137         display = self.msg if len(lines) <= h else "\n".join(lines[-h:])
138         self.d.infobox(display, title=self.title, height=self.height,
139                        width=self.width)
140
141     def success(self, result, new_line=True):
142         """Print result after an action is completed successfully"""
143         self.output(result, new_line)
144
145     def warn(self, msg, new_line=True):
146         """Print a warning message"""
147         self.output("Warning: %s" % msg, new_line)
148
149     def finalize(self):
150         """Finalize the output. After this is called, the InfoboxOutput
151         instance should be destroyed
152         """
153         self.d.msgbox(self.msg, title=self.title, height=(self.height + 2),
154                       width=self.width)
155
156 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :