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