74d2da89e12b2bc7152e2011086868db168e1497
[mudpy.git] / mudpy / data.py
1 """Data interface functions for the mudpy engine."""
2
3 # Copyright (c) 2004-2016 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 and their facets."""
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, create elements and poplulate facets accordingly."""
27         self.modified = False
28         try:
29             self.data = yaml.safe_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 node in list(self.data):
88             if node == "__control__":
89                 continue
90             facet_pos = node.rfind(".") + 1
91             if not facet_pos:
92                 mudpy.misc.Element(node, self.universe, self.filename,
93                                    old_style=True)
94             else:
95                 prefix = node[:facet_pos].strip(".")
96                 try:
97                     element = self.universe.contents[prefix]
98                 except KeyError:
99                     element = mudpy.misc.Element(prefix, self.universe,
100                                                  self.filename)
101                 element.set(node[facet_pos:], self.data[node])
102                 if prefix.startswith("mudpy.movement."):
103                     self.universe.directions.add(
104                         prefix[prefix.rfind(".") + 1:])
105         for include_file in includes:
106             if not os.path.isabs(include_file):
107                 include_file = find_file(
108                     include_file,
109                     relative=self.filename,
110                     universe=self.universe
111                 )
112             if (include_file not in self.universe.files or not
113                     self.universe.files[include_file].is_writeable()):
114                 DataFile(include_file, self.universe)
115
116     def save(self):
117         """Write the data, if necessary."""
118         normal_umask = 0o0022
119         private_umask = 0o0077
120         private_file_mode = 0o0600
121
122         # when modified, writeable and has content or the file exists
123         if self.modified and self.is_writeable() and (
124            self.data or os.path.exists(self.filename)
125            ):
126
127             # make parent directories if necessary
128             if not os.path.exists(os.path.dirname(self.filename)):
129                 old_umask = os.umask(normal_umask)
130                 os.makedirs(os.path.dirname(self.filename))
131                 os.umask(old_umask)
132
133             # backup the file
134             if "__control__" in self.data and "backup_count" in self.data[
135                     "__control__"]:
136                 max_count = self.data["__control__"]["backup_count"]
137             elif "mudpy.limit" in self.universe.contents:
138                 max_count = self.universe.contents["mudpy.limit"].get(
139                     "backups", 0)
140             else:
141                 max_count = 0
142             if os.path.exists(self.filename) and max_count:
143                 backups = []
144                 for candidate in os.listdir(os.path.dirname(self.filename)):
145                     if re.match(
146                        os.path.basename(self.filename) +
147                        r"""\.\d+$""", candidate
148                        ):
149                         backups.append(int(candidate.split(".")[-1]))
150                 backups.sort()
151                 backups.reverse()
152                 for old_backup in backups:
153                     if old_backup >= max_count - 1:
154                         os.remove(self.filename + "." + str(old_backup))
155                     elif not os.path.exists(
156                         self.filename + "." + str(old_backup + 1)
157                     ):
158                         os.rename(
159                             self.filename + "." + str(old_backup),
160                             self.filename + "." + str(old_backup + 1)
161                         )
162                 if not os.path.exists(self.filename + ".0"):
163                     os.rename(self.filename, self.filename + ".0")
164
165             # our data file
166             if self.filename in self.universe.private_files:
167                 old_umask = os.umask(private_umask)
168                 file_descriptor = open(self.filename, "w")
169                 if oct(stat.S_IMODE(os.stat(
170                         self.filename)[stat.ST_MODE])) != private_file_mode:
171                     # if it's marked private, chmod it appropriately
172                     os.chmod(self.filename, private_file_mode)
173             else:
174                 old_umask = os.umask(normal_umask)
175                 file_descriptor = open(self.filename, "w")
176             os.umask(old_umask)
177
178             # write and close the file
179             yaml.safe_dump(self.data, allow_unicode=True,
180                            default_flow_style=False, stream=file_descriptor)
181             file_descriptor.close()
182
183             # unset the modified flag
184             self.modified = False
185
186     def is_writeable(self):
187         """Returns True if the __control__ read_only is False."""
188         try:
189             return not self.data["__control__"].get("read_only", False)
190         except KeyError:
191             return True
192
193
194 def find_file(
195     file_name=None,
196     prefix=None,
197     relative=None,
198     search=None,
199     stash=None,
200     universe=None
201 ):
202     """Return an absolute file path based on configuration."""
203
204     # this is all unnecessary if it's already absolute
205     if file_name and os.path.isabs(file_name):
206         return os.path.realpath(file_name)
207
208     # if a universe was provided, try to get some defaults from there
209     if universe:
210
211         if hasattr(
212                 universe, "contents") and "mudpy.filing" in universe.contents:
213             filing = universe.contents["mudpy.filing"]
214             if not prefix:
215                 prefix = filing.get("prefix")
216             if not search:
217                 search = filing.get("search")
218             if not stash:
219                 stash = filing.get("stash")
220
221         # if there's only one file loaded, try to work around a chicken<egg
222         elif hasattr(universe, "files") and len(
223             universe.files
224         ) == 1 and not universe.files[
225                 list(universe.files.keys())[0]].is_writeable():
226             data_file = universe.files[list(universe.files.keys())[0]].data
227
228             # try for a fallback default directory
229             if not stash:
230                 stash = data_file.get(".mudpy.filing.stash", "")
231
232             # try for a fallback root path
233             if not prefix:
234                 prefix = data_file.get(".mudpy.filing.prefix", "")
235
236             # try for a fallback search path
237             if not search:
238                 search = data_file.get(".mudpy.filing.search", "")
239
240         # another fallback root path, this time from the universe startdir
241         if hasattr(universe, "startdir"):
242             if not prefix:
243                 prefix = universe.startdir
244             elif not os.path.isabs(prefix):
245                 prefix = os.path.join(universe.startdir, prefix)
246
247     # when no root path is specified, assume the current working directory
248     if not prefix:
249         prefix = os.getcwd()
250
251     # make sure it's absolute
252     prefix = os.path.realpath(prefix)
253
254     # if there's no search path, just use the root path and etc
255     if not search:
256         search = [prefix, "etc"]
257
258     # work on a copy of the search path, to avoid modifying the caller's
259     else:
260         search = search[:]
261
262     # if there's no default path, use the last component of the search path
263     if not stash:
264         stash = search[-1]
265
266     # if an existing file or directory reference was supplied, prepend it
267     if relative:
268         if os.path.isdir(relative):
269             search = [relative] + search
270         else:
271             search = [os.path.dirname(relative)] + search
272
273     # make the search path entries absolute and throw away any dupes
274     clean_search = []
275     for each_path in search:
276         if not os.path.isabs(each_path):
277             each_path = os.path.realpath(os.path.join(prefix, each_path))
278         if each_path not in clean_search:
279             clean_search.append(each_path)
280
281     # start hunting for the file now
282     for each_path in clean_search:
283
284         # if the file exists and is readable, we're done
285         if os.path.isfile(os.path.join(each_path, file_name)):
286             file_name = os.path.realpath(os.path.join(each_path, file_name))
287             break
288
289     # it didn't exist after all, so use the default path instead
290     if not os.path.isabs(file_name):
291         file_name = os.path.join(stash, file_name)
292     if not os.path.isabs(file_name):
293         file_name = os.path.join(prefix, file_name)
294
295     # and normalize it last thing before returning
296     file_name = os.path.realpath(file_name)
297
298     # normalize the resulting file path and hand it back
299     return file_name