diff --git a/IPython/gui/wx/non_blocking_ip_shell.py b/IPython/gui/wx/ipshell_nonblocking.py similarity index 88% rename from IPython/gui/wx/non_blocking_ip_shell.py rename to IPython/gui/wx/ipshell_nonblocking.py index 8c758c1..be9a9a8 100644 --- a/IPython/gui/wx/non_blocking_ip_shell.py +++ b/IPython/gui/wx/ipshell_nonblocking.py @@ -111,6 +111,29 @@ class NonBlockingIPShell(object): @param time_loop: Define the sleep time between two thread's loop @type int ''' + #ipython0 initialisation + self.initIpython0(argv, user_ns, user_global_ns, + cin, cout, cerr, + ask_exit_handler) + + #vars used by _execute + self._iter_more = 0 + self._history_level = 0 + self._complete_sep = re.compile('[\s\{\}\[\]\(\)]') + self._prompt = str(self._IP.outputcache.prompt1).strip() + + #thread working vars + self._line_to_execute = '' + + #vars that will be checked by GUI loop to handle thread states... + #will be replaced later by PostEvent GUI funtions... + self._doc_text = None + self._help_text = None + self._add_button = None + + def initIpython0(self, argv=[], user_ns={}, user_global_ns=None, + cin=None, cout=None, cerr=None, + ask_exit_handler=None): #first we redefine in/out/error functions of IPython if cin: IPython.Shell.Term.cin = cin @@ -146,32 +169,12 @@ class NonBlockingIPShell(object): #we replace the ipython default input command caller by our method IPython.iplib.raw_input_original = self._raw_input #we replace the ipython default exit command by our method - self._IP.exit = self._setAskExit - #we modify Exit and Quit Magic - ip = IPython.ipapi.get() - ip.expose_magic('Exit', self._setDoExit) - ip.expose_magic('Quit', self._setDoExit) + self._IP.exit = ask_exit_handler #we replace the help command self._IP.user_ns['help'] = _Helper(self._pager_help) sys.excepthook = excepthook - #vars used by _execute - self._iter_more = 0 - self._history_level = 0 - self._complete_sep = re.compile('[\s\{\}\[\]\(\)]') - self._prompt = str(self._IP.outputcache.prompt1).strip() - - #thread working vars - self._line_to_execute = '' - - #vars that will be checked by GUI loop to handle thread states... - #will be replaced later by PostEvent GUI funtions... - self._doc_text = None - self._help_text = None - self._ask_exit = False - self._add_button = None - #----------------------- Thread management section ---------------------- def doExecute(self,line): """ @@ -179,24 +182,11 @@ class NonBlockingIPShell(object): """ self._line_to_execute = line - + #we launch the ipython line execution in a thread to make it interruptible self.ce = _CodeExecutor(self,self._afterExecute) self.ce.start() #----------------------- IPython management section ---------------------- - def getAskExit(self): - ''' - returns the _ask_exit variable that can be checked by GUI to see if - IPython request an exit handling - ''' - return self._ask_exit - - def clearAskExit(self): - ''' - clear the _ask_exit var when GUI as handled the request. - ''' - self._ask_exit = False - def getDocText(self): """ Returns the output of the processing that need to be paged (if any) @@ -349,20 +339,12 @@ class NonBlockingIPShell(object): ''' pass - def _setAskExit(self): - ''' - set the _ask_exit variable that can be checked by GUI to see if - IPython request an exit handling - ''' - self._ask_exit = True + #def _askExit(self): + # ''' + # Can be redefined to generate post event to exit the Ipython shell + # ''' + # pass - def _setDoExit(self, toto, arg): - ''' - set the _do_exit variable that can be checked by GUI to see if - IPython do a direct exit of the app - ''' - self._do_exit = True - def _getHistoryMaxIndex(self): ''' returns the max length of the history buffer diff --git a/IPython/gui/wx/ipython_view.py b/IPython/gui/wx/ipython_view.py index 029ad27..b693352 100644 --- a/IPython/gui/wx/ipython_view.py +++ b/IPython/gui/wx/ipython_view.py @@ -37,8 +37,8 @@ try: except Exception,e: raise "Error importing IPython (%s)" % str(e) +from ipshell_nonblocking import NonBlockingIPShell -from non_blocking_ip_shell import NonBlockingIPShell class WxNonBlockingIPShell(NonBlockingIPShell): ''' @@ -50,20 +50,19 @@ class WxNonBlockingIPShell(NonBlockingIPShell): ask_exit_handler=None): NonBlockingIPShell.__init__(self,argv,user_ns,user_global_ns, - cin, cout, cerr, - ask_exit_handler) + cin, cout, cerr, + ask_exit_handler) self.parent = parent self.ask_exit_callback = ask_exit_handler - self._IP.ask_exit = self._askExit + self._IP.exit = self._askExit - def addGUIShortcut(self,text,func): wx.CallAfter(self.parent.add_button_handler, button_info={ 'text':text, 'func':self.parent.doExecuteLine(func)}) - + def _askExit(self): wx.CallAfter(self.ask_exit_callback, ()) @@ -475,7 +474,7 @@ class IPShellWidget(wx.Panel): ''' wx.Panel.__init__(self,parent,-1) - ### IPython thread instanciation ### + ### IPython non blocking shell instanciation ### self.cout = StringIO() self.add_button_handler = add_button_handler @@ -487,6 +486,7 @@ class IPShellWidget(wx.Panel): self.IP = WxNonBlockingIPShell(self, cout=self.cout,cerr=self.cout, ask_exit_handler = ask_exit_handler) + ### IPython wx console view instanciation ### #If user didn't defined an intro text, we create one for him #If you really wnat an empty intrp just call wxIPythonViewPanel @@ -514,39 +514,36 @@ class IPShellWidget(wx.Panel): #and we focus on the widget :) self.SetFocus() - self.cur_state = 'IDLE' + #widget state management (for key handling different cases) + self.setCurrentState('IDLE') self.pager_state = 'DONE' #---------------------- IPython Thread Management ------------------------ def stateDoExecuteLine(self): #print >>sys.__stdout__,"command:",self.getCurrentLine() - self.doExecuteLine(self.text_ctrl.getCurrentLine()) - - def doExecuteLine(self,line): - #print >>sys.__stdout__,"command:",line + line=self.text_ctrl.getCurrentLine() self.IP.doExecute(line.replace('\t',' '*4)) self.updateHistoryTracker(self.text_ctrl.getCurrentLine()) - self.cur_state = 'WAIT_END_OF_EXECUTION' - + self.setCurrentState('WAIT_END_OF_EXECUTION') def evtStateExecuteDone(self,evt): self.doc = self.IP.getDocText() self.help = self.IP.getHelpText() if self.doc: - self.pager_state = 'INIT' - self.cur_state = 'SHOW_DOC' + self.pager_lines = self.doc[7:].split('\n') + self.pager_state = 'INIT' + self.setCurrentState('SHOW_DOC') self.pager(self.doc) - #if self.pager_state == 'DONE': - if self.help: - self.pager_state = 'INIT_HELP' - self.cur_state = 'SHOW_DOC' - self.pager(self.help) - + elif self.help: + self.pager_lines = self.help.split('\n') + self.pager_state = 'INIT' + self.setCurrentState('SHOW_DOC') + self.pager(self.help) else: self.stateShowPrompt() def stateShowPrompt(self): - self.cur_state = 'SHOW_PROMPT' + self.setCurrentState('SHOW_PROMPT') self.text_ctrl.setPrompt(self.IP.getPrompt()) self.text_ctrl.setIndentation(self.IP.getIndentation()) self.text_ctrl.setPromptCount(self.IP.getPromptCount()) @@ -555,62 +552,53 @@ class IPShellWidget(wx.Panel): self.text_ctrl.showReturned(rv) self.cout.truncate(0) self.IP.initHistoryIndex() - self.cur_state = 'IDLE' + self.setCurrentState('IDLE') - #------------------------ IPython pager ---------------------------------- - def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None): - if self.pager_state == 'WAITING': - #print >>sys.__stdout__,"PAGER waiting" - return + def setCurrentState(self, state): + self.cur_state = state + self.updateStatusTracker(self.cur_state) - if self.pager_state == 'INIT': - #print >>sys.__stdout__,"PAGER state:",self.pager_state - self.pager_lines = text[7:].split('\n') - self.pager_nb_lines = len(self.pager_lines) - self.pager_index = 0 - self.pager_do_remove = False - self.text_ctrl.write('\n') - self.pager_state = 'PROCESS_LINES' + #---------------------------- IPython pager --------------------------------------- + def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None): - if self.pager_state == 'INIT_HELP': - #print >>sys.__stdout__,"HELP PAGER state:",self.pager_state - self.pager_lines = text[:].split('\n') + if self.pager_state == 'INIT': + #print >>sys.__stdout__,"PAGER state:",self.pager_state self.pager_nb_lines = len(self.pager_lines) - self.pager_index = 0 - self.pager_do_remove = False - self.text_ctrl.write('\n') - self.pager_state = 'PROCESS_LINES' - - if self.pager_state == 'PROCESS_LINES': - #print >>sys.__stdout__,"PAGER state:",self.pager_state - if self.pager_do_remove == True: - self.text_ctrl.removeCurrentLine() - self.pager_do_remove = False - - if self.pager_nb_lines > 10: - #print >>sys.__stdout__,"PAGER processing 10 lines" - if self.pager_index > 0: - self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n') - else: - self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n') - - for line in self.pager_lines[self.pager_index+1:self.pager_index+9]: - self.text_ctrl.write("\x01\x1b[1;36m\x02 "+line+'\n') - self.pager_index += 10 - self.pager_nb_lines -= 10 - self.text_ctrl.write("--- Push Enter to continue or 'Q' to quit---") - self.pager_do_remove = True - self.pager_state = 'WAITING' - return - else: - #print >>sys.__stdout__,"PAGER processing last lines" - if self.pager_nb_lines > 0: - if self.pager_index > 0: - self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n') - else: - self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n') - - self.pager_index += 1 + self.pager_index = 0 + self.pager_do_remove = False + self.text_ctrl.write('\n') + self.pager_state = 'PROCESS_LINES' + + if self.pager_state == 'PROCESS_LINES': + #print >>sys.__stdout__,"PAGER state:",self.pager_state + if self.pager_do_remove == True: + self.text_ctrl.removeCurrentLine() + self.pager_do_remove = False + + if self.pager_nb_lines > 10: + #print >>sys.__stdout__,"PAGER processing 10 lines" + if self.pager_index > 0: + self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n') + else: + self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n') + + for line in self.pager_lines[self.pager_index+1:self.pager_index+9]: + self.text_ctrl.write("\x01\x1b[1;36m\x02 "+line+'\n') + self.pager_index += 10 + self.pager_nb_lines -= 10 + self.text_ctrl.write("--- Push Enter to continue or 'Q' to quit---") + self.pager_do_remove = True + self.pager_state = 'WAITING' + return + else: + #print >>sys.__stdout__,"PAGER processing last lines" + if self.pager_nb_lines > 0: + if self.pager_index > 0: + self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n') + else: + self.text_ctrl.write("\x01\x1b[1;36m\x02 "+self.pager_lines[self.pager_index]+'\n') + + self.pager_index += 1 self.pager_nb_lines -= 1 if self.pager_nb_lines > 0: for line in self.pager_lines[self.pager_index:]: @@ -636,7 +624,7 @@ class IPShellWidget(wx.Panel): if event.KeyCode == wx.WXK_RETURN: if self.cur_state == 'IDLE': #we change the state ot the state machine - self.cur_state = 'DO_EXECUTE_LINE' + self.setCurrentState('DO_EXECUTE_LINE') self.stateDoExecuteLine() return if self.pager_state == 'WAITING': diff --git a/IPython/gui/wx/wxIPython.py b/IPython/gui/wx/wxIPython.py index 67c4897..8891d33 100644 --- a/IPython/gui/wx/wxIPython.py +++ b/IPython/gui/wx/wxIPython.py @@ -2,11 +2,13 @@ # -*- coding: iso-8859-15 -*- import wx.aui -import wx.py + +#used for about dialog from wx.lib.wordwrap import wordwrap -from ipython_view import IPShellWidget -from ipython_history import * +#used for ipython GUI objects +from IPython.gui.wx.ipython_view import IPShellWidget +from IPython.gui.wx.ipython_history import IPythonHistoryPanel __version__ = 0.8 __author__ = "Laurent Dufrechou"