MCPcopy Create free account
hub / github.com/ipython/ipython / Alias

Class Alias

IPython/core/alias.py:120–186  ·  view source on GitHub ↗

Callable object storing the details of one alias. Instances are registered as magic functions to allow use of aliases.

Source from the content-addressed store, hash-verified

118 pass
119
120class Alias(object):
121 """Callable object storing the details of one alias.
122
123 Instances are registered as magic functions to allow use of aliases.
124 """
125
126 # Prepare blacklist
127 blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
128
129 def __init__(self, shell, name, cmd):
130 self.shell = shell
131 self.name = name
132 self.cmd = cmd
133 self.__doc__ = "Alias for `!{}`".format(cmd)
134 self.nargs = self.validate()
135
136 def validate(self):
137 """Validate the alias, and return the number of arguments."""
138 if self.name in self.blacklist:
139 raise InvalidAliasError("The name %s can't be aliased "
140 "because it is a keyword or builtin." % self.name)
141 try:
142 caller = self.shell.magics_manager.magics['line'][self.name]
143 except KeyError:
144 pass
145 else:
146 if not isinstance(caller, Alias):
147 raise InvalidAliasError("The name %s can't be aliased "
148 "because it is another magic command." % self.name)
149
150 if not (isinstance(self.cmd, str)):
151 raise InvalidAliasError("An alias command must be a string, "
152 "got: %r" % self.cmd)
153
154 nargs = self.cmd.count('%s') - self.cmd.count('%%s')
155
156 if (nargs > 0) and (self.cmd.find('%l') >= 0):
157 raise InvalidAliasError('The %s and %l specifiers are mutually '
158 'exclusive in alias definitions.')
159
160 return nargs
161
162 def __repr__(self):
163 return "<alias {} for {!r}>".format(self.name, self.cmd)
164
165 def __call__(self, rest=''):
166 cmd = self.cmd
167 nargs = self.nargs
168 # Expand the %l special to be the user's input line
169 if cmd.find('%l') >= 0:
170 cmd = cmd.replace('%l', rest)
171 rest = ''
172
173 if nargs==0:
174 if cmd.find('%%s') >= 1:
175 cmd = cmd.replace('%%s', '%s')
176 # Simple, argument-less aliases
177 cmd = '%s %s' % (cmd, rest)

Callers 1

define_aliasMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected