Show More
@@ -1,171 +1,173 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 | 40 | import sys |
|
41 | 41 | |
|
42 | 42 | from .error import TryNext |
|
43 | 43 | |
|
44 | 44 | # List here all the default hooks. For now it's just the editor functions |
|
45 | 45 | # but over time we'll move here all the public API for user-accessible things. |
|
46 | 46 | |
|
47 | 47 | __all__ = [ |
|
48 | 48 | "editor", |
|
49 | 49 | "synchronize_with_editor", |
|
50 | 50 | "show_in_pager", |
|
51 | 51 | "pre_prompt_hook", |
|
52 | 52 | "clipboard_get", |
|
53 | 53 | ] |
|
54 | 54 | |
|
55 | 55 | deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event", |
|
56 | 56 | 'late_startup_hook': "a callback for the 'shell_initialized' event", |
|
57 | 57 | 'shutdown_hook': "the atexit module", |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | def editor(self, filename, linenum=None, wait=True): |
|
61 | 61 | """Open the default editor at the given filename and linenumber. |
|
62 | 62 | |
|
63 | 63 | This is IPython's default editor hook, you can use it as an example to |
|
64 | 64 | write your own modified one. To set your own editor function as the |
|
65 | 65 | new editor hook, call ip.set_hook('editor',yourfunc).""" |
|
66 | 66 | |
|
67 | 67 | # IPython configures a default editor at startup by reading $EDITOR from |
|
68 | 68 | # the environment, and falling back on vi (unix) or notepad (win32). |
|
69 | 69 | editor = self.editor |
|
70 | 70 | |
|
71 | 71 | # marker for at which line to open the file (for existing objects) |
|
72 | 72 | if linenum is None or editor=='notepad': |
|
73 | 73 | linemark = '' |
|
74 | 74 | else: |
|
75 | 75 | linemark = '+%d' % int(linenum) |
|
76 | 76 | |
|
77 | 77 | # Enclose in quotes if necessary and legal |
|
78 | 78 | if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
|
79 | 79 | editor = '"%s"' % editor |
|
80 | 80 | |
|
81 | 81 | # Call the actual editor |
|
82 | 82 | proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename), |
|
83 | 83 | shell=True) |
|
84 | 84 | if wait and proc.wait() != 0: |
|
85 | 85 | raise TryNext() |
|
86 | 86 | |
|
87 | 87 | |
|
88 | 88 | def synchronize_with_editor(self, filename, linenum, column): |
|
89 | 89 | pass |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | class CommandChainDispatcher: |
|
93 | 93 | """ Dispatch calls to a chain of commands until some func can handle it |
|
94 | 94 | |
|
95 | 95 | Usage: instantiate, execute "add" to add commands (with optional |
|
96 | 96 | priority), execute normally via f() calling mechanism. |
|
97 | 97 | |
|
98 | 98 | """ |
|
99 | 99 | def __init__(self,commands=None): |
|
100 | 100 | if commands is None: |
|
101 | 101 | self.chain = [] |
|
102 | 102 | else: |
|
103 | 103 | self.chain = commands |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | def __call__(self,*args, **kw): |
|
107 | 107 | """ Command chain is called just like normal func. |
|
108 | 108 | |
|
109 | 109 | This will call all funcs in chain with the same args as were given to |
|
110 | 110 | this function, and return the result of first func that didn't raise |
|
111 | 111 | TryNext""" |
|
112 | 112 | last_exc = TryNext() |
|
113 | 113 | for prio,cmd in self.chain: |
|
114 | 114 | #print "prio",prio,"cmd",cmd #dbg |
|
115 | 115 | try: |
|
116 | 116 | return cmd(*args, **kw) |
|
117 | 117 | except TryNext as exc: |
|
118 | 118 | last_exc = exc |
|
119 | 119 | # if no function will accept it, raise TryNext up to the caller |
|
120 | 120 | raise last_exc |
|
121 | 121 | |
|
122 | 122 | def __str__(self): |
|
123 | 123 | return str(self.chain) |
|
124 | 124 | |
|
125 | 125 | def add(self, func, priority=0): |
|
126 | 126 | """ Add a func to the cmd chain with given priority """ |
|
127 | 127 | self.chain.append((priority, func)) |
|
128 | 128 | self.chain.sort(key=lambda x: x[0]) |
|
129 | 129 | |
|
130 | 130 | def __iter__(self): |
|
131 | 131 | """ Return all objects in chain. |
|
132 | 132 | |
|
133 | 133 | Handy if the objects are not callable. |
|
134 | 134 | """ |
|
135 | 135 | return iter(self.chain) |
|
136 | 136 | |
|
137 | 137 | |
|
138 | 138 | def show_in_pager(self, data, start, screen_lines): |
|
139 | 139 | """ Run a string through pager """ |
|
140 | 140 | # raising TryNext here will use the default paging functionality |
|
141 | 141 | raise TryNext |
|
142 | 142 | |
|
143 | 143 | |
|
144 | 144 | def pre_prompt_hook(self): |
|
145 | 145 | """ Run before displaying the next prompt |
|
146 | 146 | |
|
147 | 147 | Use this e.g. to display output from asynchronous operations (in order |
|
148 | 148 | to not mess up text entry) |
|
149 | 149 | """ |
|
150 | 150 | |
|
151 | 151 | return None |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | def clipboard_get(self): |
|
155 | 155 | """ Get text from the clipboard. |
|
156 | 156 | """ |
|
157 | 157 | from ..lib.clipboard import ( |
|
158 |
osx_clipboard_get, |
|
|
159 |
|
|
|
158 | osx_clipboard_get, | |
|
159 | tkinter_clipboard_get, | |
|
160 | win32_clipboard_get, | |
|
161 | wayland_clipboard_get, | |
|
160 | 162 | ) |
|
161 | 163 | if sys.platform == 'win32': |
|
162 | 164 | chain = [win32_clipboard_get, tkinter_clipboard_get] |
|
163 | 165 | elif sys.platform == 'darwin': |
|
164 | 166 | chain = [osx_clipboard_get, tkinter_clipboard_get] |
|
165 | 167 | else: |
|
166 | 168 | chain = [wayland_clipboard_get, tkinter_clipboard_get] |
|
167 | 169 | dispatcher = CommandChainDispatcher() |
|
168 | 170 | for func in chain: |
|
169 | 171 | dispatcher.add(func) |
|
170 | 172 | text = dispatcher() |
|
171 | 173 | return text |
General Comments 0
You need to be logged in to leave comments.
Login now