Show More
@@ -42,7 +42,8 b' from IPython.utils.autoattr import auto_attr' | |||||
42 | # Global utilities, errors and constants |
|
42 | # Global utilities, errors and constants | |
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 |
|
44 | |||
45 |
|
45 | # Warning, these cannot be changed unless various regular expressions | ||
|
46 | # are updated in a number of places. Not great, but at least we told you. | |||
46 | ESC_SHELL = '!' |
|
47 | ESC_SHELL = '!' | |
47 | ESC_SH_CAP = '!!' |
|
48 | ESC_SH_CAP = '!!' | |
48 | ESC_HELP = '?' |
|
49 | ESC_HELP = '?' | |
@@ -231,6 +232,7 b' class PrefilterManager(Component):' | |||||
231 |
|
232 | |||
232 | def prefilter_line_info(self, line_info): |
|
233 | def prefilter_line_info(self, line_info): | |
233 | """Prefilter a line that has been converted to a LineInfo object.""" |
|
234 | """Prefilter a line that has been converted to a LineInfo object.""" | |
|
235 | # print "prefilter_line_info: ", line_info | |||
234 | handler = self.find_handler(line_info) |
|
236 | handler = self.find_handler(line_info) | |
235 | return handler.handle(line_info) |
|
237 | return handler.handle(line_info) | |
236 |
|
238 | |||
@@ -239,12 +241,15 b' class PrefilterManager(Component):' | |||||
239 | for checker in self.sorted_checkers: |
|
241 | for checker in self.sorted_checkers: | |
240 | handler = checker.check(line_info) |
|
242 | handler = checker.check(line_info) | |
241 | if handler: |
|
243 | if handler: | |
|
244 | # print "Used checker: ", checker | |||
|
245 | # print "Using handler: ", handler | |||
242 | return handler |
|
246 | return handler | |
243 | return self.get_handler_by_name('normal') |
|
247 | return self.get_handler_by_name('normal') | |
244 |
|
248 | |||
245 | def prefilter_line(self, line, continue_prompt): |
|
249 | def prefilter_line(self, line, continue_prompt): | |
246 | """Prefilter a single input line as text.""" |
|
250 | """Prefilter a single input line as text.""" | |
247 |
|
251 | |||
|
252 | # print "prefilter_line: ", line, continue_prompt | |||
248 | # All handlers *must* return a value, even if it's blank (''). |
|
253 | # All handlers *must* return a value, even if it's blank (''). | |
249 |
|
254 | |||
250 | # Lines are NOT logged here. Handlers should process the line as |
|
255 | # Lines are NOT logged here. Handlers should process the line as | |
@@ -254,7 +259,7 b' class PrefilterManager(Component):' | |||||
254 | # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt)) |
|
259 | # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt)) | |
255 |
|
260 | |||
256 | # save the line away in case we crash, so the post-mortem handler can |
|
261 | # save the line away in case we crash, so the post-mortem handler can | |
257 |
# record it |
|
262 | # record it | |
258 | self.shell._last_input_line = line |
|
263 | self.shell._last_input_line = line | |
259 |
|
264 | |||
260 | if not line: |
|
265 | if not line: | |
@@ -284,7 +289,9 b' class PrefilterManager(Component):' | |||||
284 | if continue_prompt and not self.multi_line_specials: |
|
289 | if continue_prompt and not self.multi_line_specials: | |
285 | return normal_handler.handle(line_info) |
|
290 | return normal_handler.handle(line_info) | |
286 |
|
291 | |||
287 |
|
|
292 | prefiltered = self.prefilter_line_info(line_info) | |
|
293 | # print "prefiltered line: %r" % prefiltered | |||
|
294 | return prefiltered | |||
288 |
|
295 | |||
289 | def prefilter_lines(self, lines, continue_prompt): |
|
296 | def prefilter_lines(self, lines, continue_prompt): | |
290 | """Prefilter multiple input lines of text. |
|
297 | """Prefilter multiple input lines of text. | |
@@ -330,6 +337,8 b' class PrefilterChecker(Component):' | |||||
330 | """Inspect line_info and return a handler or None.""" |
|
337 | """Inspect line_info and return a handler or None.""" | |
331 | return None |
|
338 | return None | |
332 |
|
339 | |||
|
340 | def __str__(self): | |||
|
341 | return "<%s(priority=%i)>" % (self.__class__.__name__, self.priority) | |||
333 |
|
342 | |||
334 | class EmacsChecker(PrefilterChecker): |
|
343 | class EmacsChecker(PrefilterChecker): | |
335 |
|
344 | |||
@@ -401,6 +410,10 b' class EscCharsChecker(PrefilterChecker):' | |||||
401 | return self.prefilter_manager.get_handler_by_esc(line_info.pre_char) |
|
410 | return self.prefilter_manager.get_handler_by_esc(line_info.pre_char) | |
402 |
|
411 | |||
403 |
|
412 | |||
|
413 | _assign_system_re = re.compile('\s*=\s*!(?P<cmd>.*)') | |||
|
414 | _assign_magic_re = re.compile('\s*=\s*%(?P<cmd>.*)') | |||
|
415 | ||||
|
416 | ||||
404 | class AssignmentChecker(PrefilterChecker): |
|
417 | class AssignmentChecker(PrefilterChecker): | |
405 |
|
418 | |||
406 | priority = Int(600, config=True) |
|
419 | priority = Int(600, config=True) | |
@@ -412,8 +425,15 b' class AssignmentChecker(PrefilterChecker):' | |||||
412 | This allows users to assign to either alias or magic names true python |
|
425 | This allows users to assign to either alias or magic names true python | |
413 | variables (the magic/alias systems always take second seat to true |
|
426 | variables (the magic/alias systems always take second seat to true | |
414 | python code). E.g. ls='hi', or ls,that=1,2""" |
|
427 | python code). E.g. ls='hi', or ls,that=1,2""" | |
415 |
if line_info.the_rest |
|
428 | if line_info.the_rest: | |
416 | return self.prefilter_manager.get_handler_by_name('normal') |
|
429 | if line_info.the_rest[0] in '=,': | |
|
430 | # m = _assign_system_re.match(line_info.the_rest) | |||
|
431 | # if m is not None: | |||
|
432 | # return self.prefilter_manager.get_handler_by_name('assign_system') | |||
|
433 | # m = _assign_magic_re.match(line_info.the_rest) | |||
|
434 | # if m is not None: | |||
|
435 | # return self.prefilter_manager.get_handler_by_name('assign_magic') | |||
|
436 | return self.prefilter_manager.get_handler_by_name('normal') | |||
417 | else: |
|
437 | else: | |
418 | return None |
|
438 | return None | |
419 |
|
439 | |||
@@ -529,6 +549,7 b' class PrefilterHandler(Component):' | |||||
529 | return PrefilterManager.get_instances(root=self.root)[0] |
|
549 | return PrefilterManager.get_instances(root=self.root)[0] | |
530 |
|
550 | |||
531 | def handle(self, line_info): |
|
551 | def handle(self, line_info): | |
|
552 | # print "normal: ", line_info | |||
532 | """Handle normal input lines. Use as a template for handlers.""" |
|
553 | """Handle normal input lines. Use as a template for handlers.""" | |
533 |
|
554 | |||
534 | # With autoindent on, we need some way to exit the input loop, and I |
|
555 | # With autoindent on, we need some way to exit the input loop, and I | |
@@ -547,11 +568,52 b' class PrefilterHandler(Component):' | |||||
547 | self.shell.log(line, line, continue_prompt) |
|
568 | self.shell.log(line, line, continue_prompt) | |
548 | return line |
|
569 | return line | |
549 |
|
570 | |||
|
571 | def __str__(self): | |||
|
572 | return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name) | |||
|
573 | ||||
|
574 | class AssignSystemHandler(PrefilterHandler): | |||
|
575 | ||||
|
576 | handler_name = Str('assign_system') | |||
|
577 | ||||
|
578 | @auto_attr | |||
|
579 | def normal_handler(self): | |||
|
580 | return self.prefilter_manager.get_handler_by_name('normal') | |||
|
581 | ||||
|
582 | def handle(self, line_info): | |||
|
583 | new_line = line_info.line | |||
|
584 | m = _assign_system_re.match(line_info.the_rest) | |||
|
585 | if m is not None: | |||
|
586 | cmd = m.group('cmd') | |||
|
587 | expr = make_quoted_expr("sc -l =%s" % cmd) | |||
|
588 | new_line = '%s%s = get_ipython().magic(%s)' % (line_info.pre_whitespace, | |||
|
589 | line_info.ifun, expr) | |||
|
590 | self.shell.log(line_info.line, new_line, line_info.continue_prompt) | |||
|
591 | return new_line | |||
|
592 | ||||
|
593 | ||||
|
594 | class AssignMagicHandler(PrefilterHandler): | |||
|
595 | ||||
|
596 | handler_name = Str('assign_magic') | |||
|
597 | ||||
|
598 | @auto_attr | |||
|
599 | def normal_handler(self): | |||
|
600 | return self.prefilter_manager.get_handler_by_name('normal') | |||
|
601 | ||||
|
602 | def handle(self, line_info): | |||
|
603 | new_line = line_info.line | |||
|
604 | m = _assign_magic_re.match(line_info.the_rest) | |||
|
605 | if m is not None: | |||
|
606 | cmd = m.group('cmd') | |||
|
607 | expr = make_quoted_expr(cmd) | |||
|
608 | new_line = '%s%s = get_ipython().magic(%s)' % (line_info.pre_whitespace, | |||
|
609 | line_info.ifun, expr) | |||
|
610 | self.shell.log(line_info.line, new_line, line_info.continue_prompt) | |||
|
611 | return new_line | |||
|
612 | ||||
550 |
|
613 | |||
551 | class AliasHandler(PrefilterHandler): |
|
614 | class AliasHandler(PrefilterHandler): | |
552 |
|
615 | |||
553 | handler_name = Str('alias') |
|
616 | handler_name = Str('alias') | |
554 | esc_strings = List([]) |
|
|||
555 |
|
617 | |||
556 | @auto_attr |
|
618 | @auto_attr | |
557 | def alias_manager(self): |
|
619 | def alias_manager(self): | |
@@ -768,5 +830,7 b' _default_handlers = [' | |||||
768 | AutoHandler, |
|
830 | AutoHandler, | |
769 | HelpHandler, |
|
831 | HelpHandler, | |
770 | EmacsHandler |
|
832 | EmacsHandler | |
|
833 | # AssignSystemHandler, | |||
|
834 | # AssignMagicHandler | |||
771 | ] |
|
835 | ] | |
772 |
|
836 |
@@ -46,19 +46,25 b" line_split = re.compile(r'^([,;/%?]|!!?|\\s*)'" | |||||
46 | r'\s*([\w\.]+)' |
|
46 | r'\s*([\w\.]+)' | |
47 | r'(\s+.*$|$)') |
|
47 | r'(\s+.*$|$)') | |
48 |
|
48 | |||
|
49 | # r'[\w\.]+' | |||
|
50 | # r'\s*=\s*%.*' | |||
49 |
|
51 | |||
50 | def split_user_input(line, pattern=None): |
|
52 | def split_user_input(line, pattern=None): | |
51 |
"""Split user input into pre-char/whitespace, function part and rest. |
|
53 | """Split user input into pre-char/whitespace, function part and rest. | |
|
54 | ||||
|
55 | This is currently handles lines with '=' in them in a very inconsistent | |||
|
56 | manner. | |||
|
57 | """ | |||
52 |
|
58 | |||
53 | if pattern is None: |
|
59 | if pattern is None: | |
54 | pattern = line_split |
|
60 | pattern = line_split | |
55 | match = pattern.match(line) |
|
61 | match = pattern.match(line) | |
56 | if not match: |
|
62 | if not match: | |
57 | #print "match failed for line '%s'" % line |
|
63 | # print "match failed for line '%s'" % line | |
58 | try: |
|
64 | try: | |
59 | ifun, the_rest = line.split(None,1) |
|
65 | ifun, the_rest = line.split(None,1) | |
60 | except ValueError: |
|
66 | except ValueError: | |
61 | #print "split failed for line '%s'" % line |
|
67 | # print "split failed for line '%s'" % line | |
62 | ifun, the_rest = line,'' |
|
68 | ifun, the_rest = line,'' | |
63 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
69 | pre = re.match('^(\s*)(.*)',line).groups()[0] | |
64 | else: |
|
70 | else: |
@@ -47,7 +47,7 b' def init_handlers():' | |||||
47 |
|
47 | |||
48 | init_handlers() |
|
48 | init_handlers() | |
49 |
|
49 | |||
50 |
def regex_prefilter_f(self,line): |
|
50 | def regex_prefilter_f(self,line): | |
51 | for pat, handler in ip.meta.re_prefilters: |
|
51 | for pat, handler in ip.meta.re_prefilters: | |
52 | mo = pat.match(line) |
|
52 | mo = pat.match(line) | |
53 | if mo: |
|
53 | if mo: |
General Comments 0
You need to be logged in to leave comments.
Login now