Accumulate logs if necessary while loading data
[mudpy.git] / lib / mudpy / data.py
1 """Data interface functions for the mudpy engine."""
2
3 # Copyright (c) 2004-2015 Jeremy Stanley <fungi@yuggoth.org>. Permission
4 # to use, copy, modify, and distribute this software is granted under
5 # terms provided in the LICENSE file distributed with this software.
6
7 import os
8 import re
9 import stat
10
11 import mudpy
12 import yaml
13
14
15 class DataFile:
16
17     """A file containing universe elements."""
18
19     def __init__(self, filename, universe):
20         self.filename = filename
21         self.universe = universe
22         self.data = {}
23         self.load()
24
25     def load(self):
26         """Read a file and create elements accordingly."""
27         self.modified = False
28         try:
29             self.data = yaml.load(open(self.filename))
30         except FileNotFoundError:
31             # it's normal if the file is one which doesn't exist yet
32             log_entry = ("File %s is unavailable." % self.filename, 6)
33             try:
34                 mudpy.misc.log(*log_entry)
35             except NameError:
36                 # happens when we're not far enough along in the init process
37                 self.universe.setup_loglines.append(log_entry)
38         if not hasattr(self.universe, "files"):
39             self.universe.files = {}
40         self.universe.files[self.filename] = self
41         includes = []
42         if "__control__" in self.data:
43             if "include_files" in self.data["__control__"]:
44                 for included in self.data["__control__"]["include_files"]:
45                     included = find_file(
46                         included,
47                         relative=self.filename,
48                         universe=self.universe)
49                     if included not in includes:
50                         includes.append(included)
51             if "include_dirs" in self.data["__control__"]:
52                 for included in [
53                     os.path.join(x, "__init__.yaml") for x in
54                         self.data["__control__"]["include_dirs"]
55                 ]:
56                     included = find_file(
57                         included,
58                         relative=self.filename,
59                         universe=self.universe
60                     )
61                     if included not in includes:
62                         includes.append(included)
63             if "default_files" in self.data["__control__"]:
64                 origins = self.data["__control__"]["default_files"]
65                 for key in origins.keys():
66                     origins[key] = find_file(
67                         origins[key],
68                         relative=self.filename,
69                         universe=self.universe
70                     )
71                     if origins[key] not in includes:
72                         includes.append(origins[key])
73                     self.universe.default_origins[key] = origins[key]
74                     if key not in self.universe.categories:
75                         self.universe.categories[key] = {}
76             if "private_files" in self.data["__control__"]:
77                 for item in self.data["__control__"]["private_files"]:
78                     item = find_file(
79                         item,
80                         relative=self.filename,
81                         universe=self.universe
82                     )
83                     if item not in includes:
84                         includes.append(item)
85                     if item not in self.universe.private_files:
86                         self.universe.private_files.append(item)
87         for element in self.data:
88             if element != "__control__":
89                 mudpy.misc.Element(element, self.universe, self.filename)
90         for include_file in includes:
91             if not os.path.isabs(include_file):
92                 include_file = find_file(
93                     include_file,
94                     relative=self.filename,
95                     universe=self.universe
96                 )
97             if (include_file not in self.universe.files or not
98                     self.universe.files[include_file].is_writeable()):
99                 DataFile(include_file, self.universe)
100
101     def save(self):
102         """Write the data, if necessary."""
103
104         # when modified, writeable and has content or the file exists
105         if self.modified and self.is_writeable() and (
106            self.data or os.path.exists(self.filename)
107            ):
108
109             # make parent directories if necessary
110             if not os.path.exists(os.path.dirname(self.filename)):
111                 os.makedirs(os.path.dirname(self.filename))
112
113             # backup the file
114             if "__control__" in self.data and "backup_count" in self.data[
115                     "__control__"]:
116                 max_count = self.data["__control__"]["backup_count"]
117             else:
118                 max_count = self.universe.categories[
119                     "internal"
120                 ][
121                     "limits"
122                 ].get("default_backup_count")
123             if os.path.exists(self.filename) and max_count:
124                 backups = []
125                 for candidate in os.listdir(os.path.dirname(self.filename)):
126                     if re.match(
127                        os.path.basename(self.filename) +
128                        """\.\d+$""", candidate
129                        ):
130                         backups.append(int(candidate.split(".")[-1]))
131                 backups.sort()
132                 backups.reverse()
133                 for old_backup in backups:
134                     if old_backup >= max_count - 1:
135                         os.remove(self.filename + "." + str(old_backup))
136                     elif not os.path.exists(
137                         self.filename + "." + str(old_backup + 1)
138                     ):
139                         os.rename(
140                             self.filename + "." + str(old_backup),
141                             self.filename + "." + str(old_backup + 1)
142                         )
143                 if not os.path.exists(self.filename + ".0"):
144                     os.rename(self.filename, self.filename + ".0")
145
146             # our data file
147             file_descriptor = open(self.filename, "w")
148
149             # if it's marked private, chmod it appropriately
150             if self.filename in self.universe.private_files and oct(
151                stat.S_IMODE(os.stat(self.filename)[stat.ST_MODE])
152                ) != 0o0600:
153                 os.chmod(self.filename, 0o0600)
154
155             # write and close the file
156             yaml.dump(self.data, allow_unicode=True, default_flow_style=False,
157                       stream=file_descriptor)
158             file_descriptor.close()
159
160             # unset the modified flag
161             self.modified = False
162
163     def is_writeable(self):
164         """Returns True if the __control__ read_only is False."""
165         try:
166             return not self.data["__control__"].get("read_only", False)
167         except KeyError:
168             return True
169
170
171 def find_file(
172     file_name=None,
173     root_path=None,
174     search_path=None,
175     default_dir=None,
176     relative=None,
177     universe=None
178 ):
179     """Return an absolute file path based on configuration."""
180
181     # make sure to get rid of any surrounding quotes first thing
182     if file_name:
183         file_name = file_name.strip("\"'")
184
185     # this is all unnecessary if it's already absolute
186     if file_name and os.path.isabs(file_name):
187         return os.path.realpath(file_name)
188
189     # if a universe was provided, try to get some defaults from there
190     if universe:
191
192         if hasattr(
193            universe,
194            "contents"
195            ) and "internal:storage" in universe.contents:
196             storage = universe.categories["internal"]["storage"]
197             if not root_path:
198                 root_path = storage.get("root_path").strip("\"'")
199             if not search_path:
200                 search_path = storage.get("search_path")
201             if not default_dir:
202                 default_dir = storage.get("default_dir").strip("\"'")
203
204         # if there's only one file loaded, try to work around a chicken<egg
205         elif hasattr(universe, "files") and len(
206             universe.files
207         ) == 1 and not universe.files[
208                 list(universe.files.keys())[0]].is_writeable():
209             data_file = universe.files[list(universe.files.keys())[0]].data
210
211             # try for a fallback default directory
212             if not default_dir:
213                 default_dir = data_file.get(
214                     "internal:storage", "").get("default_dir", "")
215
216             # try for a fallback root path
217             if not root_path:
218                 root_path = data_file.get(
219                     "internal:storage", "").get("root_path", "")
220
221             # try for a fallback search path
222             if not search_path:
223                 search_path = data_file.get(
224                     "internal:storage", "").get("search_path", "")
225
226         # another fallback root path, this time from the universe startdir
227         if not root_path and hasattr(universe, "startdir"):
228             root_path = universe.startdir
229
230     # when no root path is specified, assume the current working directory
231     if not root_path:
232         root_path = os.getcwd()
233
234     # otherwise, make sure it's absolute
235     elif not os.path.isabs(root_path):
236         root_path = os.path.realpath(root_path)
237
238     # if there's no search path, just use the root path and etc
239     if not search_path:
240         search_path = [root_path, "etc"]
241
242     # work on a copy of the search path, to avoid modifying the caller's
243     else:
244         search_path = search_path[:]
245
246     # if there's no default path, use the last element of the search path
247     if not default_dir:
248         default_dir = search_path[-1]
249
250     # if an existing file or directory reference was supplied, prepend it
251     if relative:
252         relative = relative.strip("\"'")
253         if os.path.isdir(relative):
254             search_path = [relative] + search_path
255         else:
256             search_path = [os.path.dirname(relative)] + search_path
257
258     # make the search path entries absolute and throw away any dupes
259     clean_search_path = []
260     for each_path in search_path:
261         each_path = each_path.strip("\"'")
262         if not os.path.isabs(each_path):
263             each_path = os.path.realpath(os.path.join(root_path, each_path))
264         if each_path not in clean_search_path:
265             clean_search_path.append(each_path)
266
267     # start hunting for the file now
268     for each_path in clean_search_path:
269
270         # if the file exists and is readable, we're done
271         if os.path.isfile(os.path.join(each_path, file_name)):
272             file_name = os.path.realpath(os.path.join(each_path, file_name))
273             break
274
275     # it didn't exist after all, so use the default path instead
276     if not os.path.isabs(file_name):
277         file_name = os.path.join(default_dir, file_name)
278     if not os.path.isabs(file_name):
279         file_name = os.path.join(root_path, file_name)
280
281     # and normalize it last thing before returning
282     file_name = os.path.realpath(file_name)
283
284     # normalize the resulting file path and hand it back
285     return file_name