forked from merwin-asm/UFT
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocalDB.py
97 lines (94 loc) · 3.18 KB
/
LocalDB.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
import json
import os
class DB:
def __init__(self,name="main.json"):
self.name = name
try:
self.file = open(name,"x")
self.file.write('[{"users":[]}]')
self.file.close()
print("DB Created")
except:
print("DB is already found")
def user_num(self):
self.file = open(self.name,"r")
self.raw_data = self.file.read()
self.file.close()
self.data = json.loads(self.raw_data)
return len(self.data[0]["users"])
def user_exists(self, user_name):
self.file = open(self.name,"r")
self.data = self.file.read()
self.file.close()
self.data = json.loads(self.data)
no = 0
for user in self.data[0]["users"]:
if user["user"] == user_name:
return True , no
no+=1
return False , no
def add_user(self,username,password):
a , b = self.user_exists(username)
if a == False:
self.file = open(self.name, "r")
self.data = self.file.read()
self.file.close()
self.data = json.loads(self.data)
self.user = {"user": username, "password": password,}
self.data[0]["users"].append(self.user)
self.new_data = json.dumps(self.data)
self.file = open(self.name, "w")
self.file.write(self.new_data)
self.file.close()
return True
else:
return False
def delete_user(self,user_name):
self.file = open(self.name, "r")
self.data = self.file.read()
self.file.close()
self.data = json.loads(self.data)
self.count = 0
for user in self.data[0]["users"]:
if user.get("user") == user_name:
self.data[0]["users"].pop(self.count)
file = open(self.name, "w")
file.write(json.dumps(self.data))
file.close()
break
self.count += 1
def get_user_password(self,id):
if True:
self.file = open(self.name, "r")
self.data = self.file.read()
self.file.close()
self.data = json.loads(self.data)
for user in self.data[0]["users"]:
if user.get("user") == id:
return [user.get("password")]
return False
def edit_user_password(self,id,password):
a = self.user_exists(id)
if a:
# print("found")
self.file = open(self.name, "r")
self.data = self.file.read()
self.file.close()
self.data = json.loads(self.data)
self.count = 0
for user in self.data[0]["users"]:
if user.get("user") == id:
break
self.data[0]["users"][self.count]["password"] = password
file = open(self.name, "w")
file.write(json.dumps(self.data))
file.close()
return True
else:
return False
def confirm_password(self,id,password):
a = self.get_user_password(id)
if a == password:
return True
else:
return False