Skip to content

Commit

Permalink
refactor: change cli folder name and add cli.token module
Browse files Browse the repository at this point in the history
  • Loading branch information
Thungghuan committed Oct 8, 2022
1 parent 4bd6122 commit c33fe32
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
token
__pycache__

.token
1 change: 1 addition & 0 deletions cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from cli.main import main
File renamed without changes.
42 changes: 2 additions & 40 deletions gzic_bus_cli/cli.py → cli/main.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
import os.path as path
from datetime import datetime
import questionary
from gzic_bus_cli.console import clear, reset_console
from get_token import get_token, check_token_expired
from cli.console import clear, reset_console
from cli.token import load_token
from bus import Bus

def load_token():
token = ""
print("读取token文件中...")

if path.exists("token"):
with open("token") as f:
token = f.read().strip()

if check_token_expired(token):
print("token过期")
login()
else:
print("token读取成功")
else:
token = login()

return token


def login():
print("请先使用统一认证账号登陆获取token")
username = questionary.text("学号:").ask()
password = questionary.password("密码:").ask()

if not username or not password:
print("请输入用户名和密码")
exit()

token = get_token(username, password)
print("登陆成功,写入token文件")

with open("token", "w+") as f:
f.write(token)

return token


def check_reserve(bus: Bus):
tickets = bus.list_reserve(status=1)["list"]
ticket_choices = []
Expand Down
42 changes: 42 additions & 0 deletions cli/token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import questionary
from get_token import check_token_expired, get_token
import os.path as path

TOKEN_PATH = ".token"


def load_token():
token = ""
print("读取token文件中...")

if path.exists(TOKEN_PATH):
with open(TOKEN_PATH) as f:
token = f.read().strip()

if check_token_expired(token):
print("token过期")
login()
else:
print("token读取成功")
else:
token = login()

return token


def login():
print("请先使用统一认证账号登陆获取token")
username = questionary.text("学号:").ask()
password = questionary.password("密码:").ask()

if not username or not password:
print("请输入用户名和密码")
exit()

token = get_token(username, password)
print("登陆成功,写入token文件")

with open(TOKEN_PATH, "w+") as f:
f.write(token)

return token
1 change: 0 additions & 1 deletion gzic_bus_cli/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gzic_bus_cli import main
from cli import main


if __name__ == "__main__":
Expand Down

0 comments on commit c33fe32

Please sign in to comment.