1 """Data interface functions for the mudpy engine."""
3 # Copyright (c) 2004-2022 mudpy authors. Permission to use, copy,
4 # modify, and distribute this software is granted under terms
5 # provided in the LICENSE file distributed with this software.
15 class _IBSEmitter(yaml.emitter.Emitter):
17 """Override the default YAML Emitter to indent block sequences."""
19 def expect_block_sequence(self):
20 """Match the expectations of the ``yamllint`` style checker."""
22 # TODO(fungi) Get an option for this implemented upstream in
24 self.increase_indent(flow=False, indentless=False)
25 self.state = self.expect_first_block_sequence_item
28 class _IBSDumper(yaml.SafeDumper, _IBSEmitter):
30 """Use our _IBSEmitter instead of the default implementation."""
37 """A file containing universe elements and their facets."""
46 self.universe = universe
51 self.relative = relative
55 """Read a file, create elements and poplulate facets accordingly."""
57 self.source = find_file(
58 self.source, relative=self.relative, universe=self.universe)
60 with open(self.source) as datafd:
61 self.data = yaml.safe_load(datafd)
62 log_entry = ("Loaded file %s into memory." % self.source, 5)
63 except FileNotFoundError:
64 # it's normal if the file is one which doesn't exist yet
67 "File %s was not found and will be created." % self.source, 6)
69 mudpy.misc.log(*log_entry)
71 # happens when we're not far enough along in the init process
72 self.universe.setup_loglines.append(log_entry)
73 if not hasattr(self.universe, "files"):
74 self.universe.files = {}
75 self.universe.files[self.source] = self
77 for node in list(self.data):
79 includes += self.data["_load"]
81 if node.startswith("_"):
83 facet_pos = node.rfind(".") + 1
84 prefix = node[:facet_pos].strip(".")
86 element = self.universe.contents[prefix]
88 element = mudpy.misc.Element(prefix, self.universe, self)
89 element.set(node[facet_pos:], self.data[node])
90 if prefix.startswith("mudpy.movement."):
91 self.universe.directions.add(
92 prefix[prefix.rfind(".") + 1:])
93 for include_file in includes:
94 if not os.path.isabs(include_file):
95 include_file = find_file(
98 universe=self.universe
100 if (include_file not in self.universe.files or not
101 self.universe.files[include_file].is_writeable()):
102 Data(include_file, self.universe)
105 """Write the data, if necessary."""
106 normal_umask = 0o0022
107 private_umask = 0o0077
108 private_file_mode = 0o0600
110 # when modified, writeable and has content or the file exists
111 if self.modified and self.is_writeable() and (
112 self.data or os.path.exists(self.source)
115 # make parent directories if necessary
116 old_umask = os.umask(normal_umask)
117 os.makedirs(os.path.dirname(self.source), exist_ok=True)
121 if "mudpy.limit" in self.universe.contents:
122 max_count = self.universe.contents["mudpy.limit"].get(
126 if os.path.exists(self.source) and max_count:
128 for candidate in os.listdir(os.path.dirname(self.source)):
130 os.path.basename(self.source) +
131 r"""\.\d+$""", candidate
133 backups.append(int(candidate.split(".")[-1]))
136 for old_backup in backups:
137 if old_backup >= max_count - 1:
138 os.remove(self.source + "." + str(old_backup))
139 elif not os.path.exists(
140 self.source + "." + str(old_backup + 1)
143 self.source + "." + str(old_backup),
144 self.source + "." + str(old_backup + 1)
146 if not os.path.exists(self.source + ".0"):
147 os.rename(self.source, self.source + ".0")
150 if "private" in self.flags:
151 old_umask = os.umask(private_umask)
152 file_descriptor = open(self.source, "w")
153 if oct(stat.S_IMODE(os.stat(
154 self.source)[stat.ST_MODE])) != private_file_mode:
155 # if it's marked private, chmod it appropriately
156 os.chmod(self.source, private_file_mode)
158 old_umask = os.umask(normal_umask)
159 file_descriptor = open(self.source, "w")
162 # write and close the file
163 yaml.dump(self.data, Dumper=_IBSDumper, allow_unicode=True,
164 default_flow_style=False, explicit_start=True, indent=4,
165 stream=file_descriptor)
166 file_descriptor.close()
168 # unset the modified flag
169 self.modified = False
171 def is_writeable(self):
172 """Returns True if the _lock is False."""
174 return not self.data.get("_lock", False)
188 """Return an absolute file path based on configuration."""
190 # this is all unnecessary if it's already absolute
191 if file_name and os.path.isabs(file_name):
192 return os.path.realpath(file_name)
194 # if a universe was provided, try to get some defaults from there
198 universe, "contents") and "mudpy.filing" in universe.contents:
199 filing = universe.contents["mudpy.filing"]
201 prefix = filing.get("prefix")
203 search = filing.get("search")
205 stash = filing.get("stash")
207 # if there's only one file loaded, try to work around a chicken<egg
208 elif hasattr(universe, "files") and len(
210 ) == 1 and not universe.files[
211 list(universe.files.keys())[0]].is_writeable():
212 data_file = universe.files[list(universe.files.keys())[0]].data
214 # try for a fallback default directory
216 stash = data_file.get(".mudpy.filing.stash", "")
218 # try for a fallback root path
220 prefix = data_file.get(".mudpy.filing.prefix", "")
222 # try for a fallback search path
224 search = data_file.get(".mudpy.filing.search", "")
226 # another fallback root path, this time from the universe startdir
227 if hasattr(universe, "startdir"):
229 prefix = universe.startdir
230 elif not os.path.isabs(prefix):
231 prefix = os.path.join(universe.startdir, prefix)
233 # when no root path is specified, assume the current working directory
234 if (not prefix or prefix == ".") and hasattr(universe, "startdir"):
235 prefix = universe.startdir
237 # make sure it's absolute
238 prefix = os.path.realpath(prefix)
240 # if there's no search path, just use the root path and etc
242 search = [prefix, "etc"]
244 # work on a copy of the search path, to avoid modifying the caller's
248 # if there's no default path, use the last component of the search path
252 # if an existing file or directory reference was supplied, prepend it
254 if os.path.isdir(relative):
255 search = [relative] + search
257 search = [os.path.dirname(relative)] + search
259 # make the search path entries absolute and throw away any dupes
261 for each_path in search:
262 if not os.path.isabs(each_path):
263 each_path = os.path.realpath(os.path.join(prefix, each_path))
264 if each_path not in clean_search:
265 clean_search.append(each_path)
267 # start hunting for the file now
268 for each_path in clean_search:
270 # construct the candidate path
271 candidate = os.path.join(each_path, file_name)
273 # if the file exists and is readable, we're done
274 if os.path.isfile(candidate):
275 file_name = os.path.realpath(candidate)
278 # if the path is a directory, look for an __init__ file
279 if os.path.isdir(candidate):
280 file_name = os.path.realpath(
281 os.path.join(candidate, "__init__.yaml"))
284 # it didn't exist after all, so use the default path instead
285 if not os.path.isabs(file_name):
286 file_name = os.path.join(stash, file_name)
287 if not os.path.isabs(file_name):
288 file_name = os.path.join(prefix, file_name)
290 # and normalize it last thing before returning
291 file_name = os.path.realpath(file_name)