| 1 |
"""Hooks for IPython. |
| 2 |
|
| 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 |
| 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 |
| 7 |
overridden by the user. |
| 8 |
|
| 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 |
| 11 |
instance methods. The self argument will be the IPython running instance |
| 12 |
itself, so hooks have full access to the entire IPython object. |
| 13 |
|
| 14 |
If you wish to define a new hook and activate it, you can make an :doc:`extension |
| 15 |
</config/extensions/index>` or a :ref:`startup script <startup_files>`. For |
| 16 |
example, you could use a startup file like this:: |
| 17 |
|
| 18 |
import os |
| 19 |
|
| 20 |
def calljed(self,filename, linenum): |
| 21 |
"My editor hook calls the jed editor directly." |
| 22 |
print("Calling my own editor, jed ...") |
| 23 |
if os.system('jed +%d %s' % (linenum,filename)) != 0: |
| 24 |
raise TryNext() |
| 25 |
|
| 26 |
def load_ipython_extension(ip): |
| 27 |
ip.set_hook('editor', calljed) |
| 28 |
|
| 29 |
""" |
| 30 |
|
| 31 |
#***************************************************************************** |
| 32 |
# Copyright (C) 2005 Fernando Perez. <[email protected]> |
| 33 |
# |
| 34 |
# Distributed under the terms of the BSD License. The full license is in |
| 35 |
# the file COPYING, distributed as part of this software. |
| 36 |
#***************************************************************************** |
| 37 |
|
| 38 |
import os |
| 39 |
import subprocess |
| 40 |
import sys |
| 41 |
|
| 42 |
from .error import TryNext |
| 43 |
|
| 44 |
# List here all the default hooks. For now it's just the editor functions |
| 45 |
# but over time we'll move here all the public API for user-accessible things. |
| 46 |
|
| 47 |
__all__ = [ |
| 48 |
"editor", |
| 49 |
"synchronize_with_editor", |
| 50 |
"show_in_pager", |
| 51 |
"clipboard_get", |
| 52 |
] |
| 53 |
|
| 54 |
def editor(self, filename, linenum=None, wait=True): |
| 55 |
"""Open the default editor at the given filename and linenumber. |
| 56 |
|
| 57 |
This is IPython's default editor hook, you can use it as an example to |
| 58 |
write your own modified one. To set your own editor function as the |
| 59 |
new editor hook, call ip.set_hook('editor',yourfunc).""" |
| 60 |
|
| 61 |
# IPython configures a default editor at startup by reading $EDITOR from |
| 62 |
# the environment, and falling back on vi (unix) or notepad (win32). |
| 63 |
editor = self.editor |
| 64 |
|
| 65 |
# marker for at which line to open the file (for existing objects) |
| 66 |
if linenum is None or editor=='notepad': |
| 67 |
linemark = '' |
| 68 |
else: |
| 69 |
linemark = '+%d' % int(linenum) |
| 70 |
|
| 71 |
# Enclose in quotes if necessary and legal |
| 72 |
if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
| 73 |
editor = '"%s"' % editor |
| 74 |
|
| 75 |
# Call the actual editor |
| 76 |
proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename), |
| 77 |
shell=True) |
| 78 |
if wait and proc.wait() != 0: |
| 79 |
raise TryNext() |
| 80 |
|
| 81 |
|
| 82 |
def synchronize_with_editor(self, filename, linenum, column): |
| 83 |
pass |
| 84 |
|
| 85 |
|
| 86 |
class CommandChainDispatcher: |
| 87 |
""" Dispatch calls to a chain of commands until some func can handle it |
| 88 |
|
| 89 |
Usage: instantiate, execute "add" to add commands (with optional |
| 90 |
priority), execute normally via f() calling mechanism. |
| 91 |
|
| 92 |
""" |
| 93 |
def __init__(self,commands=None): |
| 94 |
if commands is None: |
| 95 |
self.chain = [] |
| 96 |
else: |
| 97 |
self.chain = commands |
| 98 |
|
| 99 |
|
| 100 |
def __call__(self,*args, **kw): |
| 101 |
""" Command chain is called just like normal func. |
| 102 |
|
| 103 |
This will call all funcs in chain with the same args as were given to |
| 104 |
this function, and return the result of first func that didn't raise |
| 105 |
TryNext""" |
| 106 |
last_exc = TryNext() |
| 107 |
for prio,cmd in self.chain: |
| 108 |
# print("prio",prio,"cmd",cmd) # dbg |
| 109 |
try: |
| 110 |
return cmd(*args, **kw) |
| 111 |
except TryNext as exc: |
| 112 |
last_exc = exc |
| 113 |
# if no function will accept it, raise TryNext up to the caller |
| 114 |
raise last_exc |
| 115 |
|
| 116 |
def __str__(self): |
| 117 |
return str(self.chain) |
| 118 |
|
| 119 |
def add(self, func, priority=0): |
| 120 |
""" Add a func to the cmd chain with given priority """ |
| 121 |
self.chain.append((priority, func)) |
| 122 |
self.chain.sort(key=lambda x: x[0]) |
| 123 |
|
| 124 |
def __iter__(self): |
| 125 |
""" Return all objects in chain. |
| 126 |
|
| 127 |
Handy if the objects are not callable. |
| 128 |
""" |
| 129 |
return iter(self.chain) |
| 130 |
|
| 131 |
|
| 132 |
def show_in_pager(self, data, start, screen_lines): |
| 133 |
""" Run a string through pager """ |
| 134 |
# raising TryNext here will use the default paging functionality |
| 135 |
raise TryNext |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
def clipboard_get(self): |
| 140 |
""" Get text from the clipboard. |
| 141 |
""" |
| 142 |
from ..lib.clipboard import ( |
| 143 |
osx_clipboard_get, |
| 144 |
tkinter_clipboard_get, |
| 145 |
win32_clipboard_get, |
| 146 |
wayland_clipboard_get, |
| 147 |
) |
| 148 |
if sys.platform == 'win32': |
| 149 |
chain = [win32_clipboard_get, tkinter_clipboard_get] |
| 150 |
elif sys.platform == 'darwin': |
| 151 |
chain = [osx_clipboard_get, tkinter_clipboard_get] |
| 152 |
else: |
| 153 |
chain = [wayland_clipboard_get, tkinter_clipboard_get] |
| 154 |
dispatcher = CommandChainDispatcher() |
| 155 |
for func in chain: |
| 156 |
dispatcher.add(func) |
| 157 |
text = dispatcher() |
| 158 |
return text |