diff options
author | Christian Cleberg <hello@cleberg.net> | 2024-11-02 16:58:31 -0500 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2024-11-02 16:58:31 -0500 |
commit | 598e71b5ea1392f25cca0290c5544ab1135c37a2 (patch) | |
tree | 76cd61b733e3bffae640e61146bc0c30c317c5cc /process.py | |
parent | 0bef69a049f4bace9b06cb4beb3b0505dd7b7a44 (diff) | |
download | yoshi-cli-598e71b5ea1392f25cca0290c5544ab1135c37a2.tar.gz yoshi-cli-598e71b5ea1392f25cca0290c5544ab1135c37a2.tar.bz2 yoshi-cli-598e71b5ea1392f25cca0290c5544ab1135c37a2.zip |
add pylint workflow
Diffstat (limited to 'process.py')
-rw-r--r-- | process.py | 157 |
1 files changed, 124 insertions, 33 deletions
@@ -1,4 +1,29 @@ -from Account import Account +""" +Password Vault Manager + +This script provides various functions for managing password vaults. +It allows users to create, list, edit and delete accounts. + +The `Account` class represents an individual account, with attributes for +the application name, username, password, and URL. The database module is used +to interact with the SQLite database file (`vault.sqlite`) that stores the +accounts data. + +Functions: + generate_characters(n): generates a list of random characters + shuffle_characters(characters): shuffles the characters to create a password + generate_passphrase(n, sep): generates an XKCD-style passphrase with n words and separator + list_accounts(): lists all saved accounts in the database + delete_account(uuid): deletes an account by its UUID + purge_accounts(): purges the entire database (irreversible) + create_account(): creates a new account by prompting user for details + edit_account(uuid, edit_parameter): edits an existing account's details + +Usage: + Run this script in your terminal to access these functions. +""" + +from account import Account import database from string import ascii_letters, punctuation, digits import random @@ -6,39 +31,70 @@ import uuid from prettytable import PrettyTable -# Generate a list of characters def generate_characters(n: int) -> list: - characters = list() + """ + Generates a list of n random characters from the set of ASCII letters, + punctuation and digits. + + Args: + n (int): The number of characters to generate + + Returns: + list: A list of n random characters + """ + characters = [] password_format = ascii_letters + punctuation + digits - for x in range(n): + for _ in range(n): characters.append(random.choice(password_format)) return characters -# Randomly shuffle the characters def shuffle_characters(characters: list) -> str: + """ + Shuffles the characters to create a password. + + Args: + characters (list): The list of characters + + Returns: + str: A string representation of the shuffled characters + """ random.shuffle(characters) character_string = ''.join(characters) return character_string -# Generate a combination of passphrases def generate_passphrase(n: int, sep: str) -> str: + """ + Generates an XKCD-style passphrase with n words and separator. + + Args: + n (int): The number of words to include + sep (str): The separator symbol + + Returns: + str: A string representation of the passphrase + """ phrases = [] lucky_number = random.choice(range(0, n)) - for x in range(0, n): + for _ in range(n): line = random.choice(open('wordlist.txt').readlines()) line = line.replace('\n', '') - if x == lucky_number: - phrases.append(line.strip().capitalize() + str(x)) + if _ == lucky_number: + phrases.append(line.strip().capitalize() + str(_)) else: phrases.append(line.strip().capitalize()) passphrase = sep.join(phrases) return passphrase -# List all saved accounts def list_accounts() -> None: + """ + Lists all saved accounts in the database. + + Returns: + None + """ accounts = database.find_accounts() t = PrettyTable(['UUID', 'Application', 'Username', 'Password', 'URL']) for account in accounts: @@ -46,10 +102,18 @@ def list_accounts() -> None: print(t) -# Delete a single account by UUID def delete_account(uuid: str) -> None: + """ + Deletes an account by its UUID. + + Args: + uuid (str): The UUID of the account to delete + + Returns: + None + """ account_record = database.find_account(uuid) - account: Account = Account(account_record[0][0], + account = Account(account_record[0][0], account_record[0][1], account_record[0][2], account_record[0][3], @@ -58,52 +122,79 @@ def delete_account(uuid: str) -> None: print('Account successfully deleted.') -# Purge the `accounts` table and `vault.sqlite` file def purge_accounts() -> None: + """ + Purges the entire database (irreversible). + + Returns: + None + """ check = input( 'Are you absolutely sure you want to delete your password vault? This action is irreversible. (y/n): ') - if check: + if check.lower() == 'y': database.purge_table() database.purge_database() print('The password vault has been purged. You may now exit or create a new one.') -# Request user input and create an account def create_account() -> None: + """ + Creates a new account by prompting user for details. + + Returns: + None + """ application_string = input('Please enter a name for this account: ') username_string = input('Please enter your username for this account: ') url_string = input('(Optional) Please enter a URL for this account: ') + password_type = input( - '''Do you want a random character password (p), an XKCD-style passphrase -(x), or a custom password (c)? (p|x|c): ''') - if password_type == "x" or password_type == "xkcd": + '''Do you want a random character password (p), an XKCD-style passphrase +(x), or a custom password (c)? (p|x|c): ''' + ) + if password_type not in ['p', 'x', 'c']: + print('Error: Invalid choice. Please choose p, x, or c.') + return + + if password_type == 'x': password_length = int( - input('Please enter number of words to include (min. 2): ')) - password_separator = input( - 'Please enter your desired separator symbol (_, -, ~, etc.: ') + input('Please enter number of words to include (min. 2): ') + ) if password_length < 3: print('Error: Your passphrase length must be at least 3 words.') - else: - password_string = generate_passphrase( - password_length, password_separator) - elif password_type == "p": + return + password_separator = input( + 'Please enter your desired separator symbol (_,-, ~, etc.): ' + ) + password_string = generate_passphrase(password_length, password_separator) + elif password_type == 'p': password_length = int( - input('Please enter your desired password length (min. 8): ')) + input('Please enter your desired password length (min. 8): ') + ) if password_length < 8: print('Error: Your password length must be at least 8 characters.') - else: - password_characters = generate_characters(password_length) - password_string = shuffle_characters(password_characters) + return + password_string = generate_password(password_length) else: password_string = input('Please enter your desired password: ') + account = Account(str(uuid.uuid4()), application_string, username_string, password_string, url_string) account.save_account() print('Account saved to the vault. Use `--list` to see all saved accounts.') -# Allow users to edit any account info except the UUID + def edit_account(uuid: str, edit_parameter: int) -> None: + """ + Allow users to edit any account information except the UUID. + + Args: + uuid (str): Unique identifier of the account. + edit_parameter (int): Parameter indicating which field to edit. + Valid values are 1 for application name, 2 for username, + 3 for password, and 4 for URL. + """ if edit_parameter == 1: field_name = 'application' new_value = input('Please enter your desired Application name: ') @@ -113,10 +204,10 @@ def edit_account(uuid: str, edit_parameter: int) -> None: elif edit_parameter == 3: field_name = 'password' type_check = input( - 'Do you want a new random password or to enter a custom password? (random/custom): ') + 'Do you want a new random password or to enter a custom password? ' + '(random/custom): ').lower() if type_check == 'random': - password_length = int( - input('Please enter your desired password length: ')) + password_length = int(input('Please enter your desired password length: ')) if password_length < 8: print('Error: Your password length must be at least 8 characters.') else: |