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