summaryrefslogtreecommitdiffstats
path: root/pym/_emerge/userquery.py
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-06-22 20:02:48 +0000
committerZac Medico <zmedico@gentoo.org>2009-06-22 20:02:48 +0000
commit4827f2de62ec604fbf3a6dafbcfbe2c180481efe (patch)
tree918bbd1b90db48effaa3ccd0790e224b66da01a8 /pym/_emerge/userquery.py
parentbf9282b6782ad433b2ca905a5131bd0c424a2d94 (diff)
downloadportage-4827f2de62ec604fbf3a6dafbcfbe2c180481efe.tar.gz
portage-4827f2de62ec604fbf3a6dafbcfbe2c180481efe.tar.bz2
portage-4827f2de62ec604fbf3a6dafbcfbe2c180481efe.zip
Bug #275047 - Split _emerge/__init__.py into smaller pieces (part 4).
Thanks to Sebastian Mingramm (few) <s.mingramm@gmx.de> for this patch. svn path=/main/trunk/; revision=13669
Diffstat (limited to 'pym/_emerge/userquery.py')
-rw-r--r--pym/_emerge/userquery.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/pym/_emerge/userquery.py b/pym/_emerge/userquery.py
new file mode 100644
index 000000000..64a8761e0
--- /dev/null
+++ b/pym/_emerge/userquery.py
@@ -0,0 +1,44 @@
+import sys
+
+from portage.output import bold, create_color_func
+
+def userquery(prompt, responses=None, colours=None):
+ """Displays a prompt and a set of responses, then waits for a response
+ which is checked against the responses and the first to match is
+ returned. An empty response will match the first value in responses. The
+ input buffer is *not* cleared prior to the prompt!
+
+ prompt: a String.
+ responses: a List of Strings.
+ colours: a List of Functions taking and returning a String, used to
+ process the responses for display. Typically these will be functions
+ like red() but could be e.g. lambda x: "DisplayString".
+ If responses is omitted, defaults to ["Yes", "No"], [green, red].
+ If only colours is omitted, defaults to [bold, ...].
+
+ Returns a member of the List responses. (If called without optional
+ arguments, returns "Yes" or "No".)
+ KeyboardInterrupt is converted to SystemExit to avoid tracebacks being
+ printed."""
+ if responses is None:
+ responses = ["Yes", "No"]
+ colours = [
+ create_color_func("PROMPT_CHOICE_DEFAULT"),
+ create_color_func("PROMPT_CHOICE_OTHER")
+ ]
+ elif colours is None:
+ colours=[bold]
+ colours=(colours*len(responses))[:len(responses)]
+ print bold(prompt),
+ try:
+ while True:
+ response=raw_input("["+"/".join([colours[i](responses[i]) for i in range(len(responses))])+"] ")
+ for key in responses:
+ # An empty response will match the first value in responses.
+ if response.upper()==key[:len(response)].upper():
+ return key
+ print "Sorry, response '%s' not understood." % response,
+ except (EOFError, KeyboardInterrupt):
+ print "Interrupted."
+ sys.exit(1)
+