コード例 #1
0
ファイル: pythonInterpreter.py プロジェクト: momose-d/USD
    def __init__(self, textEdit, initialPrompt, locals = None):
        """Constructor.

        The optional 'locals' argument specifies the dictionary in
        which code will be executed; it defaults to a newly created
        dictionary with key "__name__" set to "__console__" and key
        "__doc__" set to None.

        """

        super(Controller, self).__init__()

        self.interpreter = Interpreter(textEdit, locals)
        self.interpreter.locals['help'] = _Helper(self, self)

        self.completer = _Completer(self.interpreter.locals)
        # last line + last incomplete lines
        self.lines = []

        # flag: the interpreter needs more input to run the last lines. 
        self.more = 0

        # history
        self.history = []
        self.historyPointer = None
        self.historyInput = ''

        # flag: readline() is being used for e.g. raw_input and input().
        # We use a nested QEventloop here because we want to emulate
        # modeless UI even though the readline protocol requires blocking calls.
        self.readlineEventLoop = QtCore.QEventLoop(textEdit)

        # interpreter prompt.
        try:
            sys.ps1
        except AttributeError:
            sys.ps1 = ">>> "
        try:
            sys.ps2
        except AttributeError:
            sys.ps2 = "... "
            
        self.textEdit = textEdit
        self.textEdit.destroyed.connect(self._TextEditDestroyedSlot)

        self.textEdit.returnPressed.connect(self._ReturnPressedSlot)

        self.textEdit.requestComplete.connect(self._CompleteSlot)

        self.textEdit.requestNext.connect(self._NextSlot)

        self.textEdit.requestPrev.connect(self._PrevSlot)

        appInstance = QtWidgets.QApplication.instance()
        appInstance.aboutToQuit.connect(self._QuitSlot)
        
        self.textEdit.setTabChangesFocus(False)

        self.textEdit.setWordWrapMode(QtGui.QTextOption.WrapAnywhere)
        self.textEdit.setWindowTitle('Interpreter')
        
        self.textEdit.promptLength = len(sys.ps1)

        # Do initial auto-import.
        self._DoAutoImports()

        # interpreter banner
        self.write('Python %s on %s.\n' % (sys.version, sys.platform))

        # Run $PYTHONSTARTUP startup script.
        startupFile = os.getenv('PYTHONSTARTUP')
        if startupFile:
            path = os.path.realpath(os.path.expanduser(startupFile))
            if os.path.isfile(path):
                self.ExecStartupFile(path)

        self.write(initialPrompt)
        self.write(sys.ps1)
        self.SetInputStart()