Show More
@@ -22,6 +22,7 b' import sys' | |||
|
22 | 22 | import os |
|
23 | 23 | import locale |
|
24 | 24 | import time |
|
25 | import pydoc,__builtin__,site | |
|
25 | 26 | from ThreadEx import Thread |
|
26 | 27 | from StringIO import StringIO |
|
27 | 28 | |
@@ -29,7 +30,33 b' try:' | |||
|
29 | 30 | import IPython |
|
30 | 31 | except Exception,e: |
|
31 | 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 | 60 | class IterableIPShell(Thread): |
|
34 | 61 | ''' |
|
35 | 62 | Create an IPython instance inside a dedicated thread. |
@@ -40,7 +67,8 b' class IterableIPShell(Thread):' | |||
|
40 | 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 | 72 | cin=None, cout=None, cerr=None, |
|
45 | 73 | exit_handler=None,time_loop = 0.1): |
|
46 | 74 | ''' |
@@ -99,7 +127,7 b' class IterableIPShell(Thread):' | |||
|
99 | 127 | IPython.iplib.raw_input_original = self._raw_input |
|
100 | 128 | #we replace the ipython default exit command by our method |
|
101 | 129 | self._IP.exit = self._setAskExit |
|
102 | ||
|
130 | ||
|
103 | 131 | sys.excepthook = excepthook |
|
104 | 132 | |
|
105 | 133 | self._iter_more = 0 |
@@ -110,15 +138,18 b' class IterableIPShell(Thread):' | |||
|
110 | 138 | #thread working vars |
|
111 | 139 | self._terminate = False |
|
112 | 140 | self._time_loop = time_loop |
|
113 | self._has_doc = False | |
|
114 | 141 | self._do_execute = False |
|
115 | 142 | self._line_to_execute = '' |
|
116 | 143 | |
|
117 | 144 | #vars that will be checked by GUI loop to handle thread states... |
|
118 | 145 | #will be replaced later by PostEvent GUI funtions... |
|
119 | 146 | self._doc_text = None |
|
147 | self._help_text = None | |
|
120 | 148 | self._ask_exit = False |
|
121 | 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 | 154 | #----------------------- Thread management section ---------------------- |
|
124 | 155 | def run (self): |
@@ -132,6 +163,7 b' class IterableIPShell(Thread):' | |||
|
132 | 163 | try: |
|
133 | 164 | if self._do_execute: |
|
134 | 165 | self._doc_text = None |
|
166 | self._help_text = None | |
|
135 | 167 | self._execute() |
|
136 | 168 | self._do_execute = False |
|
137 | 169 | self._afterExecute() #used for uper class to generate event after execution |
@@ -183,6 +215,15 b' class IterableIPShell(Thread):' | |||
|
183 | 215 | """ |
|
184 | 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 | 227 | def getBanner(self): |
|
187 | 228 | """ |
|
188 | 229 | Returns the IPython banner for useful info on IPython instance |
@@ -343,6 +384,18 b' class IterableIPShell(Thread):' | |||
|
343 | 384 | rv = self._IP.input_hist_raw[self._history_level].strip('\n') |
|
344 | 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 | 399 | def _pager(self,IP,text): |
|
347 | 400 | ''' |
|
348 | 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 | 544 | #---------------------------- IPython Thread Management --------------------------------------- |
|
545 | 545 | def stateDoExecuteLine(self): |
|
546 |
#print >>s |
|
|
546 | #print >>sys.__stdout__,"command:",self.getCurrentLine() | |
|
547 | 547 | self.doExecuteLine(self.text_ctrl.getCurrentLine()) |
|
548 | 548 | |
|
549 | 549 | def doExecuteLine(self,line): |
@@ -555,11 +555,16 b' class WxIPythonViewPanel(wx.Panel):' | |||
|
555 | 555 | |
|
556 | 556 | def evtStateExecuteDone(self,evt): |
|
557 | 557 | self.doc = self.IP.getDocText() |
|
558 | self.help = self.IP.getHelpText() | |
|
558 | 559 | if self.doc: |
|
559 | 560 | self.pager_state = 'INIT' |
|
560 | 561 | self.cur_state = 'SHOW_DOC' |
|
561 | 562 | self.pager(self.doc) |
|
562 | 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 | 569 | else: |
|
565 | 570 | self.stateShowPrompt() |
@@ -577,7 +582,7 b' class WxIPythonViewPanel(wx.Panel):' | |||
|
577 | 582 | self.cur_state = 'IDLE' |
|
578 | 583 | |
|
579 | 584 | ## def runStateMachine(self,event): |
|
580 |
## #print >>s |
|
|
585 | ## #print >>sys.__stdout__,"state:",self.cur_state | |
|
581 | 586 | ## self.updateStatusTracker(self.cur_state) |
|
582 | 587 | ## |
|
583 | 588 | ## #if self.cur_state == 'DO_EXECUTE_LINE': |
@@ -626,26 +631,35 b' class WxIPythonViewPanel(wx.Panel):' | |||
|
626 | 631 | #---------------------------- IPython pager --------------------------------------- |
|
627 | 632 | def pager(self,text):#,start=0,screen_lines=0,pager_cmd = None): |
|
628 | 633 | if self.pager_state == 'WAITING': |
|
629 |
#print >>s |
|
|
634 | #print >>sys.__stdout__,"PAGER waiting" | |
|
630 | 635 | return |
|
631 | 636 | |
|
632 | 637 | if self.pager_state == 'INIT': |
|
633 |
#print >>s |
|
|
638 | #print >>sys.__stdout__,"PAGER state:",self.pager_state | |
|
634 | 639 | self.pager_lines = text[7:].split('\n') |
|
635 | 640 | self.pager_nb_lines = len(self.pager_lines) |
|
636 | 641 | self.pager_index = 0 |
|
637 | 642 | self.pager_do_remove = False |
|
638 | 643 | self.text_ctrl.write('\n') |
|
639 | 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 | 655 | if self.pager_state == 'PROCESS_LINES': |
|
642 |
#print >>s |
|
|
656 | #print >>sys.__stdout__,"PAGER state:",self.pager_state | |
|
643 | 657 | if self.pager_do_remove == True: |
|
644 | 658 | self.text_ctrl.removeCurrentLine() |
|
645 | 659 | self.pager_do_remove = False |
|
646 | 660 | |
|
647 | 661 | if self.pager_nb_lines > 10: |
|
648 |
#print >>s |
|
|
662 | #print >>sys.__stdout__,"PAGER processing 10 lines" | |
|
649 | 663 | if self.pager_index > 0: |
|
650 | 664 | self.text_ctrl.write(">\x01\x1b[1;36m\x02"+self.pager_lines[self.pager_index]+'\n') |
|
651 | 665 | else: |
@@ -660,7 +674,7 b' class WxIPythonViewPanel(wx.Panel):' | |||
|
660 | 674 | self.pager_state = 'WAITING' |
|
661 | 675 | return |
|
662 | 676 | else: |
|
663 |
#print >>s |
|
|
677 | #print >>sys.__stdout__,"PAGER processing last lines" | |
|
664 | 678 | if self.pager_nb_lines > 0: |
|
665 | 679 | if self.pager_index > 0: |
|
666 | 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