· last year · Feb 06, 2025, 05:10 PM
1from typing import Any, Dict
2from json import load, JSONDecodeError, dumps
3from os.path import exists, getsize
4
5
6def read_json(file_name: str) -> Any:
7 """
8 Reading a JSON file.
9
10 Args:
11 file_name: str: The name of the file.
12
13 Returns:
14 Any
15 """
16 try:
17 file = open(file_name, "r")
18 data: Any = load(file)
19 file.close()
20 return data
21 except JSONDecodeError as error:
22 print(f"Failed to decode file.\nFile Path: {file_name}\nError: {error}")
23 return None
24
25def check_data_file(file_name: str) -> Dict[str, Any]:
26 """
27 Checking the data from the file and creating if it is empty or not existing.
28
29 Args:
30 file_name: str: The name of the file.
31
32 Returns:
33 Dict[str, Any]
34 """
35 if not (exists(file_name) and getsize(file_name) > 0):
36 print(f"Data not found.\nFile Path: {file_name}")
37 file = open(file_name, "w")
38 data: Dict[str, str] = {
39 "Status": "No Accounts"
40 }
41 file.write(dumps(data, indent=4))
42 file.close()
43 return data
44 return read_json(file_name)
45
46def check_status(data: Dict[str, Any], file_name: str) -> None:
47 """
48 Checking the status of the data before processing the data.
49
50 Args:
51 data: Dict[str, Any]: The data
52 file_name: str: The name of the file.
53
54 Returns:
55 None
56 """
57 username: str
58 password: str
59 username_placeholder: str
60 if "Status" in data and data["Status"] == "No Accounts":
61 print("No Accounts Found!")
62 username = str(input("Enter a username: "))
63 password = str(input("Enter a password: "))
64 file = open(file_name, "w")
65 username_placeholder = f"Account_{username}"
66 data[username_placeholder] = {
67 "password": password
68 }
69 del data["Status"]
70 file.write(dumps(data, indent=4))
71 file.close()
72 print(f"Account Created!\nUsername: {username}")
73 return None
74 print("Accounts Found!")
75 username = str(input("Enter your username: "))
76 password = str(input("Enter your password: "))
77 username_placeholder = f"Account_{username}"
78 if data[username_placeholder]["password"] == password:
79 print(f"User Logged in!\nUsername: {username}")
80 return None
81 print(f"Incorrect Password!\nUsername: {username}")
82 return None
83
84file_name: str = "data.json"
85data: Dict[str, Any] = check_data_file(file_name)
86check_status(data, file_name)
87exit()