forked from AWSCC-PUP-DWD/AWSCC-CodeQuest-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password-manager.py
142 lines (115 loc) · 3.61 KB
/
password-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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import json
import os
def print_menu():
menu = ["1 - Add Username and Password",
"2 - View",
"3 - Search",
"4 - Delete",
"5 - Update",
"6 - Exit"]
for i in menu:
print(i)
def check_input(_input):
global running
if _input == "1":
add()
elif _input == "2":
view()
elif _input == "3":
website = input("Enter website: ")
search(website)
elif _input == "4":
existing = False
while not existing:
website = input("Enter website: ")
existing = is_existing(website)
delete(website)
elif _input == "5":
existing = False
while not existing:
website = input("Enter website: ")
existing = is_existing(website)
update(website)
elif _input == "6":
running = False
def is_existing(website):
with open('data.json', 'r') as f:
data = json.load(f)
if website in data:
return True
return False
def add():
website = input("Enter website: ")
email = input("Enter email: ")
password = input("Enter password: ")
new_data = {
website: [{
'email': email,
'password': password
}]
}
with open('data.json', 'r') as f:
data = json.load(f)
if website in data:
data[website].append({'email': email, 'password': password})
else:
data.update(new_data)
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)
os.system('cls')
print("Successfully added!")
def view():
os.system('cls')
with open('data.json', 'r') as f:
data = json.load(f)
for key, val in data.items():
print(f"Website: {key}")
for i in range(len(val)):
print(f" Email: {val[i]['email']}")
print(f" Password: {val[i]['password']}\n")
def search(website):
with open('data.json', 'r') as f:
data = json.load(f)
for key, val in data.items():
if key == website:
print(f"Website: {key}")
for i in range(len(val)):
print(f" {i+1} Email: {val[i]['email']}")
print(f" Password: {val[i]['password']}\n")
return True
print("No websites found.")
return
def delete(website):
with open('data.json', 'r') as f:
data = json.load(f)
search(website)
valid_idx = False
while not valid_idx:
num = int(input("Enter the number you want to delete: "))
valid_idx = 0 <= num-1 < len(data[website])
data[website].pop(num-1)
if len(data[website]) == 0:
data.pop(website)
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)
print("Successfully removed.")
def update(website):
with open('data.json', 'r') as f:
data = json.load(f)
search(website)
valid_idx = False
while not valid_idx:
num = int(input("Enter the number you want to update: "))
valid_idx = 0 <= num-1 < len(data[website])
to_update = input("'email' or 'password': ").lower()
new_val = input(f"Enter your new {to_update}: ")
data[website][num-1][to_update] = new_val
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)
print("Successfully updated!")
running = True
while running:
print("=======PASSWORD MANAGER=======")
print_menu()
user_input = input("Enter a number: ")
check_input(user_input)