Rename internal:limits element to .mudpy.limit
[mudpy.git] / lib / mudpy / data.py
index de76416..02a9139 100644 (file)
-# -*- coding: utf-8 -*-
 """Data interface functions for the mudpy engine."""
 
-# Copyright (c) 2004-2013 Jeremy Stanley <fungi@yuggoth.org>. Permission
+# Copyright (c) 2004-2016 Jeremy Stanley <fungi@yuggoth.org>. 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
+import stat
+
+import mudpy
+import yaml
+
 
 class DataFile:
 
-    """A file containing universe elements."""
+    """A file containing universe elements and their facets."""
 
     def __init__(self, filename, universe):
         self.filename = filename
         self.universe = universe
+        self.data = {}
         self.load()
 
     def load(self):
-        """Read a file and create elements accordingly."""
-        import mudpy.misc
-        import os
-        import os.path
-        # TODO: remove this check after the switch to py3k
-        try:
-            import configparser
-        except ImportError:
-            import ConfigParser as configparser
-        self.data = configparser.RawConfigParser()
+        """Read a file, create elements and poplulate facets accordingly."""
         self.modified = False
-        if os.access(self.filename, os.R_OK):
-            self.data.read(self.filename)
+        try:
+            self.data = yaml.safe_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(*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.filename] = self
         includes = []
-        if self.data.has_option("__control__", "include_files"):
-            for included in makelist(
-                self.data.get("__control__", "include_files")
-            ):
-                included = find_file(
-                    included,
-                    relative=self.filename,
-                    universe=self.universe
-                )
-                if included not in includes:
-                    includes.append(included)
-        if self.data.has_option("__control__", "include_dirs"):
-            for included in [
-                os.path.join(x, "__init__.mpy") for x in makelist(
-                    self.data.get("__control__", "include_dirs")
-                )
-            ]:
-                included = find_file(
-                    included,
-                    relative=self.filename,
-                    universe=self.universe
-                )
-                if included not in includes:
-                    includes.append(included)
-        if self.data.has_option("__control__", "default_files"):
-            origins = makedict(
-                self.data.get("__control__", "default_files")
-            )
-            for key in origins.keys():
-                origins[key] = find_file(
-                    origins[key],
-                    relative=self.filename,
-                    universe=self.universe
-                )
-                if origins[key] not in includes:
-                    includes.append(origins[key])
-                self.universe.default_origins[key] = origins[key]
-                if key not in self.universe.categories:
-                    self.universe.categories[key] = {}
-        if self.data.has_option("__control__", "private_files"):
-            for item in makelist(
-                self.data.get("__control__", "private_files")
-            ):
-                item = find_file(
-                    item,
-                    relative=self.filename,
-                    universe=self.universe
-                )
-                if item not in includes:
-                    includes.append(item)
-                if item not in self.universe.private_files:
-                    self.universe.private_files.append(item)
-        for section in self.data.sections():
-            if section != "__control__":
-                mudpy.misc.Element(section, self.universe, self.filename)
+        if "__control__" in self.data:
+            if "include_files" in self.data["__control__"]:
+                for included in self.data["__control__"]["include_files"]:
+                    included = find_file(
+                        included,
+                        relative=self.filename,
+                        universe=self.universe)
+                    if included not in includes:
+                        includes.append(included)
+            if "include_dirs" in self.data["__control__"]:
+                for included in [
+                    os.path.join(x, "__init__.yaml") for x in
+                        self.data["__control__"]["include_dirs"]
+                ]:
+                    included = find_file(
+                        included,
+                        relative=self.filename,
+                        universe=self.universe
+                    )
+                    if included not in includes:
+                        includes.append(included)
+            if "default_files" in self.data["__control__"]:
+                origins = self.data["__control__"]["default_files"]
+                for key in origins.keys():
+                    origins[key] = find_file(
+                        origins[key],
+                        relative=self.filename,
+                        universe=self.universe
+                    )
+                    if origins[key] not in includes:
+                        includes.append(origins[key])
+                    self.universe.default_origins[key] = origins[key]
+                    if key not in self.universe.categories:
+                        self.universe.categories[key] = {}
+            if "private_files" in self.data["__control__"]:
+                for item in self.data["__control__"]["private_files"]:
+                    item = find_file(
+                        item,
+                        relative=self.filename,
+                        universe=self.universe
+                    )
+                    if item not in includes:
+                        includes.append(item)
+                    if item not in self.universe.private_files:
+                        self.universe.private_files.append(item)
+        for node in list(self.data):
+            if node == "__control__":
+                continue
+            facet_pos = node.rfind(".") + 1
+            if not facet_pos:
+                mudpy.misc.Element(node, self.universe, self.filename,
+                                   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.filename)
+                element.set(node[facet_pos:], self.data[node])
         for include_file in includes:
             if not os.path.isabs(include_file):
                 include_file = find_file(
@@ -101,31 +112,28 @@ class DataFile:
 
     def save(self):
         """Write the data, if necessary."""
-        import codecs
-        import os
-        import os.path
-        import re
-        import stat
+        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 (
-           self.data.sections() or os.path.exists(self.filename)
+           self.data or os.path.exists(self.filename)
            ):
 
             # 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 self.data.has_option("__control__", "backup_count"):
-                max_count = self.data.has_option(
-                    "__control__", "backup_count")
+            if "__control__" in self.data and "backup_count" in self.data[
+                    "__control__"]:
+                max_count = self.data["__control__"]["backup_count"]
             else:
-                max_count = self.universe.categories[
-                    "internal"
-                ][
-                    "limits"
-                ].getint("default_backup_count")
+                max_count = self.universe.contents["mudpy.limit"].get(
+                    "backups")
             if os.path.exists(self.filename) and max_count:
                 backups = []
                 for candidate in os.listdir(os.path.dirname(self.filename)):
@@ -138,42 +146,33 @@ class DataFile:
                 backups.reverse()
                 for old_backup in backups:
                     if old_backup >= max_count - 1:
-                        os.remove(self.filename + "." + old_backup)
+                        os.remove(self.filename + "." + str(old_backup))
                     elif not os.path.exists(
-                        self.filename + "." + old_backup + 1
+                        self.filename + "." + str(old_backup + 1)
                     ):
                         os.rename(
-                            self.filename + "." + old_backup,
-                            self.filename + "." + old_backup + 1
+                            self.filename + "." + str(old_backup),
+                            self.filename + "." + str(old_backup + 1)
                         )
                 if not os.path.exists(self.filename + ".0"):
                     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)
