-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsm.py
76 lines (55 loc) · 1.92 KB
/
fsm.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
72
73
74
75
76
from typing import Optional
from prompt_toolkit.validation import Validator
from eggella import Eggella
from eggella.fsm import IntStateGroup
class LoginForm(IntStateGroup):
EMAIL = 0
PASSWORD = 1
ACCEPT = 2
# prompt validators
password_validator = Validator.from_callable(
lambda s: len(s) > 6 or s == "..", error_message="password len should be bigger than 6"
)
email_validator = Validator.from_callable(lambda s: "@" in s or s == "..", error_message="email is not valid")
confirm_validator = Validator.from_callable(lambda s: s in {"y", "n"})
app = Eggella(__name__)
app.register_states(LoginForm)
@app.on_command("auth")
def auth(email: Optional[str] = None, password: Optional[str] = None):
"""auth to service.
if email and password not passed - invoke interactive auth
"""
if email and password:
print("Success auth!")
print("Email:", email)
print("Password:", "*" * len(password))
else:
app.fsm.run(LoginForm)
@app.on_state(LoginForm.EMAIL)
def email():
result = app.cmd.prompt("Enter email > ", validator=email_validator)
if result == "..":
return app.fsm.finish()
app.fsm.ctx["email"] = result
app.fsm.next()
@app.on_state(LoginForm.PASSWORD)
def password():
# alias from prompt_toolkit.prompt functon
result = app.cmd.prompt("Enter password > ", is_password=True, validator=password_validator)
if result == "..":
return app.fsm.prev()
app.fsm.ctx["password"] = result
app.fsm.next()
@app.on_state(LoginForm.ACCEPT)
def finish():
print("Your input:")
print("email:", app.fsm["email"])
print("if correct, type `y` or `n` for back prev step")
confirm = app.cmd.prompt("(y/n)> ", validator=confirm_validator)
if confirm == "n":
return app.fsm.prev()
auth(app.fsm["email"], app.fsm["password"])
# don't forget to close FSM!
app.fsm.finish()
if __name__ == '__main__':
app.loop()