ThePageMaster

Basic Master Login App - Python

Aug 27th, 2021 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. from sys import *
  2. from pathlib import Path
  3. from os import stat
  4.  
  5. def view():
  6.     contents = ''
  7.     try:
  8.         if stat('logins.gco').st_size > 0:
  9.             file = open("logins.gco", "r")
  10.             contents = file.read()
  11.             file.close()
  12.         else:
  13.             print('File is empty.')
  14.             contents = None
  15.     except Exception as e:
  16.         print('There is no file currently saved.')
  17.         print('Would you like to create a file to save to? (y/n)')
  18.         make_file = input('> ').lower()
  19.         if make_file == 'y':
  20.             file = open('logins.gco', 'w')
  21.             if stat('logins.gco').st_size == 0:
  22.                 print('A new file was created!')
  23.                 print('No logins to view!')
  24.         else:
  25.             print('No file was created!')
  26.             print('To view saved longins, please add a login.')
  27.     return contents
  28.  
  29. def add(name, password):
  30.     user_name = name
  31.     user_password = password
  32.  
  33.     file = open("logins.gco", "a")
  34.     file.write(f'{user_name}:{user_password}\n')
  35.     file.close()
  36.  
  37. key_file = Path('master.gco')
  38. master_key = ''
  39. if key_file.exists():
  40.     file = open('master.gco', 'r')
  41.     master_key = file.readline()
  42.     file.close()
  43. else:
  44.     print('A master key does not seem to exist for this program.\n Please enter one:')
  45.     master_key = input('> ')
  46.     file = open('master.gco', 'w')
  47.     file.write(master_key)
  48.     file.close()
  49.  
  50. print("Welcome! Please enter your master key to continue:")
  51. check_key = input('> ')
  52.  
  53. quit_program = False
  54. while not quit_program:
  55.     if check_key == master_key and quit_program == False:
  56.         print(f'''What would you like to do?
  57.  
  58.                View - view saved logins
  59.                Add - add a new login
  60.                Quit - exit program
  61.                ''')
  62.         while True:
  63.             mode = input('> ').lower()
  64.             if mode == 'view':
  65.                 saved_logins = view()
  66.                 print(saved_logins)
  67.             elif mode == 'add':
  68.                 user_name = input('Username: ')
  69.                 user_password = input('Password: ')
  70.                 add(user_name, user_password)
  71.             elif mode =='quit':
  72.                 quit_program = True
  73.                 break
  74.             else:
  75.                 print(f'The command {mode} is not recognized')
  76.                 continue
  77.     elif check_key == master_key and quit_program == True:
  78.         break
  79.     else:
  80.         print(f'Sorry, but the password {check_password} is invalid, please try again!')
  81.  
  82. #end of app.py
Add Comment
Please, Sign In to add comment