Imported from archive.
[mudpy.git] / mudpy.py
index b149fc4..006b6a8 100644 (file)
--- a/mudpy.py
+++ b/mudpy.py
@@ -43,39 +43,45 @@ class Element:
        def facets(self):
                """Return a list of facets for this element."""
                return universe.files[self.origin].data.options(self.key)
-       def get(self, facet, default=""):
+       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)
                else: return default
-       def getboolean(self, facet, default=False):
+       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)
                else: return default
-       def getint(self, facet, default=0):
+       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)
                else: return default
-       def getfloat(self, facet, default=0.0):
+       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)
                else: return default
-       def getlist(self, facet, default=[]):
+       def getlist(self, facet, default=None):
                """Return values as list type."""
+               if default is None: default = []
                value = self.get(facet)
-               if not value: return default
-               else: return makelist(value)
-       def getdict(self, facet, default={}):
+               if value: return makelist(value)
+               else: return default
+       def getdict(self, facet, default=None):
                """Return values as dict type."""
+               if default is None: default = {}
                value = self.get(facet)
-               if not value: return default
-               else: return makedict(value)
+               if value: return makedict(value)
+               else: return default
        def set(self, facet, value):
                """Set values."""
-               if type(value) is long: value = repr(value).rstrip("L")
+               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)
 
@@ -111,7 +117,7 @@ class DataFile:
                                include_file = path_join(dirname(filename), include_file)
                        DataFile(include_file, universe)
        def save(self):
-               if self.data.sections() and ( not self.data.has_option("control", "read_only") or not self.data.getboolean("control", "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") ):
                        if not exists(dirname(self.filename)): makedirs(dirname)
                        file_descriptor = file(self.filename, "w")
                        if self.filename in universe.private_files and oct(S_IMODE(stat(self.filename)[ST_MODE])) != 0600:
@@ -400,21 +406,29 @@ class User:
 
        def new_avatar(self):
                """Instantiate a new, unconfigured avatar for this user."""
-               counter = universe.categories["internal"]["counters"].getint("next_avatar")
-               while "avatar:" + repr(counter + 1) in universe.categories["actor"].keys(): counter += 1
-               universe.categories["internal"]["counters"].set("next_avatar", counter + 1)
-               self.avatar = Element("actor:avatar:" + repr(counter), universe)
+               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)
                avatars = self.account.getlist("avatars")
                avatars.append(self.avatar.key)
                self.account.set("avatars", avatars)
 
-       def list_avatar_names(self):
-               """A test function to list names of assigned avatars."""
+       def delete_avatar(self, avatar):
+               """Remove an avatar from the world and from the user's list."""
+               if self.avatar is universe.contents[avatar]: self.avatar = None
+               universe.contents[avatar].delete()
                avatars = self.account.getlist("avatars")
-               avatar_names = []
-               for avatar in avatars:
-                       avatar_names.append(universe.contents[avatar].get("name"))
-               return avatar_names
+               avatars.remove(avatar)
+               self.account.set("avatars", avatars)
+
+       def delete(self):
+               """Delete the user and associated avatars."""
+               for avatar in self.account.getlist("avatars"): self.delete_avatar(avatar)
+               self.account.delete()
+
+       def list_avatar_names(self):
+               """List names of assigned avatars."""
+               return [ universe.contents[avatar].get("name") for avatar in self.account.getlist("avatars") ]
 
 def makelist(value):
        """Turn string into list type."""
@@ -626,7 +640,7 @@ def on_pulse():
 
        # update the log every now and then
        if check_time("frequency_log"):
-               log(repr(len(universe.userlist)) + " connection(s)")
+               log(str(len(universe.userlist)) + " connection(s)")
 
        # periodically save everything
        if check_time("frequency_save"):
@@ -722,8 +736,8 @@ def get_default_menu_choice(state):
 
 def get_formatted_default_menu_choice(state):
        """Default menu choice foratted for inclusion in a prompt string."""
-       default = get_default_menu_choice(state)
-       if default: return "[$(red)" + default + "$(nrm)] "
+       default_choice = get_default_menu_choice(state)
+       if default_choice: return "[$(red)" + default_choice + "$(nrm)] "
        else: return ""
 
 def get_menu_description(state, error):
@@ -756,23 +770,26 @@ def get_menu_prompt(state):
 
 def get_menu_choices(user):
        """Return a dict of choice:meaning."""
-       choices = {}
+       menu = universe.categories["menu"][user.state]
+       create_choices = menu.get("create")
+       if create_choices: choices = eval(create_choices)
+       else: choices = {}
        ignores = []
        options = {}
        creates = {}
-       for facet in universe.categories["menu"][user.state].facets():
+       for facet in menu.facets():
                if facet.startswith("demand_") and not eval(universe.categories["menu"][user.state].get(facet)):
                        ignores.append(facet.split("_", 2)[1])
-               elif facet.startswith("choice_"):
-                       options[facet] = facet.split("_", 2)[1]
                elif facet.startswith("create_"):
                        creates[facet] = facet.split("_", 2)[1]
-       for facet in options.keys():
-               if not options[facet] in ignores:
-                       choices[options[facet]] = universe.categories["menu"][user.state].get(facet)
+               elif facet.startswith("choice_"):
+                       options[facet] = facet.split("_", 2)[1]
        for facet in creates.keys():
                if not creates[facet] in ignores:
-                       choices[creates[facet]] = eval(universe.categories["menu"][user.state].get(facet))
+                       choices[creates[facet]] = eval(menu.get(facet))
+       for facet in options.keys():
+               if not options[facet] in ignores:
+                       choices[options[facet]] = menu.get(facet)
        return choices
 
 def get_formatted_menu_choices(state, choices):
@@ -800,7 +817,6 @@ def get_default_branch(state):
 def get_choice_branch(user, choice):
        """Returns the new state matching the given choice."""
        branches = get_menu_branches(user.state)
-       if not choice: choice = get_default_menu_choice(user.state)
        if choice in branches.keys(): return branches[choice]
        elif choice in user.menu_choices.keys(): return get_default_branch(user.state)
        else: return ""
@@ -820,7 +836,6 @@ def get_default_action(state):
 def get_choice_action(user, choice):
        """Run any indicated script for the given choice."""
        actions = get_menu_actions(user.state)
-       if not choice: choice = get_default_menu_choice(user.state)
        if choice in actions.keys(): return actions[choice]
        elif choice in user.menu_choices.keys(): return get_default_action(user.state)
        else: return ""
@@ -848,7 +863,7 @@ def generic_menu_handler(user):
                choice = user.input_queue.pop(0)
                if choice: choice = choice.lower()
        else: choice = ""
-
+       if not choice: choice = get_default_menu_choice(user.state)
        if choice in user.menu_choices:
                exec(get_choice_action(user, choice))
                new_state = get_choice_branch(user, choice)
@@ -1011,7 +1026,7 @@ def command_reload(user, command="", parameters=""):
 
 def command_quit(user, command="", parameters=""):
        """Quit the world."""
-       user.state = "disconnecting"
+       user.state = "main_utility"
 
 def command_help(user, command="", parameters=""):
        """List available commands and provide help for commands."""