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