-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.py
71 lines (50 loc) · 1.94 KB
/
prompt.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import re
class Prompt:
def __init__(self, question):
self.question = question
self.response_input = None
def respond(self, response_input):
if self.validate(response_input):
self.response_input = response_input
def validate(self, value):
return True
def satisfied(self):
return self.response_input != None
def response(self):
return self.response_input
class IntegerPrompt(Prompt):
def __init__(self, question, maximum=float('inf')):
super().__init__(question)
self.maximum = maximum
def validate(self, value):
return value.isdigit() and int(value) <= self.maximum
def response(self):
return int(self.response_input)
class SetPrompt(Prompt):
def __init__(self, question, answer_set, exclude=[]):
super().__init__(question)
self.answer_set = [x for x in answer_set if x not in exclude]
def validate(self, value):
matches = []
query = re.compile('.*?'.join(list(value)), re.I)
for answer in self.answer_set:
if query.search(str(answer)) != None:
matches.append(answer)
if len(matches) == 0:
print('[{}]'.format(', '.join(str(answer) for answer in self.answer_set)))
return False
elif len(matches) > 1:
print('[{}]'.format(', '.join(str(answer) for answer in matches)))
return False
else:
self.response_input = matches[0]
return True
def respond(self, response_input):
self.validate(response_input)
class BooleanPrompt(Prompt):
true_pattern = re.compile('^yes|y|true|1$', re.I)
false_pattern = re.compile('^no|n|false|0$', re.I)
def validate(self, value):
return BooleanPrompt.true_pattern.match(value) or BooleanPrompt.false_pattern.match(value)
def response(self):
return BooleanPrompt.true_pattern.match(self.response_input)