Skip to content

Commit

Permalink
Refactored code for PEP 8 compliance IMGIITRoorkee#17
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshat1276 committed Jan 5, 2025
1 parent ab87640 commit f8273c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Binary file added __pycache__/manager.cpython-312.pyc
Binary file not shown.
21 changes: 12 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os #will allow user to interact with the operating system
import os # Allows user to interact with the operating system
from manager import PasswordManager
import pyperclip
import sys
Expand Down Expand Up @@ -40,15 +40,19 @@ def validate_key_loaded(pm : PasswordManager):
print("Key not loaded. Please load a key first.")
return False
return True
def clear_screen(): #Defining the clear screen function to clear the CLI.
os.system('cls' if os.name == 'nt' else 'clear') #'cls' is used to clear the terminal screen in windows, for Linux, MacOS etc. 'clear' is used
"""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()

menu = """What would you like to do?
Expand All @@ -64,7 +68,7 @@ def main():
"""

print(menu)

done = False
while not done:
choice = get_input_with_timeout("Enter choice: ")
Expand All @@ -81,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 @@ -119,6 +123,5 @@ def main():
else:
print("Invalid choice. Please try again.")


if __name__ == '__main__':
main()
main()
11 changes: 8 additions & 3 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +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 TO ENTER THE PASSWORD IN THE CORRECT FORMAT
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

0 comments on commit f8273c9

Please sign in to comment.