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