-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
74 lines (65 loc) · 2.36 KB
/
Copy pathstorage.py
File metadata and controls
74 lines (65 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
from logs import logger
# File path to store the dictionary data
DATA_FILE = "saveFile.json"
def convert_to_bool(value):
"""
Converts a string value to a boolean.
If the value is "True" (case-insensitive), it returns True.
If the value is "False" (case-insensitive), it returns False.
For all other values, it returns the original value.
"""
if isinstance(value, str):
if value.lower() == "true":
return True
elif value.lower() == "false":
return False
return value
def convert_values(obj):
"""
Recursively converts string values of "True" and "False" to booleans in a dictionary or list.
"""
if isinstance(obj, dict):
return {key: convert_values(value) for key, value in obj.items()}
elif isinstance(obj, list):
return [convert_values(item) for item in obj]
else:
return convert_to_bool(obj)
# Function to load data from the JSON file
def load_data():
try:
with open(DATA_FILE, "r") as file:
data = file.read()
if data.strip() == "":
logger.warning("JSON file is empty. Creating a new file")
data = {}
new_json(data)
elif data.strip() == "{}":
logger.warning("JSON file contains an empty dictionary. Initializing with an empty dictionary")
data = {}
else:
data = json.loads(data)
logger.info("Data loaded successfully from JSON file")
except FileNotFoundError:
logger.warning("JSON file not found. Creating a new file")
data = {}
new_json(data) # Create a new file
# Convert string values of "True" and "False" to booleans
data = convert_values(data)
return data
# Function to create new JSON file to save data
def new_json(data):
try:
with open(DATA_FILE, "w") as file:
json.dump(data, file)
logger.info("Successfully created JSON file")
except Exception as e:
logger.error(f"Error creating JSON file: {e}")
# Function to save data to the JSON file
async def save_data(data):
try:
with open(DATA_FILE, "w") as file:
json.dump(data, file)
logger.info("Data saved successfully to JSON file")
except Exception as e:
logger.error(f"Error saving data to JSON file: {e}")