add load and save functions to classes

move files
update character creater to allowing to stack  classes
This commit is contained in:
2024-04-04 03:35:44 +02:00
parent 4616eda30e
commit d67e7a44cd
7 changed files with 211 additions and 112 deletions
+32 -2
View File
@@ -1,6 +1,9 @@
from json import load as json_load
import os
import my_helpers.myPickle as my_pickle
from json import load as json_load
import logging, os
LOGGING = logging.getLogger(__name__)
class Map:
def __init__(self, path):
self.path = path
@@ -20,3 +23,30 @@ class Map:
def __getitem__(self, key):
return self.story[key]
@classmethod
def load_from_file(cls, file_options):
LOGGING.debug(f"Loading from file: {file_options}")
if os.path.exists(file_options["path"]):
with open(file_options["path"], "rb") as f:
data = my_pickle.load(f, file_options['compression'], None)
new_class = cls(file_options["path"])
new_class.__dict__.update(data.__dict__)
LOGGING.debug(f"Loaded {data}", {"loaded_data": new_class})
return new_class
else:
raise FileNotFoundError(f"Could not load map file: {file_options['path']}")
def save_to_file(self, file_options):
LOGGING.debug(f"Saving Object {self} To file: {file_options}")
os.makedirs(os.path.dirname(file_options["path"]), exist_ok=True)
with open(file_options["path"], "wb") as f:
my_pickle.dump(self, f, file_options['compression'], None)
def by_the_place(self, is_by_a_place, chapter, moves):
LOGGING.debug(is_by_a_place)
if is_by_a_place:
return self[chapter]["Places"][is_by_a_place]
return self[chapter]["World"][moves]