summaryrefslogtreecommitdiff
path: root/Build/source/utils/dialog/dialog-1.1-20080819/samples/dialog.py
blob: db66fd7f2f4b31ff9f7b1daf98afeee675ed6abe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# $Id: dialog.py,v 1.3 2004/09/21 00:52:15 tom Exp $
# Module: dialog.py
# Copyright (c) 2000 Robb Shecter <robb@acm.org>
# All rights reserved.
# This source is covered by the GNU GPL.
#
# This module is a Python wrapper around the Linux "dialog" utility
# by Savio Lam and Stuart Herbert.  My goals were to make dialog as
# easy to use from Python as possible.  The demo code at the end of
# the module is a good example of how to use it.  To run the demo,
# execute:
#
#                       python dialog.py
#
# This module has one class in it, "Dialog".  An application typically
# creates an instance of it, and possibly sets the background title option.
# Then, methods can be called on it for interacting with the user.
#
# I wrote this because I want to use my 486-33 laptop as my main
# development computer (!), and I wanted a way to nicely interact with the
# user in console mode.  There are apparently other modules out there
# with similar functionality, but they require the Python curses library.
# Writing this module from scratch was easier than figuring out how to
# recompile Python with curses enabled. :)
#
# One interesting feature is that the menu and selection windows allow
# *any* objects to be displayed and selected, not just strings.
#
# TO DO:
#   Add code so that the input buffer is flushed before a dialog box is
#     shown.  This would make the UI more predictable for users.  This
#     feature could be turned on and off through an instance method.
#   Drop using temporary files when interacting with 'dialog'
#     (it's possible -- I've already tried :-).
#   Try detecting the terminal window size in order to make reasonable
#     height and width defaults.  Hmmm - should also then check for 
#     terminal resizing...
#   Put into a package name to make more reusable - reduce the possibility
#     of name collisions.
#
# NOTES:
#         there is a bug in (at least) Linux-Mandrake 7.0 Russian Edition
#         running on AMD K6-2 3D that causes core dump when 'dialog' 
#         is running with --gauge option;
#         in this case you'll have to recompile 'dialog' program.
#
# Modifications:
# Jul 2000, Sultanbek Tezadov (http://sultan.da.ru)
#    Added:
#       - 'gauge' widget *)
#       - 'title' option to some widgets
#       - 'checked' option to checklist dialog; clicking "Cancel" is now
#           recognizable
#       - 'selected' option to radiolist dialog; clicking "Cancel" is now
#           recognizable
#       - some other cosmetic changes and improvements
#   

import os
from tempfile import mktemp
from string import split
from time import sleep

#
# Path of the dialog executable
#
DIALOG = os.getenv("DIALOG");
if DIALOG is None:
	DIALOG="../dialog";

