Imported from archive.
[mudpy.git] / lib / muff / muffmenu.py
diff --git a/lib/muff/muffmenu.py b/lib/muff/muffmenu.py
new file mode 100644 (file)
index 0000000..0a9989b
--- /dev/null
@@ -0,0 +1,112 @@
+"""Menu objects for the MUFF Engine"""
+
+# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#    - Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#    - Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials provided
+#      with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+import ConfigParser
+import os
+import re
+import string
+
+import muff
+for module in muff.__all__:
+       exec("import " + module)
+
+try:
+       if muffconf.config_data.get("general", "menu_path"):
+               pass
+except AttributeError:
+       reload(muffconf)
+
+menu_files = []
+menu_path = muffconf.config_data.get("general", "menu_path")
+for each_file in os.listdir(menu_path):
+       menu_files.append(menu_path + "/" + each_file)
+menu_data = ConfigParser.SafeConfigParser()
+menu_data.read(menu_files)
+
+def get_default(user):
+       return menu_data.get(user.state, "default")
+
+def get_menu(user):
+       if not user.menu_seen:
+               if user.echoing:
+                       spacer = ""
+               else:
+                       spacer = "$(eol)"
+               try:
+                       if menu_data.get(user.state, "echo") == "off" and user.echoing:
+                               echo = chr(255) + chr(251) + chr(1) + chr(0)
+                               user.echoing = False
+                       else:
+                               echo = ""
+               except:
+                       if not user.echoing:
+                               echo = chr(255) + chr(252) + chr(1) + chr(0)
+                               user.echoing = True
+                       else:
+                               echo = ""
+               if user.error:
+                       try:
+                               description = "$(red)" + menu_data.get(user.state, "error_" + user.error) + "$(nrm)$(eol)$(eol)"
+                       except:
+                               description = "$(red)That is not a valid choice...$(nrm)$(eol)$(eol)"
+                       user.error = ""
+               else:
+                       try:
+                               description = menu_data.get(user.state, "description") + "$(eol)$(eol)"
+                       except:
+                               description = ""
+               try:
+                       choices = {}
+                       for option in menu_data.options(user.state):
+                               if re.match("choice_", option):
+                                       choices[option.split("_")[1]] = menu_data.get(user.state, option)
+                       choice_keys = choices.keys()
+                       choice_keys.sort()
+                       choicestring = ""
+                       for choice in choice_keys:
+                               choicestring += "   [$(red)" + choice + "$(nrm)]  " + choices[choice] + "$(eol)"
+                       if choicestring:
+                               choicestring += "$(eol)"
+               except:
+                       choicestring = ""
+               try:
+                       prompt = menu_data.get(user.state, "prompt") + " "
+               except:
+                       prompt = ""
+               try:
+                       default = "[$(red)" + menu_data.get(user.state, "default") + "$(nrm)] "
+               except:
+                       default = ""
+               if user.echoing:
+                       echoing = ""
+               else:
+                       echoing = "(won't echo) "
+               user.send(echo + spacer + description + choicestring + prompt + default + echoing, "")
+               user.menu_seen = True
+