##// END OF EJS Templates
Merge pull request #10504 from ipython/auto-backport-of-pr-10414...
Matthias Bussonnier -
r23587:25d473a1 merge
parent child Browse files
Show More
@@ -1,226 +1,226 b''
1 """Hooks for IPython.
1 """Hooks for IPython.
2
2
3 In Python, it is possible to overwrite any method of any object if you really
3 In Python, it is possible to overwrite any method of any object if you really
4 want to. But IPython exposes a few 'hooks', methods which are *designed* to
4 want to. But IPython exposes a few 'hooks', methods which are *designed* to
5 be overwritten by users for customization purposes. This module defines the
5 be overwritten by users for customization purposes. This module defines the
6 default versions of all such hooks, which get used by IPython if not
6 default versions of all such hooks, which get used by IPython if not
7 overridden by the user.
7 overridden by the user.
8
8
9 Hooks are simple functions, but they should be declared with ``self`` as their
9 Hooks are simple functions, but they should be declared with ``self`` as their
10 first argument, because when activated they are registered into IPython as
10 first argument, because when activated they are registered into IPython as
11 instance methods. The self argument will be the IPython running instance
11 instance methods. The self argument will be the IPython running instance
12 itself, so hooks have full access to the entire IPython object.
12 itself, so hooks have full access to the entire IPython object.
13
13
14 If you wish to define a new hook and activate it, you can make an :doc:`extension
14 If you wish to define a new hook and activate it, you can make an :doc:`extension
15 </config/extensions/index>` or a :ref:`startup script <startup_files>`. For
15 </config/extensions/index>` or a :ref:`startup script <startup_files>`. For
16 example, you could use a startup file like this::
16 example, you could use a startup file like this::
17
17
18 import os
18 import os
19
19
20 def calljed(self,filename, linenum):
20 def calljed(self,filename, linenum):
21 "My editor hook calls the jed editor directly."
21 "My editor hook calls the jed editor directly."
22 print "Calling my own editor, jed ..."
22 print "Calling my own editor, jed ..."
23 if os.system('jed +%d %s' % (linenum,filename)) != 0:
23 if os.system('jed +%d %s' % (linenum,filename)) != 0:
24 raise TryNext()
24 raise TryNext()
25
25
26 def load_ipython_extension(ip):
26 def load_ipython_extension(ip):
27 ip.set_hook('editor', calljed)
27 ip.set_hook('editor', calljed)
28
28
29 """
29 """
30
30
31 #*****************************************************************************
31 #*****************************************************************************
32 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
32 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
33 #
33 #
34 # Distributed under the terms of the BSD License. The full license is in
34 # Distributed under the terms of the BSD License. The full license is in
35 # the file COPYING, distributed as part of this software.
35 # the file COPYING, distributed as part of this software.
36 #*****************************************************************************
36 #*****************************************************************************
37
37
38 import os
38 import os
39 import subprocess
39 import subprocess
40 import warnings
40 import warnings
41 import sys
41 import sys
42
42
43 from IPython.core.error import TryNext
43 from IPython.core.error import TryNext
44
44
45 # List here all the default hooks. For now it's just the editor functions
45 # List here all the default hooks. For now it's just the editor functions
46 # but over time we'll move here all the public API for user-accessible things.
46 # but over time we'll move here all the public API for user-accessible things.
47
47
48 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
48 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
49 'shutdown_hook', 'late_startup_hook',
49 'shutdown_hook', 'late_startup_hook',
50 'show_in_pager','pre_prompt_hook',
50 'show_in_pager','pre_prompt_hook',
51 'pre_run_code_hook', 'clipboard_get']
51 'pre_run_code_hook', 'clipboard_get']
52
52
53 deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event",
53 deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event",
54 'late_startup_hook': "a callback for the 'shell_initialized' event",
54 'late_startup_hook': "a callback for the 'shell_initialized' event",
55 'shutdown_hook': "the atexit module",
55 'shutdown_hook': "the atexit module",
56 }
56 }
57
57
58 def editor(self, filename, linenum=None, wait=True):
58 def editor(self, filename, linenum=None, wait=True):
59 """Open the default editor at the given filename and linenumber.
59 """Open the default editor at the given filename and linenumber.
60
60
61 This is IPython's default editor hook, you can use it as an example to
61 This is IPython's default editor hook, you can use it as an example to
62 write your own modified one. To set your own editor function as the
62 write your own modified one. To set your own editor function as the
63 new editor hook, call ip.set_hook('editor',yourfunc)."""
63 new editor hook, call ip.set_hook('editor',yourfunc)."""
64
64
65 # IPython configures a default editor at startup by reading $EDITOR from
65 # IPython configures a default editor at startup by reading $EDITOR from
66 # the environment, and falling back on vi (unix) or notepad (win32).
66 # the environment, and falling back on vi (unix) or notepad (win32).
67 editor = self.editor
67 editor = self.editor
68
68
69 # marker for at which line to open the file (for existing objects)
69 # marker for at which line to open the file (for existing objects)
70 if linenum is None or editor=='notepad':
70 if linenum is None or editor=='notepad':
71 linemark = ''
71 linemark = ''
72 else:
72 else:
73 linemark = '+%d' % int(linenum)
73 linemark = '+%d' % int(linenum)
74
74
75 # Enclose in quotes if necessary and legal
75 # Enclose in quotes if necessary and legal
76 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
76 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
77 editor = '"%s"' % editor
77 editor = '"%s"' % editor
78
78
79 # Call the actual editor
79 # Call the actual editor
80 proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename),
80 proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename),
81 shell=True)
81 shell=True)
82 if wait and proc.wait() != 0:
82 if wait and proc.wait() != 0:
83 raise TryNext()
83 raise TryNext()
84
84
85 import tempfile
85 import tempfile
86 def fix_error_editor(self,filename,linenum,column,msg):
86 def fix_error_editor(self,filename,linenum,column,msg):
87 """DEPRECATED
87 """DEPRECATED
88
88
89 Open the editor at the given filename, linenumber, column and
89 Open the editor at the given filename, linenumber, column and
90 show an error message. This is used for correcting syntax errors.
90 show an error message. This is used for correcting syntax errors.
91 The current implementation only has special support for the VIM editor,
91 The current implementation only has special support for the VIM editor,
92 and falls back on the 'editor' hook if VIM is not used.
92 and falls back on the 'editor' hook if VIM is not used.
93
93
94 Call ip.set_hook('fix_error_editor',yourfunc) to use your own function,
94 Call ip.set_hook('fix_error_editor',yourfunc) to use your own function,
95 """
95 """
96
96
97 warnings.warn("""
97 warnings.warn("""
98 `fix_error_editor` is pending deprecation as of IPython 5.0 and will be removed
98 `fix_error_editor` is pending deprecation as of IPython 5.0 and will be removed
99 in future versions. It appears to be used only for automatically fixing syntax
99 in future versions. It appears to be used only for automatically fixing syntax
100 error that has been broken for a few years and has thus been removed. If you
100 error that has been broken for a few years and has thus been removed. If you
101 happend to use this function and still need it please make your voice heard on
101 happend to use this function and still need it please make your voice heard on
102 the mailing list ipython-dev@scipy.org , or on the GitHub Issue tracker:
102 the mailing list ipython-dev@python.org , or on the GitHub Issue tracker:
103 https://github.com/ipython/ipython/issues/9649 """, UserWarning)
103 https://github.com/ipython/ipython/issues/9649 """, UserWarning)
104
104
105 def vim_quickfix_file():
105 def vim_quickfix_file():
106 t = tempfile.NamedTemporaryFile()
106 t = tempfile.NamedTemporaryFile()
107 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
107 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
108 t.flush()
108 t.flush()
109 return t
109 return t
110 if os.path.basename(self.editor) != 'vim':
110 if os.path.basename(self.editor) != 'vim':
111 self.hooks.editor(filename,linenum)
111 self.hooks.editor(filename,linenum)
112 return
112 return
113 t = vim_quickfix_file()
113 t = vim_quickfix_file()
114 try:
114 try:
115 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
115 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
116 raise TryNext()
116 raise TryNext()
117 finally:
117 finally:
118 t.close()
118 t.close()
119
119
120
120
121 def synchronize_with_editor(self, filename, linenum, column):
121 def synchronize_with_editor(self, filename, linenum, column):
122 pass
122 pass
123
123
124
124
125 class CommandChainDispatcher:
125 class CommandChainDispatcher:
126 """ Dispatch calls to a chain of commands until some func can handle it
126 """ Dispatch calls to a chain of commands until some func can handle it
127
127
128 Usage: instantiate, execute "add" to add commands (with optional
128 Usage: instantiate, execute "add" to add commands (with optional
129 priority), execute normally via f() calling mechanism.
129 priority), execute normally via f() calling mechanism.
130
130
131 """
131 """
132 def __init__(self,commands=None):
132 def __init__(self,commands=None):
133 if commands is None:
133 if commands is None:
134 self.chain = []
134 self.chain = []
135 else:
135 else:
136 self.chain = commands
136 self.chain = commands
137
137
138
138
139 def __call__(self,*args, **kw):
139 def __call__(self,*args, **kw):
140 """ Command chain is called just like normal func.
140 """ Command chain is called just like normal func.
141
141
142 This will call all funcs in chain with the same args as were given to
142 This will call all funcs in chain with the same args as were given to
143 this function, and return the result of first func that didn't raise
143 this function, and return the result of first func that didn't raise
144 TryNext"""
144 TryNext"""
145 last_exc = TryNext()
145 last_exc = TryNext()
146 for prio,cmd in self.chain:
146 for prio,cmd in self.chain:
147 #print "prio",prio,"cmd",cmd #dbg
147 #print "prio",prio,"cmd",cmd #dbg
148 try:
148 try:
149 return cmd(*args, **kw)
149 return cmd(*args, **kw)
150 except TryNext as exc:
150 except TryNext as exc:
151 last_exc = exc
151 last_exc = exc
152 # if no function will accept it, raise TryNext up to the caller
152 # if no function will accept it, raise TryNext up to the caller
153 raise last_exc
153 raise last_exc
154
154
155 def __str__(self):
155 def __str__(self):
156 return str(self.chain)
156 return str(self.chain)
157
157
158 def add(self, func, priority=0):
158 def add(self, func, priority=0):
159 """ Add a func to the cmd chain with given priority """
159 """ Add a func to the cmd chain with given priority """
160 self.chain.append((priority, func))
160 self.chain.append((priority, func))
161 self.chain.sort(key=lambda x: x[0])
161 self.chain.sort(key=lambda x: x[0])
162
162
163 def __iter__(self):
163 def __iter__(self):
164 """ Return all objects in chain.
164 """ Return all objects in chain.
165
165
166 Handy if the objects are not callable.
166 Handy if the objects are not callable.
167 """
167 """
168 return iter(self.chain)
168 return iter(self.chain)
169
169
170
170
171 def shutdown_hook(self):
171 def shutdown_hook(self):
172 """ default shutdown hook
172 """ default shutdown hook
173
173
174 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
174 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
175 """
175 """
176
176
177 #print "default shutdown hook ok" # dbg
177 #print "default shutdown hook ok" # dbg
178 return
178 return
179
179
180
180
181 def late_startup_hook(self):
181 def late_startup_hook(self):
182 """ Executed after ipython has been constructed and configured
182 """ Executed after ipython has been constructed and configured
183
183
184 """
184 """
185 #print "default startup hook ok" # dbg
185 #print "default startup hook ok" # dbg
186
186
187
187
188 def show_in_pager(self, data, start, screen_lines):
188 def show_in_pager(self, data, start, screen_lines):
189 """ Run a string through pager """
189 """ Run a string through pager """
190 # raising TryNext here will use the default paging functionality
190 # raising TryNext here will use the default paging functionality
191 raise TryNext
191 raise TryNext
192
192
193
193
194 def pre_prompt_hook(self):
194 def pre_prompt_hook(self):
195 """ Run before displaying the next prompt
195 """ Run before displaying the next prompt
196
196
197 Use this e.g. to display output from asynchronous operations (in order
197 Use this e.g. to display output from asynchronous operations (in order
198 to not mess up text entry)
198 to not mess up text entry)
199 """
199 """
200
200
201 return None
201 return None
202
202
203
203
204 def pre_run_code_hook(self):
204 def pre_run_code_hook(self):
205 """ Executed before running the (prefiltered) code in IPython """
205 """ Executed before running the (prefiltered) code in IPython """
206 return None
206 return None
207
207
208
208
209 def clipboard_get(self):
209 def clipboard_get(self):
210 """ Get text from the clipboard.
210 """ Get text from the clipboard.
211 """
211 """
212 from IPython.lib.clipboard import (
212 from IPython.lib.clipboard import (
213 osx_clipboard_get, tkinter_clipboard_get,
213 osx_clipboard_get, tkinter_clipboard_get,
214 win32_clipboard_get
214 win32_clipboard_get
215 )
215 )
216 if sys.platform == 'win32':
216 if sys.platform == 'win32':
217 chain = [win32_clipboard_get, tkinter_clipboard_get]
217 chain = [win32_clipboard_get, tkinter_clipboard_get]
218 elif sys.platform == 'darwin':
218 elif sys.platform == 'darwin':
219 chain = [osx_clipboard_get, tkinter_clipboard_get]
219 chain = [osx_clipboard_get, tkinter_clipboard_get]
220 else:
220 else:
221 chain = [tkinter_clipboard_get]
221 chain = [tkinter_clipboard_get]
222 dispatcher = CommandChainDispatcher()
222 dispatcher = CommandChainDispatcher()
223 for func in chain:
223 for func in chain:
224 dispatcher.add(func)
224 dispatcher.add(func)
225 text = dispatcher()
225 text = dispatcher()
226 return text
226 return text
@@ -1,123 +1,123 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Release data for the IPython project."""
2 """Release data for the IPython project."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2008, IPython Development Team.
5 # Copyright (c) 2008, IPython Development Team.
6 # Copyright (c) 2001, Fernando Perez <fernando.perez@colorado.edu>
6 # Copyright (c) 2001, Fernando Perez <fernando.perez@colorado.edu>
7 # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
7 # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
8 # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
8 # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
9 #
9 #
10 # Distributed under the terms of the Modified BSD License.
10 # Distributed under the terms of the Modified BSD License.
11 #
11 #
12 # The full license is in the file COPYING.txt, distributed with this software.
12 # The full license is in the file COPYING.txt, distributed with this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Name of the package for release purposes. This is the name which labels
15 # Name of the package for release purposes. This is the name which labels
16 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
16 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
17 name = 'ipython'
17 name = 'ipython'
18
18
19 # IPython version information. An empty _version_extra corresponds to a full
19 # IPython version information. An empty _version_extra corresponds to a full
20 # release. 'dev' as a _version_extra string means this is a development
20 # release. 'dev' as a _version_extra string means this is a development
21 # version
21 # version
22 _version_major = 5
22 _version_major = 5
23 _version_minor = 4
23 _version_minor = 4
24 _version_patch = 0
24 _version_patch = 0
25 _version_extra = '.dev'
25 _version_extra = '.dev'
26 # _version_extra = 'rc1'
26 # _version_extra = 'rc1'
27 #_version_extra = '' # Uncomment this for full releases
27 #_version_extra = '' # Uncomment this for full releases
28
28
29 # release.codename is deprecated in 2.0, will be removed in 3.0
29 # release.codename is deprecated in 2.0, will be removed in 3.0
30 codename = ''
30 codename = ''
31
31
32 # Construct full version string from these.
32 # Construct full version string from these.
33 _ver = [_version_major, _version_minor, _version_patch]
33 _ver = [_version_major, _version_minor, _version_patch]
34
34
35 __version__ = '.'.join(map(str, _ver))
35 __version__ = '.'.join(map(str, _ver))
36 if _version_extra:
36 if _version_extra:
37 __version__ = __version__ + _version_extra
37 __version__ = __version__ + _version_extra
38
38
39 version = __version__ # backwards compatibility name
39 version = __version__ # backwards compatibility name
40 version_info = (_version_major, _version_minor, _version_patch, _version_extra)
40 version_info = (_version_major, _version_minor, _version_patch, _version_extra)
41
41
42 # Change this when incrementing the kernel protocol version
42 # Change this when incrementing the kernel protocol version
43 kernel_protocol_version_info = (5, 0)
43 kernel_protocol_version_info = (5, 0)
44 kernel_protocol_version = "%i.%i" % kernel_protocol_version_info
44 kernel_protocol_version = "%i.%i" % kernel_protocol_version_info
45
45
46 description = "IPython: Productive Interactive Computing"
46 description = "IPython: Productive Interactive Computing"
47
47
48 long_description = \
48 long_description = \
49 """
49 """
50 IPython provides a rich toolkit to help you make the most out of using Python
50 IPython provides a rich toolkit to help you make the most out of using Python
51 interactively. Its main components are:
51 interactively. Its main components are:
52
52
53 * A powerful interactive Python shell
53 * A powerful interactive Python shell
54 * A `Jupyter <http://jupyter.org/>`_ kernel to work with Python code in Jupyter
54 * A `Jupyter <http://jupyter.org/>`_ kernel to work with Python code in Jupyter
55 notebooks and other interactive frontends.
55 notebooks and other interactive frontends.
56
56
57 The enhanced interactive Python shells have the following main features:
57 The enhanced interactive Python shells have the following main features:
58
58
59 * Comprehensive object introspection.
59 * Comprehensive object introspection.
60
60
61 * Input history, persistent across sessions.
61 * Input history, persistent across sessions.
62
62
63 * Caching of output results during a session with automatically generated
63 * Caching of output results during a session with automatically generated
64 references.
64 references.
65
65
66 * Extensible tab completion, with support by default for completion of python
66 * Extensible tab completion, with support by default for completion of python
67 variables and keywords, filenames and function keywords.
67 variables and keywords, filenames and function keywords.
68
68
69 * Extensible system of 'magic' commands for controlling the environment and
69 * Extensible system of 'magic' commands for controlling the environment and
70 performing many tasks related either to IPython or the operating system.
70 performing many tasks related either to IPython or the operating system.
71
71
72 * A rich configuration system with easy switching between different setups
72 * A rich configuration system with easy switching between different setups
73 (simpler than changing $PYTHONSTARTUP environment variables every time).
73 (simpler than changing $PYTHONSTARTUP environment variables every time).
74
74
75 * Session logging and reloading.
75 * Session logging and reloading.
76
76
77 * Extensible syntax processing for special purpose situations.
77 * Extensible syntax processing for special purpose situations.
78
78
79 * Access to the system shell with user-extensible alias system.
79 * Access to the system shell with user-extensible alias system.
80
80
81 * Easily embeddable in other Python programs and GUIs.
81 * Easily embeddable in other Python programs and GUIs.
82
82
83 * Integrated access to the pdb debugger and the Python profiler.
83 * Integrated access to the pdb debugger and the Python profiler.
84
84
85 The latest development version is always available from IPython's `GitHub
85 The latest development version is always available from IPython's `GitHub
86 site <http://github.com/ipython>`_.
86 site <http://github.com/ipython>`_.
87 """
87 """
88
88
89 license = 'BSD'
89 license = 'BSD'
90
90
91 authors = {'Fernando' : ('Fernando Perez','fperez.net@gmail.com'),
91 authors = {'Fernando' : ('Fernando Perez','fperez.net@gmail.com'),
92 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
92 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
93 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
93 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
94 'Ville' : ('Ville Vainio','vivainio@gmail.com'),
94 'Ville' : ('Ville Vainio','vivainio@gmail.com'),
95 'Brian' : ('Brian E Granger', 'ellisonbg@gmail.com'),
95 'Brian' : ('Brian E Granger', 'ellisonbg@gmail.com'),
96 'Min' : ('Min Ragan-Kelley', 'benjaminrk@gmail.com'),
96 'Min' : ('Min Ragan-Kelley', 'benjaminrk@gmail.com'),
97 'Thomas' : ('Thomas A. Kluyver', 'takowl@gmail.com'),
97 'Thomas' : ('Thomas A. Kluyver', 'takowl@gmail.com'),
98 'Jorgen' : ('Jorgen Stenarson', 'jorgen.stenarson@bostream.nu'),
98 'Jorgen' : ('Jorgen Stenarson', 'jorgen.stenarson@bostream.nu'),
99 'Matthias' : ('Matthias Bussonnier', 'bussonniermatthias@gmail.com'),
99 'Matthias' : ('Matthias Bussonnier', 'bussonniermatthias@gmail.com'),
100 }
100 }
101
101
102 author = 'The IPython Development Team'
102 author = 'The IPython Development Team'
103
103
104 author_email = 'ipython-dev@scipy.org'
104 author_email = 'ipython-dev@python.org'
105
105
106 url = 'http://ipython.org'
106 url = 'https://ipython.org'
107
107
108
108
109 platforms = ['Linux','Mac OSX','Windows']
109 platforms = ['Linux','Mac OSX','Windows']
110
110
111 keywords = ['Interactive','Interpreter','Shell', 'Embedding']
111 keywords = ['Interactive','Interpreter','Shell', 'Embedding']
112
112
113 classifiers = [
113 classifiers = [
114 'Framework :: IPython',
114 'Framework :: IPython',
115 'Intended Audience :: Developers',
115 'Intended Audience :: Developers',
116 'Intended Audience :: Science/Research',
116 'Intended Audience :: Science/Research',
117 'License :: OSI Approved :: BSD License',
117 'License :: OSI Approved :: BSD License',
118 'Programming Language :: Python',
118 'Programming Language :: Python',
119 'Programming Language :: Python :: 2',
119 'Programming Language :: Python :: 2',
120 'Programming Language :: Python :: 2.7',
120 'Programming Language :: Python :: 2.7',
121 'Programming Language :: Python :: 3',
121 'Programming Language :: Python :: 3',
122 'Topic :: System :: Shells'
122 'Topic :: System :: Shells'
123 ]
123 ]
General Comments 0
You need to be logged in to leave comments. Login now