From 1e15fb5d2e14b167f80a366c181df0259f7a5c6f Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Fri, 18 Nov 2005 21:03:52 +0000 Subject: [PATCH] Imported from archive. * archetype, mudpy.conf (__control), mudpy.py (User.new_avatar), template: Retermed template as archetype. * mudpy.py: Added more code commentary, and refactored some sections of the source to improve readability. (DataFile.is_writeable, DataFile.save): Abstracted out the writeable check from the save method into a separate method. (DataFile.save): Adjusted to only write when elements within that file have been modified. (Element.delete): Removed obsolete method, recently implemented more cleanly as remove_facet. --- template => archetype | 7 ++- example/index | 4 ++ example/second_square/index | 4 ++ example => example/second_square/location | 0 example/second_square/prop | 5 ++ mudpy.conf | 6 +-- mudpy.py | 89 +++++++++++++++++++------------ 7 files changed, 76 insertions(+), 39 deletions(-) rename template => archetype (55%) create mode 100644 example/index create mode 100644 example/second_square/index rename example => example/second_square/location (100%) create mode 100644 example/second_square/prop diff --git a/template b/archetype similarity index 55% rename from template rename to archetype index 1dd5d10..ad43eba 100644 --- a/template +++ b/archetype @@ -1,7 +1,10 @@ [__control__] read_only = yes -[template:actor] -default_location = location:0,0,1 +[archetype:actor] is_actor = yes +[archetype:avatar] +default_location = location:0,0,1 +inherit = archetype:actor + diff --git a/example/index b/example/index new file mode 100644 index 0000000..10773c7 --- /dev/null +++ b/example/index @@ -0,0 +1,4 @@ +[__control__] +include_files = second_square/index +read_only = yes + diff --git a/example/second_square/index b/example/second_square/index new file mode 100644 index 0000000..2836fe9 --- /dev/null +++ b/example/second_square/index @@ -0,0 +1,4 @@ +[__control__] +include_files = [ "location", "prop" ] +read_only = yes + diff --git a/example b/example/second_square/location similarity index 100% rename from example rename to example/second_square/location diff --git a/example/second_square/prop b/example/second_square/prop new file mode 100644 index 0000000..64d1fd8 --- /dev/null +++ b/example/second_square/prop @@ -0,0 +1,5 @@ +[prop:fountain] +impression = An inviting public fountain bubbles here, tempting you with thirst. +keywords = fountain water +location = location:0,0,0 + diff --git a/mudpy.conf b/mudpy.conf index b70aecf..7ede114 100644 --- a/mudpy.conf +++ b/mudpy.conf @@ -1,6 +1,6 @@ [__control__] -default_files = {"account": "account", "actor": "actor", "command": "command", "internal": "internal", "location": "location", "menu": "menu", "other": "other" } -include_files = ["example", "template"] +default_files = { "account": "account", "actor": "actor", "command": "command", "internal": "internal", "location": "location", "menu": "menu", "other": "other", "prop": "prop" } +include_files = [ "archetype", "example/index" ] private_files = account read_only = yes @@ -14,7 +14,7 @@ punctuation_muse = ... punctuation_say = . [internal:limits] -#default_admins = admin +default_admins = admin max_avatars = 7 password_tries = 3 diff --git a/mudpy.py b/mudpy.py index 8519d05..4155214 100644 --- a/mudpy.py +++ b/mudpy.py @@ -38,11 +38,19 @@ sys.excepthook = excepthook class Element: """An element of the universe.""" - def __init__(self, key, universe, origin=""): - """Default values for the in-memory element variables.""" + def __init__(self, key, universe, filename=""): + """Set up a new element.""" + + # not owned by a user by default (used for avatars) self.owner = None + + # no contents in here by default self.contents = {} + + # keep track of our key name self.key = key + + # parse out appropriate category and subkey names, add to list if self.key.find(":") > 0: self.category, self.subkey = self.key.split(":", 1) else: @@ -50,37 +58,44 @@ class Element: self.subkey = self.key if not self.category in universe.categories: self.category = "other" universe.categories[self.category][self.subkey] = self - self.origin = origin - if not self.origin: self.origin = universe.default_origins[self.category] - if not isabs(self.origin): - self.origin = abspath(self.origin) + + # get an appropriate filename for the origin + if not filename: filename = universe.default_origins[self.category] + if not isabs(filename): filename = abspath(filename) + + # add the file if it doesn't exist yet + if not filename in universe.files: DataFile(filename, universe) + + # record a pointer to the origin file + self.origin = universe.files[filename] + + # add a data section to the origin if necessary + if not self.origin.data.has_section(self.key): + self.origin.data.add_section(self.key) + + # add this element to the universe contents universe.contents[self.key] = self - if not self.origin in universe.files: - DataFile(self.origin, universe) - if not universe.files[self.origin].data.has_section(self.key): - universe.files[self.origin].data.add_section(self.key) + def destroy(self): """Remove an element from the universe and destroy it.""" log("Destroying: " + self.key + ".", 2) - universe.files[self.origin].data.remove_section(self.key) + self.origin.data.remove_section(self.key) del universe.categories[self.category][self.subkey] del universe.contents[self.key] del self - def delete(self, facet): - """Delete a facet from the element.""" - if universe.files[self.origin].data.has_option(self.key, facet): - universe.files[self.origin].data.remove_option(self.key, facet) def facets(self): """Return a list of non-inherited facets for this element.""" - if self.key in universe.files[self.origin].data.sections(): - return universe.files[self.origin].data.options(self.key) + if self.key in self.origin.data.sections(): + return self.origin.data.options(self.key) else: return [] def has_facet(self, facet): """Return whether the non-inherited facet exists.""" return facet in self.facets() def remove_facet(self, facet): """Remove a facet from the element.""" - if self.has_facet(facet): universe.files[self.origin].data.remove_option(self.key, facet) + if self.has_facet(facet): + self.origin.data.remove_option(self.key, facet) + self.origin.modified = True def ancestry(self): """Return a list of the element's inheritance lineage.""" if self.has_facet("inherit"): @@ -94,8 +109,8 @@ class Element: def get(self, facet, default=None): """Retrieve values.""" if default is None: default = "" - if universe.files[self.origin].data.has_option(self.key, facet): - return universe.files[self.origin].data.get(self.key, facet) + if self.origin.data.has_option(self.key, facet): + return self.origin.data.get(self.key, facet) elif self.has_facet("inherit"): for ancestor in self.ancestry(): if universe.contents[ancestor].has_facet(facet): @@ -104,8 +119,8 @@ class Element: def getboolean(self, facet, default=None): """Retrieve values as boolean type.""" if default is None: default=False - if universe.files[self.origin].data.has_option(self.key, facet): - return universe.files[self.origin].data.getboolean(self.key, facet) + if self.origin.data.has_option(self.key, facet): + return self.origin.data.getboolean(self.key, facet) elif self.has_facet("inherit"): for ancestor in self.ancestry(): if universe.contents[ancestor].has_facet(facet): @@ -114,8 +129,8 @@ class Element: def getint(self, facet, default=None): """Return values as int/long type.""" if default is None: default = 0 - if universe.files[self.origin].data.has_option(self.key, facet): - return universe.files[self.origin].data.getint(self.key, facet) + if self.origin.data.has_option(self.key, facet): + return self.origin.data.getint(self.key, facet) elif self.has_facet("inherit"): for ancestor in self.ancestry(): if universe.contents[ancestor].has_facet(facet): @@ -124,8 +139,8 @@ class Element: def getfloat(self, facet, default=None): """Return values as float type.""" if default is None: default = 0.0 - if universe.files[self.origin].data.has_option(self.key, facet): - return universe.files[self.origin].data.getfloat(self.key, facet) + if self.origin.data.has_option(self.key, facet): + return self.origin.data.getfloat(self.key, facet) elif self.has_facet("inherit"): for ancestor in self.ancestry(): if universe.contents[ancestor].has_facet(facet): @@ -145,9 +160,11 @@ class Element: else: return default def set(self, facet, value): """Set values.""" - if type(value) is long: value = str(value) - elif not type(value) is str: value = repr(value) - universe.files[self.origin].data.set(self.key, facet, value) + if not self.has_facet(facet) or not self.get(facet) == value: + if type(value) is long: value = str(value) + elif not type(value) is str: value = repr(value) + self.origin.data.set(self.key, facet, value) + self.origin.modified = True def append(self, facet, value): """Append value tp a list.""" if type(value) is long: value = str(value) @@ -223,6 +240,7 @@ class Element: class DataFile: """A file containing universe elements.""" def __init__(self, filename, universe): + self.modified = False self.data = RawConfigParser() if access(filename, R_OK): self.data.read(filename) self.filename = filename @@ -251,11 +269,14 @@ class DataFile: if not isabs(include_file): include_file = path_join(dirname(filename), include_file) DataFile(include_file, universe) + 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") def save(self): """Write the data, if necessary.""" - # when there is content or the file exists, but is not read-only - if ( self.data.sections() or exists(self.filename) ) and not ( self.data.has_option("__control__", "read_only") and self.data.getboolean("__control__", "read_only") ): + # when modified, writeable and has content or the file exists + if self.modified and self.is_writeable() and ( self.data.sections() or exists(self.filename) ): # make parent directories if necessary if not exists(dirname(self.filename)): @@ -704,7 +725,7 @@ class User: counter = 0 while "avatar:" + self.account.get("name") + ":" + str(counter) in universe.categories["actor"].keys(): counter += 1 self.avatar = Element("actor:avatar:" + self.account.get("name") + ":" + str(counter), universe) - self.avatar.append("inherit", "template:actor") + self.avatar.append("inherit", "archetype:avatar") self.account.append("avatars", self.avatar.key) def delete_avatar(self, avatar): @@ -1464,7 +1485,7 @@ def command_move(user, parameters): def command_look(user, parameters): """Look around.""" - if parameters: user.send("You look at or in anything yet.") + if parameters: user.send("You can't look at or in anything yet.") else: user.avatar.look_at(user.avatar.get("location")) def command_say(user, parameters): @@ -1627,7 +1648,7 @@ def command_delete(user, parameters): if element not in universe.contents: message = "The \"" + element + "\" element does not exist." elif facet not in universe.contents[element].facets(): message = "The \"" + element + "\" element has no \"" + facet + "\" facet." else: - universe.contents[element].delete(facet) + universe.contents[element].remove_facet(facet) message = "You have successfully deleted the \"" + facet + "\" facet of element \"" + element + "\". Try \"show element " + element + "\" for verification." user.send(message) -- 2.11.0