Skip to content

Commit f01bf47

Browse files
committed
update password generator to now include exception and error handling
1 parent 1779af5 commit f01bf47

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

Password-Generator-v2/main.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from tkinter import *
23
from tkinter import messagebox
34
from random import choice, shuffle, randint
@@ -23,20 +24,35 @@ def generate_password():
2324

2425

2526
# ---------------------------- SAVE PASSWORD ------------------------------- #
26-
def add_information():
27+
def save():
2728
website = website_input.get()
2829
username = username_input.get()
2930
password = password_input.get()
3031

3132
# Validate Entries
3233
if validate_entries(website, username, password):
33-
account_data = f"{website} | {username} | {password}"
34-
print(account_data)
34+
new_data = {website: {
35+
"email": username,
36+
"password": password
37+
}}
38+
print(new_data)
3539
pyperclip.copy(password)
36-
37-
# Write information to data file
38-
with open("data.txt", "a") as file:
39-
file.write(account_data)
40+
try:
41+
# Write information to data file
42+
with open("data.json", "r") as file:
43+
# Read/Import json to python dict
44+
data = json.load(file)
45+
except FileNotFoundError:
46+
# Create new data.json file
47+
with open("data.json", "w") as file:
48+
json.dump(new_data, file, indent=4)
49+
else:
50+
# If file exists, Update dict to add new data
51+
data.update(new_data)
52+
with open("data.json", "w") as file:
53+
# Add python dict to json
54+
json.dump(data, file, indent=4)
55+
finally:
4056
website_input.delete(0, END)
4157
password_input.delete(0, END)
4258
display_messagebox("Your info was added successfully!")
@@ -55,6 +71,23 @@ def validate_entries(website, username, password):
5571
return True
5672

5773

74+
def find_password():
75+
website = website_input.get()
76+
try:
77+
with open("data.json", "r") as file:
78+
data = json.load(file)
79+
except FileNotFoundError:
80+
display_messagebox("No data file found.")
81+
else:
82+
if website in data:
83+
email = data[website]['email']
84+
password = data[website]['password']
85+
display_messagebox(f"Email: {email} \nPassword: {password}")
86+
pyperclip.copy(password)
87+
else:
88+
display_messagebox(f"No details for the {website} exists.")
89+
90+
5891
# ---------------------------- UI SETUP ------------------------------- #
5992
window = Tk()
6093
window.title("Password Manager")
@@ -69,10 +102,13 @@ def validate_entries(website, username, password):
69102
website_label = Label(text="Website:")
70103
website_label.grid(row=1, column=0)
71104

72-
website_input = Entry(width=38)
73-
website_input.grid(row=1, column=1, columnspan=2)
105+
website_input = Entry(width=21)
106+
website_input.grid(row=1, column=1)
74107
website_input.focus()
75108

109+
website_search = Button(text="Search", width=13, command=find_password)
110+
website_search.grid(row=1, column=2)
111+
76112
# Username Input
77113
username_label = Label(text="Email/Username:")
78114
username_label.grid(row=2, column=0)
@@ -92,7 +128,7 @@ def validate_entries(website, username, password):
92128
password_btn.grid(row=3, column=2)
93129

94130
# Add to list button
95-
add_info_btn = Button(text="Add", width=36, command=add_information)
131+
add_info_btn = Button(text="Add", width=36, command=save)
96132
add_info_btn.grid(row=4, column=1, columnspan=2)
97133

98134
window.mainloop()

0 commit comments

Comments
 (0)