X-Git-Url: https://mudpy.org/gitweb?a=blobdiff_plain;f=lib%2Fmudpy%2Fdata.py;h=062bd8713c7af1b75f3935d5cfeafd5e5415e203;hb=7e08337e078efd9ecf09b69bfbb093c50487d528;hp=3eddc43bce9e90052414f7868405e91985026598;hpb=b2d5ba9c3c2ab6967d041410bd91062577b09aee;p=mudpy.git diff --git a/lib/mudpy/data.py b/lib/mudpy/data.py index 3eddc43..062bd87 100644 --- a/lib/mudpy/data.py +++ b/lib/mudpy/data.py @@ -1,11 +1,9 @@ -# -*- coding: utf-8 -*- """Data interface functions for the mudpy engine.""" -# Copyright (c) 2004-2014 Jeremy Stanley . Permission +# Copyright (c) 2004-2015 Jeremy Stanley . Permission # to use, copy, modify, and distribute this software is granted under # terms provided in the LICENSE file distributed with this software. -import codecs import os import re import stat @@ -31,11 +29,12 @@ class DataFile: self.data = yaml.load(open(self.filename)) except FileNotFoundError: # it's normal if the file is one which doesn't exist yet + log_entry = ("File %s is unavailable." % self.filename, 6) try: - mudpy.misc.log("Couldn't read %s file." % self.filename, 6) + mudpy.misc.log(*log_entry) except NameError: # happens when we're not far enough along in the init process - pass + self.universe.setup_loglines.append(log_entry) if not hasattr(self.universe, "files"): self.universe.files = {} self.universe.files[self.filename] = self @@ -101,6 +100,9 @@ class DataFile: def save(self): """Write the data, if necessary.""" + normal_umask = 0o0022 + private_umask = 0o0077 + private_file_mode = 0o0600 # when modified, writeable and has content or the file exists if self.modified and self.is_writeable() and ( @@ -109,7 +111,9 @@ class DataFile: # make parent directories if necessary if not os.path.exists(os.path.dirname(self.filename)): + old_umask = os.umask(normal_umask) os.makedirs(os.path.dirname(self.filename)) + os.umask(old_umask) # backup the file if "__control__" in self.data and "backup_count" in self.data[ @@ -145,17 +149,21 @@ class DataFile: os.rename(self.filename, self.filename + ".0") # our data file - file_descriptor = codecs.open(self.filename, "w", "utf-8") - - # if it's marked private, chmod it appropriately - if self.filename in self.universe.private_files and oct( - stat.S_IMODE(os.stat(self.filename)[stat.ST_MODE]) - ) != 0o0600: - os.chmod(self.filename, 0o0600) + if self.filename in self.universe.private_files: + old_umask = os.umask(private_umask) + file_descriptor = open(self.filename, "w") + if oct(stat.S_IMODE(os.stat( + self.filename)[stat.ST_MODE])) != private_file_mode: + # if it's marked private, chmod it appropriately + os.chmod(self.filename, private_file_mode) + else: + old_umask = os.umask(normal_umask) + file_descriptor = open(self.filename, "w") + os.umask(old_umask) - # write, flush and close the file - file_descriptor.write(yaml.dump(self.data)) - file_descriptor.flush() + # write and close the file + yaml.dump(self.data, allow_unicode=True, default_flow_style=False, + stream=file_descriptor) file_descriptor.close() # unset the modified flag