-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (100 loc) · 3.15 KB
/
main.py
File metadata and controls
117 lines (100 loc) · 3.15 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
115
116
117
from PyInquirer import prompt
from lib.api import login, signup
from lib.common import custom_style, validate_nonempty
from cli_flows.create_flow import run_create_flow
from cli_flows.read_flow import run_read_flow
from cli_flows.edit_flow import run_edit_flow
from cli_flows.delete_flow import run_delete_flow
def run_login_flow():
"""
Command line interface questions for logging in or signing up.
Asks whether to login or signup and then credentials.
Attempts to login or signup.
"""
questions = [
{
'type': 'list',
'name': 'login_choice',
'message': 'Welcome to Password Mangaer!',
'choices': [
'Login',
'Sign up',
]
},
{
'type': 'input',
'name': 'username',
'message': 'Enter username:',
'validate': validate_nonempty,
'filter': lambda val: val.lower(),
},
{
'type': 'password',
'name': 'master_password',
'message': 'Enter master password:',
'validate': validate_nonempty,
},
{
'type': 'password',
'name': 'master_password_confirm',
'message': 'Confirm master password:',
'when': lambda answers: answers['login_choice'] == 'Sign up',
'default': '',
},
]
answers = prompt(questions, style=custom_style)
if len(answers) == 0:
exit()
session = None
if answers['login_choice'] == 'Login':
session = login(answers['username'], answers['master_password'])
elif answers['login_choice'] == 'Sign up':
if answers['master_password'] == answers['master_password_confirm']:
session = signup(answers['username'], answers['master_password'])
else:
print('Passwords did not match. Exiting...')
exit()
print('Welcome ' + session.username + '!')
return session
def run_main_flow(session):
"""
Command line interface questions for what user wants to do now that they are logged in.
"""
questions = [
{
'type': 'list',
'name': 'theme',
'message': 'What do you want to do? Ctrl+C to exit.',
'choices': [
{
'name': 'Create a new account',
'value': run_create_flow,
},
{
'name': 'View an account',
'value': run_read_flow,
},
{
'name': 'Edit an account',
'value': run_edit_flow,
},
{
'name': 'Delete an account',
'value': run_delete_flow,
},
]
},
]
answers = prompt(questions, style=custom_style)
if len(answers) == 0:
exit()
answers['theme'](session)
def main():
"""
Run login flow and then continuously run the main flow until user exits.
"""
session = run_login_flow()
while True:
run_main_flow(session)
if __name__ == "__main__":
main()