0a9989be5f00cdfab0756216830dcf6ee20bc4e2
[mudpy.git] / lib / muff / muffmenu.py
1 """Menu objects for the MUFF Engine"""
2
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 #    - Redistributions of source code must retain the above copyright
11 #      notice, this list of conditions and the following disclaimer.
12 #    - Redistributions in binary form must reproduce the above
13 #      copyright notice, this list of conditions and the following
14 #      disclaimer in the documentation and/or other materials provided
15 #      with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 import ConfigParser
31 import os
32 import re
33 import string
34
35 import muff
36 for module in muff.__all__:
37         exec("import " + module)
38
39 try:
40         if muffconf.config_data.get("general", "menu_path"):
41                 pass
42 except AttributeError:
43         reload(muffconf)
44
45 menu_files = []
46 menu_path = muffconf.config_data.get("general", "menu_path")
47 for each_file in os.listdir(menu_path):
48         menu_files.append(menu_path + "/" + each_file)
49 menu_data = ConfigParser.SafeConfigParser()
50 menu_data.read(menu_files)
51
52 def get_default(user):
53         return menu_data.get(user.state, "default")
54
55 def get_menu(user):
56         if not user.menu_seen:
57                 if user.echoing:
58                         spacer = ""
59                 else:
60                         spacer = "$(eol)"
61                 try:
62                         if menu_data.get(user.state, "echo") == "off" and user.echoing:
63                                 echo = chr(255) + chr(251) + chr(1) + chr(0)
64                                 user.echoing = False
65                         else:
66                                 echo = ""
67                 except:
68                         if not user.echoing:
69                                 echo = chr(255) + chr(252) + chr(1) + chr(0)
70                                 user.echoing = True
71                         else:
72                                 echo = ""
73                 if user.error:
74                         try:
75                                 description = "$(red)" + menu_data.get(user.state, "error_" + user.error) + "$(nrm)$(eol)$(eol)"
76                         except:
77                                 description = "$(red)That is not a valid choice...$(nrm)$(eol)$(eol)"
78                         user.error = ""
79                 else:
80                         try:
81                                 description = menu_data.get(user.state, "description") + "$(eol)$(eol)"
82                         except:
83                                 description = ""
84                 try:
85                         choices = {}
86                         for option in menu_data.options(user.state):
87                                 if re.match("choice_", option):
88                                         choices[option.split("_")[1]] = menu_data.get(user.state, option)
89                         choice_keys = choices.keys()
90                         choice_keys.sort()
91                         choicestring = ""
92                         for choice in choice_keys:
93                                 choicestring += "   [$(red)" + choice + "$(nrm)]  " + choices[choice] + "$(eol)"
94                         if choicestring:
95                                 choicestring += "$(eol)"
96                 except:
97                         choicestring = ""
98                 try:
99                         prompt = menu_data.get(user.state, "prompt") + " "
100                 except:
101                         prompt = ""
102                 try:
103                         default = "[$(red)" + menu_data.get(user.state, "default") + "$(nrm)] "
104                 except:
105                         default = ""
106                 if user.echoing:
107                         echoing = ""
108                 else:
109                         echoing = "(won't echo) "
110                 user.send(echo + spacer + description + choicestring + prompt + default + echoing, "")
111                 user.menu_seen = True
112