class Dialog:
    def __init__(self):
	self.__bgTitle = ''               # Default is no background title


    def setBackgroundTitle(self, text):
	self.__bgTitle = '--backtitle "%s"' % text


    def __perform(self, cmd):
	"""Do the actual work of invoking dialog and getting the output."""
	fName = mktemp()
	rv = os.system('%s %s %s 2> %s' % (DIALOG, self.__bgTitle, cmd, fName))
	f = open(fName)
	output = f.readlines()
	f.close()
	os.unlink(fName)
	return (rv, output)


    def __perform_no_options(self, cmd):
	"""Call dialog w/out passing any more options. Needed by --clear."""
	return os.system(DIALOG + ' ' + cmd)


    def __handleTitle(self, title):
	if len(title) == 0:
	    return ''
	else:
	    return '--title "%s" ' % title


    def yesno(self, text, height=10, width=30, title=''):
	"""
	Put a Yes/No question to the user.
	Uses the dialog --yesno option.
	Returns a 1 or a 0.
	"""
	(code, output) = self.__perform(self.__handleTitle(title) +\
	    '--yesno "%s" %d %d' % (text, height, width))
	return code == 0


    def msgbox(self, text, height=10, width=30, title=''):
	"""
	Pop up a message to the user which has to be clicked
	away with "ok".
	"""
	self.__perform(self.__handleTitle(title) +\
	    '--msgbox "%s" %d %d' % (text, height, width))


    def infobox(self, text, height=10, width=30):
	"""Make a message to the user, and return immediately."""
	self.__perform('--infobox "%s" %d %d' % (text, height, width))


    def inputbox(self, text, height=10, width=30, init='', title=''):
	"""
	Request a line of input from the user.
	Returns the user's input or None if cancel was chosen.
	"""
	(c, o) = self.__perform(self.__handleTitle(title) +\
	    '--inputbox "%s" %d %d "%s"' % (text, height, width, init))
	try:
	    return o[0]
	except IndexError:
	    if c == 0:  # empty string entered
		return ''
	    else:  # canceled
		return None


    def textbox(self, filename, height=20, width=60, title=None):
	"""Display a file in a scrolling text box."""
	if title is None:
	    title = filename
	self.__perform(self.__handleTitle(title) +\
	    ' --textbox "%s" %d %d' % (filename, height, width))


    def menu(self, text, height=15, width=54, list=[]):
	"""
	Display a menu of options to the user.  This method simplifies the
	--menu option of dialog, which allows for complex arguments.  This
	method receives a simple list of objects, and each one is assigned
	a choice number.
	The selected object is returned, or None if the dialog was canceled.
	"""
	menuheight = height - 8
	pairs = map(lambda i, item: (i + 1, item), range(len(list)), list)
	choices = reduce(lambda res, pair: res + '%d "%s" ' % pair, pairs, '')
	(code, output) = self.__perform('--menu "%s" %d %d %d %s' %\
	    (text, height, width, menuheight, choices))
	try:
	    return list[int(output[0]) - 1]
	except IndexError:
	    return None


    def checklist(self, text, height=15, width=54, list=[], checked=None):
	"""
	Returns a list of the selected objects.
	Returns an empty list if nothing was selected.
	Returns None if the window was canceled.
	checked -- a list of boolean (0/1) values; len(checked) must equal 
	    len(list).
	"""
	if checked is None:
	    checked = [0]*len(list)
	menuheight = height - 8
	triples = map(
	    lambda i, item, onoff, fs=('off', 'on'): (i + 1, item, fs[onoff]),
	    range(len(list)), list, checked)
	choices = reduce(lambda res, triple: res + '%d "%s" %s ' % triple,
	    triples, '')
	(c, o) = self.__perform('--checklist "%s" %d %d %d %s' %\
	    (text, height, width, menuheight, choices))
	try:
	    output = o[0]
	    indexList  = map(lambda x: int(x[1:-1]), split(output))
	    objectList = filter(lambda item, list=list, indexList=indexList: 
		    list.index(item) + 1 in indexList,
		list)
	    return objectList
	except IndexError:
	    if c == 0:                        # Nothing was selected
		return []
	    return None  # Was canceled


    def radiolist(self, text, height=15, width=54, list=[], selected=0):
	"""
	Return the selected object.
	Returns empty string if no choice was selected.
	Returns None if window was canceled.
	selected -- the selected item (must be between 1 and len(list)
	    or 0, meaning no selection).
	"""
	menuheight = height - 8
	triples = map(lambda i, item: (i + 1, item, 'off'),
	    range(len(list)), list)
	if selected:
	    i, item, tmp = triples[selected - 1]
	    triples[selected - 1] = (i, item, 'on')
	choices = reduce(lambda res, triple: res + '%d "%s" %s ' % triple,
	    triples, '')
	(c, o) = self.__perform('--radiolist "%s" %d %d %d %s' %\
	    (text, height, width, menuheight, choices))
	try:
	    return list[int(o[0]) - 1]
	except IndexError:
	    if c == 0:
		return ''
	    return None
 

    def clear(self):
	"""
	Clear the screen. Equivalent to the dialog --clear option.
	"""
	self.__perform_no_options('--clear')


    def scrollbox(self, text, height=20, width=60, title=''):
	"""
	This is a bonus method.  The dialog package only has a function to
	display a file in a scrolling text field.  This method allows any
	string to be displayed by first saving it in a temp file, and calling
	--textbox.
	"""
	fName = mktemp()
	f = open(fName, 'w')
	f.write(text)
	f.close()
	self.__perform(self.__handleTitle(title) +\
	    '--textbox "%s" %d %d' % (fName, height, width))
	os.unlink(fName)


    def gauge_start(self, perc=0, text='', height=8, width=54, title=''):
	"""
	Display gauge output window.
	Gauge normal usage (assuming that there is an instace of 'Dialog'
	class named 'd'):
	    d.gauge_start()
	    # do something
	    d.gauge_iterate(10)  # passed throgh 10%
	    # ...
	    d.gauge_iterate(100, 'any text here')  # work is done
	    d.stop_gauge()  # clean-up actions
	"""
	cmd = self.__handleTitle(title) +\
	    '--gauge "%s" %d %d %d' % (text, height, width, perc)
	cmd = '%s %s %s 2> /dev/null' % (DIALOG, self.__bgTitle, cmd)
	self.pipe = os.popen(cmd, 'w')
    #/gauge_start()


    def gauge_iterate(self, perc, text=''):
	"""
	Update percentage point value.
	
	See gauge_start() function above for the usage.
	"""
	if text:
	    text = 'XXX\n%d\n%s\nXXX\n' % (perc, text)
	else:
	    text = '%d\n' % perc
	self.pipe.write(text)
	self.pipe.flush()
    #/gauge_iterate()
    
    
    def gauge_stop(self):
	"""
	Finish previously started gauge.
	
	See gauge_start() function above for the usage.
	"""
	self.pipe.close()
    #/gauge_stop()



