Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import sys
- from getpass import getpass
- from pathlib import Path
- from appdirs import user_config_dir
- https://pypi.org/project/appdirs/
- config_dir = Path(user_config_dir(), "programm")
- token_file = config_dir / "token.conf"
- print("token_file:", token_file)
- print("token_file.parent:", token_file.parent)
- print("config_dir:", config_dir)
- config_dir.mkdir(exist_ok=True)
- def get_creds():
- token = input("Token: ")
- username = input("Username: ")
- password = getpass()
- # maybe not required to hide password
- return {"token": token, "username": username, "password": password}
- def load_token():
- if token_file.exists():
- with token_file.open() as fd:
- try:
- credentials = json.load(fd)
- except ValueError:
- # print(f"Invalid json data in token file: {token_file}", file=sys.stderr)
- raise SystemExit(f"Invalid json data in token file: {token_file}")
- else:
- credentials = get_creds()
- with token_file.open("w") as fd:
- json.dump(credentials, fd)
- return credentials
- if __name__ == "__main__":
- print(load_token())
Add Comment
Please, Sign In to add comment