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