alias.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # encoding: utf-8
  2. """
  3. System command aliases.
  4. Authors:
  5. * Fernando Perez
  6. * Brian Granger
  7. """
  8. #-----------------------------------------------------------------------------
  9. # Copyright (C) 2008-2011 The IPython Development Team
  10. #
  11. # Distributed under the terms of the BSD License.
  12. #
  13. # The full license is in the file COPYING.txt, distributed with this software.
  14. #-----------------------------------------------------------------------------
  15. #-----------------------------------------------------------------------------
  16. # Imports
  17. #-----------------------------------------------------------------------------
  18. import os
  19. import re
  20. import sys
  21. from traitlets.config.configurable import Configurable
  22. from .error import UsageError
  23. from traitlets import List, Instance
  24. from logging import error
  25. #-----------------------------------------------------------------------------
  26. # Utilities
  27. #-----------------------------------------------------------------------------
  28. # This is used as the pattern for calls to split_user_input.
  29. shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)')
  30. def default_aliases():
  31. """Return list of shell aliases to auto-define.
  32. """
  33. # Note: the aliases defined here should be safe to use on a kernel
  34. # regardless of what frontend it is attached to. Frontends that use a
  35. # kernel in-process can define additional aliases that will only work in
  36. # their case. For example, things like 'less' or 'clear' that manipulate
  37. # the terminal should NOT be declared here, as they will only work if the
  38. # kernel is running inside a true terminal, and not over the network.
  39. if os.name == 'posix':
  40. default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
  41. ('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'),
  42. ('cat', 'cat'),
  43. ]
  44. # Useful set of ls aliases. The GNU and BSD options are a little
  45. # different, so we make aliases that provide as similar as possible
  46. # behavior in ipython, by passing the right flags for each platform
  47. if sys.platform.startswith('linux'):
  48. ls_aliases = [('ls', 'ls -F --color'),
  49. # long ls
  50. ('ll', 'ls -F -o --color'),
  51. # ls normal files only
  52. ('lf', 'ls -F -o --color %l | grep ^-'),
  53. # ls symbolic links
  54. ('lk', 'ls -F -o --color %l | grep ^l'),
  55. # directories or links to directories,
  56. ('ldir', 'ls -F -o --color %l | grep /$'),
  57. # things which are executable
  58. ('lx', 'ls -F -o --color %l | grep ^-..x'),
  59. ]
  60. elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'):
  61. # OpenBSD, NetBSD. The ls implementation on these platforms do not support
  62. # the -G switch and lack the ability to use colorized output.
  63. ls_aliases = [('ls', 'ls -F'),
  64. # long ls
  65. ('ll', 'ls -F -l'),
  66. # ls normal files only
  67. ('lf', 'ls -F -l %l | grep ^-'),
  68. # ls symbolic links
  69. ('lk', 'ls -F -l %l | grep ^l'),
  70. # directories or links to directories,
  71. ('ldir', 'ls -F -l %l | grep /$'),
  72. # things which are executable
  73. ('lx', 'ls -F -l %l | grep ^-..x'),
  74. ]
  75. else:
  76. # BSD, OSX, etc.
  77. ls_aliases = [('ls', 'ls -F -G'),
  78. # long ls
  79. ('ll', 'ls -F -l -G'),
  80. # ls normal files only
  81. ('lf', 'ls -F -l -G %l | grep ^-'),
  82. # ls symbolic links
  83. ('lk', 'ls -F -l -G %l | grep ^l'),
  84. # directories or links to directories,
  85. ('ldir', 'ls -F -G -l %l | grep /$'),
  86. # things which are executable
  87. ('lx', 'ls -F -l -G %l | grep ^-..x'),
  88. ]
  89. default_aliases = default_aliases + ls_aliases
  90. elif os.name in ['nt', 'dos']:
  91. default_aliases = [('ls', 'dir /on'),
  92. ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'),
  93. ('mkdir', 'mkdir'), ('rmdir', 'rmdir'),
  94. ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'),
  95. ]
  96. else:
  97. default_aliases = []
  98. return default_aliases
  99. class AliasError(Exception):
  100. pass
  101. class InvalidAliasError(AliasError):
  102. pass
  103. class Alias(object):
  104. """Callable object storing the details of one alias.
  105. Instances are registered as magic functions to allow use of aliases.
  106. """
  107. # Prepare blacklist
  108. blacklist = {'cd','popd','pushd','dhist','alias','unalias'}
  109. def __init__(self, shell, name, cmd):
  110. self.shell = shell
  111. self.name = name
  112. self.cmd = cmd
  113. self.__doc__ = "Alias for `!{}`".format(cmd)
  114. self.nargs = self.validate()
  115. def validate(self):
  116. """Validate the alias, and return the number of arguments."""
  117. if self.name in self.blacklist:
  118. raise InvalidAliasError("The name %s can't be aliased "
  119. "because it is a keyword or builtin." % self.name)
  120. try:
  121. caller = self.shell.magics_manager.magics['line'][self.name]
  122. except KeyError:
  123. pass
  124. else:
  125. if not isinstance(caller, Alias):
  126. raise InvalidAliasError("The name %s can't be aliased "
  127. "because it is another magic command." % self.name)
  128. if not (isinstance(self.cmd, str)):
  129. raise InvalidAliasError("An alias command must be a string, "
  130. "got: %r" % self.cmd)
  131. nargs = self.cmd.count('%s') - self.cmd.count('%%s')
  132. if (nargs > 0) and (self.cmd.find('%l') >= 0):
  133. raise InvalidAliasError('The %s and %l specifiers are mutually '
  134. 'exclusive in alias definitions.')
  135. return nargs
  136. def __repr__(self):
  137. return "<alias {} for {!r}>".format(self.name, self.cmd)
  138. def __call__(self, rest=''):
  139. cmd = self.cmd
  140. nargs = self.nargs
  141. # Expand the %l special to be the user's input line
  142. if cmd.find('%l') >= 0:
  143. cmd = cmd.replace('%l', rest)
  144. rest = ''
  145. if nargs==0:
  146. if cmd.find('%%s') >= 1:
  147. cmd = cmd.replace('%%s', '%s')
  148. # Simple, argument-less aliases
  149. cmd = '%s %s' % (cmd, rest)
  150. else:
  151. # Handle aliases with positional arguments
  152. args = rest.split(None, nargs)
  153. if len(args) < nargs:
  154. raise UsageError('Alias <%s> requires %s arguments, %s given.' %
  155. (self.name, nargs, len(args)))
  156. cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
  157. self.shell.system(cmd)
  158. #-----------------------------------------------------------------------------
  159. # Main AliasManager class
  160. #-----------------------------------------------------------------------------
  161. class AliasManager(Configurable):
  162. default_aliases = List(default_aliases()).tag(config=True)
  163. user_aliases = List(default_value=[]).tag(config=True)
  164. shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
  165. def __init__(self, shell=None, **kwargs):
  166. super(AliasManager, self).__init__(shell=shell, **kwargs)
  167. # For convenient access
  168. self.linemagics = self.shell.magics_manager.magics['line']
  169. self.init_aliases()
  170. def init_aliases(self):
  171. # Load default & user aliases
  172. for name, cmd in self.default_aliases + self.user_aliases:
  173. if cmd.startswith('ls ') and self.shell.colors == 'NoColor':
  174. cmd = cmd.replace(' --color', '')
  175. self.soft_define_alias(name, cmd)
  176. @property
  177. def aliases(self):
  178. return [(n, func.cmd) for (n, func) in self.linemagics.items()
  179. if isinstance(func, Alias)]
  180. def soft_define_alias(self, name, cmd):
  181. """Define an alias, but don't raise on an AliasError."""
  182. try:
  183. self.define_alias(name, cmd)
  184. except AliasError as e:
  185. error("Invalid alias: %s" % e)
  186. def define_alias(self, name, cmd):
  187. """Define a new alias after validating it.
  188. This will raise an :exc:`AliasError` if there are validation
  189. problems.
  190. """
  191. caller = Alias(shell=self.shell, name=name, cmd=cmd)
  192. self.shell.magics_manager.register_function(caller, magic_kind='line',
  193. magic_name=name)
  194. def get_alias(self, name):
  195. """Return an alias, or None if no alias by that name exists."""
  196. aname = self.linemagics.get(name, None)
  197. return aname if isinstance(aname, Alias) else None
  198. def is_alias(self, name):
  199. """Return whether or not a given name has been defined as an alias"""
  200. return self.get_alias(name) is not None
  201. def undefine_alias(self, name):
  202. if self.is_alias(name):
  203. del self.linemagics[name]
  204. else:
  205. raise ValueError('%s is not an alias' % name)
  206. def clear_aliases(self):
  207. for name, cmd in self.aliases:
  208. self.undefine_alias(name)
  209. def retrieve_alias(self, name):
  210. """Retrieve the command to which an alias expands."""
  211. caller = self.get_alias(name)
  212. if caller:
  213. return caller.cmd
  214. else:
  215. raise ValueError('%s is not an alias' % name)