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:
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"):
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):
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):
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):
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):
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)
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
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)):
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):
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):
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)