Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created August 16, 2020 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/90361fe4c0c96d47de46481ba0e35880 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/90361fe4c0c96d47de46481ba0e35880 to your computer and use it in GitHub Desktop.
Python credentials boilerplate with decouple and getpass
from decouple import config
from getpass import getpass
import os
# Dynamic prompt for creds stored as tuple
# 0: eml_addr
# 1: passwd
# print(dyn_creds[0:]) # ('nopez@no.com', 'yesnononoyes')
def hardcode_death():
eml_addr = input('Please enter your email address: ')
passwd = getpass('Enter app-specific password: ')
return eml_addr, passwd
# decouple library
# ~/.env
env = os.getcwd() + '/.env'
if os.path.exists(env):
eml_addr = config('email')
passwd = config('password')
creds = {'email': eml_addr,
'token': passwd}
else:
dyn_creds = hardcode_death()
creds = {'email': dyn_creds[0],
'token': dyn_creds[1]}
# ~/.cfg
if os.path.exists(os.getenv('HOME') + '/.cfg'):
client = Client(**get_ini_config())
else:
dyn_creds = hardcode_death()
testconfig = {
'email': dyn_creds[0],
'password': dyn_creds[1],
'url': 'https://fqdn.com',
'token': True
}
client = Client(**testconfig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment