810 def extractchoices(prompt): |
811 def extractchoices(prompt): |
811 """Extract prompt message and list of choices from specified prompt. |
812 """Extract prompt message and list of choices from specified prompt. |
812 |
813 |
813 This returns tuple "(message, choices)", and "choices" is the |
814 This returns tuple "(message, choices)", and "choices" is the |
814 list of tuple "(response character, text without &)". |
815 list of tuple "(response character, text without &)". |
815 """ |
816 |
816 parts = prompt.split('$$') |
817 >>> ui.extractchoices("awake? $$ &Yes $$ &No") |
817 msg = parts[0].rstrip(' ') |
818 ('awake? ', [('y', 'Yes'), ('n', 'No')]) |
818 choices = [p.strip(' ') for p in parts[1:]] |
819 >>> ui.extractchoices("line\\nbreak? $$ &Yes $$ &No") |
|
820 ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')]) |
|
821 >>> ui.extractchoices("want lots of $$money$$?$$Ye&s$$N&o") |
|
822 ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')]) |
|
823 """ |
|
824 |
|
825 # Sadly, the prompt string may have been built with a filename |
|
826 # containing "$$" so let's try to find the first valid-looking |
|
827 # prompt to start parsing. Sadly, we also can't rely on |
|
828 # choices containing spaces, ASCII, or basically anything |
|
829 # except an ampersand followed by a character. |
|
830 m = re.match(r'(?s)(.+?)\$\$([^\$]*&[^ \$].*)', prompt) |
|
831 msg = m.group(1) |
|
832 choices = [p.strip(' ') for p in m.group(2).split('$$')] |
819 return (msg, |
833 return (msg, |
820 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1)) |
834 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1)) |
821 for s in choices]) |
835 for s in choices]) |
822 |
836 |
823 def promptchoice(self, prompt, default=0): |
837 def promptchoice(self, prompt, default=0): |