1"""
2Autocall capabilities for IPython.core.
3
4Authors:
5
6* Brian Granger
7* Fernando Perez
8* Thomas Kluyver
9
10Notes
11-----
12"""
13
14#-----------------------------------------------------------------------------
15# Copyright (C) 2008-2011 The IPython Development Team
16#
17# Distributed under the terms of the BSD License. The full license is in
18# the file COPYING, distributed as part of this software.
19#-----------------------------------------------------------------------------
20
21#-----------------------------------------------------------------------------
22# Imports
23#-----------------------------------------------------------------------------
24
25
26#-----------------------------------------------------------------------------
27# Code
28#-----------------------------------------------------------------------------
29
30class IPyAutocall:
31 """Instances of this class are always autocalled
32
33 This happens regardless of 'autocall' variable state. Use this to
34 develop macro-like mechanisms.
35 """
36 _ip = None
37 rewrite = True
38 def __init__(self, ip=None):
39 self._ip = ip
40
41 def set_ip(self, ip):
42 """Will be used to set _ip point to current ipython instance b/f call
43
44 Override this method if you don't want this to happen.
45
46 """
47 self._ip = ip
48
49
50class ExitAutocall(IPyAutocall):
51 """An autocallable object which will be added to the user namespace so that
52 exit, exit(), quit or quit() are all valid ways to close the shell."""
53 rewrite = False
54
55 def __call__(self):
56 self._ip.ask_exit()
57
58
59class 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()