diff options
author | Martin Nagy <mnagy@notas.(none)> | 2008-07-21 12:25:37 +0200 |
---|---|---|
committer | Rob Crittenden <rcrit@ipa.greyoak.com> | 2008-07-23 10:05:06 -0400 |
commit | f7ca405716b1ee8b92a940e07cd611f6b025795d (patch) | |
tree | 5246bd49daa8b72fe39f55d90b4280a8ec1d8b3e /ipa-python | |
parent | 72a3114a01d0c67cf3b8faf7b28da93e8a6f2de3 (diff) | |
download | freeipa-f7ca405716b1ee8b92a940e07cd611f6b025795d.tar.gz freeipa-f7ca405716b1ee8b92a940e07cd611f6b025795d.tar.xz freeipa-f7ca405716b1ee8b92a940e07cd611f6b025795d.zip |
Wrap up the raw_input() to user_input() for convenience and uniformity.
Diffstat (limited to 'ipa-python')
-rw-r--r-- | ipa-python/ipautil.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/ipa-python/ipautil.py b/ipa-python/ipautil.py index 4e065fc4b..3526cc7a8 100644 --- a/ipa-python/ipautil.py +++ b/ipa-python/ipautil.py @@ -29,6 +29,7 @@ import os, sys, traceback, readline import stat import shutil +from ipa import ipavalidate from types import * import re @@ -482,6 +483,71 @@ def read_items_file(filename): if fd != sys.stdin: fd.close() return items +def user_input(prompt, default = None, allow_empty = True): + if default == None: + while True: + ret = raw_input("%s: " % prompt) + if allow_empty or ret.strip(): + return ret + + if isinstance(default, basestring): + while True: + ret = raw_input("%s [%s]: " % (prompt, default)) + if not ret and (allow_empty or default): + return default + elif ret.strip(): + return ret + if isinstance(default, bool): + if default: + choice = "yes" + else: + choice = "no" + while True: + ret = raw_input("%s [%s]: " % (prompt, choice)) + if not ret: + return default + elif ret.lower()[0] == "y": + return True + elif ret.lower()[0] == "n": + return False + if isinstance(default, int): + while True: + try: + ret = raw_input("%s [%s]: " % (prompt, default)) + if not ret: + return default + ret = int(ret) + except ValueError: + pass + else: + return ret + +def user_input_email(prompt, default = None, allow_empty = False): + if default != None and allow_empty: + prompt += " (enter \"none\" for empty)" + while True: + ret = user_input(prompt, default, allow_empty) + if allow_empty and ret.lower() == "none": + return "" + if not ipavalidate.Email(ret, not allow_empty): + return ret.strip() + +def user_input_plain(prompt, default = None, allow_empty = True, allow_spaces = True): + while True: + ret = user_input(prompt, default, allow_empty) + if not ipavalidate.Plain(ret, not allow_empty, allow_spaces): + return ret + +def user_input_path(prompt, default = None, allow_empty = True): + if default != None and allow_empty: + prompt += " (enter \"none\" for empty)" + while True: + ret = user_input(prompt, default, allow_empty) + if allow_empty and ret.lower() == "none": + return "" + if not ipavalidate.Path(ret, not allow_empty): + return ret + class AttributeValueCompleter: ''' |