Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/core/alias.py: 38%
107 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1# encoding: utf-8
2"""
3System command aliases.
5Authors:
7* Fernando Perez
8* Brian Granger
9"""
11#-----------------------------------------------------------------------------
12# Copyright (C) 2008-2011 The IPython Development Team
13#
14# Distributed under the terms of the BSD License.
15#
16# The full license is in the file COPYING.txt, distributed with this software.
17#-----------------------------------------------------------------------------
19#-----------------------------------------------------------------------------
20# Imports
21#-----------------------------------------------------------------------------
23import os
24import re
25import sys
27from traitlets.config.configurable import Configurable
28from .error import UsageError
30from traitlets import List, Instance
31from logging import error
33#-----------------------------------------------------------------------------
34# Utilities
35#-----------------------------------------------------------------------------
37# This is used as the pattern for calls to split_user_input.
38shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
40def default_aliases():
41 """Return list of shell aliases to auto-define.
42 """
43 # Note: the aliases defined here should be safe to use on a kernel
44 # regardless of what frontend it is attached to. Frontends that use a
45 # kernel in-process can define additional aliases that will only work in
46 # their case. For example, things like 'less' or 'clear' that manipulate
47 # the terminal should NOT be declared here, as they will only work if the
48 # kernel is running inside a true terminal, and not over the network.
50 if os.name == 'posix':
51 default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
52 ('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'),
53 ('cat', 'cat'),
54 ]
55 # Useful set of ls aliases. The GNU and BSD options are a little
56 # different, so we make aliases that provide as similar as possible
57 # behavior in ipython, by passing the right flags for each platform
58 if sys.platform.startswith('linux'):
59 ls_aliases = [('ls', 'ls -F --color'),
60 # long ls
61 ('ll', 'ls -F -o --color'),
62 # ls normal files only
63 ('lf', 'ls -F -o --color %l | grep ^-'),
64 # ls symbolic links
65 ('lk', 'ls -F -o --color %l | grep ^l'),
66 # directories or links to directories,
67 ('ldir', 'ls -F -o --color %l | grep /$'),
68 # things which are executable
69 ('lx', 'ls -F -o --color %l | grep ^-..x'),
70 ]
71 elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'):
72 # OpenBSD, NetBSD. The ls implementation on these platforms do not support
73 # the -G switch and lack the ability to use colorized output.
74 ls_aliases = [('ls', 'ls -F'),
75 # long ls
76 ('ll', 'ls -F -l'),
77 # ls normal files only
78 ('lf', 'ls -F -l %l | grep ^-'),
79 # ls symbolic links
80 ('lk', 'ls -F -l %l | grep ^l'),
81 # directories or links to directories,
82 ('ldir', 'ls -F -l %l | grep /$'),
83 # things which are executable
84 ('lx', 'ls -F -l %l | grep ^-..x'),
85 ]
86 else:
87 # BSD, OSX, etc.
88 ls_aliases = [('ls', 'ls -F -G'),
89 # long ls
90 ('ll', 'ls -F -l -G'),
91 # ls normal files only
92 ('lf', 'ls -F -l -G %l | grep ^-'),
93 # ls symbolic links
94 ('lk', 'ls -F -l -G %l | grep ^l'),
95 # directories or links to directories,
96 ('ldir', 'ls -F -G -l %l | grep /$'),
97 # things which are executable
98 ('lx', 'ls -F -l -G %l | grep ^-..x'),
99 ]
100 default_aliases = default_aliases + ls_aliases
101 elif os.name in ['nt', 'dos']:
102 default_aliases = [('ls', 'dir /on'),
103 ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
104 ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
105 ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
106 ]
107 else:
108 default_aliases = []
110 return default_aliases
113class AliasError(Exception):
114 pass
117class InvalidAliasError(AliasError):
118 pass
120class Alias(object):
121 """Callable object storing the details of one alias.
123 Instances are registered as magic functions to allow use of aliases.
124 """
126 # Prepare blacklist
127 blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
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()
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)
150 if not (isinstance(self.cmd, str)):
151 raise InvalidAliasError("An alias command must be a string, "
152 "got: %r" % self.cmd)
154 nargs = self.cmd.count('%s') - self.cmd.count('%%s')
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.')
160 return nargs
162 def __repr__(self):
163 return "<alias {} for {!r}>".format(self.name, self.cmd)
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 = ''
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)
178 else:
179 # Handle aliases with positional arguments
180 args = rest.split(None, nargs)
181 if len(args) < nargs:
182 raise UsageError('Alias <%s> requires %s arguments, %s given.' %
183 (self.name, nargs, len(args)))
184 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
186 self.shell.system(cmd)
188#-----------------------------------------------------------------------------
189# Main AliasManager class
190#-----------------------------------------------------------------------------
192class AliasManager(Configurable):
194 default_aliases = List(default_aliases()).tag(config=True)
195 user_aliases = List(default_value=[]).tag(config=True)
196 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
198 def __init__(self, shell=None, **kwargs):
199 super(AliasManager, self).__init__(shell=shell, **kwargs)
200 # For convenient access
201 self.linemagics = self.shell.magics_manager.magics['line']
202 self.init_aliases()
204 def init_aliases(self):
205 # Load default & user aliases
206 for name, cmd in self.default_aliases + self.user_aliases:
207 if cmd.startswith('ls ') and self.shell.colors == 'NoColor':
208 cmd = cmd.replace(' --color', '')
209 self.soft_define_alias(name, cmd)
211 @property
212 def aliases(self):
213 return [(n, func.cmd) for (n, func) in self.linemagics.items()
214 if isinstance(func, Alias)]
216 def soft_define_alias(self, name, cmd):
217 """Define an alias, but don't raise on an AliasError."""
218 try:
219 self.define_alias(name, cmd)
220 except AliasError as e:
221 error("Invalid alias: %s" % e)
223 def define_alias(self, name, cmd):
224 """Define a new alias after validating it.
226 This will raise an :exc:`AliasError` if there are validation
227 problems.
228 """
229 caller = Alias(shell=self.shell, name=name, cmd=cmd)
230 self.shell.magics_manager.register_function(caller, magic_kind='line',
231 magic_name=name)
233 def get_alias(self, name):
234 """Return an alias, or None if no alias by that name exists."""
235 aname = self.linemagics.get(name, None)
236 return aname if isinstance(aname, Alias) else None
238 def is_alias(self, name):
239 """Return whether or not a given name has been defined as an alias"""
240 return self.get_alias(name) is not None
242 def undefine_alias(self, name):
243 if self.is_alias(name):
244 del self.linemagics[name]
245 else:
246 raise ValueError('%s is not an alias' % name)
248 def clear_aliases(self):
249 for name, cmd in self.aliases:
250 self.undefine_alias(name)
252 def retrieve_alias(self, name):
253 """Retrieve the command to which an alias expands."""
254 caller = self.get_alias(name)
255 if caller:
256 return caller.cmd
257 else:
258 raise ValueError('%s is not an alias' % name)