Show More
@@ -1,229 +1,190 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 | import warnings | |
|
41 | 40 | import sys |
|
42 | 41 | |
|
43 | 42 | from .error import TryNext |
|
44 | 43 | |
|
45 | 44 | # List here all the default hooks. For now it's just the editor functions |
|
46 | 45 | # but over time we'll move here all the public API for user-accessible things. |
|
47 | 46 | |
|
48 | 47 | __all__ = ['editor', 'synchronize_with_editor', |
|
49 | 48 | 'shutdown_hook', 'late_startup_hook', |
|
50 | 49 | 'show_in_pager','pre_prompt_hook', |
|
51 | 50 | 'pre_run_code_hook', 'clipboard_get'] |
|
52 | 51 | |
|
53 | 52 | deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event", |
|
54 | 53 | 'late_startup_hook': "a callback for the 'shell_initialized' event", |
|
55 | 54 | 'shutdown_hook': "the atexit module", |
|
56 | 55 | } |
|
57 | 56 | |
|
58 | 57 | def editor(self, filename, linenum=None, wait=True): |
|
59 | 58 | """Open the default editor at the given filename and linenumber. |
|
60 | 59 | |
|
61 | 60 | This is IPython's default editor hook, you can use it as an example to |
|
62 | 61 | write your own modified one. To set your own editor function as the |
|
63 | 62 | new editor hook, call ip.set_hook('editor',yourfunc).""" |
|
64 | 63 | |
|
65 | 64 | # IPython configures a default editor at startup by reading $EDITOR from |
|
66 | 65 | # the environment, and falling back on vi (unix) or notepad (win32). |
|
67 | 66 | editor = self.editor |
|
68 | 67 | |
|
69 | 68 | # marker for at which line to open the file (for existing objects) |
|
70 | 69 | if linenum is None or editor=='notepad': |
|
71 | 70 | linemark = '' |
|
72 | 71 | else: |
|
73 | 72 | linemark = '+%d' % int(linenum) |
|
74 | 73 | |
|
75 | 74 | # Enclose in quotes if necessary and legal |
|
76 | 75 | if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
|
77 | 76 | editor = '"%s"' % editor |
|
78 | 77 | |
|
79 | 78 | # Call the actual editor |
|
80 | 79 | proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename), |
|
81 | 80 | shell=True) |
|
82 | 81 | if wait and proc.wait() != 0: |
|
83 | 82 | raise TryNext() |
|
84 | 83 | |
|
85 | import tempfile | |
|
86 | from ..utils.decorators import undoc | |
|
87 | ||
|
88 | @undoc | |
|
89 | def fix_error_editor(self,filename,linenum,column,msg): | |
|
90 | """DEPRECATED | |
|
91 | ||
|
92 | Open the editor at the given filename, linenumber, column and | |
|
93 | show an error message. This is used for correcting syntax errors. | |
|
94 | The current implementation only has special support for the VIM editor, | |
|
95 | and falls back on the 'editor' hook if VIM is not used. | |
|
96 | ||
|
97 | Call ip.set_hook('fix_error_editor',yourfunc) to use your own function, | |
|
98 | """ | |
|
99 | ||
|
100 | warnings.warn(""" | |
|
101 | `fix_error_editor` is deprecated as of IPython 6.0 and will be removed | |
|
102 | in future versions. It appears to be used only for automatically fixing syntax | |
|
103 | error that has been broken for a few years and has thus been removed. If you | |
|
104 | happened to use this function and still need it please make your voice heard on | |
|
105 | the mailing list ipython-dev@python.org , or on the GitHub Issue tracker: | |
|
106 | https://github.com/ipython/ipython/issues/9649 """, UserWarning) | |
|
107 | ||
|
108 | def vim_quickfix_file(): | |
|
109 | t = tempfile.NamedTemporaryFile() | |
|
110 | t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg)) | |
|
111 | t.flush() | |
|
112 | return t | |
|
113 | if os.path.basename(self.editor) != 'vim': | |
|
114 | self.hooks.editor(filename,linenum) | |
|
115 | return | |
|
116 | t = vim_quickfix_file() | |
|
117 | try: | |
|
118 | if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name): | |
|
119 | raise TryNext() | |
|
120 | finally: | |
|
121 | t.close() | |
|
122 | ||
|
123 | 84 | |
|
124 | 85 | def synchronize_with_editor(self, filename, linenum, column): |
|
125 | 86 | pass |
|
126 | 87 | |
|
127 | 88 | |
|
128 | 89 | class CommandChainDispatcher: |
|
129 | 90 | """ Dispatch calls to a chain of commands until some func can handle it |
|
130 | 91 | |
|
131 | 92 | Usage: instantiate, execute "add" to add commands (with optional |
|
132 | 93 | priority), execute normally via f() calling mechanism. |
|
133 | 94 | |
|
134 | 95 | """ |
|
135 | 96 | def __init__(self,commands=None): |
|
136 | 97 | if commands is None: |
|
137 | 98 | self.chain = [] |
|
138 | 99 | else: |
|
139 | 100 | self.chain = commands |
|
140 | 101 | |
|
141 | 102 | |
|
142 | 103 | def __call__(self,*args, **kw): |
|
143 | 104 | """ Command chain is called just like normal func. |
|
144 | 105 | |
|
145 | 106 | This will call all funcs in chain with the same args as were given to |
|
146 | 107 | this function, and return the result of first func that didn't raise |
|
147 | 108 | TryNext""" |
|
148 | 109 | last_exc = TryNext() |
|
149 | 110 | for prio,cmd in self.chain: |
|
150 | 111 | #print "prio",prio,"cmd",cmd #dbg |
|
151 | 112 | try: |
|
152 | 113 | return cmd(*args, **kw) |
|
153 | 114 | except TryNext as exc: |
|
154 | 115 | last_exc = exc |
|
155 | 116 | # if no function will accept it, raise TryNext up to the caller |
|
156 | 117 | raise last_exc |
|
157 | 118 | |
|
158 | 119 | def __str__(self): |
|
159 | 120 | return str(self.chain) |
|
160 | 121 | |
|
161 | 122 | def add(self, func, priority=0): |
|
162 | 123 | """ Add a func to the cmd chain with given priority """ |
|
163 | 124 | self.chain.append((priority, func)) |
|
164 | 125 | self.chain.sort(key=lambda x: x[0]) |
|
165 | 126 | |
|
166 | 127 | def __iter__(self): |
|
167 | 128 | """ Return all objects in chain. |
|
168 | 129 | |
|
169 | 130 | Handy if the objects are not callable. |
|
170 | 131 | """ |
|
171 | 132 | return iter(self.chain) |
|
172 | 133 | |
|
173 | 134 | |
|
174 | 135 | def shutdown_hook(self): |
|
175 | 136 | """ default shutdown hook |
|
176 | 137 | |
|
177 | 138 | Typically, shutdown hooks should raise TryNext so all shutdown ops are done |
|
178 | 139 | """ |
|
179 | 140 | |
|
180 | 141 | #print "default shutdown hook ok" # dbg |
|
181 | 142 | return |
|
182 | 143 | |
|
183 | 144 | |
|
184 | 145 | def late_startup_hook(self): |
|
185 | 146 | """ Executed after ipython has been constructed and configured |
|
186 | 147 | |
|
187 | 148 | """ |
|
188 | 149 | #print "default startup hook ok" # dbg |
|
189 | 150 | |
|
190 | 151 | |
|
191 | 152 | def show_in_pager(self, data, start, screen_lines): |
|
192 | 153 | """ Run a string through pager """ |
|
193 | 154 | # raising TryNext here will use the default paging functionality |
|
194 | 155 | raise TryNext |
|
195 | 156 | |
|
196 | 157 | |
|
197 | 158 | def pre_prompt_hook(self): |
|
198 | 159 | """ Run before displaying the next prompt |
|
199 | 160 | |
|
200 | 161 | Use this e.g. to display output from asynchronous operations (in order |
|
201 | 162 | to not mess up text entry) |
|
202 | 163 | """ |
|
203 | 164 | |
|
204 | 165 | return None |
|
205 | 166 | |
|
206 | 167 | |
|
207 | 168 | def pre_run_code_hook(self): |
|
208 | 169 | """ Executed before running the (prefiltered) code in IPython """ |
|
209 | 170 | return None |
|
210 | 171 | |
|
211 | 172 | |
|
212 | 173 | def clipboard_get(self): |
|
213 | 174 | """ Get text from the clipboard. |
|
214 | 175 | """ |
|
215 | 176 | from ..lib.clipboard import ( |
|
216 | 177 | osx_clipboard_get, tkinter_clipboard_get, |
|
217 | 178 | win32_clipboard_get |
|
218 | 179 | ) |
|
219 | 180 | if sys.platform == 'win32': |
|
220 | 181 | chain = [win32_clipboard_get, tkinter_clipboard_get] |
|
221 | 182 | elif sys.platform == 'darwin': |
|
222 | 183 | chain = [osx_clipboard_get, tkinter_clipboard_get] |
|
223 | 184 | else: |
|
224 | 185 | chain = [tkinter_clipboard_get] |
|
225 | 186 | dispatcher = CommandChainDispatcher() |
|
226 | 187 | for func in chain: |
|
227 | 188 | dispatcher.add(func) |
|
228 | 189 | text = dispatcher() |
|
229 | 190 | return text |
General Comments 0
You need to be logged in to leave comments.
Login now