Skip to content

Commit 0038599

Browse files
committed
initial release
1 parent 70b3207 commit 0038599

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

LICENSE

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright (c) 2013 by Marwan Alsabbagh and contributors.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above
11+
copyright notice, this list of conditions and the following
12+
disclaimer in the documentation and/or other materials provided
13+
with the distribution.
14+
15+
* The names of the contributors may not be used to endorse or
16+
promote products derived from this software without specific
17+
prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

whiptail.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# whiptail.py - Use whiptail to display dialog boxes from shell scripts
2+
# Copyright (C) 2013 Marwan Alsabbagh
3+
# license: BSD, see LICENSE for more details.
4+
5+
import sys
6+
import shlex
7+
from utile import save_args, flatten
8+
from subprocess import Popen, PIPE
9+
from collections import namedtuple
10+
11+
Response = namedtuple('Response', 'returncode value')
12+
13+
14+
class Whiptail(object):
15+
def __init__(self, title='', backtitle='', height=10, width=50, auto_exit=True):
16+
save_args(self, vars())
17+
18+
def run(self, control, msg, extra=(), exit_on=(1, 255)):
19+
cmd = ['whiptail', '--title', self.title, '--backtitle', self.backtitle,
20+
'--' + control, msg, str(self.height), str(self.width)] + list(extra)
21+
p = Popen(cmd, stderr=PIPE)
22+
out, err = p.communicate()
23+
if self.auto_exit and p.returncode in exit_on:
24+
print 'User cancelled operation.'
25+
sys.exit(p.returncode)
26+
return Response(p.returncode, err)
27+
28+
def prompt(self, msg, default='', password=False):
29+
control = 'passwordbox' if password else 'inputbox'
30+
return self.run(control, msg, [default]).value
31+
32+
def confirm(self, msg, default='yes'):
33+
defaultno = '--defaultno' if default == 'no' else ''
34+
return self.run('yesno', msg, [defaultno], [255]).returncode == 0
35+
36+
def alert(self, msg):
37+
self.run('msgbox', msg)
38+
39+
def view_file(self, path):
40+
self.run('textbox', path, ['--scrolltext'])
41+
42+
def calc_height(self, msg):
43+
height_offset = 8 if msg else 7
44+
return [str(self.height - height_offset)]
45+
46+
def menu(self, msg='', items=(), prefix=' - '):
47+
if isinstance(items[0], basestring):
48+
items = [(i, '') for i in items]
49+
else:
50+
items = [(k, prefix + v) for k, v in items]
51+
extra = self.calc_height(msg) + flatten(items)
52+
return self.run('menu', msg, extra).value
53+
54+
def showlist(self, control, msg, items, prefix):
55+
if isinstance(items[0], basestring):
56+
items = [(i, '', 'OFF') for i in items]
57+
else:
58+
items = [(k, prefix + v, s) for k, v, s in items]
59+
extra = self.calc_height(msg) + flatten(items)
60+
return shlex.split(self.run(control, msg, extra).value)
61+
62+
def radiolist(self, msg='', items=(), prefix=' - '):
63+
return self.showlist('radiolist', msg, items, prefix)
64+
65+
def checklist(self, msg='', items=(), prefix=' - '):
66+
return self.showlist('checklist', msg, items, prefix)

0 commit comments

Comments
 (0)