]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/interpreter.py
5f12c01f3403be4ee30cb73a038dbc23ae703c88
[wxWidgets.git] / wxPython / wxPython / lib / PyCrust / interpreter.py
1 """PyCrust Interpreter executes Python commands.
2 """
3
4 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __cvsid__ = "$Id$"
6 __date__ = "July 1, 2001"
7 __version__ = "$Revision$"[11:-2]
8
9 import os
10 import sys
11
12 from code import InteractiveInterpreter
13 import introspect
14
15
16 class Interpreter(InteractiveInterpreter):
17 """PyCrust Interpreter based on code.InteractiveInterpreter."""
18 revision = __version__
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\n%s' % \
32 (sys.version, sys.platform, 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