Show More
@@ -658,6 +658,34 b' def transform_ipy_prompt(line):' | |||
|
658 | 658 | return line |
|
659 | 659 | |
|
660 | 660 | |
|
661 | def _make_help_call(target, esc, lspace): | |
|
662 | """Prepares a pinfo(2)/psearch call from a target name and the escape | |
|
663 | (i.e. ? or ??)""" | |
|
664 | method = 'pinfo2' if esc == '??' \ | |
|
665 | else 'psearch' if '*' in target \ | |
|
666 | else 'pinfo' | |
|
667 | ||
|
668 | tpl = '%sget_ipython().magic(u"%s %s")' | |
|
669 | return tpl % (lspace, method, target) | |
|
670 | ||
|
671 | _initial_space_re = re.compile(r'\s*') | |
|
672 | _help_end_re = re.compile(r"""(%? | |
|
673 | [a-zA-Z_*][a-zA-Z0-9_*]* # Variable name | |
|
674 | (.[a-zA-Z_*][a-zA-Z0-9_*]*)* # .etc.etc | |
|
675 | ) | |
|
676 | (\?\??)$ # ? or ??""", | |
|
677 | re.VERBOSE) | |
|
678 | def transform_help_end(line): | |
|
679 | """Translate lines with ?/?? at the end""" | |
|
680 | m = _help_end_re.search(line) | |
|
681 | if m is None: | |
|
682 | return line | |
|
683 | target = m.group(1) | |
|
684 | esc = m.group(3) | |
|
685 | lspace = _initial_space_re.match(line).group(0) | |
|
686 | return _make_help_call(target, esc, lspace) | |
|
687 | ||
|
688 | ||
|
661 | 689 | class EscapedTransformer(object): |
|
662 | 690 | """Class to transform lines that are explicitly escaped out.""" |
|
663 | 691 | |
@@ -694,28 +722,8 b' class EscapedTransformer(object):' | |||
|
694 | 722 | # A naked help line should just fire the intro help screen |
|
695 | 723 | if not line_info.line[1:]: |
|
696 | 724 | return 'get_ipython().show_usage()' |
|
697 | ||
|
698 | # There may be one or two '?' at the end, move them to the front so that | |
|
699 | # the rest of the logic can assume escapes are at the start | |
|
700 | l_ori = line_info | |
|
701 | line = line_info.line | |
|
702 | if line.endswith('?'): | |
|
703 | line = line[-1] + line[:-1] | |
|
704 | if line.endswith('?'): | |
|
705 | line = line[-1] + line[:-1] | |
|
706 | line_info = LineInfo(line) | |
|
707 | ||
|
708 | # From here on, simply choose which level of detail to get, and | |
|
709 | # special-case the psearch syntax | |
|
710 | pinfo = 'pinfo' # default | |
|
711 | if '*' in line_info.line: | |
|
712 | pinfo = 'psearch' | |
|
713 | elif line_info.esc == '??': | |
|
714 | pinfo = 'pinfo2' | |
|
715 | ||
|
716 | tpl = '%sget_ipython().magic(u"%s %s")' | |
|
717 | return tpl % (line_info.lspace, pinfo, | |
|
718 | ' '.join([line_info.fpart, line_info.rest]).strip()) | |
|
725 | ||
|
726 | return _make_help_call(line_info.fpart, line_info.esc, line_info.lspace) | |
|
719 | 727 | |
|
720 | 728 | @staticmethod |
|
721 | 729 | def _tr_magic(line_info): |
@@ -756,14 +764,9 b' class EscapedTransformer(object):' | |||
|
756 | 764 | # Get line endpoints, where the escapes can be |
|
757 | 765 | line_info = LineInfo(line) |
|
758 | 766 | |
|
759 | # If the escape is not at the start, only '?' needs to be special-cased. | |
|
760 | # All other escapes are only valid at the start | |
|
761 | 767 | if not line_info.esc in self.tr: |
|
762 | if line.endswith(ESC_HELP): | |
|
763 |
|
|
|
764 | else: | |
|
765 | # If we don't recognize the escape, don't modify the line | |
|
766 | return line | |
|
768 | # If we don't recognize the escape, don't modify the line | |
|
769 | return line | |
|
767 | 770 | |
|
768 | 771 | return self.tr[line_info.esc](line_info) |
|
769 | 772 | |
@@ -815,9 +818,9 b' class IPythonInputSplitter(InputSplitter):' | |||
|
815 | 818 | |
|
816 | 819 | lines_list = lines.splitlines() |
|
817 | 820 | |
|
818 |
transforms = [transform_escaped, transform_ |
|
|
819 |
transform_assign_magic, |
|
|
820 | transform_classic_prompt] | |
|
821 | transforms = [transform_escaped, transform_help_end, | |
|
822 | transform_assign_system, transform_assign_magic, | |
|
823 | transform_ipy_prompt, transform_classic_prompt] | |
|
821 | 824 | |
|
822 | 825 | # Transform logic |
|
823 | 826 | # |
@@ -434,12 +434,21 b' syntax = \\' | |||
|
434 | 434 | [ ('?', 'get_ipython().show_usage()'), |
|
435 | 435 | ('?x1', 'get_ipython().magic(u"pinfo x1")'), |
|
436 | 436 | ('??x2', 'get_ipython().magic(u"pinfo2 x2")'), |
|
437 |
(' |
|
|
438 |
(' |
|
|
439 |
(' |
|
|
440 | ('f*?', 'get_ipython().magic(u"psearch f*")'), | |
|
441 | ('ax.*aspe*?', 'get_ipython().magic(u"psearch ax.*aspe*")'), | |
|
437 | ('?a.*s', 'get_ipython().magic(u"psearch a.*s")'), | |
|
438 | ('?%hist', 'get_ipython().magic(u"pinfo %hist")'), | |
|
439 | ('?abc = qwe', 'get_ipython().magic(u"pinfo abc")'), | |
|
442 | 440 | ], |
|
441 | ||
|
442 | end_help = | |
|
443 | [ ('x3?', 'get_ipython().magic(u"pinfo x3")'), | |
|
444 | ('x4??', 'get_ipython().magic(u"pinfo2 x4")'), | |
|
445 | ('%hist?', 'get_ipython().magic(u"pinfo %hist")'), | |
|
446 | ('f*?', 'get_ipython().magic(u"psearch f*")'), | |
|
447 | ('ax.*aspe*?', 'get_ipython().magic(u"psearch ax.*aspe*")'), | |
|
448 | ('a = abc?', 'get_ipython().magic(u"pinfo abc")'), | |
|
449 | ('a = abc.qe??', 'get_ipython().magic(u"pinfo2 abc.qe")'), | |
|
450 | ('a = *.items?', 'get_ipython().magic(u"psearch *.items")'), | |
|
451 | ], | |
|
443 | 452 | |
|
444 | 453 | # Explicit magic calls |
|
445 | 454 | escaped_magic = |
@@ -513,7 +522,9 b' def test_ipy_prompt():' | |||
|
513 | 522 | transform_checker(syntax['ipy_prompt'], isp.transform_ipy_prompt) |
|
514 | 523 | for example in syntax_ml['ipy_prompt']: |
|
515 | 524 | transform_checker(example, isp.transform_ipy_prompt) |
|
516 | ||
|
525 | ||
|
526 | def test_end_help(): | |
|
527 | transform_checker(syntax['end_help'], isp.transform_help_end) | |
|
517 | 528 | |
|
518 | 529 | def test_escaped_noesc(): |
|
519 | 530 | transform_checker(syntax['escaped_noesc'], isp.transform_escaped) |
General Comments 0
You need to be logged in to leave comments.
Login now