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