##// END OF EJS Templates
Deprecate pending deprecation feature, and remove from __all__...
Matthias Bussonnier -
Show More
@@ -1,226 +1,229 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', '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 from IPython.utils.decorators import undoc
87
88 @undoc
86 def fix_error_editor(self,filename,linenum,column,msg):
89 def fix_error_editor(self,filename,linenum,column,msg):
87 """DEPRECATED
90 """DEPRECATED
88
91
89 Open the editor at the given filename, linenumber, column and
92 Open the editor at the given filename, linenumber, column and
90 show an error message. This is used for correcting syntax errors.
93 show an error message. This is used for correcting syntax errors.
91 The current implementation only has special support for the VIM editor,
94 The current implementation only has special support for the VIM editor,
92 and falls back on the 'editor' hook if VIM is not used.
95 and falls back on the 'editor' hook if VIM is not used.
93
96
94 Call ip.set_hook('fix_error_editor',yourfunc) to use your own function,
97 Call ip.set_hook('fix_error_editor',yourfunc) to use your own function,
95 """
98 """
96
99
97 warnings.warn("""
100 warnings.warn("""
98 `fix_error_editor` is pending deprecation as of IPython 5.0 and will be removed
101 `fix_error_editor` is deprecated as of IPython 6.0 and will be removed
99 in future versions. It appears to be used only for automatically fixing syntax
102 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
103 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
104 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:
105 the mailing list ipython-dev@scipy.org , or on the GitHub Issue tracker:
103 https://github.com/ipython/ipython/issues/9649 """, UserWarning)
106 https://github.com/ipython/ipython/issues/9649 """, UserWarning)
104
107
105 def vim_quickfix_file():
108 def vim_quickfix_file():
106 t = tempfile.NamedTemporaryFile()
109 t = tempfile.NamedTemporaryFile()
107 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
110 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
108 t.flush()
111 t.flush()
109 return t
112 return t
110 if os.path.basename(self.editor) != 'vim':
113 if os.path.basename(self.editor) != 'vim':
111 self.hooks.editor(filename,linenum)
114 self.hooks.editor(filename,linenum)
112 return
115 return
113 t = vim_quickfix_file()
116 t = vim_quickfix_file()
114 try:
117 try:
115 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
118 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
116 raise TryNext()
119 raise TryNext()
117 finally:
120 finally:
118 t.close()
121 t.close()
119
122
120
123
121 def synchronize_with_editor(self, filename, linenum, column):
124 def synchronize_with_editor(self, filename, linenum, column):
122 pass
125 pass
123
126
124
127
125 class CommandChainDispatcher:
128 class CommandChainDispatcher:
126 """ Dispatch calls to a chain of commands until some func can handle it
129 """ Dispatch calls to a chain of commands until some func can handle it
127
130
128 Usage: instantiate, execute "add" to add commands (with optional
131 Usage: instantiate, execute "add" to add commands (with optional
129 priority), execute normally via f() calling mechanism.
132 priority), execute normally via f() calling mechanism.
130
133
131 """
134 """
132 def __init__(self,commands=None):
135 def __init__(self,commands=None):
133 if commands is None:
136 if commands is None:
134 self.chain = []
137 self.chain = []
135 else:
138 else:
136 self.chain = commands
139 self.chain = commands
137
140
138
141
139 def __call__(self,*args, **kw):
142 def __call__(self,*args, **kw):
140 """ Command chain is called just like normal func.
143 """ Command chain is called just like normal func.
141
144
142 This will call all funcs in chain with the same args as were given to
145 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
146 this function, and return the result of first func that didn't raise
144 TryNext"""
147 TryNext"""
145 last_exc = TryNext()
148 last_exc = TryNext()
146 for prio,cmd in self.chain:
149 for prio,cmd in self.chain:
147 #print "prio",prio,"cmd",cmd #dbg
150 #print "prio",prio,"cmd",cmd #dbg
148 try:
151 try:
149 return cmd(*args, **kw)
152 return cmd(*args, **kw)
150 except TryNext as exc:
153 except TryNext as exc:
151 last_exc = exc
154 last_exc = exc
152 # if no function will accept it, raise TryNext up to the caller
155 # if no function will accept it, raise TryNext up to the caller
153 raise last_exc
156 raise last_exc
154
157
155 def __str__(self):
158 def __str__(self):
156 return str(self.chain)
159 return str(self.chain)
157
160
158 def add(self, func, priority=0):
161 def add(self, func, priority=0):
159 """ Add a func to the cmd chain with given priority """
162 """ Add a func to the cmd chain with given priority """
160 self.chain.append((priority, func))
163 self.chain.append((priority, func))
161 self.chain.sort(key=lambda x: x[0])
164 self.chain.sort(key=lambda x: x[0])
162
165
163 def __iter__(self):
166 def __iter__(self):
164 """ Return all objects in chain.
167 """ Return all objects in chain.
165
168
166 Handy if the objects are not callable.
169 Handy if the objects are not callable.
167 """
170 """
168 return iter(self.chain)
171 return iter(self.chain)
169
172
170
173
171 def shutdown_hook(self):
174 def shutdown_hook(self):
172 """ default shutdown hook
175 """ default shutdown hook
173
176
174 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
177 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
175 """
178 """
176
179
177 #print "default shutdown hook ok" # dbg
180 #print "default shutdown hook ok" # dbg
178 return
181 return
179
182
180
183
181 def late_startup_hook(self):
184 def late_startup_hook(self):
182 """ Executed after ipython has been constructed and configured
185 """ Executed after ipython has been constructed and configured
183
186
184 """
187 """
185 #print "default startup hook ok" # dbg
188 #print "default startup hook ok" # dbg
186
189
187
190
188 def show_in_pager(self, data, start, screen_lines):
191 def show_in_pager(self, data, start, screen_lines):
189 """ Run a string through pager """
192 """ Run a string through pager """
190 # raising TryNext here will use the default paging functionality
193 # raising TryNext here will use the default paging functionality
191 raise TryNext
194 raise TryNext
192
195
193
196
194 def pre_prompt_hook(self):
197 def pre_prompt_hook(self):
195 """ Run before displaying the next prompt
198 """ Run before displaying the next prompt
196
199
197 Use this e.g. to display output from asynchronous operations (in order
200 Use this e.g. to display output from asynchronous operations (in order
198 to not mess up text entry)
201 to not mess up text entry)
199 """
202 """
200
203
201 return None
204 return None
202
205
203
206
204 def pre_run_code_hook(self):
207 def pre_run_code_hook(self):
205 """ Executed before running the (prefiltered) code in IPython """
208 """ Executed before running the (prefiltered) code in IPython """
206 return None
209 return None
207
210
208
211
209 def clipboard_get(self):
212 def clipboard_get(self):
210 """ Get text from the clipboard.
213 """ Get text from the clipboard.
211 """
214 """
212 from IPython.lib.clipboard import (
215 from IPython.lib.clipboard import (
213 osx_clipboard_get, tkinter_clipboard_get,
216 osx_clipboard_get, tkinter_clipboard_get,
214 win32_clipboard_get
217 win32_clipboard_get
215 )
218 )
216 if sys.platform == 'win32':
219 if sys.platform == 'win32':
217 chain = [win32_clipboard_get, tkinter_clipboard_get]
220 chain = [win32_clipboard_get, tkinter_clipboard_get]
218 elif sys.platform == 'darwin':
221 elif sys.platform == 'darwin':
219 chain = [osx_clipboard_get, tkinter_clipboard_get]
222 chain = [osx_clipboard_get, tkinter_clipboard_get]
220 else:
223 else:
221 chain = [tkinter_clipboard_get]
224 chain = [tkinter_clipboard_get]
222 dispatcher = CommandChainDispatcher()
225 dispatcher = CommandChainDispatcher()
223 for func in chain:
226 for func in chain:
224 dispatcher.add(func)
227 dispatcher.add(func)
225 text = dispatcher()
228 text = dispatcher()
226 return text
229 return text
General Comments 0
You need to be logged in to leave comments. Login now