Mercurial > parpg-source
comparison console.py @ 178:c706963ed0c3
Made the entities of the current map available in the console.
author | Beliar <KarstenBock@gmx.net> |
---|---|
date | Tue, 06 Mar 2012 15:39:14 +0100 |
parents | 7a89ea5404b1 |
children | 7b6aba7839ea |
comparison
equal
deleted
inserted
replaced
177:62aed6388159 | 178:c706963ed0c3 |
---|---|
46 ] | 46 ] |
47 self.app_listener = app_listener | 47 self.app_listener = app_listener |
48 self.view = self.app_listener.view | 48 self.view = self.app_listener.view |
49 self.model = self.app_listener.model | 49 self.model = self.app_listener.model |
50 self.game_state = self.app_listener.view.model.game_state | 50 self.game_state = self.app_listener.view.model.game_state |
51 self.console_locals = {"__name__":"__paprg_console__",\ | |
52 "__doc__": None,\ | |
53 "app_listener":self.app_listener,\ | |
54 "model":self.app_listener.model,\ | |
55 "view":self.app_listener.view,\ | |
56 "engine":self.app_listener.engine} | |
57 | |
58 self.console = code.InteractiveConsole(self.console_locals) | |
59 | 51 |
60 def handleQuit(self, command): | 52 def handleQuit(self, command): |
61 """Implements the quit console command | 53 """Implements the quit console command |
62 @type command: string | 54 @type command: string |
63 @param command: The command to run | 55 @param command: The command to run |
102 for cmd in self.commands: | 94 for cmd in self.commands: |
103 res += "%10s: %s\n" % (cmd["cmd"], cmd["help"]) | 95 res += "%10s: %s\n" % (cmd["cmd"], cmd["help"]) |
104 return res | 96 return res |
105 | 97 |
106 def handlePython(self,command): | 98 def handlePython(self,command): |
99 console_locals = { | |
100 "__name__":"__paprg_console__", | |
101 "__doc__": None, | |
102 } | |
103 vals, funcs = self.game_state.getGameEnvironment() | |
104 console_locals.update(vals) | |
105 console_locals.update(funcs) | |
106 | |
107 console = code.InteractiveConsole(console_locals) | |
108 | |
107 user_code = command[7:len(command)] | 109 user_code = command[7:len(command)] |
108 | 110 |
109 codeOut = StringIO() | 111 codeOut = StringIO() |
110 | 112 |
111 #make stdout and stderr write to our file, not the terminal | 113 #make stdout and stderr write to our file, not the terminal |
115 #Workaround it not being possible to enter a blank line in the console | 117 #Workaround it not being possible to enter a blank line in the console |
116 if user_code == " ": | 118 if user_code == " ": |
117 user_code = "" | 119 user_code = "" |
118 | 120 |
119 #Process the code | 121 #Process the code |
120 self.console.push(user_code) | 122 console.push(user_code) |
121 if len(self.console.buffer) == 0: | 123 if len(console.buffer) == 0: |
122 output = codeOut.getvalue() | 124 output = codeOut.getvalue() |
123 else: | 125 else: |
124 output = "..." | 126 output = "..." |
125 | 127 |
126 | 128 |
127 #restore stdout and stderr | 129 #restore stdout and stderr |
128 sys.stdout = sys.__stdout__ | 130 sys.stdout = sys.__stdout__ |
129 sys.stderr = sys.__stderr__ | 131 sys.stderr = sys.__stderr__ |
130 | |
131 temp_output = output | 132 temp_output = output |
132 output = "" | 133 output = "" |
133 counter = 0 | 134 counter = 0 |
134 | 135 |
135 #Make the output fit in the console screen | 136 #Make the output fit in the console screen |