]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/interpreter.py
   1 """Interpreter executes Python commands.""" 
   3 __author__ 
= "Patrick K. O'Brien <pobrien@orbtech.com>" 
   5 __revision__ 
= "$Revision$"[11:-2] 
   9 from code 
import InteractiveInterpreter
 
  14 class Interpreter(InteractiveInterpreter
): 
  15     """Interpreter based on code.InteractiveInterpreter.""" 
  17     revision 
= __revision__
 
  19     def __init__(self
, locals=None, rawin
=None,  
  20                  stdin
=sys
.stdin
, stdout
=sys
.stdout
, stderr
=sys
.stderr
, 
  21                  showInterpIntro
=True): 
  22         """Create an interactive interpreter object.""" 
  23         InteractiveInterpreter
.__init
__(self
, locals=locals) 
  29             __builtin__
.raw_input = rawin
 
  32             copyright 
= 'Type "help", "copyright", "credits" or "license"' 
  33             copyright 
+= ' for more information.' 
  34             self
.introText 
= 'Python %s on %s%s%s' % \
 
  35                              (sys
.version
, sys
.platform
, os
.linesep
, copyright
) 
  38         except AttributeError: 
  42         except AttributeError: 
  45         # List of lists to support recursive push(). 
  46         self
.commandBuffer 
= [] 
  47         self
.startupScript 
= None 
  50     def push(self
, command
): 
  51         """Send command to the interpreter to be executed. 
  53         Because this may be called recursively, we append a new list 
  54         onto the commandBuffer list and then append commands into 
  55         that.  If the passed in command is part of a multi-line 
  56         command we keep appending the pieces to the last list in 
  57         commandBuffer until we have a complete command. If not, we 
  58         delete that last list.""" 
  60         # In case the command is unicode try encoding it 
  61         if type(command
) == unicode: 
  63                 command 
= command
.encode(wx
.GetDefaultPyEncoding()) 
  64             except UnicodeEncodeError: 
  65                 pass # otherwise leave it alone 
  68             try: del self
.commandBuffer
[-1] 
  69             except IndexError: pass 
  70         if not self
.more
: self
.commandBuffer
.append([]) 
  71         self
.commandBuffer
[-1].append(command
) 
  72         source 
= '\n'.join(self
.commandBuffer
[-1]) 
  73         more 
= self
.more 
= self
.runsource(source
) 
  74         dispatcher
.send(signal
='Interpreter.push', sender
=self
, 
  75                         command
=command
, more
=more
, source
=source
) 
  78     def runsource(self
, source
): 
  79         """Compile and run source code in the interpreter.""" 
  80         stdin
, stdout
, stderr 
= sys
.stdin
, sys
.stdout
, sys
.stderr
 
  81         sys
.stdin
, sys
.stdout
, sys
.stderr 
= \
 
  82                    self
.stdin
, self
.stdout
, self
.stderr
 
  83         more 
= InteractiveInterpreter
.runsource(self
, source
) 
  84         # If sys.std* is still what we set it to, then restore it. 
  85         # But, if the executed source changed sys.std*, assume it was 
  86         # meant to be changed and leave it. Power to the people. 
  87         if sys
.stdin 
== self
.stdin
: 
  89         if sys
.stdout 
== self
.stdout
: 
  91         if sys
.stderr 
== self
.stderr
: 
  95     def getAutoCompleteKeys(self
): 
  96         """Return list of auto-completion keycodes.""" 
  99     def getAutoCompleteList(self
, command
='', *args
, **kwds
): 
 100         """Return list of auto-completion options for a command. 
 102         The list of options will be based on the locals namespace.""" 
 103         stdin
, stdout
, stderr 
= sys
.stdin
, sys
.stdout
, sys
.stderr
 
 104         sys
.stdin
, sys
.stdout
, sys
.stderr 
= \
 
 105                    self
.stdin
, self
.stdout
, self
.stderr
 
 106         l 
= introspect
.getAutoCompleteList(command
, self
.locals, 
 108         sys
.stdin
, sys
.stdout
, sys
.stderr 
= stdin
, stdout
, stderr
 
 111     def getCallTip(self
, command
='', *args
, **kwds
): 
 112         """Return call tip text for a command. 
 114         Call tip information will be based on the locals namespace.""" 
 115         return introspect
.getCallTip(command
, self
.locals, *args
, **kwds
) 
 118 class InterpreterAlaCarte(Interpreter
): 
 119     """Demo Interpreter.""" 
 121     def __init__(self
, locals, rawin
, stdin
, stdout
, stderr
,  
 122                  ps1
='main prompt', ps2
='continuation prompt'): 
 123         """Create an interactive interpreter object.""" 
 124         Interpreter
.__init
__(self
, locals=locals, rawin
=rawin
,  
 125                              stdin
=stdin
, stdout
=stdout
, stderr
=stderr
)