##// END OF EJS Templates
alternate notebook upload methods...
alternate notebook upload methods fixes #1562

File last commit:

r5500:8a974f53
r6838:334470c6
Show More
hooks.py
228 lines | 7.2 KiB | text/x-python | PythonLexer
Ville M. Vainio
crlf -> lf
r1032 """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
Erik Tollerud
purged ipythonrc from documentation and docstrings where no longer relevant
r4467 from within your profile's ipython_config.py configuration.
Ville M. Vainio
crlf -> lf
r1032
For example, suppose that you have a module called 'myiphooks' in your
PYTHONPATH, which contains the following definition:
import os
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 from IPython.core import ipapi
ip = ipapi.get()
Ville M. Vainio
crlf -> lf
r1032
def calljed(self,filename, linenum):
"My editor hook calls the jed editor directly."
print "Calling my own editor, jed ..."
Ville M. Vainio
TryNext editor if editor hook fails. Patch by Lukasz Pankowski
r1744 if os.system('jed +%d %s' % (linenum,filename)) != 0:
Brian Granger
Continuing a massive refactor of everything.
r2205 raise TryNext()
Ville M. Vainio
crlf -> lf
r1032
ip.set_hook('editor', calljed)
You can then enable the functionality by doing 'import myiphooks'
somewhere in your configuration files or ipython command line.
Fernando Perez
Remove svn-style $Id marks from docstrings and Release imports....
r1853 """
Ville M. Vainio
crlf -> lf
r1032
#*****************************************************************************
# 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.
#*****************************************************************************
Brian Granger
Merging -r 1181 from lp:ipython....
r2128 import os, bisect
Robert Kern
Add %paste magic for direct pasting from the clipboard.
r1841 import sys
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Brian Granger
Refactor of prompts and the displayhook....
r2781 from IPython.core.error import TryNext
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Ville M. Vainio
crlf -> lf
r1032 # 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.
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Brian Granger
Refactor of prompts and the displayhook....
r2781 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor',
Ville M. Vainio
crlf -> lf
r1032 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
Thomas Kluyver
Remove generate_prompt from hooks' __all__ declaration.
r5500 'show_in_pager','pre_prompt_hook',
Fernando Perez
Update many names to pep-8: savehist -> save_hist....
r3096 'pre_run_code_hook', 'clipboard_get']
Ville M. Vainio
crlf -> lf
r1032
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).
Brian Granger
Massive, crazy refactoring of everything....
r2202 editor = self.editor
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 # 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)
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 # Enclose in quotes if necessary and legal
if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
editor = '"%s"' % editor
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 # Call the actual editor
Ville M. Vainio
TryNext editor if editor hook fails. Patch by Lukasz Pankowski
r1744 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
Brian Granger
Continuing a massive refactor of everything.
r2205 raise TryNext()
Ville M. Vainio
crlf -> lf
r1032
import tempfile
def fix_error_editor(self,filename,linenum,column,msg):
Bernardo B. Marques
remove all trailling spaces
r4872 """Open the editor at the given filename, linenumber, column and
Ville M. Vainio
crlf -> lf
r1032 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
Brian Granger
Massive, crazy refactoring of everything....
r2202 if os.path.basename(self.editor) != 'vim':
Ville M. Vainio
crlf -> lf
r1032 self.hooks.editor(filename,linenum)
return
t = vim_quickfix_file()
try:
Ville M. Vainio
TryNext editor if editor hook fails. Patch by Lukasz Pankowski
r1744 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
Brian Granger
Continuing a massive refactor of everything.
r2205 raise TryNext()
Ville M. Vainio
crlf -> lf
r1032 finally:
t.close()
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
vds
synchronize with editor patch
r1241 def synchronize_with_editor(self, filename, linenum, column):
Fernando Perez
Cleanup check_sources and remove hard tabs from some files....
r2116 pass
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Ville M. Vainio
crlf -> lf
r1032
class CommandChainDispatcher:
""" Dispatch calls to a chain of commands until some func can handle it
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 Usage: instantiate, execute "add" to add commands (with optional
priority), execute normally via f() calling mechanism.
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 """
def __init__(self,commands=None):
if commands is None:
self.chain = []
else:
self.chain = commands
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 def __call__(self,*args, **kw):
Bernardo B. Marques
remove all trailling spaces
r4872 """ Command chain is called just like normal func.
Ville M. Vainio
crlf -> lf
r1032 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 """
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 for prio,cmd in self.chain:
#print "prio",prio,"cmd",cmd #dbg
try:
Fernando Perez
Fix quitting: now, typing bare 'exit' or 'quit' unconditionally quits....
r2370 return cmd(*args, **kw)
Brian Granger
Continuing a massive refactor of everything.
r2205 except TryNext, exc:
Ville M. Vainio
crlf -> lf
r1032 if exc.args or exc.kwargs:
args = exc.args
kw = exc.kwargs
# if no function will accept it, raise TryNext up to the caller
Matt Cottingham
Fix issue #880 - initialize TryNext with args and kwargs....
r5303 raise TryNext(*args, **kw)
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 def __str__(self):
return str(self.chain)
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 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.
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 Handy if the objects are not callable.
"""
return iter(self.chain)
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Bernardo B. Marques
remove all trailling spaces
r4872 def input_prefilter(self,line):
Ville M. Vainio
crlf -> lf
r1032 """ Default input prefilter
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 This returns the line as unchanged, so that the interpreter
knows that nothing was done and proceeds with "classic" prefiltering
Bernardo B. Marques
remove all trailling spaces
r4872 (%magics, !shell commands etc.).
Ville M. Vainio
crlf -> lf
r1032 Note that leading whitespace is not passed to this hook. Prefilter
can't alter indentation.
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 """
#print "attempt to rewrite",line #dbg
return line
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Ville M. Vainio
crlf -> lf
r1032 def shutdown_hook(self):
""" default shutdown hook
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
"""
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 #print "default shutdown hook ok" # dbg
return
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Ville M. Vainio
crlf -> lf
r1032 def late_startup_hook(self):
Bernardo B. Marques
remove all trailling spaces
r4872 """ Executed after ipython has been constructed and configured
Ville M. Vainio
crlf -> lf
r1032 """
#print "default startup hook ok" # dbg
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Ville M. Vainio
crlf -> lf
r1032 def show_in_pager(self,s):
""" Run a string through pager """
# raising TryNext here will use the default paging functionality
Brian Granger
Continuing a massive refactor of everything.
r2205 raise TryNext
Ville M. Vainio
crlf -> lf
r1032
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Ville M. Vainio
crlf -> lf
r1032 def pre_prompt_hook(self):
""" Run before displaying the next prompt
Bernardo B. Marques
remove all trailling spaces
r4872
Use this e.g. to display output from asynchronous operations (in order
to not mess up text entry)
Ville M. Vainio
crlf -> lf
r1032 """
Bernardo B. Marques
remove all trailling spaces
r4872
Ville M. Vainio
crlf -> lf
r1032 return None
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Fernando Perez
Update many names to pep-8: savehist -> save_hist....
r3096 def pre_run_code_hook(self):
vivainio2
implement pre_runcode_hook, make ipy_autoreload use that instead of monkeypatching
r1036 """ Executed before running the (prefiltered) code in IPython """
return None
Ville M. Vainio
crlf -> lf
r1032
Brian Granger
Cleaned up embedded shell and added cleanup method to InteractiveShell....
r2226
Robert Kern
Add %paste magic for direct pasting from the clipboard.
r1841 def clipboard_get(self):
""" Get text from the clipboard.
"""
Brian Granger
Merging -r 1181 from lp:ipython....
r2128 from IPython.lib.clipboard import (
Bernardo B. Marques
remove all trailling spaces
r4872 osx_clipboard_get, tkinter_clipboard_get,
Brian Granger
Merging -r 1181 from lp:ipython....
r2128 win32_clipboard_get
)
Robert Kern
Add %paste magic for direct pasting from the clipboard.
r1841 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