-
Notifications
You must be signed in to change notification settings - Fork 0
/
gloombackend.py
119 lines (94 loc) · 3.64 KB
/
gloombackend.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
class Backend:
# this value is also in .gitignore, change it together with this one
save_path = "__gloomsave__"
save_extension = ".json.gml"
backup_extension = ".prev"
delete_extension = ".old"
new_extension = ".new"
def check_saves(self) -> list:
"""
Check for current save files on disk
"""
list_saves = []
if os.path.exists(self.save_path):
for save_file in os.listdir(self.save_path):
if save_file.endswith(self.save_extension):
list_saves.append(
save_file.replace(self.save_extension, "")
)
return list_saves
@staticmethod
def write_new_text_file(file_name: str, file_text: str):
"""
file_name (str): path name of the file to write
file_text (str): text to write in the file
Write a new text file to disk.
"""
assert isinstance(file_name, str)
assert isinstance(file_text, str)
with open(file_name, "x") as file:
file.write(file_text)
def _backup_creation_(self, save_file: str):
"""
This function should only be used by saveToFile.
Use at your own risk.
save_file (str): file name of the save file
Sets the current save file to become a backup file.
Sets the new save file to become the current save file.
"""
assert isinstance(save_file, str)
os.rename(save_file, save_file + self.backup_extension)
os.rename(save_file + self.new_extension, save_file)
def save_to_file(
self,
save_file: str,
save_info: str,
save_path: str = None,
save_extension: str = None
):
"""
save_path (str): relative path where to save the file
save_file (str): base name of the save file to write
save_info (str): save info to write in the file
Saves text to a file.
Also, checks if a directory for saving and backups exist,
creates a directory and rotates saves and backups if present.
"""
if save_path is None:
save_path = self.save_path
if save_extension is None:
save_extension = self.save_extension
assert isinstance(save_file, str)
assert isinstance(save_info, str)
assert isinstance(save_path, str)
assert isinstance(save_extension, str)
save_file = save_path + "/" + save_file + save_extension
if os.path.exists(save_path):
if os.path.exists(save_file):
self.write_new_text_file(
file_name=save_file + self.new_extension,
file_text=save_info
)
if os.path.exists(save_file + self.backup_extension):
os.rename(
save_file + self.backup_extension,
save_file + self.delete_extension
)
self._backup_creation_(save_file=save_file)
os.remove(save_file + self.delete_extension)
else:
self._backup_creation_(save_file=save_file)
return
else:
os.mkdir(save_path)
self.write_new_text_file(file_name=save_file, file_text=save_info)
@classmethod
def load_save_file_as_text(cls, save_file: str) -> str:
"""
Get the text info in a save file on disk
"""
assert isinstance(save_file, str)
with open(cls.save_path + "/" + save_file + cls.save_extension, "r") as file:
save_text = file.read()
return save_text