##// END OF EJS Templates
cast clipboard_get strings on OSX and Linux to unicode
Jörgen Stenarson -
Show More
@@ -1,54 +1,56 b''
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
8 import IPython.lib.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 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
34 35 return text
35 36
36 37 def tkinter_clipboard_get():
37 38 """ Get the clipboard's text using Tkinter.
38 39
39 40 This is the default on systems that are not Windows or OS X. It may
40 41 interfere with other UI toolkits and should be replaced with an
41 42 implementation that uses that toolkit.
42 43 """
43 44 try:
44 45 import Tkinter
45 46 except ImportError:
46 47 raise TryNext("Getting text from the clipboard on this platform "
47 48 "requires Tkinter.")
48 49 root = Tkinter.Tk()
49 50 root.withdraw()
50 51 text = root.clipboard_get()
51 52 root.destroy()
53 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
52 54 return text
53 55
54 56
General Comments 0
You need to be logged in to leave comments. Login now