summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Client/__init__.py
blob: 6d1cb9d40ef0d47eb1213bcffce9990af9bf291d (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
"""This contains all Bcfg2 Client modules"""

import os
import sys
import select
from Bcfg2.Compat import input  # pylint: disable=W0622


def prompt(msg):
    """ Helper to give a yes/no prompt to the user.  Flushes input
    buffers, handles exceptions, etc.  Returns True if the user
    answers in the affirmative, False otherwise.

    :param msg: The message to show to the user.  The message is not
                altered in any way for display; i.e., it should
                contain "[y/N]" if desired, etc.
    :type msg: string
    :returns: bool - True if yes, False if no """
    while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0]) > 0:
        os.read(sys.stdin.fileno(), 4096)
    try:
        ans = input(msg)
        return ans in ['y', 'Y']
    except UnicodeEncodeError:
        ans = input(msg.encode('utf-8'))
        return ans in ['y', 'Y']
    except EOFError:
        # handle ^C on rhel-based platforms
        raise SystemExit(1)