Rename internal:storage to .mudpy.filing
[mudpy.git] / mudpy / data.py
index d7de13a..74d2da8 100644 (file)
@@ -134,15 +134,17 @@ class DataFile:
             if "__control__" in self.data and "backup_count" in self.data[
                     "__control__"]:
                 max_count = self.data["__control__"]["backup_count"]
-            else:
+            elif "mudpy.limit" in self.universe.contents:
                 max_count = self.universe.contents["mudpy.limit"].get(
-                    "backups")
+                    "backups", 0)
+            else:
+                max_count = 0
             if os.path.exists(self.filename) and max_count:
                 backups = []
                 for candidate in os.listdir(os.path.dirname(self.filename)):
                     if re.match(
                        os.path.basename(self.filename) +
-                       """\.\d+$""", candidate
+                       r"""\.\d+$""", candidate
                        ):
                         backups.append(int(candidate.split(".")[-1]))
                 backups.sort()
@@ -191,18 +193,14 @@ class DataFile:
 
 def find_file(
     file_name=None,
-    root_path=None,
-    search_path=None,
-    default_dir=None,
+    prefix=None,
     relative=None,
+    search=None,
+    stash=None,
     universe=None
 ):
     """Return an absolute file path based on configuration."""
 
-    # make sure to get rid of any surrounding quotes first thing
-    if file_name:
-        file_name = file_name.strip("\"'")
-
     # this is all unnecessary if it's already absolute
     if file_name and os.path.isabs(file_name):
         return os.path.realpath(file_name)
@@ -211,16 +209,14 @@ def find_file(
     if universe:
 
         if hasattr(
-           universe,
-           "contents"
-           ) and "internal:storage" in universe.contents:
-            storage = universe.categories["internal"]["storage"]
-            if not root_path:
-                root_path = storage.get("root_path").strip("\"'")
-            if not search_path:
-                search_path = storage.get("search_path")
-            if not default_dir:
-                default_dir = storage.get("default_dir").strip("\"'")
+                universe, "contents") and "mudpy.filing" in universe.contents:
+            filing = universe.contents["mudpy.filing"]
+            if not prefix:
+                prefix = filing.get("prefix")
+            if not search:
+                search = filing.get("search")
+            if not stash:
+                stash = filing.get("stash")
 
         # if there's only one file loaded, try to work around a chicken<egg
         elif hasattr(universe, "files") and len(
@@ -230,63 +226,60 @@ def find_file(
             data_file = universe.files[list(universe.files.keys())[0]].data
 
             # try for a fallback default directory
-            if not default_dir:
-                default_dir = data_file.get(
-                    "internal:storage", "").get("default_dir", "")
+            if not stash:
+                stash = data_file.get(".mudpy.filing.stash", "")
 
             # try for a fallback root path
-            if not root_path:
-                root_path = data_file.get(
-                    "internal:storage", "").get("root_path", "")
+            if not prefix:
+                prefix = data_file.get(".mudpy.filing.prefix", "")
 
             # try for a fallback search path
-            if not search_path:
-                search_path = data_file.get(
-                    "internal:storage", "").get("search_path", "")
+            if not search:
+                search = data_file.get(".mudpy.filing.search", "")
 
         # another fallback root path, this time from the universe startdir
-        if not root_path and hasattr(universe, "startdir"):
-            root_path = universe.startdir
+        if hasattr(universe, "startdir"):
+            if not prefix:
+                prefix = universe.startdir
+            elif not os.path.isabs(prefix):
+                prefix = os.path.join(universe.startdir, prefix)
 
     # when no root path is specified, assume the current working directory
-    if not root_path:
-        root_path = os.getcwd()
+    if not prefix:
+        prefix = os.getcwd()
 
-    # otherwise, make sure it's absolute
-    elif not os.path.isabs(root_path):
-        root_path = os.path.realpath(root_path)
+    # make sure it's absolute
+    prefix = os.path.realpath(prefix)
 
     # if there's no search path, just use the root path and etc
-    if not search_path:
-        search_path = [root_path, "etc"]
+    if not search:
+        search = [prefix, "etc"]
 
     # work on a copy of the search path, to avoid modifying the caller's
     else:
-        search_path = search_path[:]
+        search = search[:]
 
     # if there's no default path, use the last component of the search path
-    if not default_dir:
-        default_dir = search_path[-1]
+    if not stash:
+        stash = search[-1]
 
     # if an existing file or directory reference was supplied, prepend it
     if relative:
-        relative = relative.strip("\"'")
         if os.path.isdir(relative):
-            search_path = [relative] + search_path
+            search = [relative] + search
         else:
-            search_path = [os.path.dirname(relative)] + search_path
+            search = [os.path.dirname(relative)] + search
 
     # make the search path entries absolute and throw away any dupes
-    clean_search_path = []
-    for each_path in search_path:
-        each_path = each_path.strip("\"'")
+    clean_search = []
+    for each_path in search:
         if not os.path.isabs(each_path):
-            each_path = os.path.realpath(os.path.join(root_path, each_path))
-        if each_path not in clean_search_path:
-            clean_search_path.append(each_path)
+            each_path = os.path.realpath(os.path.join(prefix, each_path))
+        if each_path not in clean_search:
+            clean_search.append(each_path)
 
     # start hunting for the file now
-    for each_path in clean_search_path:
+    for each_path in clean_search:
 
         # if the file exists and is readable, we're done
         if os.path.isfile(os.path.join(each_path, file_name)):
@@ -295,9 +288,9 @@ def find_file(
 
     # it didn't exist after all, so use the default path instead
     if not os.path.isabs(file_name):
-        file_name = os.path.join(default_dir, file_name)
+        file_name = os.path.join(stash, file_name)
     if not os.path.isabs(file_name):
-        file_name = os.path.join(root_path, file_name)
+        file_name = os.path.join(prefix, file_name)
 
     # and normalize it last thing before returning
     file_name = os.path.realpath(file_name)