]>
Commit | Line | Data |
---|---|---|
1 | """Interpreter executes Python commands.""" | |
2 | ||
3 | __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" | |
4 | __cvsid__ = "$Id$" | |
5 | __revision__ = "$Revision$"[11:-2] | |
6 | ||
7 | import os | |
8 | import sys | |
9 | from code import InteractiveInterpreter | |
10 | import dispatcher | |
11 | import introspect | |
12 | import wx | |
13 | ||
14 | class Interpreter(InteractiveInterpreter): | |
15 | """Interpreter based on code.InteractiveInterpreter.""" | |
16 | ||
17 | revision = __revision__ | |
18 | ||
19 | def __init__(self, locals=None, rawin=None, | |
20 | stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr): | |
21 | """Create an interactive interpreter object.""" | |
22 | InteractiveInterpreter.__init__(self, locals=locals) | |
23 | self.stdin = stdin | |
24 | self.stdout = stdout | |
25 | self.stderr = stderr | |
26 | if rawin: | |
27 | import __builtin__ | |
28 | __builtin__.raw_input = rawin | |
29 | del __builtin__ | |
30 | copyright = 'Type "help", "copyright", "credits" or "license"' | |
31 | copyright += ' for more information.' | |
32 | self.introText = 'Python %s on %s%s%s' % \ | |
33 | (sys.version, sys.platform, os.linesep, copyright) | |
34 | try: | |
35 | sys.ps1 | |
36 | except AttributeError: | |
37 | sys.ps1 = '>>> ' | |
38 | try: | |
39 | sys.ps2 | |
40 | except AttributeError: | |
41 | sys.ps2 = '... ' | |
42 | self.more = 0 | |
43 | # List of lists to support recursive push(). | |
44 | self.commandBuffer = [] | |
45 | self.startupScript = os.environ.get('PYTHONSTARTUP') | |
46 | ||
47 | def push(self, command): | |
48 | """Send command to the interpreter to be executed. | |
49 | ||
50 | Because this may be called recursively, we append a new list | |
51 | onto the commandBuffer list and then append commands into | |
52 | that. If the passed in command is part of a multi-line | |
53 | command we keep appending the pieces to the last list in | |
54 | commandBuffer until we have a complete command. If not, we | |
55 | delete that last list.""" | |
56 | ||
57 | # In case the command is unicode try encoding it | |
58 | if type(command) == unicode: | |
59 | try: | |
60 | command = command.encode(wx.GetDefaultPyEncoding()) | |
61 | except UnicodeEncodeError: | |
62 | pass # otherwise leave it alone | |
63 | ||
64 | if not self.more: | |
65 | try: del self.commandBuffer[-1] | |
66 | except IndexError: pass | |
67 | if not self.more: self.commandBuffer.append([]) | |
68 | self.commandBuffer[-1].append(command) | |
69 | source = '\n'.join(self.commandBuffer[-1]) | |
70 | more = self.more = self.runsource(source) | |
71 | dispatcher.send(signal='Interpreter.push', sender=self, | |
72 | command=command, more=more, source=source) | |
73 | return more | |
74 | ||
75 | def runsource(self, source): | |
76 | """Compile and run source code in the interpreter.""" | |
77 | stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr | |
78 | sys.stdin, sys.stdout, sys.stderr = \ | |
79 | self.stdin, self.stdout, self.stderr | |
80 | more = InteractiveInterpreter.runsource(self, source) | |
81 | # If sys.std* is still what we set it to, then restore it. | |
82 | # But, if the executed source changed sys.std*, assume it was | |
83 | # meant to be changed and leave it. Power to the people. | |
84 | if sys.stdin == self.stdin: | |
85 | sys.stdin = stdin | |
86 | if sys.stdout == self.stdout: | |
87 | sys.stdout = stdout | |
88 | if sys.stderr == self.stderr: | |
89 | sys.stderr = stderr | |
90 | return more | |
91 | ||
92 | def getAutoCompleteKeys(self): | |
93 | """Return list of auto-completion keycodes.""" | |
94 | return [ord('.')] | |
95 | ||
96 | def getAutoCompleteList(self, command='', *args, **kwds): | |
97 | """Return list of auto-completion options for a command. | |
98 | ||
99 | The list of options will be based on the locals namespace.""" | |
100 | stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr | |
101 | sys.stdin, sys.stdout, sys.stderr = \ | |
102 | self.stdin, self.stdout, self.stderr | |
103 | l = introspect.getAutoCompleteList(command, self.locals, | |
104 | *args, **kwds) | |
105 | sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr | |
106 | return l | |
107 | ||
108 | def getCallTip(self, command='', *args, **kwds): | |
109 | """Return call tip text for a command. | |
110 | ||
111 | Call tip information will be based on the locals namespace.""" | |
112 | return introspect.getCallTip(command, self.locals, *args, **kwds) | |
113 | ||
114 | ||
115 | class InterpreterAlaCarte(Interpreter): | |
116 | """Demo Interpreter.""" | |
117 | ||
118 | def __init__(self, locals, rawin, stdin, stdout, stderr, | |
119 | ps1='main prompt', ps2='continuation prompt'): | |
120 | """Create an interactive interpreter object.""" | |
121 | Interpreter.__init__(self, locals=locals, rawin=rawin, | |
122 | stdin=stdin, stdout=stdout, stderr=stderr) | |
123 | sys.ps1 = ps1 | |
124 | sys.ps2 = ps2 | |
125 | ||
126 |