private_files: [ "account.yaml" ]
read_only: yes
+.mudpy.filing.prefix: "."
+.mudpy.filing.search: [ "", "etc", "share", "data" ]
+.mudpy.filing.stash: "data"
+
.mudpy.linguistic.actions: { "?": "ask", ",": "begin", "-": "begin", ":": "begin", ";": "begin", "!": "exclaim", "...": "muse", ".": "say" }
.mudpy.linguistic.default_punctuation: .
.mudpy.linguistic.typos: { "i": "I", "i'd": "I'd", "i'll": "I'll", "i'm": "I'm", "teh": "the", "theyre": "they're", "youre": "you're" }
.mudpy.timing.increment: 0.1
.mudpy.timing.save: 600
.mudpy.timing.status: 6000
-
-internal:storage:
- default_dir: "data"
- root_path: "."
- search_path: [ "", "etc", "share", "data" ]
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."""
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")
- if not search_path:
- search_path = storage.get("search_path")
- if not default_dir:
- default_dir = storage.get("default_dir")
+ 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(
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 hasattr(universe, "startdir"):
- if not root_path:
- root_path = universe.startdir
- elif not os.path.isabs(root_path):
- root_path = os.path.join(universe.startdir, root_path)
+ 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()
# make sure it's absolute
- root_path = os.path.realpath(root_path)
+ 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:
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:
+ 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)):
# 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)