##// END OF EJS Templates
Added help() support...
ldufrechou -
Show More
@@ -22,6 +22,7 b' import sys'
22 import os
22 import os
23 import locale
23 import locale
24 import time
24 import time
25 import pydoc,__builtin__,site
25 from ThreadEx import Thread
26 from ThreadEx import Thread
26 from StringIO import StringIO
27 from StringIO import StringIO
27
28
@@ -29,7 +30,33 b' try:'
29 import IPython
30 import IPython
30 except Exception,e:
31 except Exception,e:
31 raise "Error importing IPython (%s)" % str(e)
32 raise "Error importing IPython (%s)" % str(e)
33
34 class _Helper(object):
35 """Redefine the built-in 'help'.
36 This is a wrapper around pydoc.help (with a twist).
37 """
38 def __init__(self,pager):
39 self._pager = pager
40
41 def __repr__(self):
42 return "Type help() for interactive help, " \
43 "or help(object) for help about object."
44 def __call__(self, *args, **kwds):
45 class DummyWriter(object):
46 def __init__(self,pager):
47 self._pager = pager
48
49 def write(self,data):
50 self._pager(data)
51
52 import pydoc
53 pydoc.help.output = DummyWriter(self._pager)
54 pydoc.help.interact = lambda :1
32
55
56 #helper.output.write = self.doc.append
57 return pydoc.help(*args, **kwds)
58
59
33 class IterableIPShell(Thread):
60 class IterableIPShell(Thread):
34 '''
61 '''
35 Create an IPython instance inside a dedicated thread.
62 Create an IPython instance inside a dedicated thread.
@@ -40,7 +67,8 b' class IterableIPShell(Thread):'
40 via raise_exc()
67 via raise_exc()
41 '''
68 '''
42
69
43 def __init__(self,argv=[],user_ns={},user_global_ns=None,
70 def __init__(self,argv
71 =[],user_ns={},user_global_ns=None,
44 cin=None, cout=None, cerr=None,
72 cin=None, cout=None, cerr=None,
45 exit_handler=None,time_loop = 0.1):
73 exit_handler=None,time_loop = 0.1):
46 '''
74 '''
@@ -99,7 +127,7 b' class IterableIPShell(Thread):'
99 IPython.iplib.raw_input_original = self._raw_input
127 IPython.iplib.raw_input_original = self._raw_input
100 #we replace the ipython default exit command by our method
128 #we replace the ipython default exit command by our method
101 self._IP.exit = self._setAskExit
129 self._IP.exit = self._setAskExit
102
130
103 sys.excepthook = excepthook
131 sys.excepthook = excepthook
104
132
105 self._iter_more = 0
133 self._iter_more = 0
@@ -110,15 +138,18 b' class IterableIPShell(Thread):'
110 #thread working vars
138 #thread working vars
111 self._terminate = False
139 self._terminate = False
112 self._time_loop = time_loop
140 self._time_loop = time_loop
113 self._has_doc = False
114 self._do_execute = False
141 self._do_execute = False
115 self._line_to_execute = ''
142 self._line_to_execute = ''
116
143
117 #vars that will be checked by GUI loop to handle thread states...
144 #vars that will be checked by GUI loop to handle thread states...
118 #will be replaced later by PostEvent GUI funtions...
145 #will be replaced later by PostEvent GUI funtions...
119 self._doc_text = None
146 self._doc_text = None
147 self._help_text = None
120 self._ask_exit = False
148 self._ask_exit = False
121 self._add_button = None
149 self._add_button = None
150
151 #we replace the help command
152 self._IP.user_ns['help'] = _Helper(self._pager_help)
122
153
123 #----------------------- Thread management section ----------------------
154 #----------------------- Thread management section ----------------------
124 def run (self):
155 def run (self):
@@ -132,6 +163,7 b' class IterableIPShell(Thread):'
132 try:
163 try:
133 if self._do_execute:
164 if self._do_execute:
134 self._doc_text = None
165 self._doc_text = None
166 self._help_text = None
135 self._execute()
167 self._execute()
136 self._do_execute = False
168 self._do_execute = False
137 self._afterExecute() #used for uper class to generate event after execution
169 self._afterExecute() #used for uper class to generate event after execution
@@ -183,6 +215,15 b' class IterableIPShell(Thread):'
183 """
215 """
184 return self._doc_text
216 return self._doc_text
185
217
218 def getHelpText(self):
219 """
220 Returns the output of the processing that need to be paged via help pager(if any)
221
222 @return: The std output string.
223 @rtype: string
224 """
225 return self._help_text
226
186 def getBanner(self):
227 def getBanner(self):
187 """
228 """
188 Returns the IPython banner for useful info on IPython instance
229 Returns the IPython banner for useful info on IPython instance
@@ -343,6 +384,18 b' class IterableIPShell(Thread):'
343 rv = self._IP.input_hist_raw[self._history_level].strip('\n')
384 rv = self._IP.input_hist_raw[self._history_level].strip('\n')
344 return rv
385 return rv
345
386
387 def _pager_help(self,text):
388 '''
389 This function is used as a callback replacment to IPython help pager function
390
391 It puts the 'text' value inside the self._help_text string that can be retrived via getHelpText
392 function.
393 '''
394 if self._help_text == None:
395 self._help_text = text
396 else:
397 self._help_text += text
398
346 def _pager(self,IP,text):
399 def _pager(self,IP,text):
347 '''
400 '''
348 This function is used as a callback replacment to IPython pager function
401 This function is used as a callback replacment to IPython pager function
@@ -543,7 +543,7 b' class WxIPythonViewPanel(wx.Panel):'
543
543
544 #---------------------------- IPython Thread Management ---------------------------------------
544 #---------------------------- IPython Thread Management ---------------------------------------
545 def stateDoExecuteLine(self):
545 def stateDoExecuteLine(self):
546 #print >>self.sys_stdout,"command:",self.getCurrentLine()
546 #print >>sys.__stdout__,"command:",self.getCurrentLine()
547 self.doExecuteLine(self.text_ctrl.getCurrentLine())
547 self.doExecuteLine(self.text_ctrl.getCurrentLine())
548
548
549 def doExecuteLine(self,line):
549 def doExecuteLine(self,line):
@@ -555,11 +555,16 b' class WxIPythonViewPanel(wx.Panel):'
555
555
556 def evtStateExecuteDone(self,evt):
556 def evtStateExecuteDone(self,evt):
557 self.doc = self.IP.getDocText()
557 self.doc = self.IP.getDocText()
558 self.help = self.IP.getHelpText()
558 if self.doc:
559 if self.doc:
559 self.pager_state = 'INIT'
560 self.pager_state = 'INIT'
560 self.cur_state = 'SHOW_DOC'
561 self.cur_state = 'SHOW_DOC'
561 self.pager(self.doc)
562 self.pager(self.doc)
562 #if self.pager_state == 'DONE':
563 #if self.pager_state == 'DONE':
564 if self.help:
565 self.pager_state = 'INIT_HELP'
566 self.cur_state = 'SHOW_DOC'
567 self.pager(self.help)
563
568
564 else:
569 else:
565 self.stateShowPrompt()
570 self.stateShowPrompt()
@@ -577,7 +582,7 b' class WxIPythonViewPanel(wx.Panel):'
577 self.cur_state = 'IDLE'
582 self.cur_state = 'IDLE'
578
583
579 ## def runStateMachine(self,event):
584 ## def runStateMachine(self,event):
580 ## #print >>self.sys_stdout,"state:",self.cur_state
585 ## #print >>sys.__stdout__,"state:",self.cur_state
581 ## self.updateStatusTracker(self.cur_state)
586 ## self.updateStatusTracker(self.cur_state)
582 ##
587 ##
583 ## #if self.cur_state == 'DO_EXECUTE_LINE':
588 ## #if self.cur_state == 'DO_EXECUTE_LINE':
@@ -626,26 +631,35 b' class WxIPythonViewPanel(wx.Panel):'
626 #---------------------------- IPython pager ---------------------------------------
631 #---------------------------- IPython pager ---------------------------------------
627 def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None):
632 def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None):
628 if self.pager_state == 'WAITING':
633 if self.pager_state == 'WAITING':
629 #print >>self.sys_stdout,"PAGER waiting"
634 #print >>sys.__stdout__,"PAGER waiting"
630 return
635 return
631
636
632 if self.pager_state == 'INIT':
637 if self.pager_state == 'INIT':
633 #print >>self.sys_stdout,"PAGER state:",self.pager_state
638 #print >>sys.__stdout__,"PAGER state:",self.pager_state
634 self.pager_lines = text[7:].split('\n')
639 self.pager_lines = text[7:].split('\n')
635 self.pager_nb_lines = len(self.pager_lines)
640 self.pager_nb_lines = len(self.pager_lines)
636 self.pager_index = 0
641 self.pager_index = 0
637 self.pager_do_remove = False
642 self.pager_do_remove = False
638 self.text_ctrl.write('\n')
643 self.text_ctrl.write('\n')
639 self.pager_state = 'PROCESS_LINES'
644 self.pager_state = 'PROCESS_LINES'
645
646 if self.pager_state == 'INIT_HELP':
647 #print >>sys.__stdout__,"HELP PAGER state:",self.pager_state
648 self.pager_lines = text[:].split('\n')
649 self.pager_nb_lines = len(self.pager_lines)
650 self.pager_index = 0
651 self.pager_do_remove = False
652 self.text_ctrl.write('\n')
653 self.pager_state = 'PROCESS_LINES'
640
654
641 if self.pager_state == 'PROCESS_LINES':
655 if self.pager_state == 'PROCESS_LINES':
642 #print >>self.sys_stdout,"PAGER state:",self.pager_state
656 #print >>sys.__stdout__,"PAGER state:",self.pager_state
643 if self.pager_do_remove == True:
657 if self.pager_do_remove == True:
644 self.text_ctrl.removeCurrentLine()
658 self.text_ctrl.removeCurrentLine()
645 self.pager_do_remove = False
659 self.pager_do_remove = False
646
660
647 if self.pager_nb_lines > 10:
661 if self.pager_nb_lines > 10:
648 #print >>self.sys_stdout,"PAGER processing 10 lines"
662 #print >>sys.__stdout__,"PAGER processing 10 lines"
649 if self.pager_index > 0:
663 if self.pager_index > 0:
650 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
664 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
651 else:
665 else:
@@ -660,7 +674,7 b' class WxIPythonViewPanel(wx.Panel):'
660 self.pager_state = 'WAITING'
674 self.pager_state = 'WAITING'
661 return
675 return
662 else:
676 else:
663 #print >>self.sys_stdout,"PAGER processing last lines"
677 #print >>sys.__stdout__,"PAGER processing last lines"
664 if self.pager_nb_lines > 0:
678 if self.pager_nb_lines > 0:
665 if self.pager_index > 0:
679 if self.pager_index > 0:
666 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
680 self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n')
General Comments 0
You need to be logged in to leave comments. Login now