Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code for pep8 compliance#17 #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Visit the **python** channel and ping `2Y` for assistance.
- `4`: Load an existing password file.
- `5`: Add a new password to the file.
- `6`: Retrieve a password from the file.
- `c`: Clear the CLI
- `q`: Quit the application.

---
Expand Down Expand Up @@ -139,4 +140,4 @@ Password for github is securepassword123
## Security Note

- **Keep Your Encryption Key Safe**:
The encryption key is crucial for accessing your passwords. Losing it means your passwords cannot be decrypted.
The encryption key is crucial for accessing your passwords. Losing it means your passwords cannot be decrypted.
Binary file added __pycache__/manager.cpython-312.pyc
Binary file not shown.
26 changes: 19 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os # Allows user to interact with the operating system
from manager import PasswordManager
import pyperclip
import sys
Expand Down Expand Up @@ -39,27 +40,35 @@ def validate_key_loaded(pm : PasswordManager):
print("Key not loaded. Please load a key first.")
return False
return True
"""The entire code now follows python's PEP 8 standard"""
def clear_screen():
"""Clears the CLI screen."""
os.system('cls' if os.name == 'nt' else 'clear')

def main():
password = {
"""Main function to handle the password manager operations."""
passwords = {
"gmail": "password1",
"facebook": "password2",
"twitter": "password3"
}

pm = PasswordManager()

print("""What would you like to do?
menu = """What would you like to do?
1. Create a new key
2. Load an existing key
3. Create a new password file
4. Load an existing password file
5. Add a password
6. Get a password
7. List all sites
c. Clear Screen
q. Quit
""")

"""

print(menu)

done = False
while not done:
choice = get_input_with_timeout("Enter choice: ")
Expand All @@ -76,7 +85,7 @@ def main():
pm.load_key(path)
elif choice == '3' and validate_key_loaded(pm):
path = input("Enter password file path: ").strip()
pm.create_password_file(path, password)
pm.create_password_file(path, passwords)
elif choice == '4' and validate_key_loaded(pm):
path = input("Enter password file path: ").strip()
pm.load_password_file(path)
Expand Down Expand Up @@ -104,12 +113,15 @@ def main():
print("Saved Sites:")
for site in pm.password_dict:
print(site)
elif choice == 'c': #CHECK CONDITION AND CLEAR THE CLI
clear_screen()
print(menu)
print("Cleared the screen.")
elif choice == 'q':
done = True
print("Goodbye!")
else:
print("Invalid choice. Please try again.")


if __name__ == '__main__':
main()
12 changes: 10 additions & 2 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,58 @@


class PasswordManager:

def __init__(self):
self.key = None
self.password_file = None
self.password_dict = {}
self.keyloaded = False

def create_key(self, path):
"""Generates and saves a new key to the specified path."""
self.key = Fernet.generate_key()
with open(path, 'wb') as f:
f.write(self.key)
self.keyloaded = True

def load_key(self, path):
"""Loads the key from the specified path."""
with open(path, 'rb') as f:
self.key = f.read()
self.keyloaded = True


def create_password_file(self, path, initial_values=None):
"""Creates a password file and optionally adds initial passwords."""
self.password_file = path
if initial_values is not None:
for site in initial_values:
print(initial_values[site])
self.add_password(site, initial_values[site])

def load_password_file(self, path):
"""Loads passwords from a file and decrypts them."""
self.password_file = path
with open(path, 'r') as f:
for line in f:
site, encrypted = line.split(":")
self.password_dict[site] = Fernet(self.key).decrypt(encrypted.encode()).decode()
self.password_dict[site] = Fernet(self.key).decrypt(
encrypted.encode()).decode()

def add_password(self, site, password):
if site in self.password_dict:
print(f"Warning: A password for the site '{site}' already exists.")
return
while len(password) < 8: #ENSURES THAT PASSWORD IS ATLEAST 8 CHARACTERS LONG
print("Error: Password must be at least 8 characters long.")
password = input("Please enter a valid password: ").strip() # Will continue prompting user
self.password_dict[site] = password
if self.password_file is not None:
with open(self.password_file, 'a+') as f:
encrypted = Fernet(self.key).encrypt(password.encode()).decode()
f.write(f"{site}:{encrypted}\n")

def get_password(self, site):
"""Returns the password for a site, or a message if not found."""
return self.password_dict.get(site, "Password not found.")
def validate_strength(self, password):
# a password is strong if it has length greater than 8
Expand Down