Show More
@@ -42,7 +42,8 b' from IPython.utils.autoattr import auto_attr' | |||
|
42 | 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 | 47 | ESC_SHELL = '!' |
|
47 | 48 | ESC_SH_CAP = '!!' |
|
48 | 49 | ESC_HELP = '?' |
@@ -231,6 +232,7 b' class PrefilterManager(Component):' | |||
|
231 | 232 | |
|
232 | 233 | def prefilter_line_info(self, line_info): |
|
233 | 234 | """Prefilter a line that has been converted to a LineInfo object.""" |
|
235 | # print "prefilter_line_info: ", line_info | |
|
234 | 236 | handler = self.find_handler(line_info) |
|
235 | 237 | return handler.handle(line_info) |
|
236 | 238 | |
@@ -239,12 +241,15 b' class PrefilterManager(Component):' | |||
|
239 | 241 | for checker in self.sorted_checkers: |
|
240 | 242 | handler = checker.check(line_info) |
|
241 | 243 | if handler: |
|
244 | # print "Used checker: ", checker | |
|
245 | # print "Using handler: ", handler | |
|
242 | 246 | return handler |
|
243 | 247 | return self.get_handler_by_name('normal') |
|
244 | 248 | |
|
245 | 249 | def prefilter_line(self, line, continue_prompt): |
|
246 | 250 | """Prefilter a single input line as text.""" |
|
247 | 251 | |
|
252 | # print "prefilter_line: ", line, continue_prompt | |
|
248 | 253 | # All handlers *must* return a value, even if it's blank (''). |
|
249 | 254 | |
|
250 | 255 | # Lines are NOT logged here. Handlers should process the line as |
@@ -254,7 +259,7 b' class PrefilterManager(Component):' | |||
|
254 | 259 | # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt)) |
|
255 | 260 | |
|
256 | 261 | # save the line away in case we crash, so the post-mortem handler can |
|
257 |
# record it |
|
|
262 | # record it | |
|
258 | 263 | self.shell._last_input_line = line |
|
259 | 264 | |
|
260 | 265 | if not line: |
@@ -284,7 +289,9 b' class PrefilterManager(Component):' | |||
|
284 | 289 | if continue_prompt and not self.multi_line_specials: |
|
285 | 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 | 296 | def prefilter_lines(self, lines, continue_prompt): |
|
290 | 297 | """Prefilter multiple input lines of text. |
@@ -330,6 +337,8 b' class PrefilterChecker(Component):' | |||
|
330 | 337 | """Inspect line_info and return a handler or None.""" |
|
331 | 338 | return None |
|
332 | 339 | |
|
340 | def __str__(self): | |
|
341 | return "<%s(priority=%i)>" % (self.__class__.__name__, self.priority) | |
|
333 | 342 | |
|
334 | 343 | class EmacsChecker(PrefilterChecker): |
|
335 | 344 | |
@@ -401,6 +410,10 b' class EscCharsChecker(PrefilterChecker):' | |||
|
401 | 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 | 417 | class AssignmentChecker(PrefilterChecker): |
|
405 | 418 | |
|
406 | 419 | priority = Int(600, config=True) |
@@ -412,8 +425,15 b' class AssignmentChecker(PrefilterChecker):' | |||
|
412 | 425 | This allows users to assign to either alias or magic names true python |
|
413 | 426 | variables (the magic/alias systems always take second seat to true |
|
414 | 427 | python code). E.g. ls='hi', or ls,that=1,2""" |
|
415 |
if line_info.the_rest |
|
|
416 | return self.prefilter_manager.get_handler_by_name('normal') | |
|
428 | if line_info.the_rest: | |
|
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 | 437 | else: |
|
418 | 438 | return None |
|
419 | 439 | |
@@ -529,6 +549,7 b' class PrefilterHandler(Component):' | |||
|
529 | 549 | return PrefilterManager.get_instances(root=self.root)[0] |
|
530 | 550 | |
|
531 | 551 | def handle(self, line_info): |
|
552 | # print "normal: ", line_info | |
|
532 | 553 | """Handle normal input lines. Use as a template for handlers.""" |
|
533 | 554 | |
|
534 | 555 | # With autoindent on, we need some way to exit the input loop, and I |
@@ -547,11 +568,52 b' class PrefilterHandler(Component):' | |||
|
547 | 568 | self.shell.log(line, line, continue_prompt) |
|
548 | 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 | 614 | class AliasHandler(PrefilterHandler): |
|
552 | 615 | |
|
553 | 616 | handler_name = Str('alias') |
|
554 | esc_strings = List([]) | |
|
555 | 617 | |
|
556 | 618 | @auto_attr |
|
557 | 619 | def alias_manager(self): |
@@ -768,5 +830,7 b' _default_handlers = [' | |||
|
768 | 830 | AutoHandler, |
|
769 | 831 | HelpHandler, |
|
770 | 832 | EmacsHandler |
|
833 | # AssignSystemHandler, | |
|
834 | # AssignMagicHandler | |
|
771 | 835 | ] |
|
772 | 836 |
@@ -46,19 +46,25 b" line_split = re.compile(r'^([,;/%?]|!!?|\\s*)'" | |||
|
46 | 46 | r'\s*([\w\.]+)' |
|
47 | 47 | r'(\s+.*$|$)') |
|
48 | 48 | |
|
49 | # r'[\w\.]+' | |
|
50 | # r'\s*=\s*%.*' | |
|
49 | 51 | |
|
50 | 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 | 59 | if pattern is None: |
|
54 | 60 | pattern = line_split |
|
55 | 61 | match = pattern.match(line) |
|
56 | 62 | if not match: |
|
57 | #print "match failed for line '%s'" % line | |
|
63 | # print "match failed for line '%s'" % line | |
|
58 | 64 | try: |
|
59 | 65 | ifun, the_rest = line.split(None,1) |
|
60 | 66 | except ValueError: |
|
61 | #print "split failed for line '%s'" % line | |
|
67 | # print "split failed for line '%s'" % line | |
|
62 | 68 | ifun, the_rest = line,'' |
|
63 | 69 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
64 | 70 | else: |
General Comments 0
You need to be logged in to leave comments.
Login now