##// END OF EJS Templates
Remove generate_prompt from hooks' __all__ declaration.
Thomas Kluyver -
Show More
@@ -1,228 +1,228 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 need to put the
14 If you wish to define a new hook and activate it, you need to put the
15 necessary code into a python file which can be either imported or execfile()'d
15 necessary code into a python file which can be either imported or execfile()'d
16 from within your profile's ipython_config.py configuration.
16 from within your profile's ipython_config.py configuration.
17
17
18 For example, suppose that you have a module called 'myiphooks' in your
18 For example, suppose that you have a module called 'myiphooks' in your
19 PYTHONPATH, which contains the following definition:
19 PYTHONPATH, which contains the following definition:
20
20
21 import os
21 import os
22 from IPython.core import ipapi
22 from IPython.core import ipapi
23 ip = ipapi.get()
23 ip = ipapi.get()
24
24
25 def calljed(self,filename, linenum):
25 def calljed(self,filename, linenum):
26 "My editor hook calls the jed editor directly."
26 "My editor hook calls the jed editor directly."
27 print "Calling my own editor, jed ..."
27 print "Calling my own editor, jed ..."
28 if os.system('jed +%d %s' % (linenum,filename)) != 0:
28 if os.system('jed +%d %s' % (linenum,filename)) != 0:
29 raise TryNext()
29 raise TryNext()
30
30
31 ip.set_hook('editor', calljed)
31 ip.set_hook('editor', calljed)
32
32
33 You can then enable the functionality by doing 'import myiphooks'
33 You can then enable the functionality by doing 'import myiphooks'
34 somewhere in your configuration files or ipython command line.
34 somewhere in your configuration files or ipython command line.
35 """
35 """
36
36
37 #*****************************************************************************
37 #*****************************************************************************
38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
39 #
39 #
40 # Distributed under the terms of the BSD License. The full license is in
40 # Distributed under the terms of the BSD License. The full license is in
41 # the file COPYING, distributed as part of this software.
41 # the file COPYING, distributed as part of this software.
42 #*****************************************************************************
42 #*****************************************************************************
43
43
44 import os, bisect
44 import os, bisect
45 import sys
45 import sys
46
46
47 from IPython.core.error import TryNext
47 from IPython.core.error import TryNext
48
48
49 # List here all the default hooks. For now it's just the editor functions
49 # List here all the default hooks. For now it's just the editor functions
50 # but over time we'll move here all the public API for user-accessible things.
50 # but over time we'll move here all the public API for user-accessible things.
51
51
52 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
52 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
53 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
53 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
54 'generate_prompt', 'show_in_pager','pre_prompt_hook',
54 'show_in_pager','pre_prompt_hook',
55 'pre_run_code_hook', 'clipboard_get']
55 'pre_run_code_hook', 'clipboard_get']
56
56
57 def editor(self,filename, linenum=None):
57 def editor(self,filename, linenum=None):
58 """Open the default editor at the given filename and linenumber.
58 """Open the default editor at the given filename and linenumber.
59
59
60 This is IPython's default editor hook, you can use it as an example to
60 This is IPython's default editor hook, you can use it as an example to
61 write your own modified one. To set your own editor function as the
61 write your own modified one. To set your own editor function as the
62 new editor hook, call ip.set_hook('editor',yourfunc)."""
62 new editor hook, call ip.set_hook('editor',yourfunc)."""
63
63
64 # IPython configures a default editor at startup by reading $EDITOR from
64 # IPython configures a default editor at startup by reading $EDITOR from
65 # the environment, and falling back on vi (unix) or notepad (win32).
65 # the environment, and falling back on vi (unix) or notepad (win32).
66 editor = self.editor
66 editor = self.editor
67
67
68 # marker for at which line to open the file (for existing objects)
68 # marker for at which line to open the file (for existing objects)
69 if linenum is None or editor=='notepad':
69 if linenum is None or editor=='notepad':
70 linemark = ''
70 linemark = ''
71 else:
71 else:
72 linemark = '+%d' % int(linenum)
72 linemark = '+%d' % int(linenum)
73
73
74 # Enclose in quotes if necessary and legal
74 # Enclose in quotes if necessary and legal
75 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
75 if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
76 editor = '"%s"' % editor
76 editor = '"%s"' % editor
77
77
78 # Call the actual editor
78 # Call the actual editor
79 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
79 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
80 raise TryNext()
80 raise TryNext()
81
81
82 import tempfile
82 import tempfile
83 def fix_error_editor(self,filename,linenum,column,msg):
83 def fix_error_editor(self,filename,linenum,column,msg):
84 """Open the editor at the given filename, linenumber, column and
84 """Open the editor at the given filename, linenumber, column and
85 show an error message. This is used for correcting syntax errors.
85 show an error message. This is used for correcting syntax errors.
86 The current implementation only has special support for the VIM editor,
86 The current implementation only has special support for the VIM editor,
87 and falls back on the 'editor' hook if VIM is not used.
87 and falls back on the 'editor' hook if VIM is not used.
88
88
89 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
89 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
90 """
90 """
91 def vim_quickfix_file():
91 def vim_quickfix_file():
92 t = tempfile.NamedTemporaryFile()
92 t = tempfile.NamedTemporaryFile()
93 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
93 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
94 t.flush()
94 t.flush()
95 return t
95 return t
96 if os.path.basename(self.editor) != 'vim':
96 if os.path.basename(self.editor) != 'vim':
97 self.hooks.editor(filename,linenum)
97 self.hooks.editor(filename,linenum)
98 return
98 return
99 t = vim_quickfix_file()
99 t = vim_quickfix_file()
100 try:
100 try:
101 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
101 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
102 raise TryNext()
102 raise TryNext()
103 finally:
103 finally:
104 t.close()
104 t.close()
105
105
106
106
107 def synchronize_with_editor(self, filename, linenum, column):
107 def synchronize_with_editor(self, filename, linenum, column):
108 pass
108 pass
109
109
110
110
111 class CommandChainDispatcher:
111 class CommandChainDispatcher:
112 """ Dispatch calls to a chain of commands until some func can handle it
112 """ Dispatch calls to a chain of commands until some func can handle it
113
113
114 Usage: instantiate, execute "add" to add commands (with optional
114 Usage: instantiate, execute "add" to add commands (with optional
115 priority), execute normally via f() calling mechanism.
115 priority), execute normally via f() calling mechanism.
116
116
117 """
117 """
118 def __init__(self,commands=None):
118 def __init__(self,commands=None):
119 if commands is None:
119 if commands is None:
120 self.chain = []
120 self.chain = []
121 else:
121 else:
122 self.chain = commands
122 self.chain = commands
123
123
124
124
125 def __call__(self,*args, **kw):
125 def __call__(self,*args, **kw):
126 """ Command chain is called just like normal func.
126 """ Command chain is called just like normal func.
127
127
128 This will call all funcs in chain with the same args as were given to this
128 This will call all funcs in chain with the same args as were given to this
129 function, and return the result of first func that didn't raise
129 function, and return the result of first func that didn't raise
130 TryNext """
130 TryNext """
131
131
132 for prio,cmd in self.chain:
132 for prio,cmd in self.chain:
133 #print "prio",prio,"cmd",cmd #dbg
133 #print "prio",prio,"cmd",cmd #dbg
134 try:
134 try:
135 return cmd(*args, **kw)
135 return cmd(*args, **kw)
136 except TryNext, exc:
136 except TryNext, exc:
137 if exc.args or exc.kwargs:
137 if exc.args or exc.kwargs:
138 args = exc.args
138 args = exc.args
139 kw = exc.kwargs
139 kw = exc.kwargs
140 # if no function will accept it, raise TryNext up to the caller
140 # if no function will accept it, raise TryNext up to the caller
141 raise TryNext(*args, **kw)
141 raise TryNext(*args, **kw)
142
142
143 def __str__(self):
143 def __str__(self):
144 return str(self.chain)
144 return str(self.chain)
145
145
146 def add(self, func, priority=0):
146 def add(self, func, priority=0):
147 """ Add a func to the cmd chain with given priority """
147 """ Add a func to the cmd chain with given priority """
148 bisect.insort(self.chain,(priority,func))
148 bisect.insort(self.chain,(priority,func))
149
149
150 def __iter__(self):
150 def __iter__(self):
151 """ Return all objects in chain.
151 """ Return all objects in chain.
152
152
153 Handy if the objects are not callable.
153 Handy if the objects are not callable.
154 """
154 """
155 return iter(self.chain)
155 return iter(self.chain)
156
156
157
157
158 def input_prefilter(self,line):
158 def input_prefilter(self,line):
159 """ Default input prefilter
159 """ Default input prefilter
160
160
161 This returns the line as unchanged, so that the interpreter
161 This returns the line as unchanged, so that the interpreter
162 knows that nothing was done and proceeds with "classic" prefiltering
162 knows that nothing was done and proceeds with "classic" prefiltering
163 (%magics, !shell commands etc.).
163 (%magics, !shell commands etc.).
164
164
165 Note that leading whitespace is not passed to this hook. Prefilter
165 Note that leading whitespace is not passed to this hook. Prefilter
166 can't alter indentation.
166 can't alter indentation.
167
167
168 """
168 """
169 #print "attempt to rewrite",line #dbg
169 #print "attempt to rewrite",line #dbg
170 return line
170 return line
171
171
172
172
173 def shutdown_hook(self):
173 def shutdown_hook(self):
174 """ default shutdown hook
174 """ default shutdown hook
175
175
176 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
176 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
177 """
177 """
178
178
179 #print "default shutdown hook ok" # dbg
179 #print "default shutdown hook ok" # dbg
180 return
180 return
181
181
182
182
183 def late_startup_hook(self):
183 def late_startup_hook(self):
184 """ Executed after ipython has been constructed and configured
184 """ Executed after ipython has been constructed and configured
185
185
186 """
186 """
187 #print "default startup hook ok" # dbg
187 #print "default startup hook ok" # dbg
188
188
189
189
190 def show_in_pager(self,s):
190 def show_in_pager(self,s):
191 """ Run a string through pager """
191 """ Run a string through pager """
192 # raising TryNext here will use the default paging functionality
192 # raising TryNext here will use the default paging functionality
193 raise TryNext
193 raise TryNext
194
194
195
195
196 def pre_prompt_hook(self):
196 def pre_prompt_hook(self):
197 """ Run before displaying the next prompt
197 """ Run before displaying the next prompt
198
198
199 Use this e.g. to display output from asynchronous operations (in order
199 Use this e.g. to display output from asynchronous operations (in order
200 to not mess up text entry)
200 to not mess up text entry)
201 """
201 """
202
202
203 return None
203 return None
204
204
205
205
206 def pre_run_code_hook(self):
206 def pre_run_code_hook(self):
207 """ Executed before running the (prefiltered) code in IPython """
207 """ Executed before running the (prefiltered) code in IPython """
208 return None
208 return None
209
209
210
210
211 def clipboard_get(self):
211 def clipboard_get(self):
212 """ Get text from the clipboard.
212 """ Get text from the clipboard.
213 """
213 """
214 from IPython.lib.clipboard import (
214 from IPython.lib.clipboard import (
215 osx_clipboard_get, tkinter_clipboard_get,
215 osx_clipboard_get, tkinter_clipboard_get,
216 win32_clipboard_get
216 win32_clipboard_get
217 )
217 )
218 if sys.platform == 'win32':
218 if sys.platform == 'win32':
219 chain = [win32_clipboard_get, tkinter_clipboard_get]
219 chain = [win32_clipboard_get, tkinter_clipboard_get]
220 elif sys.platform == 'darwin':
220 elif sys.platform == 'darwin':
221 chain = [osx_clipboard_get, tkinter_clipboard_get]
221 chain = [osx_clipboard_get, tkinter_clipboard_get]
222 else:
222 else:
223 chain = [tkinter_clipboard_get]
223 chain = [tkinter_clipboard_get]
224 dispatcher = CommandChainDispatcher()
224 dispatcher = CommandChainDispatcher()
225 for func in chain:
225 for func in chain:
226 dispatcher.add(func)
226 dispatcher.add(func)
227 text = dispatcher()
227 text = dispatcher()
228 return text
228 return text
General Comments 0
You need to be logged in to leave comments. Login now