make master scripts
[redakcja.git] / apps / catalogue / management / prompt.py
1 # http://code.activestate.com/recipes/541096-prompt-the-user-for-confirmation/
2     
3 def confirm(prompt=None, resp=False):
4     """prompts for yes or no response from the user. Returns True for yes and
5     False for no.
6
7     'resp' should be set to the default value assumed by the caller when
8     user simply types ENTER.
9
10     >>> confirm(prompt='Create Directory?', resp=True)
11     Create Directory? [y]|n:
12     True
13     >>> confirm(prompt='Create Directory?', resp=False)
14     Create Directory? [n]|y:
15     False
16     >>> confirm(prompt='Create Directory?', resp=False)
17     Create Directory? [n]|y: y
18     True
19
20     """
21
22     if prompt is None:
23         prompt = 'Confirm'
24
25     if resp:
26         prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
27     else:
28         prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
29
30     while True:
31         ans = raw_input(prompt)
32         if not ans:
33             return resp
34         if ans not in ['y', 'Y', 'n', 'N']:
35             print 'please enter y or n.'
36             continue
37         if ans == 'y' or ans == 'Y':
38             return True
39         if ans == 'n' or ans == 'N':
40             return False
41