-
-            # write it back sorted, instead of using configparser
-            sections = self.data.sections()
-            sections.sort()
-            for section in sections:
-                file_descriptor.write("[" + section + "]\n")
-                options = self.data.options(section)
-                options.sort()
-                for option in options:
-                    file_descriptor.write(
-                        option + " = " +
-                        self.data.get(section, option) + "\n"
-                    )
-                file_descriptor.write("\n")
+            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)
 
-            # flush and close the file
-            file_descriptor.flush()
+            # write and close the file
+            yaml.safe_dump(self.data, allow_unicode=True,
+                           default_flow_style=False, stream=file_descriptor)
             file_descriptor.close()
 
             # unset the modified flag
@@ -181,11 +180,10 @@ class DataFile:
 
     def is_writeable(self):
         """Returns True if the __control__ read_only is False."""
-        return not self.data.has_option(
-            "__control__", "read_only"
-        ) or not self.data.getboolean(
-            "__control__", "read_only"
-        )
+        try:
+            return not self.data["__control__"].get("read_only", False)
+        except KeyError:
+            return True
 
 
 def find_file(
@@ -197,9 +195,6 @@ def find_file(
     universe=None
 ):
     """Return an absolute file path based on configuration."""
-    import os
-    import os.path
-    import sys
 
     # make sure to get rid of any surrounding quotes first thing
     if file_name:
@@ -209,10 +204,6 @@ def find_file(
     if file_name and os.path.isabs(file_name):
         return os.path.realpath(file_name)
 
-    # when no file name is specified, look for <argv[0]>.conf
-    elif not file_name:
-        file_name = os.path.basename(sys.argv[0]) + ".conf"
-
     # if a universe was provided, try to get some defaults from there
     if universe:
 
@@ -224,7 +215,7 @@ def find_file(
             if not root_path:
                 root_path = storage.get("root_path").strip("\"'")
             if not search_path:
-                search_path = storage.getlist("search_path")
+                search_path = storage.get("search_path")
             if not default_dir:
                 default_dir = storage.get("default_dir").strip("\"'")
 
@@ -236,34 +227,19 @@ def find_file(
             data_file = universe.files[list(universe.files.keys())[0]].data
 
             # try for a fallback default directory
-            if not default_dir and data_file.has_option(
-               "internal:storage",
-               "default_dir"
-               ):
+            if not default_dir:
                 default_dir = data_file.get(
-                    "internal:storage",
-                    "default_dir"
-                ).strip("\"'")
+                    "internal:storage", "").get("default_dir", "")
 
             # try for a fallback root path
-            if not root_path and data_file.has_option(
-               "internal:storage",
-               "root_path"
-               ):
+            if not root_path:
                 root_path = data_file.get(
-                    "internal:storage",
-                    "root_path"
-                ).strip("\"'")
+                    "internal:storage", "").get("root_path", "")
 
             # try for a fallback search path
-            if not search_path and data_file.has_option(
-               "internal:storage",
-               "search_path"
-               ):
-                search_path = makelist(
-                    data_file.get("internal:storage",
-                                  "search_path").strip("\"'")
-                )
+            if not search_path:
+                search_path = data_file.get(
+                    "internal:storage", "").get("search_path", "")
 
         # another fallback root path, this time from the universe startdir
         if not root_path and hasattr(universe, "startdir"):
@@ -285,7 +261,7 @@ def find_file(
     else:
         search_path = search_path[:]
 
-    # if there's no default path, use the last element of the search path
+    # if there's no default path, use the last component of the search path
     if not default_dir:
         default_dir = search_path[-1]
 
@@ -325,23 +301,3 @@ def find_file(
 
     # normalize the resulting file path and hand it back
     return file_name
-
-
-def makelist(value):
-    """Turn string into list type."""
-    if value[0] + value[-1] == "[]":
-        return eval(value)
-    elif value[0] + value[-1] == "\"\"":
-        return [value[1:-1]]
-    else:
-        return [value]
-
-
-def makedict(value):
-    """Turn string into dict type."""
-    if value[0] + value[-1] == "{}":
-        return eval(value)
-    elif value.find(":") > 0:
-        return eval("{" + value + "}")
-    else:
-        return {value: None}