X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/f7b91d0fc5230f026f5e62fcb81122a495bacc2f..bca6757e1d0a5c2875ef3ee2f99ddc145b38c2a8:/apps/catalogue/management/prompt.py?ds=inline diff --git a/apps/catalogue/management/prompt.py b/apps/catalogue/management/prompt.py new file mode 100644 index 00000000..8fcb9805 --- /dev/null +++ b/apps/catalogue/management/prompt.py @@ -0,0 +1,41 @@ +# http://code.activestate.com/recipes/541096-prompt-the-user-for-confirmation/ + +def confirm(prompt=None, resp=False): + """prompts for yes or no response from the user. Returns True for yes and + False for no. + + 'resp' should be set to the default value assumed by the caller when + user simply types ENTER. + + >>> confirm(prompt='Create Directory?', resp=True) + Create Directory? [y]|n: + True + >>> confirm(prompt='Create Directory?', resp=False) + Create Directory? [n]|y: + False + >>> confirm(prompt='Create Directory?', resp=False) + Create Directory? [n]|y: y + True + + """ + + if prompt is None: + prompt = 'Confirm' + + if resp: + prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n') + else: + prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y') + + while True: + ans = raw_input(prompt) + if not ans: + return resp + if ans not in ['y', 'Y', 'n', 'N']: + print 'please enter y or n.' + continue + if ans == 'y' or ans == 'Y': + return True + if ans == 'n' or ans == 'N': + return False +