2024-02-24 20:51:58 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2024-03-14 18:36:52 +01:00
|
|
|
"""
|
|
|
|
With this module, you can store data in files easyly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2024-02-25 16:51:10 +01:00
|
|
|
import os
|
2024-02-24 20:51:58 +01:00
|
|
|
import json
|
2024-07-20 19:05:46 +02:00
|
|
|
from warnings import deprecated
|
2024-02-24 20:51:58 +01:00
|
|
|
|
|
|
|
|
2024-07-20 19:06:26 +02:00
|
|
|
@deprecated("This function is not safe to use!!!")
|
2024-02-24 20:51:58 +01:00
|
|
|
class DictFile:
|
|
|
|
"""
|
|
|
|
This class is not safe to use!
|
|
|
|
Any code in the file will be executed!
|
|
|
|
"""
|
|
|
|
def __init__(self, path: str=None):
|
|
|
|
self.path = path
|
|
|
|
|
2024-02-25 14:48:51 +01:00
|
|
|
if path is None: # create empty settings dict if path is none else load settings from file
|
2024-02-24 20:51:58 +01:00
|
|
|
self.settings = {}
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.load_from_file(path)
|
|
|
|
|
|
|
|
def load_from_file(self, path):
|
|
|
|
file = open(path, "r")
|
|
|
|
self.settings = eval(file.read())
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
def save(self, path: str=None):
|
|
|
|
if path is None:
|
|
|
|
path = self.path
|
|
|
|
|
|
|
|
file = open(path, "w")
|
|
|
|
file.write(str(self))
|
|
|
|
file.close()
|
|
|
|
|
2024-02-25 14:48:51 +01:00
|
|
|
def __str__(self): # make the dict not be just one line
|
2024-02-24 20:51:58 +01:00
|
|
|
new_settings_str = "{\n"
|
|
|
|
for key in self.settings:
|
|
|
|
new_settings_str += "\t" + repr(key) + ": " + repr(self.settings[key]) + ",\n"
|
|
|
|
|
|
|
|
new_settings_str = new_settings_str[:-2] + "\n}"
|
|
|
|
|
|
|
|
return new_settings_str
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.settings)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.settings[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.settings[key] = value
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
self.settings.pop(key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.settings
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.settings)
|
|
|
|
|
|
|
|
|
|
|
|
class DataclassJSONFile:
|
2024-03-14 18:36:52 +01:00
|
|
|
"""
|
|
|
|
Store a dataclass in a JSON file.
|
|
|
|
"""
|
|
|
|
|
2024-02-24 20:51:58 +01:00
|
|
|
def __init__(self, file_path, dataclass, class_instance=None):
|
|
|
|
self.file_path = file_path
|
|
|
|
self.dataclass = dataclass
|
|
|
|
self.class_instance = class_instance
|
|
|
|
|
2024-02-25 14:48:51 +01:00
|
|
|
if class_instance is None: # load the class instance from the file if it is None
|
2024-02-24 20:51:58 +01:00
|
|
|
self.load_from_file(file_path)
|
|
|
|
|
|
|
|
def load_from_file(self, file_path):
|
|
|
|
file = open(file_path, "r")
|
|
|
|
class_dict = json.load(file)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
self.class_instance = self.dataclass(**class_dict)
|
|
|
|
|
|
|
|
def save_to_file(self):
|
|
|
|
class_dict = self.class_instance.__dict__
|
|
|
|
|
|
|
|
file = open(self.file_path, "w")
|
|
|
|
json.dump(class_dict, file)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.class_instance)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return getattr(self.class_instance, key)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
setattr(self.class_instance, key, value)
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
delattr(self.class_instance, key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.class_instance.__iter__()
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.class_instance.__dict__)
|
2024-02-25 14:29:14 +01:00
|
|
|
|
|
|
|
|
2024-03-14 18:36:52 +01:00
|
|
|
# these 2 functions do the exact same thing as the functions in the class above
|
2024-02-25 14:29:14 +01:00
|
|
|
def load_dataclass_json(dataclass, file_path: str):
|
2024-03-14 18:36:52 +01:00
|
|
|
"""
|
|
|
|
Loads a dataclass instance from a json file.
|
|
|
|
"""
|
2024-02-25 16:51:10 +01:00
|
|
|
if os.path.exists(file_path):
|
|
|
|
file = open(file_path, "r")
|
|
|
|
class_dict = json.load(file)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
else:
|
|
|
|
class_dict = {}
|
2024-02-25 14:29:14 +01:00
|
|
|
|
|
|
|
return dataclass(**class_dict)
|
|
|
|
|
|
|
|
|
|
|
|
def save_dataclass_json(class_instance, file_path: str):
|
2024-03-14 18:36:52 +01:00
|
|
|
"""
|
|
|
|
Saves a dataclass instance to a json file.
|
|
|
|
"""
|
2024-02-25 14:29:14 +01:00
|
|
|
class_dict = class_instance.__dict__
|
2024-02-25 16:51:10 +01:00
|
|
|
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items()))
|
2024-02-25 14:29:14 +01:00
|
|
|
|
|
|
|
file = open(file_path, "w")
|
|
|
|
json.dump(class_dict, file)
|
|
|
|
file.close()
|