Show More
@@ -0,0 +1,22 | |||
|
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 class MagicsManager(Configurable): | |||
|
426 | 426 | setattr(self.user_magics, magic_name, func) |
|
427 | 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 | 430 | """Register an alias to a magic function. |
|
431 | 431 | |
|
432 | 432 | The alias is an instance of :class:`MagicAlias`, which holds the |
@@ -452,7 +452,7 class MagicsManager(Configurable): | |||
|
452 | 452 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
453 | 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 | 456 | setattr(self.user_magics, alias_name, alias) |
|
457 | 457 | record_magic(self.magics, magic_kind, alias_name, alias) |
|
458 | 458 | |
@@ -653,9 +653,10 class MagicAlias(object): | |||
|
653 | 653 | Use the :meth:`MagicsManager.register_alias` method or the |
|
654 | 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 | 657 | self.shell = shell |
|
658 | 658 | self.magic_name = magic_name |
|
659 | self.magic_params = magic_params | |
|
659 | 660 | self.magic_kind = magic_kind |
|
660 | 661 | |
|
661 | 662 | self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name) |
@@ -675,6 +676,10 class MagicAlias(object): | |||
|
675 | 676 | "magic aliases cannot call themselves.") |
|
676 | 677 | self._in_call = True |
|
677 | 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 | 683 | return fn(*args, **kwargs) |
|
679 | 684 | finally: |
|
680 | 685 | self._in_call = False |
@@ -91,6 +91,10 class BasicMagics(Magics): | |||
|
91 | 91 | 'target', |
|
92 | 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 | 98 | @line_magic |
|
95 | 99 | def alias_magic(self, line=''): |
|
96 | 100 | """Create an alias for an existing line or cell magic. |
@@ -118,7 +122,11 class BasicMagics(Magics): | |||
|
118 | 122 | |
|
119 | 123 | In [6]: %whereami |
|
120 | 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 | 130 | args = magic_arguments.parse_argstring(self.alias_magic, line) |
|
123 | 131 | shell = self.shell |
|
124 | 132 | mman = self.shell.magics_manager |
@@ -127,6 +135,12 class BasicMagics(Magics): | |||
|
127 | 135 | target = args.target.lstrip(escs) |
|
128 | 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 | 144 | # Find the requested magics. |
|
131 | 145 | m_line = shell.find_magic(target, 'line') |
|
132 | 146 | m_cell = shell.find_magic(target, 'cell') |
@@ -147,17 +161,19 class BasicMagics(Magics): | |||
|
147 | 161 | args.line = bool(m_line) |
|
148 | 162 | args.cell = bool(m_cell) |
|
149 | 163 | |
|
164 | params_str = "" if params is None else " " + params | |
|
165 | ||
|
150 | 166 | if args.line: |
|
151 | mman.register_alias(name, target, 'line') | |
|
152 | print('Created `%s%s` as an alias for `%s%s`.' % ( | |
|
167 | mman.register_alias(name, target, 'line', params) | |
|
168 | print('Created `%s%s` as an alias for `%s%s%s`.' % ( | |
|
153 | 169 | magic_escapes['line'], name, |
|
154 | magic_escapes['line'], target)) | |
|
170 | magic_escapes['line'], target, params_str)) | |
|
155 | 171 | |
|
156 | 172 | if args.cell: |
|
157 | mman.register_alias(name, target, 'cell') | |
|
158 | print('Created `%s%s` as an alias for `%s%s`.' % ( | |
|
173 | mman.register_alias(name, target, 'cell', params) | |
|
174 | print('Created `%s%s` as an alias for `%s%s%s`.' % ( | |
|
159 | 175 | magic_escapes['cell'], name, |
|
160 | magic_escapes['cell'], target)) | |
|
176 | magic_escapes['cell'], target, params_str)) | |
|
161 | 177 | |
|
162 | 178 | @line_magic |
|
163 | 179 | def lsmagic(self, parameter_s=''): |
@@ -6,6 +6,7 Needs to be run by nose (to make ipython session available). | |||
|
6 | 6 | |
|
7 | 7 | import io |
|
8 | 8 | import os |
|
9 | import re | |
|
9 | 10 | import sys |
|
10 | 11 | import warnings |
|
11 | 12 | from unittest import TestCase |
@@ -14,6 +15,8 from io import StringIO | |||
|
14 | 15 | |
|
15 | 16 | import nose.tools as nt |
|
16 | 17 | |
|
18 | import shlex | |
|
19 | ||
|
17 | 20 | from IPython import get_ipython |
|
18 | 21 | from IPython.core import magic |
|
19 | 22 | from IPython.core.error import UsageError |
@@ -867,6 +870,11 def test_alias_magic(): | |||
|
867 | 870 | nt.assert_equal(ip.run_line_magic('env', ''), |
|
868 | 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 | 878 | def test_save(): |
|
871 | 879 | """Test %save.""" |
|
872 | 880 | ip = get_ipython() |
General Comments 0
You need to be logged in to leave comments.
Login now