#
# DEMO APPLICATION
#
if __name__ == '__main__':
    """
    This demo tests all the features of the class.
    """
    d = Dialog()
    d.setBackgroundTitle('dialog.py demo')

    d.infobox(
	"One moment... Just wasting some time here to test the infobox...")
    sleep(3)

    if d.yesno("Do you like this demo?"):
	d.msgbox("Excellent!  Here's the source code:")
    else:
	d.msgbox("Send your complaints to /dev/null")
    
    d.textbox("dialog.py")

    name = d.inputbox("What's your name?", init="Snow White")
    fday = d.menu("What's your favorite day of the week?", 
	list=["Monday", "Tuesday", "Wednesday", "Thursday", 
	    "Friday (The best day of all)", "Saturday", "Sunday"])
    food = d.checklist("What sandwich toppings do you like?", 
	list=["Catsup", "Mustard", "Pesto", "Mayonaise", "Horse radish", 
	    "Sun-dried tomatoes"], checked=[0,0,0,1,1,1])
    sand = d.radiolist("What's your favorite kind of sandwich?", 
	list=["Hamburger", "Hotdog", "Burrito", "Doener", "Falafel", 
	    "Bagel", "Big Mac", "Whopper", "Quarter Pounder", 
	    "Peanut Butter and Jelly", "Grilled cheese"], selected=4)

    # Prepare the message for the final window
    bigMessage = "Here are some vital statistics about you:\n\nName: " + name +\
        "\nFavorite day of the week: " + fday +\
	"\nFavorite sandwich toppings:\n"
    for topping in food:
	bigMessage = bigMessage + "    " + topping + "\n"
    bigMessage = bigMessage + "Favorite sandwich: " + str(sand)

    d.scrollbox(bigMessage)

    #<>#  Gauge Demo
    d.gauge_start(0, 'percentage: 0', title='Gauge Demo')
    for i in range(1, 101):
	if i < 50:
	    msg = 'percentage: %d' % i
	elif i == 50:
	    msg = 'Over 50%'
	else:
	    msg = ''
	d.gauge_iterate(i, msg)
	sleep(0.1)
    d.gauge_stop()
    #<>#

    d.clear()