wobbl_tools/data_file.py

141 lines
3.4 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
2024-03-14 18:36:52 +01:00
"""
With this module, you can store data in files easyly.
"""
import os
import json
from warnings import deprecated
2024-07-20 19:06:26 +02:00
@deprecated("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):
2024-03-14 18:36:52 +01:00
"""
Loads a dataclass instance from a json file.
"""
if os.path.exists(file_path):
file = open(file_path, "r")
class_dict = json.load(file)
file.close()
else:
class_dict = {}
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.
"""
class_dict = class_instance.__dict__
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items()))
file = open(file_path, "w")
json.dump(class_dict, file)
file.close()