Format: text/x-python
⬇ Download File (1.9 KB)
   1  # encoding: utf-8
   2  """
   3  Autocall capabilities for IPython.core.
   4  
   5  Authors:
   6  
   7  * Brian Granger
   8  * Fernando Perez
   9  * Thomas Kluyver
  10  
  11  Notes
  12  -----
  13  """
  14  
  15  #-----------------------------------------------------------------------------
  16  #  Copyright (C) 2008-2011  The IPython Development Team
  17  #
  18  #  Distributed under the terms of the BSD License.  The full license is in
  19  #  the file COPYING, distributed as part of this software.
  20  #-----------------------------------------------------------------------------
  21  
  22  #-----------------------------------------------------------------------------
  23  # Imports
  24  #-----------------------------------------------------------------------------
  25  
  26  
  27  #-----------------------------------------------------------------------------
  28  # Code
  29  #-----------------------------------------------------------------------------
  30  
  31  class IPyAutocall(object):
  32      """ Instances of this class are always autocalled
  33      
  34      This happens regardless of 'autocall' variable state. Use this to
  35      develop macro-like mechanisms.
  36      """
  37      _ip = None
  38      rewrite = True
  39      def __init__(self, ip=None):
  40          self._ip = ip
  41      
  42      def set_ip(self, ip):
  43          """Will be used to set _ip point to current ipython instance b/f call
  44  
  45          Override this method if you don't want this to happen.
  46  
  47          """
  48          self._ip = ip
  49  
  50  
  51  class ExitAutocall(IPyAutocall):
  52      """An autocallable object which will be added to the user namespace so that
  53      exit, exit(), quit or quit() are all valid ways to close the shell."""
  54      rewrite = False
  55      
  56      def __call__(self):
  57          self._ip.ask_exit()
  58          
  59  class ZMQExitAutocall(ExitAutocall):
  60      """Exit IPython. Autocallable, so it needn't be explicitly called.
  61      
  62      Parameters
  63      ----------
  64      keep_kernel : bool
  65        If True, leave the kernel alive. Otherwise, tell the kernel to exit too
  66        (default).
  67      """
  68      def __call__(self, keep_kernel=False):
  69          self._ip.keepkernel_on_exit = keep_kernel
  70          self._ip.ask_exit()
  71