]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/interpreter.py
48e9056464e0a044dc97f11f7ca77ad55d5d881d
[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 is not None:
27 import __builtin__
28 __builtin__.raw_input = rawin
29 del __builtin__
30 copyright = 'Type "copyright", "credits" or "license" for more information.'
31 self.introText = 'Python %s on %s%s%s' % \
32 (sys.version, sys.platform, os.linesep, copyright)
33 try:
34 sys.ps1
35 except AttributeError:
36 sys.ps1 = '>>> '
37 try:
38 sys.ps2
39 except AttributeError:
40 sys.ps2 = '... '
41 self.more = 0
42 self.commandBuffer = [] # List of lists to support recursive push().
43 self.commandHistory = []
44 self.startupScript = os.environ.get('PYTHONSTARTUP')
45
46 def push(self, command):
47 """Send command to the interpreter to be executed.
48
49 Because this may be called recursively, we append a new list
50 onto the commandBuffer list and then append commands into that.
51 If the passed in command is part of a multi-line command we keep
52 appending the pieces to the last list in commandBuffer until we
53 have a complete command, then, finally, we delete that last list.
54 """
55 if not self.more: self.commandBuffer.append([])
56 self.commandBuffer[-1].append(command)
57 source = '\n'.join(self.commandBuffer[-1])
58 self.more = self.runsource(source)
59 if not self.more: del self.commandBuffer[-1]
60 return self.more
61
62 def runsource(self, source):
63 """Compile and run source code in the interpreter."""
64 stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr
65 sys.stdin = self.stdin
66 sys.stdout = self.stdout
67 sys.stderr = self.stderr
68 more = InteractiveInterpreter.runsource(self, source)
69 sys.stdin, sys.stdout, sys.stderr = stdin, stdout, stderr
70 return more
71
72 def getAutoCompleteList(self, command='', *args, **kwds):
73 """Return list of auto-completion options for a command.
74
75 The list of options will be based on the locals namespace."""
76 return introspect.getAutoCompleteList(command, self.locals, *args, **kwds)
77
78 def getCallTip(self, command='', *args, **kwds):
79 """Return call tip text for a command.
80
81 The call tip information will be based on the locals namespace."""
82 return introspect.getCallTip(command, self.locals, *args, **kwds)
83
84
85 class InterpreterAlaCarte(Interpreter):
86 """PyCrustAlaCarte Demo Interpreter."""
87
88 def __init__(self, locals, rawin, stdin, stdout, stderr, \
89 ps1='main prompt', ps2='continuation prompt'):
90 """Create an interactive interpreter object."""
91 Interpreter.__init__(self, locals=locals, rawin=rawin, \
92 stdin=stdin, stdout=stdout, stderr=stderr)
93 sys.ps1 = ps1
94 sys.ps2 = ps2
95