X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=blobdiff_plain;f=mudpy%2Fdata.py;h=b73959a1b63d857657dbdd4f5afce32c3746e593;hp=f113369020fd6ef02fd91ea603366b09a4452b39;hb=204ea77ed37ec8bedee7465577106a3ac420523f;hpb=182e0d2f5d588b71c57272686255a1f6684a2adf diff --git a/mudpy/data.py b/mudpy/data.py index f113369..b73959a 100644 --- a/mudpy/data.py +++ b/mudpy/data.py @@ -1,8 +1,8 @@ """Data interface functions for the mudpy engine.""" -# Copyright (c) 2004-2017 Jeremy Stanley . Permission -# to use, copy, modify, and distribute this software is granted under -# terms provided in the LICENSE file distributed with this software. +# Copyright (c) 2004-2018 mudpy authors. Permission to use, copy, +# modify, and distribute this software is granted under terms +# provided in the LICENSE file distributed with this software. import os import re @@ -12,6 +12,26 @@ import mudpy import yaml +class _IBSEmitter(yaml.emitter.Emitter): + + """Override the default YAML Emitter to indent block sequences.""" + + def expect_block_sequence(self): + """Match the expectations of the ``yamllint`` style checker.""" + + # TODO(fungi) Get an option for this implemented upstream in + # the pyyaml library + self.increase_indent(flow=False, indentless=False) + self.state = self.expect_first_block_sequence_item + + +class _IBSDumper(yaml.SafeDumper, _IBSEmitter): + + """Use our _IBSEmitter instead of the default implementation.""" + + pass + + class Data: """A file containing universe elements and their facets.""" @@ -38,42 +58,36 @@ class Data: self.source, relative=self.relative, universe=self.universe) try: self.data = yaml.safe_load(open(self.source)) + log_entry = ("Loaded file %s into memory." % self.source, 5) except FileNotFoundError: # it's normal if the file is one which doesn't exist yet self.data = {} log_entry = ("File %s is unavailable." % self.source, 6) - try: - mudpy.misc.log(*log_entry) - except NameError: - # happens when we're not far enough along in the init process - self.universe.setup_loglines.append(log_entry) + try: + mudpy.misc.log(*log_entry) + except NameError: + # happens when we're not far enough along in the init process + self.universe.setup_loglines.append(log_entry) if not hasattr(self.universe, "files"): self.universe.files = {} self.universe.files[self.source] = self includes = [] for node in list(self.data): if node == "_load": - for included in self.data["_load"]: - included = find_file( - included, - relative=self.source, - universe=self.universe) - if included not in includes: - includes.append(included) + includes += self.data["_load"] + continue + if node.startswith("_"): continue facet_pos = node.rfind(".") + 1 - if not facet_pos: - mudpy.misc.Element(node, self.universe, self, old_style=True) - else: - prefix = node[:facet_pos].strip(".") - try: - element = self.universe.contents[prefix] - except KeyError: - element = mudpy.misc.Element(prefix, self.universe, self) - element.set(node[facet_pos:], self.data[node]) - if prefix.startswith("mudpy.movement."): - self.universe.directions.add( - prefix[prefix.rfind(".") + 1:]) + prefix = node[:facet_pos].strip(".") + try: + element = self.universe.contents[prefix] + except KeyError: + element = mudpy.misc.Element(prefix, self.universe, self) + element.set(node[facet_pos:], self.data[node]) + if prefix.startswith("mudpy.movement."): + self.universe.directions.add( + prefix[prefix.rfind(".") + 1:]) for include_file in includes: if not os.path.isabs(include_file): include_file = find_file( @@ -97,10 +111,9 @@ class Data: ): # make parent directories if necessary - if not os.path.exists(os.path.dirname(self.source)): - old_umask = os.umask(normal_umask) - os.makedirs(os.path.dirname(self.source)) - os.umask(old_umask) + old_umask = os.umask(normal_umask) + os.makedirs(os.path.dirname(self.source), exist_ok=True) + os.umask(old_umask) # backup the file if "mudpy.limit" in self.universe.contents: @@ -145,8 +158,9 @@ class Data: os.umask(old_umask) # write and close the file - yaml.safe_dump(self.data, allow_unicode=True, - default_flow_style=False, stream=file_descriptor) + yaml.dump(self.data, Dumper=_IBSDumper, allow_unicode=True, + default_flow_style=False, explicit_start=True, indent=4, + stream=file_descriptor) file_descriptor.close() # unset the modified flag @@ -162,7 +176,7 @@ class Data: def find_file( file_name=None, - category=None, + group=None, prefix=None, relative=None, search=None, @@ -215,8 +229,8 @@ def find_file( prefix = os.path.join(universe.startdir, prefix) # when no root path is specified, assume the current working directory - if not prefix: - prefix = os.getcwd() + if (not prefix or prefix == ".") and hasattr(universe, "startdir"): + prefix = universe.startdir # make sure it's absolute prefix = os.path.realpath(prefix) @@ -273,6 +287,4 @@ def find_file( # and normalize it last thing before returning file_name = os.path.realpath(file_name) - - # normalize the resulting file path and hand it back return file_name