-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestions.py
More file actions
114 lines (82 loc) · 2.92 KB
/
questions.py
File metadata and controls
114 lines (82 loc) · 2.92 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import print_function, unicode_literals
from PyInquirer import prompt
import yaml
import os
from sys import platform
def _load_yml():
with open("./questions.yml", 'r') as ymlfile:
yml = yaml.safe_load(ymlfile)
return yml
def _load_title(topic):
questions_conf = _load_yml()
q_topic = questions_conf[topic]
return q_topic['title']
def _load_questions(topic, optional=None):
questions_conf = _load_yml()
q_topic = questions_conf[topic]
startup_questions = []
if q_topic['questions'] is not None:
for q in q_topic['questions']:
startup_questions.append({'name': q['question']})
# append optional questions
if optional is not None:
for q in optional:
startup_questions.append({'name': q})
return startup_questions
def _build_question(q_type, topic, optional_questions):
choices = _load_questions(topic, optional_questions)
question = _load_title(topic)
return [{
'type': q_type,
'qmark': '💻',
'message': question,
'name': topic,
'choices': choices
}]
def _clear_console():
if platform == 'win32' or platform == 'cygwin':
os.system('cls')
else:
os.system('clear')
def _get_module_by_answer(topic, answer):
yml = _load_yml()
yml_topic = yml[topic]
questions = yml_topic['questions']
# let's check if there's a configured answer and what module we need next
if questions is not None:
for q in questions:
q_obj = {topic: q['question']}
if answer == q_obj:
return q['module']
# ok, so if there's no configured answer, lets check if we got a general next module
module = yml_topic['module']
if module is not None:
return module
# ok we have nothing, so return None as default
print("Found nothing")
return None
def ask(topic, optional_questions=None, clear_console=False):
# check if we have a "selection_type" which is multiple or if
# we need to use the default "list" function
yml = _load_yml()
yml_topic = yml[topic]
if 'selection_type' in yml_topic and yml_topic['selection_type'] == 'multiple':
return _select(topic, optional_questions, clear_console)
else:
return _list(topic, optional_questions, clear_console)
def _list(topic, optional_questions=None, clear_console=False):
if clear_console:
_clear_console()
q = _build_question('list', topic, optional_questions)
answer = prompt(q)
module = _get_module_by_answer(topic, answer)
selection = answer[topic]
return module, selection
def _select(topic, optional_questions=None, clear_console=False):
if clear_console:
_clear_console()
q = _build_question('checkbox', topic, optional_questions)
answer = prompt(q)
module = _get_module_by_answer(topic, answer)
selection = answer[topic]
return module, selection