summaryrefslogtreecommitdiff
path: root/attic/Misc.py
blob: 0368c89283719ca02edb237a215a142b50f8753c (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
"""Generic functions that could be added to Cheetah.Utils.Misc .
Removed because they were never used and haven't been tested.
"""


def promptBoolean(prompt, default=None, true='y', false='n'):
    """Ask a yes/no question.
    
    in : prompt, string, the prompt string.  (With trailing '?' if desired.)
         default, value if the user enters nothing.  Should be True, False
           or None.
         true, string, character considered a 'yes' response.
         false, string, character considered a 'no' response.
    out: boolean.
    Prints the prompt, takes the input, lowercases the first character of the
    input and compares it to 'true' and 'false'.  If match, return True or
    False.  Otherwise print error and prompt again.  

    Called by Cheetah.Tests.recursiveCompile
    """
    true = true.lower()
    false = false.lower()
    while True:
        response = raw_input(prompt)
        if (not response) and default:
            return default
        r = response[0:1].lower()
        if   r == true:
            return True
        elif r == false:
            return False
        print "Enter '%s' or '%s'." % (true, false)


def interactiveDelete(path, description=None):
    """If 'path' exists, as user whether to delete it, and if yes, delete
       it recursively.  The return value is normally False, but is True if
       unless 'path' exists and the user said no; this is so you can treat
       that case as an error if you wish to.  Exceptions while deleting are
       not caught.  'description' if non-empty is used in the prompt:
       "Delete $description '$path'? (Y/N)"
       Called by promptBoolean().
    """
    if not os.path.exists(path):
        return False
    if description:
        description += ' '
    prompt = "Delete %s'%s'? (Y/N) " % (description, path)
    response = promptBoolean(prompt)
    if response:
        shutil.rmtree(path)
        return False
    else:
        return True