]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/interpreter.py
3474fe2885a11adfc0de1d07031c6969f451f31f
[wxWidgets.git] / wxPython / wxPython / lib / PyCrust / interpreter.py
1 """PyCrust Interpreter executes Python commands."""
2
3 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
4 __cvsid__ = "$Id$"
5 __date__ = "July 1, 2001"
6 __version__ = "$Revision$"[11:-2]
7
8 import os
9 import sys
10 from code import InteractiveInterpreter
11 import introspect
12
13
14 class Interpreter(InteractiveInterpreter):
15 """PyCrust Interpreter based on code.InteractiveInterpreter."""
16
17 revision = __version__
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 = \
31 'Type "copyright", "credits" or "license" 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 command
53 we keep appending the pieces to the last list in commandBuffer
54 until we have a complete command, then, finally, we delete
55 that last list."""
56 if not self.more: self.commandBuffer.append([])
57 self.commandBuffer[-1].append(command)
58 source = '\n'.join(self.commandBuffer[-1])
59 self.more = self.runsource(source)
60 if not self.more: del self.commandBuffer[-1]
61 return self.more
62
63 def runsource(self, source):
64 """Compile and run source code in the interpreter."""
65 stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
66 sys.stdin = self.stdin
67 sys.stdout = self.stdout
68 sys.stderr = self.stderr
69 more = InteractiveInterpreter.runsource(self, source)
70 # If sys.std* is still what we set it to, then restore it.
71 # But, if the executed source changed sys.std*, assume it
72 # was meant to be changed and leave it. Power to the people.
73 if sys.stdin == self.stdin:
74 sys.stdin = stdin
75 if sys.stdout == self.stdout:
76 sys.stdout = stdout
77 if sys.stderr == self.stderr:
78 sys.stderr = stderr
79 return more
80
81 def getAutoCompleteList(self, command='', *args, **kwds):
82 """Return list of auto-completion options for a command.
83
84 The list of options will be based on the locals namespace."""
85 return introspect.getAutoCompleteList(command, self.locals, *args, **kwds)
86
87 def getCallTip(self, command='', *args, **kwds):
88 """Return call tip text for a command.
89
90 The call tip information will be based on the locals namespace."""
91 return introspect.getCallTip(command, self.locals, *args, **kwds)
92
93
94 class InterpreterAlaCarte(Interpreter):
95 """PyCrustAlaCarte Demo Interpreter."""
96
97 def __init__(self, locals, rawin, stdin, stdout, stderr, \
98 ps1='main prompt', ps2='continuation prompt'):
99 """Create an interactive interpreter object."""
100 Interpreter.__init__(self, locals=locals, rawin=rawin, \
101 stdin=stdin, stdout=stdout, stderr=stderr)
102 sys.ps1 = ps1
103 sys.ps2 = ps2
104