Show More
@@ -12,6 +12,7 b' import IPython.ipapi' | |||
|
12 | 12 | import glob,os,shlex,sys |
|
13 | 13 | import inspect |
|
14 | 14 | from time import time |
|
15 | from zipimport import zipimporter | |
|
15 | 16 | ip = IPython.ipapi.get() |
|
16 | 17 | |
|
17 | 18 | try: |
@@ -86,6 +87,11 b' def moduleList(path):' | |||
|
86 | 87 | |
|
87 | 88 | if os.path.isdir(path): |
|
88 | 89 | folder_list = os.listdir(path) |
|
90 | elif path.endswith('.egg'): | |
|
91 | try: | |
|
92 | folder_list = [f for f in zipimporter(path)._files] | |
|
93 | except: | |
|
94 | folder_list = [] | |
|
89 | 95 | else: |
|
90 | 96 | folder_list = [] |
|
91 | 97 | #folder_list = glob.glob(os.path.join(path,'*')) |
@@ -1052,25 +1052,40 b' class SList(list):' | |||
|
1052 | 1052 | |
|
1053 | 1053 | p = paths = property(get_paths) |
|
1054 | 1054 | |
|
1055 | def grep(self, pattern, prune = False): | |
|
1055 | def grep(self, pattern, prune = False, field = None): | |
|
1056 | 1056 | """ Return all strings matching 'pattern' (a regex or callable) |
|
1057 | 1057 | |
|
1058 | 1058 | This is case-insensitive. If prune is true, return all items |
|
1059 | 1059 | NOT matching the pattern. |
|
1060 | 1060 | |
|
1061 | If field is specified, the match must occur in the specified | |
|
1062 | whitespace-separated field. | |
|
1063 | ||
|
1061 | 1064 | Examples:: |
|
1062 | 1065 | |
|
1063 | 1066 | a.grep( lambda x: x.startswith('C') ) |
|
1064 | 1067 | a.grep('Cha.*log', prune=1) |
|
1068 | a.grep('chm', field=-1) | |
|
1065 | 1069 | """ |
|
1070 | ||
|
1071 | def match_target(s): | |
|
1072 | if field is None: | |
|
1073 | return s | |
|
1074 | parts = s.split() | |
|
1075 | try: | |
|
1076 | tgt = parts[field] | |
|
1077 | return tgt | |
|
1078 | except IndexError: | |
|
1079 | return "" | |
|
1080 | ||
|
1066 | 1081 | if isinstance(pattern, basestring): |
|
1067 | 1082 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) |
|
1068 | 1083 | else: |
|
1069 | 1084 | pred = pattern |
|
1070 | 1085 | if not prune: |
|
1071 | return SList([el for el in self if pred(el)]) | |
|
1086 | return SList([el for el in self if pred(match_target(el))]) | |
|
1072 | 1087 | else: |
|
1073 | return SList([el for el in self if not pred(el)]) | |
|
1088 | return SList([el for el in self if not pred(match_target(el))]) | |
|
1074 | 1089 | def fields(self, *fields): |
|
1075 | 1090 | """ Collect whitespace-separated fields from string list |
|
1076 | 1091 | |
@@ -1083,6 +1098,7 b' class SList(list):' | |||
|
1083 | 1098 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] |
|
1084 | 1099 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] |
|
1085 | 1100 | (note the joining by space). |
|
1101 | a.fields(-1) is ['ChangeLog', 'IPython'] | |
|
1086 | 1102 | |
|
1087 | 1103 | IndexErrors are ignored. |
|
1088 | 1104 |
@@ -92,7 +92,7 b' class NonBlockingIPShell(object):' | |||
|
92 | 92 | |
|
93 | 93 | def __init__(self,argv=[],user_ns={},user_global_ns=None, |
|
94 | 94 | cin=None, cout=None, cerr=None, |
|
95 |
ask_exit_handler=None |
|
|
95 | ask_exit_handler=None): | |
|
96 | 96 | ''' |
|
97 | 97 | @param argv: Command line options for IPython |
|
98 | 98 | @type argv: list |
@@ -114,7 +114,7 b' class NonBlockingIPShell(object):' | |||
|
114 | 114 | #ipython0 initialisation |
|
115 | 115 | self.initIpython0(argv, user_ns, user_global_ns, |
|
116 | 116 | cin, cout, cerr, |
|
117 |
ask_exit_handler |
|
|
117 | ask_exit_handler) | |
|
118 | 118 | |
|
119 | 119 | #vars used by _execute |
|
120 | 120 | self._iter_more = 0 |
@@ -133,7 +133,7 b' class NonBlockingIPShell(object):' | |||
|
133 | 133 | |
|
134 | 134 | def initIpython0(self, argv=[], user_ns={}, user_global_ns=None, |
|
135 | 135 | cin=None, cout=None, cerr=None, |
|
136 |
ask_exit_handler=None |
|
|
136 | ask_exit_handler=None): | |
|
137 | 137 | #first we redefine in/out/error functions of IPython |
|
138 | 138 | if cin: |
|
139 | 139 | IPython.Shell.Term.cin = cin |
@@ -167,13 +167,19 b' class NonBlockingIPShell(object):' | |||
|
167 | 167 | self._IP.set_hook('shell_hook',self._shell) |
|
168 | 168 | |
|
169 | 169 | #we replace the ipython default input command caller by our method |
|
170 | IPython.iplib.raw_input_original = rawinput | |
|
171 | ||
|
170 | IPython.iplib.raw_input_original = self._raw_input | |
|
172 | 171 | #we replace the ipython default exit command by our method |
|
173 | 172 | self._IP.exit = ask_exit_handler |
|
174 | 173 | #we replace the help command |
|
175 | 174 | self._IP.user_ns['help'] = _Helper(self._pager_help) |
|
176 | 175 | |
|
176 | #we disable cpase magic... until we found a way to use it properly. | |
|
177 | #import IPython.ipapi | |
|
178 | ip = IPython.ipapi.get() | |
|
179 | def bypassMagic(self, arg): | |
|
180 | print '%this magic is currently disabled.' | |
|
181 | ip.expose_magic('cpaste', bypassMagic) | |
|
182 | ||
|
177 | 183 | sys.excepthook = excepthook |
|
178 | 184 | |
|
179 | 185 | #----------------------- Thread management section ---------------------- |
@@ -340,6 +346,12 b' class NonBlockingIPShell(object):' | |||
|
340 | 346 | ''' |
|
341 | 347 | pass |
|
342 | 348 | |
|
349 | #def _askExit(self): | |
|
350 | # ''' | |
|
351 | # Can be redefined to generate post event to exit the Ipython shell | |
|
352 | # ''' | |
|
353 | # pass | |
|
354 | ||
|
343 | 355 | def _getHistoryMaxIndex(self): |
|
344 | 356 | ''' |
|
345 | 357 | returns the max length of the history buffer |
@@ -380,6 +392,18 b' class NonBlockingIPShell(object):' | |||
|
380 | 392 | ''' |
|
381 | 393 | self._doc_text = text |
|
382 | 394 | |
|
395 | def _raw_input(self, prompt=''): | |
|
396 | ''' | |
|
397 | Custom raw_input() replacement. Get's current line from console buffer. | |
|
398 | ||
|
399 | @param prompt: Prompt to print. Here for compatability as replacement. | |
|
400 | @type prompt: string | |
|
401 | ||
|
402 | @return: The current command line text. | |
|
403 | @rtype: string | |
|
404 | ''' | |
|
405 | return self._line_to_execute | |
|
406 | ||
|
383 | 407 | def _execute(self): |
|
384 | 408 | ''' |
|
385 | 409 | Executes the current line provided by the shell object. |
@@ -47,12 +47,11 b' class WxNonBlockingIPShell(NonBlockingIPShell):' | |||
|
47 | 47 | def __init__(self, parent, |
|
48 | 48 | argv=[],user_ns={},user_global_ns=None, |
|
49 | 49 | cin=None, cout=None, cerr=None, |
|
50 |
ask_exit_handler=None |
|
|
50 | ask_exit_handler=None): | |
|
51 | 51 | |
|
52 | 52 | NonBlockingIPShell.__init__(self,argv,user_ns,user_global_ns, |
|
53 | 53 | cin, cout, cerr, |
|
54 |
ask_exit_handler |
|
|
55 | rawinput) | |
|
54 | ask_exit_handler) | |
|
56 | 55 | |
|
57 | 56 | self.parent = parent |
|
58 | 57 | |
@@ -216,6 +215,31 b' class WxConsoleView(stc.StyledTextCtrl):' | |||
|
216 | 215 | |
|
217 | 216 | self.Bind(wx.EVT_KEY_DOWN, self._onKeypress, self) |
|
218 | 217 | |
|
218 | def asyncWrite(self, text): | |
|
219 | ''' | |
|
220 | Write given text to buffer in an asynchroneous way. | |
|
221 | It is used from another thread to be able to acces the GUI. | |
|
222 | @param text: Text to append | |
|
223 | @type text: string | |
|
224 | ''' | |
|
225 | try: | |
|
226 | #print >>sys.__stdout__,'entering' | |
|
227 | wx.MutexGuiEnter() | |
|
228 | #print >>sys.__stdout__,'locking the GUI' | |
|
229 | ||
|
230 | #be sure not to be interrutpted before the MutexGuiLeave! | |
|
231 | self.write(text) | |
|
232 | #print >>sys.__stdout__,'done' | |
|
233 | ||
|
234 | except KeyboardInterrupt: | |
|
235 | #print >>sys.__stdout__,'got keyboard interrupt' | |
|
236 | wx.MutexGuiLeave() | |
|
237 | #print >>sys.__stdout__,'interrupt unlock the GUI' | |
|
238 | raise KeyboardInterrupt | |
|
239 | wx.MutexGuiLeave() | |
|
240 | #print >>sys.__stdout__,'normal unlock the GUI' | |
|
241 | ||
|
242 | ||
|
219 | 243 | def write(self, text): |
|
220 | 244 | ''' |
|
221 | 245 | Write given text to buffer. |
@@ -453,7 +477,7 b' class IPShellWidget(wx.Panel):' | |||
|
453 | 477 | |
|
454 | 478 | def __init__(self, parent, intro=None, |
|
455 | 479 | background_color="BLACK", add_button_handler=None, |
|
456 | wx_ip_shell=None, | |
|
480 | wx_ip_shell=None, user_ns={},user_global_ns=None, | |
|
457 | 481 | ): |
|
458 | 482 | ''' |
|
459 | 483 | Initialize. |
@@ -472,8 +496,7 b' class IPShellWidget(wx.Panel):' | |||
|
472 | 496 | else: |
|
473 | 497 | self.IP = WxNonBlockingIPShell(self, |
|
474 | 498 | cout = self.cout, cerr = self.cout, |
|
475 |
ask_exit_handler = self.askExitCallback |
|
|
476 | rawinput = self.rawInput) | |
|
499 | ask_exit_handler = self.askExitCallback) | |
|
477 | 500 | |
|
478 | 501 | ### IPython wx console view instanciation ### |
|
479 | 502 | #If user didn't defined an intro text, we create one for him |
@@ -492,7 +515,7 b' class IPShellWidget(wx.Panel):' | |||
|
492 | 515 | intro=welcome_text, |
|
493 | 516 | background_color=background_color) |
|
494 | 517 | |
|
495 |
self.cout.write = self.text_ctrl. |
|
|
518 | self.cout.write = self.text_ctrl.asyncWrite | |
|
496 | 519 | |
|
497 | 520 | self.text_ctrl.Bind(wx.EVT_KEY_DOWN, self.keyPress, self.text_ctrl) |
|
498 | 521 | |
@@ -509,18 +532,19 b' class IPShellWidget(wx.Panel):' | |||
|
509 | 532 | #widget state management (for key handling different cases) |
|
510 | 533 | self.setCurrentState('IDLE') |
|
511 | 534 | self.pager_state = 'DONE' |
|
535 | self.raw_input_current_line = 0 | |
|
512 | 536 | |
|
513 | 537 | def askExitCallback(self, event): |
|
514 | 538 | self.askExitHandler(event) |
|
515 | 539 | |
|
516 | 540 | #---------------------- IPython Thread Management ------------------------ |
|
517 | 541 | def stateDoExecuteLine(self): |
|
518 | #print >>sys.__stdout__,"command:",self.getCurrentLine() | |
|
519 | 542 | lines=self.text_ctrl.getCurrentLine() |
|
520 | 543 | self.text_ctrl.write('\n') |
|
521 |
|
|
|
522 | self.IP.doExecute((line.replace('\t',' '*4)).encode('cp1252')) | |
|
523 | self.updateHistoryTracker(self.text_ctrl.getCurrentLine()) | |
|
544 | lines_to_execute = lines.replace('\t',' '*4) | |
|
545 | lines_to_execute = lines_to_execute.replace('\r\n','\n') | |
|
546 | self.IP.doExecute(lines.encode('cp1252')) | |
|
547 | self.updateHistoryTracker(lines) | |
|
524 | 548 | self.setCurrentState('WAIT_END_OF_EXECUTION') |
|
525 | 549 | |
|
526 | 550 | def evtStateExecuteDone(self,evt): |
@@ -551,16 +575,7 b' class IPShellWidget(wx.Panel):' | |||
|
551 | 575 | def setCurrentState(self, state): |
|
552 | 576 | self.cur_state = state |
|
553 | 577 | self.updateStatusTracker(self.cur_state) |
|
554 | #---------------------------- Ipython raw_input ----------------------------------- | |
|
555 | def rawInput(self, prompt=''): | |
|
556 | self.setCurrentState('WAITING_USER_INPUT') | |
|
557 | while self.cur_state != 'WAIT_END_OF_EXECUTION': | |
|
558 | pass | |
|
559 | line = self.text_ctrl.getCurrentLine() | |
|
560 | line = line.split('\n') | |
|
561 | return line[-2] | |
|
562 | 578 | |
|
563 | #---------------------------- IPython pager --------------------------------------- | |
|
564 | 579 | def pager(self,text): |
|
565 | 580 | |
|
566 | 581 | if self.pager_state == 'INIT': |
@@ -1,3 +1,10 b'' | |||
|
1 | 2008-04-15 Ville Vainio <vivainio@gmail.com> | |
|
2 | ||
|
3 | * genutils.py: SList.grep supports 'field' argument | |
|
4 | ||
|
5 | * ipy_completers.py: module completer looks inside | |
|
6 | .egg zip files (patch by mc). Close #196. | |
|
7 | ||
|
1 | 8 | 2008-04-09 Ville Vainio <vivainio@gmail.com> |
|
2 | 9 | |
|
3 | 10 | * deep_reload.py: do not crash on from __future__ import |
General Comments 0
You need to be logged in to leave comments.
Login now