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