Show More
@@ -559,19 +559,23 b' def _make_help_call(target, esc, lspace, next_input=None):' | |||
|
559 | 559 | else 'pinfo' |
|
560 | 560 | arg = " ".join([method, target]) |
|
561 | 561 | |
|
562 | if next_input: | |
|
563 | tpl = '%sget_ipython().magic(%r, next_input=%r)' | |
|
564 | return tpl % (lspace, arg, next_input) | |
|
565 | else: | |
|
562 | if next_input is None: | |
|
566 | 563 | return '%sget_ipython().magic(%r)' % (lspace, arg) |
|
564 | else: | |
|
565 | return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \ | |
|
566 | (lspace, next_input, arg) | |
|
567 | ||
|
567 | 568 | |
|
568 | 569 | _initial_space_re = re.compile(r'\s*') |
|
570 | ||
|
569 | 571 | _help_end_re = re.compile(r"""(%? |
|
570 | 572 | [a-zA-Z_*][\w*]* # Variable name |
|
571 | 573 | (\.[a-zA-Z_*][\w*]*)* # .etc.etc |
|
572 | 574 | ) |
|
573 | 575 | (\?\??)$ # ? or ??""", |
|
574 | 576 | re.VERBOSE) |
|
577 | ||
|
578 | ||
|
575 | 579 | def transform_help_end(line): |
|
576 | 580 | """Translate lines with ?/?? at the end""" |
|
577 | 581 | m = _help_end_re.search(line) |
@@ -2014,7 +2014,7 b' class InteractiveShell(SingletonConfigurable):' | |||
|
2014 | 2014 | # even need a centralize colors management object. |
|
2015 | 2015 | self.magic('colors %s' % self.colors) |
|
2016 | 2016 | |
|
2017 |
def line_magic(self, magic_name, line |
|
|
2017 | def line_magic(self, magic_name, line): | |
|
2018 | 2018 | """Execute the given line magic. |
|
2019 | 2019 | |
|
2020 | 2020 | Parameters |
@@ -2024,15 +2024,7 b' class InteractiveShell(SingletonConfigurable):' | |||
|
2024 | 2024 | |
|
2025 | 2025 | line : str |
|
2026 | 2026 | The rest of the input line as a single string. |
|
2027 | ||
|
2028 | next_input : str, optional | |
|
2029 | Text to pre-load into the next input line. | |
|
2030 | 2027 | """ |
|
2031 | # Allow setting the next input - this is used if the user does `a=abs?`. | |
|
2032 | # We do this first so that magic functions can override it. | |
|
2033 | if next_input: | |
|
2034 | self.set_next_input(next_input) | |
|
2035 | ||
|
2036 | 2028 | fn = self.find_line_magic(magic_name) |
|
2037 | 2029 | if fn is None: |
|
2038 | 2030 | error("Magic function `%s` not found." % magic_name) |
@@ -2079,13 +2071,13 b' class InteractiveShell(SingletonConfigurable):' | |||
|
2079 | 2071 | Returns None if the magic isn't found.""" |
|
2080 | 2072 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2081 | 2073 | |
|
2082 |
def find_magic(self, magic_name, magic_ |
|
|
2074 | def find_magic(self, magic_name, magic_kind='line'): | |
|
2083 | 2075 | """Find and return a magic of the given type by name. |
|
2084 | 2076 | |
|
2085 | 2077 | Returns None if the magic isn't found.""" |
|
2086 |
return self.magics_manager.magics[magic_ |
|
|
2078 | return self.magics_manager.magics[magic_kind].get(magic_name) | |
|
2087 | 2079 | |
|
2088 |
def magic(self, arg_s |
|
|
2080 | def magic(self, arg_s): | |
|
2089 | 2081 | """DEPRECATED. Use line_magic() instead. |
|
2090 | 2082 | |
|
2091 | 2083 | Call a magic function by name. |
@@ -2107,7 +2099,7 b' class InteractiveShell(SingletonConfigurable):' | |||
|
2107 | 2099 | # TODO: should we issue a loud deprecation warning here? |
|
2108 | 2100 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2109 | 2101 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2110 |
return self.line_magic(magic_name, magic_arg_s |
|
|
2102 | return self.line_magic(magic_name, magic_arg_s) | |
|
2111 | 2103 | |
|
2112 | 2104 | #------------------------------------------------------------------------- |
|
2113 | 2105 | # Things related to macros |
@@ -44,7 +44,7 b' from IPython.utils.warn import error, warn' | |||
|
44 | 44 | |
|
45 | 45 | magics = dict(line={}, cell={}) |
|
46 | 46 | |
|
47 |
magic_ |
|
|
47 | magic_kinds = ('line', 'cell') | |
|
48 | 48 | magic_spec = ('line', 'cell', 'line_cell') |
|
49 | 49 | |
|
50 | 50 | #----------------------------------------------------------------------------- |
@@ -98,16 +98,16 b' def record_magic(dct, mtype, mname, func):' | |||
|
98 | 98 | dct[mtype][mname] = func |
|
99 | 99 | |
|
100 | 100 | |
|
101 |
def validate_type(magic_ |
|
|
102 |
if magic_ |
|
|
103 |
raise ValueError('magic_ |
|
|
104 |
magic_ |
|
|
101 | def validate_type(magic_kind): | |
|
102 | if magic_kind not in magic_spec: | |
|
103 | raise ValueError('magic_kind must be one of %s, %s given' % | |
|
104 | magic_kinds, magic_kind) | |
|
105 | 105 | |
|
106 | 106 | |
|
107 |
def _magic_marker(magic_ |
|
|
108 |
validate_type(magic_ |
|
|
107 | def _magic_marker(magic_kind): | |
|
108 | validate_type(magic_kind) | |
|
109 | 109 | |
|
110 |
# This is a closure to capture the magic_ |
|
|
110 | # This is a closure to capture the magic_kind. We could also use a class, | |
|
111 | 111 | # but it's overkill for just that one bit of state. |
|
112 | 112 | def magic_deco(arg): |
|
113 | 113 | call = lambda f, *a, **k: f(*a, **k) |
@@ -118,13 +118,13 b' def _magic_marker(magic_type):' | |||
|
118 | 118 | name = func.func_name |
|
119 | 119 | func.magic_name = name |
|
120 | 120 | retval = decorator(call, func) |
|
121 |
record_magic(magics, magic_ |
|
|
121 | record_magic(magics, magic_kind, name, name) | |
|
122 | 122 | elif isinstance(arg, basestring): |
|
123 | 123 | # Decorator called with arguments (@foo('bar')) |
|
124 | 124 | name = arg |
|
125 | 125 | def mark(func, *a, **kw): |
|
126 | 126 | func.magic_name = name |
|
127 |
record_magic(magics, magic_ |
|
|
127 | record_magic(magics, magic_kind, name, func.func_name) | |
|
128 | 128 | return decorator(call, func) |
|
129 | 129 | retval = mark |
|
130 | 130 | else: |
@@ -135,10 +135,10 b' def _magic_marker(magic_type):' | |||
|
135 | 135 | return magic_deco |
|
136 | 136 | |
|
137 | 137 | |
|
138 |
def _function_magic_marker(magic_ |
|
|
139 |
validate_type(magic_ |
|
|
138 | def _function_magic_marker(magic_kind): | |
|
139 | validate_type(magic_kind) | |
|
140 | 140 | |
|
141 |
# This is a closure to capture the magic_ |
|
|
141 | # This is a closure to capture the magic_kind. We could also use a class, | |
|
142 | 142 | # but it's overkill for just that one bit of state. |
|
143 | 143 | def magic_deco(arg): |
|
144 | 144 | call = lambda f, *a, **k: f(*a, **k) |
@@ -157,16 +157,14 b' def _function_magic_marker(magic_type):' | |||
|
157 | 157 | if callable(arg): |
|
158 | 158 | # "Naked" decorator call (just @foo, no args) |
|
159 | 159 | func = arg |
|
160 |
|
|
|
161 | #func.magic_name = name | |
|
162 | ip.register_magic_function(func) | |
|
160 | name = func.func_name | |
|
161 | ip.register_magic_function(func, magic_kind, name) | |
|
163 | 162 | retval = decorator(call, func) |
|
164 | 163 | elif isinstance(arg, basestring): |
|
165 | 164 | # Decorator called with arguments (@foo('bar')) |
|
166 | 165 | name = arg |
|
167 | 166 | def mark(func, *a, **kw): |
|
168 | #func.magic_name = name | |
|
169 | ip.register_magic_function(func) | |
|
167 | ip.register_magic_function(func, magic_kind, name) | |
|
170 | 168 | return decorator(call, func) |
|
171 | 169 | retval = mark |
|
172 | 170 | else: |
@@ -254,19 +252,19 b' class MagicsManager(Configurable):' | |||
|
254 | 252 | # Now that we have an instance, we can register it and update the |
|
255 | 253 | # table of callables |
|
256 | 254 | self.registry[m.__class__.__name__] = m |
|
257 |
for mtype in magic_ |
|
|
255 | for mtype in magic_kinds: | |
|
258 | 256 | self.magics[mtype].update(m.magics[mtype]) |
|
259 | 257 | |
|
260 |
def register_function(self, func, magic_ |
|
|
258 | def register_function(self, func, magic_kind='line', magic_name=None): | |
|
261 | 259 | """Expose a standalone function as magic function for ipython. |
|
262 | 260 | """ |
|
263 | 261 | |
|
264 | 262 | # Create the new method in the user_magics and register it in the |
|
265 | 263 | # global table |
|
266 |
validate_type(magic_ |
|
|
264 | validate_type(magic_kind) | |
|
267 | 265 | magic_name = func.func_name if magic_name is None else magic_name |
|
268 | 266 | setattr(self.user_magics, magic_name, func) |
|
269 |
record_magic(self.magics, magic_ |
|
|
267 | record_magic(self.magics, magic_kind, magic_name, func) | |
|
270 | 268 | |
|
271 | 269 | def define_magic(self, name, func): |
|
272 | 270 | """Support for deprecated API. |
@@ -320,7 +318,7 b' class Magics(object):' | |||
|
320 | 318 | # grab. Only now, that the instance exists, can we create the proper |
|
321 | 319 | # mapping to bound methods. So we read the info off the original names |
|
322 | 320 | # table and replace each method name by the actual bound method. |
|
323 |
for mtype in magic_ |
|
|
321 | for mtype in magic_kinds: | |
|
324 | 322 | tab = self.magics[mtype] |
|
325 | 323 | # must explicitly use keys, as we're mutating this puppy |
|
326 | 324 | for magic_name in tab.keys(): |
@@ -466,10 +466,14 b' syntax = \\' | |||
|
466 | 466 | (u'%hist?', "get_ipython().magic({u}'pinfo %hist')"), |
|
467 | 467 | (u'f*?', "get_ipython().magic({u}'psearch f*')"), |
|
468 | 468 | (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"), |
|
469 |
(u'a = abc?', "get_ipython(). |
|
|
470 |
|
|
|
471 |
(u'a = |
|
|
472 |
|
|
|
469 | (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');" | |
|
470 | "get_ipython().magic({u}'pinfo abc')"), | |
|
471 | (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');" | |
|
472 | "get_ipython().magic({u}'pinfo2 abc.qe')"), | |
|
473 | (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');" | |
|
474 | "get_ipython().magic({u}'psearch *.items')"), | |
|
475 | (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');" | |
|
476 | "get_ipython().magic({u}'pinfo a')"), | |
|
473 | 477 | (u'a*2 #comment?', 'a*2 #comment?'), |
|
474 | 478 | ]], |
|
475 | 479 |
@@ -13,10 +13,15 b' import io' | |||
|
13 | 13 | import os |
|
14 | 14 | import sys |
|
15 | 15 | from StringIO import StringIO |
|
16 | from unittest import TestCase | |
|
16 | 17 | |
|
17 | 18 | import nose.tools as nt |
|
18 | 19 | |
|
19 | 20 | from IPython.core import magic |
|
21 | from IPython.core.magic import (Magics, magics_class, line_magic, | |
|
22 | cell_magic, line_cell_magic, | |
|
23 | register_line_magic, register_cell_magic, | |
|
24 | register_line_cell_magic) | |
|
20 | 25 | from IPython.core.magics import execution |
|
21 | 26 | from IPython.nbformat.v3.tests.nbexamples import nb0 |
|
22 | 27 | from IPython.nbformat import current |
General Comments 0
You need to be logged in to leave comments.
Login now