X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=blobdiff_plain;f=mudpy%2Fmisc.py;h=32c0d688c20b94376e3e1cfe91c3b145febfdc12;hp=fb651f677d0d642d82373bf1551fb962be23959f;hb=faf8c4653aa2b1e184545aef15b5ba192dd78d03;hpb=6c702cf838cfe7ae7b8f993b172dfdea6bafc802 diff --git a/mudpy/misc.py b/mudpy/misc.py index fb651f6..32c0d68 100644 --- a/mudpy/misc.py +++ b/mudpy/misc.py @@ -1,6 +1,6 @@ """Miscellaneous functions for the mudpy engine.""" -# Copyright (c) 2004-2016 Jeremy Stanley . Permission +# 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. @@ -23,7 +23,7 @@ class Element: """An element of the universe.""" - def __init__(self, key, universe, filename=None, old_style=False): + def __init__(self, key, universe, origin=None, old_style=False): """Set up a new element.""" # TODO(fungi): This can be removed after the transition is complete @@ -62,21 +62,16 @@ class Element: self.category = "other" self.subkey = self.key if self.category not in self.universe.categories: - self.category = "other" - self.subkey = self.key + self.universe.categories[self.category] = {} - # get an appropriate filename for the origin - if not filename: - filename = self.universe.default_origins[self.category] - if not os.path.isabs(filename): - filename = os.path.abspath(filename) - - # add the file if it doesn't exist yet - if filename not in self.universe.files: - mudpy.data.DataFile(filename, self.universe) + # get an appropriate origin + if not origin: + self.universe.add_category(self.category) + origin = self.universe.files[ + self.universe.origins[self.category]["fallback"]] # record or reset a pointer to the origin file - self.origin = self.universe.files[filename] + self.origin = self.universe.files[origin.source] # add a data section to the origin if necessary if self.key not in self.origin.data: @@ -88,8 +83,7 @@ class Element: def reload(self): """Create a new element and replace this one.""" - Element(self.key, self.universe, self.origin.filename, - old_style=self.old_style) + Element(self.key, self.universe, self.origin, old_style=self.old_style) del(self) def destroy(self): @@ -154,6 +148,12 @@ class Element: def set(self, facet, value): """Set values.""" + if not self.origin.is_writeable() and not self.universe.loading: + # break if there is an attempt to update an element from a + # read-only file, unless the universe is in the midst of loading + # updated data from files + raise PermissionError("Altering elements in read-only files is " + "disallowed") if facet in ["loglevel"]: value = int(value) if not self.has_facet(facet) or not self.get(facet) == value: @@ -345,10 +345,10 @@ class Universe: """Initialize the universe.""" self.categories = {} self.contents = {} - self.default_origins = {} self.directions = set() + self.loading = False self.loglines = [] - self.private_files = [] + self.origins = {} self.reload_flag = False self.setup_loglines = [] self.startdir = os.getcwd() @@ -375,6 +375,9 @@ class Universe: def load(self): """Load universe data from persistent storage.""" + # while loading, it's safe to update elements from read-only files + self.loading = True + # it's possible for this to enter before logging configuration is read pending_loglines = [] @@ -394,11 +397,20 @@ class Universe: del self.files[data_filename] # start loading from the initial file - mudpy.data.DataFile(self.filename, self) + mudpy.data.Data(self.filename, self) + + # load default storage locations for categories + if hasattr(self, "contents") and "mudpy.filing" in self.contents: + self.origins.update(self.contents["mudpy.filing"].get( + "categories", {})) + + # add some builtin categories we know we'll need + for category in ("account", "actor", "internal"): + self.add_category(category) # make a list of inactive avatars inactive_avatars = [] - for account in self.categories["account"].values(): + for account in self.categories.get("account", {}).values(): for avatar in account.get("avatars"): try: inactive_avatars.append(self.contents[avatar]) @@ -425,6 +437,10 @@ class Universe: for element in self.contents.values(): element.update_location() element.clean_contents() + + # done loading, so disallow updating elements from read-only files + self.loading = False + return pending_loglines def new(self): @@ -491,6 +507,19 @@ class Universe: """Convenience method to get the elapsed time counter.""" return self.categories["internal"]["counters"].get("elapsed") + def add_category(self, category, fallback=None): + """Set up category tracking/metadata.""" + if category not in self.origins: + self.origins[category] = {} + if not fallback: + fallback = mudpy.data.find_file( + ".".join((category, "yaml")), universe=self) + if "fallback" not in self.origins[category]: + self.origins[category]["fallback"] = fallback + flags = self.origins[category].get("flags", None) + if fallback not in self.files: + mudpy.data.Data(fallback, self, flags=flags) + class User: @@ -536,9 +565,8 @@ class User: def check_idle(self): """Warn or disconnect idle users as appropriate.""" idletime = universe.get_time() - self.last_input - linkdead_dict = universe.categories["internal"]["time"].get( - "linkdead" - ) + linkdead_dict = universe.contents[ + "mudpy.timing.idle.disconnect"].facets() if self.state in linkdead_dict: linkdead_state = self.state else: @@ -560,7 +588,7 @@ class User: log(logline, 2) self.state = "disconnecting" self.menu_seen = False - idle_dict = universe.categories["internal"]["time"].get("idle") + idle_dict = universe.contents["mudpy.timing.idle.warn"].facets() if self.state in idle_dict: idle_state = self.state else: @@ -904,7 +932,7 @@ class User: counter = 0 while "avatar:" + self.account.get("name") + ":" + str( counter - ) in universe.categories["actor"].keys(): + ) in universe.categories.get("actor", {}).keys(): counter += 1 self.avatar = Element( "actor:avatar:" + self.account.get("name") + ":" + str( @@ -1308,7 +1336,7 @@ def replace_macros(user, text, is_input=False): replacement = replacement[:-2] else: replacement = "" - log("Couldn't read included " + incfile + " file.", 6) + log("Couldn't read included " + incfile + " file.", 7) # if we get here, log and replace it with null else: @@ -1362,18 +1390,14 @@ def on_pulse(): user.pulse() # add an element for counters if it doesn't exist - if "counters" not in universe.categories["internal"]: - universe.categories["internal"]["counters"] = Element( - "internal:counters", universe, old_style=True - ) + if "counters" not in universe.categories.get("internal", {}): + Element("internal:counters", universe, old_style=True) # update the log every now and then if not universe.categories["internal"]["counters"].get("mark"): log(str(len(universe.userlist)) + " connection(s)") universe.categories["internal"]["counters"].set( - "mark", universe.categories["internal"]["time"].get( - "frequency_log" - ) + "mark", universe.contents["mudpy.timing"].get("status") ) else: universe.categories["internal"]["counters"].set( @@ -1386,9 +1410,7 @@ def on_pulse(): if not universe.categories["internal"]["counters"].get("save"): universe.save() universe.categories["internal"]["counters"].set( - "save", universe.categories["internal"]["time"].get( - "frequency_save" - ) + "save", universe.contents["mudpy.timing"].get("save") ) else: universe.categories["internal"]["counters"].set( @@ -1398,8 +1420,7 @@ def on_pulse(): ) # pause for a configurable amount of time (decimal seconds) - time.sleep(universe.categories["internal"] - ["time"].get("increment")) + time.sleep(universe.contents["mudpy.timing"].get("increment")) # increase the elapsed increment counter universe.categories["internal"]["counters"].set( @@ -1696,7 +1717,7 @@ def handler_entering_account_name(user): user.error = "bad_name" # if that account exists, time to request a password - elif name in universe.categories["account"]: + elif name in universe.categories.get("account", {}): user.account = universe.categories["account"][name] user.state = "checking_password" @@ -2112,15 +2133,17 @@ def command_show(actor, parameters): message += "$(eol) $(grn)" + category + "$(nrm)" elif arguments[0] == "files": message = "These are the current files containing the universe:$(eol)" - filenames = list(universe.files.keys()) - filenames.sort() + filenames = sorted(universe.files) for filename in filenames: if universe.files[filename].is_writeable(): status = "rw" else: status = "ro" - message += ("$(eol) $(red)(" + status + ") $(grn)" + filename - + "$(nrm)") + message += ("$(eol) $(red)(%s) $(grn)%s$(nrm)" % + (status, filename)) + if universe.files[filename].flags: + message += (" $(yel)[%s]$(nrm)" % + ",".join(universe.files[filename].flags)) elif arguments[0] == "category": if len(arguments) != 2: message = "You must specify one category." @@ -2155,7 +2178,7 @@ def command_show(actor, parameters): elif arguments[1].strip(".") in universe.contents: element = universe.contents[arguments[1].strip(".")] message = ('These are the properties of the "' + arguments[1] - + '" element (in "' + element.origin.filename + + '" element (in "' + element.origin.source + '"):$(eol)') facets = element.facets() for facet in sorted(facets): @@ -2286,6 +2309,11 @@ def command_set(actor, parameters): else: try: universe.contents[element].set(facet, value) + except PermissionError: + message = ('The "%s" element is kept in read-only file ' + '"%s" and cannot be altered.' % + (element, universe.contents[ + element].origin.source)) except ValueError: message = ('Value "%s" of type "%s" cannot be coerced ' 'to the correct datatype for facet "%s".' %