-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount_manager.py
106 lines (87 loc) · 2.59 KB
/
account_manager.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
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
import json
import os.path
import functools
class Account:
uname: str
pwd: str
tenant: str
def __init__(self, uname, pwd, tenant):
self.uname = uname
self.pwd = pwd
self.tenant = tenant
class AccountManager:
__accounts: list[Account]
@property
def accounts(self) -> list[Account]:
return self.__accounts
def __init__(self):
if not os.path.exists("./accounts.json"):
with open("./accounts.json", "w", encoding="utf-8") as f:
struct = {
"accounts": [
{
"uname": "placeholder",
"pwd": "pwd",
"tenant": "114514"
}
]
}
t = json.dumps(struct)
f.write(t)
f.close()
self.__accounts = list()
self.refresh()
def refresh(self):
self.__accounts.clear()
with open("./accounts.json", "r", encoding="utf-8") as f:
data = json.loads(functools.reduce(lambda x, y: x + y, f.readlines()))
for acc in data["accounts"]:
self.__accounts.append(
Account(
acc["uname"],
acc["pwd"],
acc["tenant"]
)
)
f.close()
return self
def fetch(self, uname) -> Account:
for i in self.__accounts:
if i.uname == uname:
return i
def append(self, uname, pwd, tenant):
self.__accounts.append(
Account(
uname,
pwd,
tenant
)
)
return self
def contain(self, uname) -> bool:
for i in self.__accounts:
if i.uname == uname:
return True
return False
def delete(self, uname):
for i in self.__accounts:
if i.uname == uname:
self.__accounts.remove(i)
return self
def save(self):
with open("./accounts.json", "w", encoding="utf-8") as f:
struct = {
"accounts": []
}
for i in self.__accounts:
struct["accounts"].append(
{
"uname": i.uname,
"pwd": i.pwd,
"tenant": i.tenant
}
)
j = json.dumps(struct)
f.write(j)
f.close()
return self