Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/IPython/hooks.py
blob: 6e0707930921b4d89e121ba16903c2efe50fa93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""hooks for IPython.

In Python, it is possible to overwrite any method of any object if you really
want to.  But IPython exposes a few 'hooks', methods which are _designed_ to
be overwritten by users for customization purposes.  This module defines the
default versions of all such hooks, which get used by IPython if not
overridden by the user.

hooks are simple functions, but they should be declared with 'self' as their
first argument, because when activated they are registered into IPython as
instance methods.  The self argument will be the IPython running instance
itself, so hooks have full access to the entire IPython object.

If you wish to define a new hook and activate it, you need to put the
necessary code into a python file which can be either imported or execfile()'d
from within your ipythonrc configuration.

For example, suppose that you have a module called 'myiphooks' in your
PYTHONPATH, which contains the following definition:

import os
import IPython.ipapi
ip = IPython.ipapi.get()

def calljed(self,filename, linenum):
    "My editor hook calls the jed editor directly."
    print "Calling my own editor, jed ..."
    if os.system('jed +%d %s' % (linenum,filename)) != 0:
        raise ipapi.TryNext()

ip.set_hook('editor', calljed)

You can then enable the functionality by doing 'import myiphooks'
somewhere in your configuration files or ipython command line.
"""

#*****************************************************************************
#       Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#*****************************************************************************

from IPython import ipapi

import os,bisect
import sys
from genutils import Term,shell
from pprint import PrettyPrinter

# List here all the default hooks.  For now it's just the editor functions
# but over time we'll move here all the public API for user-accessible things.
# vds: >>
__all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor', 'result_display',
           'input_prefilter', 'shutdown_hook', 'late_startup_hook',
           'generate_prompt', 'generate_output_prompt','shell_hook',
           'show_in_pager','pre_prompt_hook', 'pre_runcode_hook',
           'clipboard_get']
# vds: <<

pformat = PrettyPrinter().pformat

def editor(self,filename, linenum=None):
    """Open the default editor at the given filename and linenumber.

    This is IPython's default editor hook, you can use it as an example to
    write your own modified one.  To set your own editor function as the
    new editor hook, call ip.set_hook('editor',yourfunc)."""

    # IPython configures a default editor at startup by reading $EDITOR from
    # the environment, and falling back on vi (unix) or notepad (win32).
    editor = self.rc.editor
    
    # marker for at which line to open the file (for existing objects)
    if linenum is None or editor=='notepad':
        linemark = ''
    else:
        linemark = '+%d' % int(linenum)
    
    # Enclose in quotes if necessary and legal
    if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
        editor = '"%s"' % editor
        
    # Call the actual editor
    if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
        raise ipapi.TryNext()

import tempfile
def fix_error_editor(self,filename,linenum,column,msg):
    """Open the editor at the given filename, linenumber, column and 
    show an error message. This is used for correcting syntax errors.
    The current implementation only has special support for the VIM editor,
    and falls back on the 'editor' hook if VIM is not used.

    Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
    """
    def vim_quickfix_file():
        t = tempfile.NamedTemporaryFile()
        t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
        t.flush()
        return t
    if os.path.basename(self.rc.editor) != 'vim':
        self.hooks.editor(filename,linenum)
        return
    t = vim_quickfix_file()
    try:
        if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
            raise ipapi.TryNext()
    finally:
        t.close()

# vds: >>
def synchronize_with_editor(self, filename, linenum, column):
        pass
# vds: <<

class CommandChainDispatcher:
    """ Dispatch calls to a chain of commands until some func can handle it
    
    Usage: instantiate, execute "add" to add commands (with optional
    priority), execute normally via f() calling mechanism.
    
    """
    def __init__(self,commands=None):
        if commands is None:
            self.chain = []
        else:
            self.chain = commands
            
            
    def __call__(self,*args, **kw):
        """ Command chain is called just like normal func. 
        
        This will call all funcs in chain with the same args as were given to this
        function, and return the result of first func that didn't raise
        TryNext """
        
        for prio,cmd in self.chain:
            #print "prio",prio,"cmd",cmd #dbg
            try:
                ret = cmd(*args, **kw)
                return ret
            except ipapi.TryNext, exc:
                if exc.args or exc.kwargs:
                    args = exc.args
                    kw = exc.kwargs
        # if no function will accept it, raise TryNext up to the caller
        raise ipapi.TryNext
                
    def __str__(self):
        return str(self.chain)
    
    def add(self, func, priority=0):
        """ Add a func to the cmd chain with given priority """
        bisect.insort(self.chain,(priority,func))

    def __iter__(self):
        """ Return all objects in chain.
        
        Handy if the objects are not callable.
        """
        return iter(self.chain)
    
def result_display(self,arg):
    """ Default display hook.
    
    Called for displaying the result to the user.
    """
    
    if self.rc.pprint:
        out = pformat(arg)
        if '\n' in out:
            # So that multi-line strings line up with the left column of
            # the screen, instead of having the output prompt mess up
            # their first line.                
            Term.cout.write('\n')
        print >>Term.cout, out
    else:
        # By default, the interactive prompt uses repr() to display results,
        # so we should honor this.  Users who'd rather use a different
        # mechanism can easily override this hook.
        print >>Term.cout, repr(arg)
    # the default display hook doesn't manipulate the value to put in history
    return None 

def input_prefilter(self,line):     
    """ Default input prefilter
    
    This returns the line as unchanged, so that the interpreter
    knows that nothing was done and proceeds with "classic" prefiltering
    (%magics, !shell commands etc.). 
    
    Note that leading whitespace is not passed to this hook. Prefilter
    can't alter indentation.
    
    """
    #print "attempt to rewrite",line #dbg
    return line

def shutdown_hook(self):
    """ default shutdown hook
    
    Typically, shotdown hooks should raise TryNext so all shutdown ops are done
    """
    
    #print "default shutdown hook ok" # dbg
    return

def late_startup_hook(self):
    """ Executed after ipython has been constructed and configured 
    
    """
    #print "default startup hook ok" # dbg

def generate_prompt(self, is_continuation):
    """ calculate and return a string with the prompt to display """
    ip = self.api
    if is_continuation:
        return str(ip.IP.outputcache.prompt2)
    return str(ip.IP.outputcache.prompt1)

def generate_output_prompt(self):
    ip = self.api
    return str(ip.IP.outputcache.prompt_out)

def shell_hook(self,cmd):
    """ Run system/shell command a'la os.system() """

    shell(cmd, header=self.rc.system_header, verbose=self.rc.system_verbose)

def show_in_pager(self,s):
    """ Run a string through pager """
    # raising TryNext here will use the default paging functionality
    raise ipapi.TryNext

def pre_prompt_hook(self):
    """ Run before displaying the next prompt
    
    Use this e.g. to display output from asynchronous operations (in order 
    to not mess up text entry)   
    """
    
    return None

def pre_runcode_hook(self):
    """ Executed before running the (prefiltered) code in IPython """
    return None

def clipboard_get(self):
    """ Get text from the clipboard.
    """
    from IPython.clipboard import (osx_clipboard_get, tkinter_clipboard_get, 
        win32_clipboard_get)
    if sys.platform == 'win32':
        chain = [win32_clipboard_get, tkinter_clipboard_get]
    elif sys.platform == 'darwin':
        chain = [osx_clipboard_get, tkinter_clipboard_get]
    else:
        chain = [tkinter_clipboard_get]
    dispatcher = CommandChainDispatcher()
    for func in chain:
        dispatcher.add(func)
    text = dispatcher()
    return text