-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_manager.py
88 lines (75 loc) · 2.59 KB
/
data_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
"""
data_manager.py – Handles saving, loading, and deleting passwords in a structured JSON format.
Features:
- Supports multiple entries per website
- Creates 'data/passwords.json' if it doesn't exist
- Uses error handling to ensure resilience
"""
import json
import os
# Path to the password storage file
DATA_FILE = "data/passwords.json"
class DataManager:
def __init__(self):
"""
Initializes the data manager and ensures the data directory and file exist.
"""
os.makedirs("data", exist_ok=True)
if not os.path.exists(DATA_FILE):
with open(DATA_FILE, "w") as f:
json.dump({}, f)
def save(self, website, email, password):
"""
Saves a new entry for a website. Supports multiple credentials per website.
"""
new_entry = {
"email": email,
"password": password
}
# Load existing data or start fresh
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
data = {}
# Handle duplicate websites by appending to a list
if website in data:
if isinstance(data[website], dict):
data[website] = [data[website]] # Convert to list
data[website].append(new_entry)
else:
data[website] = [new_entry]
# Write updated data back to the file
with open(DATA_FILE, "w") as f:
json.dump(data, f, indent=4)
def load(self, website):
"""
Loads and returns all credentials for the given website.
Returns None if not found or error occurs.
"""
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
return data.get(website)
except (FileNotFoundError, json.JSONDecodeError):
return None
def delete(self, website):
"""
Deletes all credentials associated with a given website.
Returns True if successful, False if not found or error occurs.
"""
try:
with open(DATA_FILE, "r") as f:
data = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return False # File missing or broken
if website not in data:
return False # Nothing to delete
# Remove the website's entry
del data[website]
try:
with open(DATA_FILE, "w") as f:
json.dump(data, f, indent=4)
return True
except Exception:
return False