Skip to content

Commit fe200a9

Browse files
committedFeb 5, 2021
Update JSON format and search
1 parent 68397a2 commit fe200a9

File tree

3 files changed

+82
-28
lines changed

3 files changed

+82
-28
lines changed
 

‎Day-29-PasswordManager/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
### An implementation of Password-Manager
44
### [PasswordManager](https://repl.it/@abhijeetpandit/PasswordManager#main.py)
55

6-
<img src= 'https://user-images.githubusercontent.com/65078610/106907740-97e84f80-6724-11eb-9128-efced1349621.gif' width="500">
6+
<img src= 'https://user-images.githubusercontent.com/65078610/107014546-c5370b00-67c1-11eb-81d4-a97f2d9a44da.gif' width="500">

‎Day-29-PasswordManager/data.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Pantaloons": {
3+
"email": "youremail@email.com",
4+
"password": "UT*l2qoWH7$2Bb6"
5+
},
6+
"Reliance": {
7+
"email": "youremail@email.com",
8+
"password": "B(6aIBE9a$0X(N*0J"
9+
},
10+
"Myntra": {
11+
"email": "youremail@email.com",
12+
"password": "letsdoshopping"
13+
},
14+
"Facebook": {
15+
"email": "youremail@email.com",
16+
"password": "Pl1lf6&HX!9FP"
17+
}
18+
}

‎Day-29-PasswordManager/main.py

+63-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,73 @@
11
from tkinter import *
22
import pyperclip
3+
import json
34
from tkinter import messagebox
45
from password_gen import generatePassword
56
LBLUE = '#eff8ff'
67
BLUE = '#cae4db'
78

89
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
10+
def genPassword():
11+
password_entry.delete(0,END)
12+
new_password = generatePassword()
13+
# Copy password to clipboard
14+
pyperclip.copy(new_password)
15+
password_entry.insert(index=0, string=new_password)
916

1017
# ---------------------------- SAVE PASSWORD ------------------------------- #
18+
def addPassword():
19+
website = website_entry.get()
20+
email = email_entry.get()
21+
password = password_entry.get()
22+
new_data = {
23+
website : {
24+
'email' : email,
25+
'password' : password
26+
}
27+
}
28+
# Check for empty fields
29+
if website=='' or email=='' or password=='':
30+
messagebox.showwarning(title='Opps!', message="Please don't leave any field empty")
31+
else:
32+
# Show confirmation popup
33+
is_ok = messagebox.askokcancel(
34+
title=f'{website}',
35+
message=f'Are you sure? \nEmail: {email} \nPassword: {password}'
36+
)
37+
# Add new data to data.json
38+
if is_ok:
39+
try:
40+
with open('data.json','r') as f:
41+
# Read old data
42+
data = json.load(f)
43+
except (FileNotFoundError, json.decoder.JSONDecodeError):
44+
# If no data found
45+
data = new_data
46+
else:
47+
# Update old data with new data
48+
data.update(new_data)
49+
finally:
50+
with open('data.json','w') as f:
51+
# Saving new data to json
52+
json.dump(data, f, indent=4)
53+
website_entry.delete(0,END)
54+
password_entry.delete(0,END)
55+
website_entry.focus()
56+
57+
# ---------------------------- FIND PASSWORD ------------------------------- #
58+
def findPassword():
59+
website = website_entry.get()
60+
try:
61+
with open('data.json','r') as f:
62+
data = json.load(f)
63+
email = data[website]['email']
64+
password = data[website]['password']
65+
messagebox.askokcancel(
66+
title=f'{website}',
67+
message=f'Email: {email} \nPassword: {password}'
68+
)
69+
except (KeyError, FileNotFoundError):
70+
messagebox.showwarning(title='Error', message=f"No details for {website} exists.")
1171

1272
# ---------------------------- UI SETUP ------------------------------- #
1373
window = Tk()
@@ -31,44 +91,20 @@
3191

3292
# Entry
3393
website_entry = Entry()
34-
website_entry.grid(row=2, column=2, padx=10, pady=5, columnspan=2, sticky='EW')
94+
website_entry.grid(row=2, column=2, padx=10, pady=5, sticky='EW')
3595
website_entry.focus()
3696
email_entry = Entry()
3797
email_entry.grid(row=3, column=2, padx=10, pady=5, columnspan=2, sticky='EW')
3898
email_entry.insert(index=END, string='youremail@email.com')
3999
password_entry = Entry()
40100
password_entry.grid(row=4, column=2, padx=10, pady=5, sticky='EW')
41101

42-
def genPassword():
43-
password_entry.delete(0,END)
44-
new_password = generatePassword()
45-
# Copy password to clipboard
46-
pyperclip.copy(new_password)
47-
password_entry.insert(index=0, string=new_password)
48-
49-
def addPassword():
50-
# Check for empty fields
51-
if website_entry.get()=='' or email_entry.get()=='' or password_entry.get()=='':
52-
messagebox.showwarning(title='Opps!', message="Please don't leave any field empty")
53-
else:
54-
# Show confirmation popup
55-
is_ok = messagebox.askokcancel(
56-
title=f'{website_entry.get()}',
57-
message=f'Are you sure? \nEmail: {email_entry.get()} \nPassword: {password_entry.get()}'
58-
)
59-
# Add new data to data.txt
60-
if is_ok:
61-
with open('data.txt','a') as f:
62-
# Using print() to write files
63-
print(f"{website_entry.get()} | {email_entry.get()} | {password_entry.get()}", file=f)
64-
website_entry.delete(0,END)
65-
password_entry.delete(0,END)
66-
website_entry.focus()
67-
68102
# Button
69103
gen_password_btn = Button(text='Generate Password', command=genPassword, bg=BLUE)
70104
gen_password_btn.grid(row=4, column=3)
71105
add_btn = Button(text='Add', command=addPassword, bg=BLUE)
72106
add_btn.grid(row=5, column=2, columnspan=2, sticky='EW')
107+
search_btn = Button(text='Search', command=findPassword, bg=BLUE)
108+
search_btn.grid(row=2, column=3, columnspan=2, sticky='EW')
73109

74110
window.mainloop()

0 commit comments

Comments
 (0)
Please sign in to comment.