|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | +from .exceptions import PyperclipException |
| 4 | + |
| 5 | +EXCEPT_MSG = """ |
| 6 | + Pyperclip could not find a copy/paste mechanism for your system. |
| 7 | + For more information, please visit https://pyperclip.readthedocs.org """ |
| 8 | +PY2 = sys.version_info[0] == 2 |
| 9 | +text_type = unicode if PY2 else str |
| 10 | + |
| 11 | + |
| 12 | +def init_osx_clipboard(): |
| 13 | + def copy_osx(text): |
| 14 | + p = subprocess.Popen(['pbcopy', 'w'], |
| 15 | + stdin=subprocess.PIPE, close_fds=True) |
| 16 | + p.communicate(input=text.encode('utf-8')) |
| 17 | + |
| 18 | + def paste_osx(): |
| 19 | + p = subprocess.Popen(['pbpaste', 'r'], |
| 20 | + stdout=subprocess.PIPE, close_fds=True) |
| 21 | + stdout, stderr = p.communicate() |
| 22 | + return stdout.decode('utf-8') |
| 23 | + |
| 24 | + return copy_osx, paste_osx |
| 25 | + |
| 26 | + |
| 27 | +def init_gtk_clipboard(): |
| 28 | + import gtk |
| 29 | + |
| 30 | + def copy_gtk(text): |
| 31 | + global cb |
| 32 | + cb = gtk.Clipboard() |
| 33 | + cb.set_text(text) |
| 34 | + cb.store() |
| 35 | + |
| 36 | + def paste_gtk(): |
| 37 | + clipboardContents = gtk.Clipboard().wait_for_text() |
| 38 | + # for python 2, returns None if the clipboard is blank. |
| 39 | + if clipboardContents is None: |
| 40 | + return '' |
| 41 | + else: |
| 42 | + return clipboardContents |
| 43 | + |
| 44 | + return copy_gtk, paste_gtk |
| 45 | + |
| 46 | + |
| 47 | +def init_qt_clipboard(): |
| 48 | + # $DISPLAY should exist |
| 49 | + from PyQt4.QtGui import QApplication |
| 50 | + |
| 51 | + app = QApplication([]) |
| 52 | + |
| 53 | + def copy_qt(text): |
| 54 | + cb = app.clipboard() |
| 55 | + cb.setText(text) |
| 56 | + |
| 57 | + def paste_qt(): |
| 58 | + cb = app.clipboard() |
| 59 | + return text_type(cb.text()) |
| 60 | + |
| 61 | + return copy_qt, paste_qt |
| 62 | + |
| 63 | + |
| 64 | +def init_xclip_clipboard(): |
| 65 | + def copy_xclip(text): |
| 66 | + p = subprocess.Popen(['xclip', '-selection', 'c'], |
| 67 | + stdin=subprocess.PIPE, close_fds=True) |
| 68 | + p.communicate(input=text.encode('utf-8')) |
| 69 | + |
| 70 | + def paste_xclip(): |
| 71 | + p = subprocess.Popen(['xclip', '-selection', 'c', '-o'], |
| 72 | + stdout=subprocess.PIPE, close_fds=True) |
| 73 | + stdout, stderr = p.communicate() |
| 74 | + return stdout.decode('utf-8') |
| 75 | + |
| 76 | + return copy_xclip, paste_xclip |
| 77 | + |
| 78 | + |
| 79 | +def init_xsel_clipboard(): |
| 80 | + def copy_xsel(text): |
| 81 | + p = subprocess.Popen(['xsel', '-b', '-i'], |
| 82 | + stdin=subprocess.PIPE, close_fds=True) |
| 83 | + p.communicate(input=text.encode('utf-8')) |
| 84 | + |
| 85 | + def paste_xsel(): |
| 86 | + p = subprocess.Popen(['xsel', '-b', '-o'], |
| 87 | + stdout=subprocess.PIPE, close_fds=True) |
| 88 | + stdout, stderr = p.communicate() |
| 89 | + return stdout.decode('utf-8') |
| 90 | + |
| 91 | + return copy_xsel, paste_xsel |
| 92 | + |
| 93 | + |
| 94 | +def init_klipper_clipboard(): |
| 95 | + def copy_klipper(text): |
| 96 | + p = subprocess.Popen( |
| 97 | + ['qdbus', 'org.kde.klipper', '/klipper', 'setClipboardContents', |
| 98 | + text.encode('utf-8')], |
| 99 | + stdin=subprocess.PIPE, close_fds=True) |
| 100 | + p.communicate(input=None) |
| 101 | + |
| 102 | + def paste_klipper(): |
| 103 | + p = subprocess.Popen( |
| 104 | + ['qdbus', 'org.kde.klipper', '/klipper', 'getClipboardContents'], |
| 105 | + stdout=subprocess.PIPE, close_fds=True) |
| 106 | + stdout, stderr = p.communicate() |
| 107 | + |
| 108 | + # Workaround for https://bugs.kde.org/show_bug.cgi?id=342874 |
| 109 | + # TODO: https://github.com/asweigart/pyperclip/issues/43 |
| 110 | + clipboardContents = stdout.decode('utf-8') |
| 111 | + # even if blank, Klipper will append a newline at the end |
| 112 | + assert len(clipboardContents) > 0 |
| 113 | + # make sure that newline is there |
| 114 | + assert clipboardContents.endswith('\n') |
| 115 | + if clipboardContents.endswith('\n'): |
| 116 | + clipboardContents = clipboardContents[:-1] |
| 117 | + return clipboardContents |
| 118 | + |
| 119 | + return copy_klipper, paste_klipper |
| 120 | + |
| 121 | + |
| 122 | +def init_no_clipboard(): |
| 123 | + class ClipboardUnavailable(object): |
| 124 | + def __call__(self, *args, **kwargs): |
| 125 | + raise PyperclipException(EXCEPT_MSG) |
| 126 | + |
| 127 | + if PY2: |
| 128 | + def __nonzero__(self): |
| 129 | + return False |
| 130 | + else: |
| 131 | + def __bool__(self): |
| 132 | + return False |
| 133 | + |
| 134 | + return ClipboardUnavailable(), ClipboardUnavailable() |
0 commit comments