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

from colors import *

# @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(BLUE + "** " + NC + BOLD + message + default_msg + NC) 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(BLUE + "** " + NC + BOLD + message + NC) 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