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