summaryrefslogtreecommitdiff
path: root/rninput.py
diff options
context:
space:
mode:
Diffstat (limited to 'rninput.py')
-rw-r--r--rninput.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/rninput.py b/rninput.py
new file mode 100644
index 0000000..86d3d90
--- /dev/null
+++ b/rninput.py
@@ -0,0 +1,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