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