##// END OF EJS Templates
AliasCaller knows its own name
Thomas Kluyver -
Show More
@@ -27,6 +27,7 b' import re'
27 import sys
27 import sys
28
28
29 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
30 from IPython.core.error import UsageError
30 from IPython.core.splitinput import split_user_input
31 from IPython.core.splitinput import split_user_input
31
32
32 from IPython.utils.traitlets import List, Instance
33 from IPython.utils.traitlets import List, Instance
@@ -105,13 +106,17 b' class InvalidAliasError(AliasError):'
105 pass
106 pass
106
107
107 class AliasCaller(object):
108 class AliasCaller(object):
108 def __init__(self, shell, cmd):
109 def __init__(self, shell, name, cmd):
109 self.shell = shell
110 self.shell = shell
111 self.name = name
110 self.cmd = cmd
112 self.cmd = cmd
111 self.nargs = cmd.count('%s')
113 self.nargs = cmd.count('%s')
112 if (self.nargs > 0) and (cmd.find('%l') >= 0):
114 if (self.nargs > 0) and (cmd.find('%l') >= 0):
113 raise InvalidAliasError('The %s and %l specifiers are mutually '
115 raise InvalidAliasError('The %s and %l specifiers are mutually '
114 'exclusive in alias definitions.')
116 'exclusive in alias definitions.')
117
118 def __repr__(self):
119 return "<alias {} for {!r}>".format(self.name, self.cmd)
115
120
116 def __call__(self, rest=''):
121 def __call__(self, rest=''):
117 cmd = self.cmd
122 cmd = self.cmd
@@ -127,8 +132,8 b' class AliasCaller(object):'
127 # Handle aliases with positional arguments
132 # Handle aliases with positional arguments
128 args = rest.split(None, nargs)
133 args = rest.split(None, nargs)
129 if len(args) < nargs:
134 if len(args) < nargs:
130 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
135 raise UsageError('Alias <%s> requires %s arguments, %s given.' %
131 (alias, nargs, len(args)))
136 (self.name, nargs, len(args)))
132 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
137 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
133
138
134 self.shell.system(cmd)
139 self.shell.system(cmd)
@@ -189,7 +194,7 b' class AliasManager(Configurable):'
189 problems.
194 problems.
190 """
195 """
191 self.validate_alias(name, cmd)
196 self.validate_alias(name, cmd)
192 caller = AliasCaller(shell=self.shell, cmd=cmd)
197 caller = AliasCaller(shell=self.shell, name=name, cmd=cmd)
193 self.shell.magics_manager.register_function(caller, magic_kind='line',
198 self.shell.magics_manager.register_function(caller, magic_kind='line',
194 magic_name=name)
199 magic_name=name)
195
200
General Comments 0
You need to be logged in to leave comments. Login now