Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Any, Dict
- from json import load, JSONDecodeError, dumps
- from os.path import exists, getsize
- def read_json(file_name: str) -> Any:
- """
- Reading a JSON file.
- Args:
- file_name: str: The name of the file.
- Returns:
- Any
- """
- try:
- file = open(file_name, "r")
- data: Any = load(file)
- file.close()
- return data
- except JSONDecodeError as error:
- print(f"Failed to decode file.\nFile Path: {file_name}\nError: {error}")
- return None
- def check_data_file(file_name: str) -> Dict[str, Any]:
- """
- Checking the data from the file and creating if it is empty or not existing.
- Args:
- file_name: str: The name of the file.
- Returns:
- Dict[str, Any]
- """
- if not (exists(file_name) and getsize(file_name) > 0):
- print(f"Data not found.\nFile Path: {file_name}")
- file = open(file_name, "w")
- data: Dict[str, str] = {
- "Status": "No Accounts"
- }
- file.write(dumps(data, indent=4))
- file.close()
- return data
- return read_json(file_name)
- def check_status(data: Dict[str, Any], file_name: str) -> None:
- """
- Checking the status of the data before processing the data.
- Args:
- data: Dict[str, Any]: The data
- file_name: str: The name of the file.
- Returns:
- None
- """
- username: str
- password: str
- username_placeholder: str
- if "Status" in data and data["Status"] == "No Accounts":
- print("No Accounts Found!")
- username = str(input("Enter a username: "))
- password = str(input("Enter a password: "))
- file = open(file_name, "w")
- username_placeholder = f"Account_{username}"
- data[username_placeholder] = {
- "password": password
- }
- del data["Status"]
- file.write(dumps(data, indent=4))
- file.close()
- print(f"Account Created!\nUsername: {username}")
- return None
- print("Accounts Found!")
- username = str(input("Enter your username: "))
- password = str(input("Enter your password: "))
- username_placeholder = f"Account_{username}"
- if data[username_placeholder]["password"] == password:
- print(f"User Logged in!\nUsername: {username}")
- return None
- print(f"Incorrect Password!\nUsername: {username}")
- return None
- file_name: str = "data.json"
- data: Dict[str, Any] = check_data_file(file_name)
- check_status(data, file_name)
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement