summaryrefslogtreecommitdiff
path: root/rninput.py
blob: 86d3d90e62e89f53b834905329348df9c3c6f18b (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
# For fileIO
import os.path

# @message - The question to display to the user.
# @default - The default if <enter> is hit.
# @accept - The list of valid responses.
# @retry - Whether to retry on a malformed input.
# Returns 'y' or 'no'
def yninput(message, default="n", accept=['yes', 'y', 'no', 'n'], retry=True):
    # TODO: Test this
    if default.lower() not in accept:
	raise TypeError('Default as %s is not in list of accepted responses.' % default)

    default_msg=" [y/N]: "
    if default.lower() == "y" or default.lower() == "yes":
	default_msg=" [Y/n]: "

    while(1):
        answer=raw_input(message + default_msg) or default.lower()
	if answer.lower() not in accept and retry:
	    print "'%s' is an invalid response.  Please try again." % answer.lower()
	    continue
        else:
            if answer.lower() == "yes" or answer.lower() == "y":
               return "y"
            return "n"

# TODO: Test with a directory returned as the answer.
def finput(message,orval):
    while(1):
        answer=raw_input(message) or orval
	if os.path.exists(answer) and os.path.isfile(answer):
	    return answer

	print "%s doesn't exist or isn't a regular file.  Try again." % answer