Show More
@@ -1,92 +1,101 b'' | |||
|
1 | 1 | """ Utilities for accessing the platform's clipboard. |
|
2 | 2 | """ |
|
3 | ||
|
3 | import os | |
|
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 | 10 | class ClipboardEmpty(ValueError): |
|
10 | 11 | pass |
|
11 | 12 | |
|
13 | ||
|
12 | 14 | def win32_clipboard_get(): |
|
13 | 15 | """ Get the current clipboard's text on Windows. |
|
14 | 16 | |
|
15 | 17 | Requires Mark Hammond's pywin32 extensions. |
|
16 | 18 | """ |
|
17 | 19 | try: |
|
18 | 20 | import win32clipboard |
|
19 | 21 | except ImportError as e: |
|
20 | 22 | raise TryNext("Getting text from the clipboard requires the pywin32 " |
|
21 | 23 | "extensions: http://sourceforge.net/projects/pywin32/") from e |
|
22 | 24 | win32clipboard.OpenClipboard() |
|
23 | 25 | try: |
|
24 | 26 | text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) |
|
25 | 27 | except (TypeError, win32clipboard.error): |
|
26 | 28 | try: |
|
27 | 29 | text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT) |
|
28 | 30 | text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING) |
|
29 | 31 | except (TypeError, win32clipboard.error) as e: |
|
30 | 32 | raise ClipboardEmpty from e |
|
31 | 33 | finally: |
|
32 | 34 | win32clipboard.CloseClipboard() |
|
33 | 35 | return text |
|
34 | 36 | |
|
37 | ||
|
35 | 38 | def osx_clipboard_get() -> str: |
|
36 | 39 | """ Get the clipboard's text on OS X. |
|
37 | 40 | """ |
|
38 | 41 | p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'], |
|
39 | 42 | stdout=subprocess.PIPE) |
|
40 | 43 | bytes_, stderr = p.communicate() |
|
41 | 44 | # Text comes in with old Mac \r line endings. Change them to \n. |
|
42 | 45 | bytes_ = bytes_.replace(b'\r', b'\n') |
|
43 | 46 | text = py3compat.decode(bytes_) |
|
44 | 47 | return text |
|
45 | 48 | |
|
49 | ||
|
46 | 50 | def tkinter_clipboard_get(): |
|
47 | 51 | """ Get the clipboard's text using Tkinter. |
|
48 | 52 | |
|
49 | 53 | This is the default on systems that are not Windows or OS X. It may |
|
50 | 54 | interfere with other UI toolkits and should be replaced with an |
|
51 | 55 | implementation that uses that toolkit. |
|
52 | 56 | """ |
|
53 | 57 | try: |
|
54 | 58 | from tkinter import Tk, TclError |
|
55 | 59 | except ImportError as e: |
|
56 | 60 | raise TryNext("Getting text from the clipboard on this platform requires tkinter.") from e |
|
57 | 61 | |
|
58 | 62 | root = Tk() |
|
59 | 63 | root.withdraw() |
|
60 | 64 | try: |
|
61 | 65 | text = root.clipboard_get() |
|
62 | 66 | except TclError as e: |
|
63 | 67 | raise ClipboardEmpty from e |
|
64 | 68 | finally: |
|
65 | 69 | root.destroy() |
|
66 | 70 | text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING) |
|
67 | 71 | return text |
|
68 | 72 | |
|
69 | 73 | |
|
70 | 74 | def wayland_clipboard_get(): |
|
71 | 75 | """Get the clipboard's text under Wayland using wl-paste command. |
|
72 | 76 | |
|
73 | 77 | This requires Wayland and wl-clipboard installed and running. |
|
74 | 78 | """ |
|
75 | 79 | if os.environ.get("XDG_SESSION_TYPE") != "wayland": |
|
76 | 80 | raise TryNext("wayland is not detected") |
|
77 | 81 | |
|
78 | 82 | try: |
|
79 | 83 | with subprocess.Popen(["wl-paste"], stdout=subprocess.PIPE) as p: |
|
80 |
raw, |
|
|
84 | raw, err = p.communicate() | |
|
85 | if p.wait(): | |
|
86 | raise TryNext(err) | |
|
81 | 87 | except FileNotFoundError as e: |
|
82 |
raise |
|
|
88 | raise TryNext( | |
|
83 | 89 | "Getting text from the clipboard under Wayland requires the wl-clipboard " |
|
84 | 90 | "extension: https://github.com/bugaevc/wl-clipboard" |
|
85 | 91 | ) from e |
|
86 | 92 | |
|
93 | if not raw: | |
|
94 | raise ClipboardEmpty | |
|
95 | ||
|
87 | 96 | try: |
|
88 | 97 | text = py3compat.decode(raw) |
|
89 | 98 | except UnicodeDecodeError as e: |
|
90 | 99 | raise ClipboardEmpty from e |
|
91 | 100 | |
|
92 | 101 | return text |
General Comments 0
You need to be logged in to leave comments.
Login now