Show More
@@ -0,0 +1,22 b'' | |||||
|
1 | Added the ability to add parameters to alias_magic. | |||
|
2 | ||||
|
3 | e.g.: | |||
|
4 | ||||
|
5 | In [2]: %alias_magic hist history --params "-l 2" --line | |||
|
6 | Created `%hist` as an alias for `%history -l 2`. | |||
|
7 | ||||
|
8 | In [3]: hist | |||
|
9 | %alias_magic hist history --params "-l 30" --line | |||
|
10 | %alias_magic hist history --params "-l 2" --line | |||
|
11 | ||||
|
12 | Previously it was only possible to have an alias attached to a single function, and you would have to pass in the given parameters every time. | |||
|
13 | ||||
|
14 | e.g.: | |||
|
15 | ||||
|
16 | In [4]: %alias_magic hist history --line | |||
|
17 | Created `%hist` as an alias for `%history`. | |||
|
18 | ||||
|
19 | In [5]: hist -l 2 | |||
|
20 | hist | |||
|
21 | %alias_magic hist history --line | |||
|
22 |
@@ -426,7 +426,7 b' class MagicsManager(Configurable):' | |||||
426 | setattr(self.user_magics, magic_name, func) |
|
426 | setattr(self.user_magics, magic_name, func) | |
427 | record_magic(self.magics, magic_kind, magic_name, func) |
|
427 | record_magic(self.magics, magic_kind, magic_name, func) | |
428 |
|
428 | |||
429 | def register_alias(self, alias_name, magic_name, magic_kind='line'): |
|
429 | def register_alias(self, alias_name, magic_name, magic_kind='line', magic_params=None): | |
430 | """Register an alias to a magic function. |
|
430 | """Register an alias to a magic function. | |
431 |
|
431 | |||
432 | The alias is an instance of :class:`MagicAlias`, which holds the |
|
432 | The alias is an instance of :class:`MagicAlias`, which holds the | |
@@ -452,7 +452,7 b' class MagicsManager(Configurable):' | |||||
452 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
452 | raise ValueError('magic_kind must be one of %s, %s given' % | |
453 | magic_kinds, magic_kind) |
|
453 | magic_kinds, magic_kind) | |
454 |
|
454 | |||
455 | alias = MagicAlias(self.shell, magic_name, magic_kind) |
|
455 | alias = MagicAlias(self.shell, magic_name, magic_kind, magic_params) | |
456 | setattr(self.user_magics, alias_name, alias) |
|
456 | setattr(self.user_magics, alias_name, alias) | |
457 | record_magic(self.magics, magic_kind, alias_name, alias) |
|
457 | record_magic(self.magics, magic_kind, alias_name, alias) | |
458 |
|
458 | |||
@@ -653,9 +653,10 b' class MagicAlias(object):' | |||||
653 | Use the :meth:`MagicsManager.register_alias` method or the |
|
653 | Use the :meth:`MagicsManager.register_alias` method or the | |
654 | `%alias_magic` magic function to create and register a new alias. |
|
654 | `%alias_magic` magic function to create and register a new alias. | |
655 | """ |
|
655 | """ | |
656 | def __init__(self, shell, magic_name, magic_kind): |
|
656 | def __init__(self, shell, magic_name, magic_kind, magic_params=None): | |
657 | self.shell = shell |
|
657 | self.shell = shell | |
658 | self.magic_name = magic_name |
|
658 | self.magic_name = magic_name | |
|
659 | self.magic_params = magic_params | |||
659 | self.magic_kind = magic_kind |
|
660 | self.magic_kind = magic_kind | |
660 |
|
661 | |||
661 | self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name) |
|
662 | self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name) | |
@@ -675,6 +676,10 b' class MagicAlias(object):' | |||||
675 | "magic aliases cannot call themselves.") |
|
676 | "magic aliases cannot call themselves.") | |
676 | self._in_call = True |
|
677 | self._in_call = True | |
677 | try: |
|
678 | try: | |
|
679 | if self.magic_params: | |||
|
680 | args_list = list(args) | |||
|
681 | args_list[0] = self.magic_params + " " + args[0] | |||
|
682 | args = tuple(args_list) | |||
678 | return fn(*args, **kwargs) |
|
683 | return fn(*args, **kwargs) | |
679 | finally: |
|
684 | finally: | |
680 | self._in_call = False |
|
685 | self._in_call = False |
@@ -91,6 +91,10 b' class BasicMagics(Magics):' | |||||
91 | 'target', |
|
91 | 'target', | |
92 | help="""Name of the existing line or cell magic.""" |
|
92 | help="""Name of the existing line or cell magic.""" | |
93 | ) |
|
93 | ) | |
|
94 | @magic_arguments.argument( | |||
|
95 | '-p', '--params', default=None, | |||
|
96 | help="""Parameters passed to the magic function.""" | |||
|
97 | ) | |||
94 | @line_magic |
|
98 | @line_magic | |
95 | def alias_magic(self, line=''): |
|
99 | def alias_magic(self, line=''): | |
96 | """Create an alias for an existing line or cell magic. |
|
100 | """Create an alias for an existing line or cell magic. | |
@@ -118,7 +122,11 b' class BasicMagics(Magics):' | |||||
118 |
|
122 | |||
119 | In [6]: %whereami |
|
123 | In [6]: %whereami | |
120 | Out[6]: u'/home/testuser' |
|
124 | Out[6]: u'/home/testuser' | |
|
125 | ||||
|
126 | In [7]: %alias_magic h history -p "-l 30" --line | |||
|
127 | Created `%h` as an alias for `%history -l 30`. | |||
121 | """ |
|
128 | """ | |
|
129 | ||||
122 | args = magic_arguments.parse_argstring(self.alias_magic, line) |
|
130 | args = magic_arguments.parse_argstring(self.alias_magic, line) | |
123 | shell = self.shell |
|
131 | shell = self.shell | |
124 | mman = self.shell.magics_manager |
|
132 | mman = self.shell.magics_manager | |
@@ -127,6 +135,12 b' class BasicMagics(Magics):' | |||||
127 | target = args.target.lstrip(escs) |
|
135 | target = args.target.lstrip(escs) | |
128 | name = args.name.lstrip(escs) |
|
136 | name = args.name.lstrip(escs) | |
129 |
|
137 | |||
|
138 | params = args.params | |||
|
139 | if (params and | |||
|
140 | ((params.startswith('"') and params.endswith('"')) | |||
|
141 | or (params.startswith("'") and params.endswith("'")))): | |||
|
142 | params = params[1:-1] | |||
|
143 | ||||
130 | # Find the requested magics. |
|
144 | # Find the requested magics. | |
131 | m_line = shell.find_magic(target, 'line') |
|
145 | m_line = shell.find_magic(target, 'line') | |
132 | m_cell = shell.find_magic(target, 'cell') |
|
146 | m_cell = shell.find_magic(target, 'cell') | |
@@ -147,17 +161,19 b' class BasicMagics(Magics):' | |||||
147 | args.line = bool(m_line) |
|
161 | args.line = bool(m_line) | |
148 | args.cell = bool(m_cell) |
|
162 | args.cell = bool(m_cell) | |
149 |
|
163 | |||
|
164 | params_str = "" if params is None else " " + params | |||
|
165 | ||||
150 | if args.line: |
|
166 | if args.line: | |
151 | mman.register_alias(name, target, 'line') |
|
167 | mman.register_alias(name, target, 'line', params) | |
152 | print('Created `%s%s` as an alias for `%s%s`.' % ( |
|
168 | print('Created `%s%s` as an alias for `%s%s%s`.' % ( | |
153 | magic_escapes['line'], name, |
|
169 | magic_escapes['line'], name, | |
154 | magic_escapes['line'], target)) |
|
170 | magic_escapes['line'], target, params_str)) | |
155 |
|
171 | |||
156 | if args.cell: |
|
172 | if args.cell: | |
157 | mman.register_alias(name, target, 'cell') |
|
173 | mman.register_alias(name, target, 'cell', params) | |
158 | print('Created `%s%s` as an alias for `%s%s`.' % ( |
|
174 | print('Created `%s%s` as an alias for `%s%s%s`.' % ( | |
159 | magic_escapes['cell'], name, |
|
175 | magic_escapes['cell'], name, | |
160 | magic_escapes['cell'], target)) |
|
176 | magic_escapes['cell'], target, params_str)) | |
161 |
|
177 | |||
162 | @line_magic |
|
178 | @line_magic | |
163 | def lsmagic(self, parameter_s=''): |
|
179 | def lsmagic(self, parameter_s=''): |
@@ -6,6 +6,7 b' Needs to be run by nose (to make ipython session available).' | |||||
6 |
|
6 | |||
7 | import io |
|
7 | import io | |
8 | import os |
|
8 | import os | |
|
9 | import re | |||
9 | import sys |
|
10 | import sys | |
10 | import warnings |
|
11 | import warnings | |
11 | from unittest import TestCase |
|
12 | from unittest import TestCase | |
@@ -14,6 +15,8 b' from io import StringIO' | |||||
14 |
|
15 | |||
15 | import nose.tools as nt |
|
16 | import nose.tools as nt | |
16 |
|
17 | |||
|
18 | import shlex | |||
|
19 | ||||
17 | from IPython import get_ipython |
|
20 | from IPython import get_ipython | |
18 | from IPython.core import magic |
|
21 | from IPython.core import magic | |
19 | from IPython.core.error import UsageError |
|
22 | from IPython.core.error import UsageError | |
@@ -867,6 +870,11 b' def test_alias_magic():' | |||||
867 | nt.assert_equal(ip.run_line_magic('env', ''), |
|
870 | nt.assert_equal(ip.run_line_magic('env', ''), | |
868 | ip.run_line_magic('env_alias', '')) |
|
871 | ip.run_line_magic('env_alias', '')) | |
869 |
|
872 | |||
|
873 | # Test that line alias with parameters passed in is created successfully. | |||
|
874 | ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3')) | |||
|
875 | nt.assert_in('history_alias', mm.magics['line']) | |||
|
876 | ||||
|
877 | ||||
870 | def test_save(): |
|
878 | def test_save(): | |
871 | """Test %save.""" |
|
879 | """Test %save.""" | |
872 | ip = get_ipython() |
|
880 | ip = get_ipython() |
General Comments 0
You need to be logged in to leave comments.
Login now