Show More
@@ -1,56 +1,56 | |||
|
1 | 1 | """ Utilities for accessing the platform's clipboard. |
|
2 | 2 | """ |
|
3 | 3 | |
|
4 | 4 | import subprocess |
|
5 | 5 | import sys |
|
6 | 6 | |
|
7 | 7 | from IPython.core.error import TryNext |
|
8 |
import IPython. |
|
|
8 | import IPython.utils.py3compat as py3compat | |
|
9 | 9 | |
|
10 | 10 | def win32_clipboard_get(): |
|
11 | 11 | """ Get the current clipboard's text on Windows. |
|
12 | 12 | |
|
13 | 13 | Requires Mark Hammond's pywin32 extensions. |
|
14 | 14 | """ |
|
15 | 15 | try: |
|
16 | 16 | import win32clipboard |
|
17 | 17 | except ImportError: |
|
18 | 18 | raise TryNext("Getting text from the clipboard requires the pywin32 " |
|
19 | 19 | "extensions: http://sourceforge.net/projects/pywin32/") |
|
20 | 20 | win32clipboard.OpenClipboard() |
|
21 | 21 | text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT) |
|
22 | 22 | # FIXME: convert \r\n to \n? |
|
23 | 23 | win32clipboard.CloseClipboard() |
|
24 | 24 | return text |
|
25 | 25 | |
|
26 | 26 | def osx_clipboard_get(): |
|
27 | 27 | """ Get the clipboard's text on OS X. |
|
28 | 28 | """ |
|
29 | 29 | p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'], |
|
30 | 30 | stdout=subprocess.PIPE) |
|
31 | 31 | text, stderr = p.communicate() |
|
32 | 32 | # Text comes in with old Mac \r line endings. Change them to \n. |
|
33 | 33 | text = text.replace('\r', '\n') |
|
34 | 34 | text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING) |
|
35 | 35 | return text |
|
36 | 36 | |
|
37 | 37 | def tkinter_clipboard_get(): |
|
38 | 38 | """ Get the clipboard's text using Tkinter. |
|
39 | 39 | |
|
40 | 40 | This is the default on systems that are not Windows or OS X. It may |
|
41 | 41 | interfere with other UI toolkits and should be replaced with an |
|
42 | 42 | implementation that uses that toolkit. |
|
43 | 43 | """ |
|
44 | 44 | try: |
|
45 | 45 | import Tkinter |
|
46 | 46 | except ImportError: |
|
47 | 47 | raise TryNext("Getting text from the clipboard on this platform " |
|
48 | 48 | "requires Tkinter.") |
|
49 | 49 | root = Tkinter.Tk() |
|
50 | 50 | root.withdraw() |
|
51 | 51 | text = root.clipboard_get() |
|
52 | 52 | root.destroy() |
|
53 | 53 | text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING) |
|
54 | 54 | return text |
|
55 | 55 | |
|
56 | 56 |
General Comments 0
You need to be logged in to leave comments.
Login now