comparison console.py @ 183:7b6aba7839ea

getGameEnvironment of GameState now returns a locals dictionary that is empty at the beginning of the game and will be saved in save games. The vals and funcs are now both in the globals dictionary. This WILL break old saves.
author Beliar <KarstenBock@gmx.net>
date Thu, 15 Mar 2012 16:24:05 +0100
parents c706963ed0c3
children
comparison
equal deleted inserted replaced
182:f131a1b01254 183:7b6aba7839ea
94 for cmd in self.commands: 94 for cmd in self.commands:
95 res += "%10s: %s\n" % (cmd["cmd"], cmd["help"]) 95 res += "%10s: %s\n" % (cmd["cmd"], cmd["help"])
96 return res 96 return res
97 97
98 def handlePython(self,command): 98 def handlePython(self,command):
99 console_locals = { 99 globals, locals = self.game_state.getGameEnvironment()
100 globals.update({
100 "__name__":"__paprg_console__", 101 "__name__":"__paprg_console__",
101 "__doc__": None, 102 "__doc__": None,
102 } 103 })
103 vals, funcs = self.game_state.getGameEnvironment()
104 console_locals.update(vals)
105 console_locals.update(funcs)
106 104
107 console = code.InteractiveConsole(console_locals) 105 console = code.InteractiveConsole(globals, locals)
108 106
109 user_code = command[7:len(command)] 107 user_code = command[7:len(command)]
110 108
111 codeOut = StringIO() 109 codeOut = StringIO()
112 110
113 #make stdout and stderr write to our file, not the terminal 111 #make stdout and stderr write to our file, not the terminal
114 sys.stdout = codeOut 112 sys.stdout = codeOut
115 sys.stderr = codeOut 113 sys.stderr = codeOut
116 114
117 #Workaround it not being possible to enter a blank line in the console
118 if user_code == " ":
119 user_code = ""
120
121 #Process the code 115 #Process the code
122 console.push(user_code) 116 console.push(user_code)
123 if len(console.buffer) == 0: 117 output = codeOut.getvalue()
124 output = codeOut.getvalue()
125 else:
126 output = "..."
127
128 118
129 #restore stdout and stderr 119 #restore stdout and stderr
130 sys.stdout = sys.__stdout__ 120 sys.stdout = sys.__stdout__
131 sys.stderr = sys.__stderr__ 121 sys.stderr = sys.__stderr__
132 temp_output = output 122 temp_output = output