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