wobbl_tools/data_file.py

154 lines
4.1 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2024-03-14 18:36:52 +01:00
"""
2024-12-05 16:26:27 +01:00
Using this module, you can store data in files easily.
2024-03-14 18:36:52 +01:00
"""
import os
import json
from deprecated import deprecated
@deprecated(reason="This function is not safe to use!!!")
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
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
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.
"""
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
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-03-14 18:36:52 +01:00
# these 2 functions do the exact same thing as the functions in the class above
def load_dataclass_json(dataclass, file_path: str, builtin_save: bool=True):
2024-03-14 18:36:52 +01:00
"""
Loads a dataclass instance from a json file.
:param dataclass: The dataclass from which the instance will be created.
:param str file_path: The path to the json file from which the data gets loaded.
:param bool builtin_save: If True, you can simply call instance.save(file_path) to store the data.
:return: An instance of the dataclass containing the data from the json file.
2024-03-14 18:36:52 +01:00
"""
if os.path.exists(file_path):
file = open(file_path, "r")
class_dict = json.load(file)
file.close()
else:
class_dict = {}
instance = dataclass(**class_dict)
if builtin_save:
dataclass.save = save_dataclass_json
return instance
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.
:param class_instance: The instance of the dataclass to be saved.
:param str file_path: The path to the file in which the data gets written.
2024-03-14 18:36:52 +01:00
"""
class_dict = class_instance.__dict__
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items())) # filter out functions
file = open(file_path, "w")
json.dump(class_dict, file)
file.close()