<commit_before>import sys
import subprocess
from .exceptions import PyperclipException

PY2 = sys.version_info[0] == 2


def init_osx_clipboard():
    def copy_osx(text):
        p = subprocess.Popen(['pbcopy', 'w'], stdin=subprocess.PIPE, close_fds=True)
        p.communicate(input=text.encode('utf-8'))

    def paste_osx():
        p = subprocess.Popen(['pbpaste', 'r'], stdout=subprocess.PIPE, close_fds=True)
        stdout, stderr = p.communicate()
        return stdout.decode('utf-8')

    return copy_osx, paste_osx


def init_gtk_clipboard():
    import gtk

    def copy_gtk(text):
        global cb
        cb = gtk.Clipboard()
        cb.set_text(text)
        cb.store()

    def paste_gtk():
        clipboardContents = gtk.Clipboard().wait_for_text()  # for python 2, returns None if the clipboard is blank.
        if clipboardContents is None:
            return ''
        else:
            return clipboardContents

    return copy_gtk, paste_gtk


def init_qt_clipboard():
    # We must not import PyQt4 at the top,
    # because `import PyQt4` sys.exit()s if $DISPLAY is not set on unix systems.
    import PyQt4.QtGui

    # FIXME: This segfaults on one of my systems?
    app = PyQt4.QtGui.QApplication([])  # TODO: is this required?
    cb = PyQt4.QtGui.QApplication.clipboard()

    def copy_qt(text):
        cb.setText(text)

    def paste_qt():
        return str(cb.text())  # TODO: should str() be replaced with STRING_FUNCTION()?

    return copy_qt, paste_qt


def init_xclip_clipboard():
    def copy_xclip(text):
        p = subprocess.Popen(['xclip', '-selection', 'c'], stdin=subprocess.PIPE, close_fds=True)
        p.communicate(input=text.encode('utf-8'))

    def paste_xclip():
        p = subprocess.Popen(['xclip', '-selection', 'c', '-o'], stdout=subprocess.PIPE, close_fds=True)
        stdout, stderr = p.communicate()
        return stdout.decode('utf-8')

    return copy_xclip, paste_xclip


def init_xsel_clipboard():
    def copy_xsel(text):
        p = subprocess.Popen(['xsel', '-b', '-i'], stdin=subprocess.PIPE, close_fds=True)
        p.communicate(input=text.encode('utf-8'))

    def paste_xsel():
        p = subprocess.Popen(['xsel', '-b', '-o'], stdout=subprocess.PIPE, close_fds=True)
        stdout, stderr = p.communicate()
        return stdout.decode('utf-8')

    return copy_xsel, paste_xsel


def init_klipper_clipboard():
    def copy_klipper(text):
        p = subprocess.Popen(
            ['qdbus', 'org.kde.klipper', '/klipper', 'setClipboardContents', text.encode('utf-8')],
            stdin=subprocess.PIPE,
            close_fds=True
        )
        p.communicate(input=None)

    def paste_klipper():
        p = subprocess.Popen(
            ['qdbus', 'org.kde.klipper', '/klipper', 'getClipboardContents'],
            stdout=subprocess.PIPE,
            close_fds=True
        )
        stdout, stderr = p.communicate()

        # Apparently Klipper has a bug that adds a newline to the end. It was reported in Klipper version 0.20.3 but I've
        # seen it in 0.9.7. The bug is unfixed. This function will remove a newline if the string has one.
        # TODO: In the future, once Klipper is fixed, check the version number to decided whether or not to strip the
        # newline.
        # https://bugs.kde.org/show_bug.cgi?id=342874

        clipboardContents = stdout.decode('utf-8')
        assert len(clipboardContents) > 0  # even if blank, Klipper will append a newline at the end
        assert clipboardContents[-1] == '\n'  # make sure that newline is there
        if len(clipboardContents) and clipboardContents[-1] == '\n':
            clipboardContents = clipboardContents[:-1]
        return clipboardContents

    return copy_klipper, paste_klipper

<commit_msg>Refactor clipboards to use endswith() in the klipper function. Code from drakulavich. Thanks Anton!<commit_after>import sys
import subprocess
from .exceptions import PyperclipException

PY2 = sys.version_info[0] == 2


def init_osx_clipboard():
    def copy_osx(text):
        p = subprocess.Popen(['pbcopy', 'w'], stdin=subprocess.PIPE, close_fds=True)
        p.communicate(input=text.encode('utf-8'))

    def paste_osx():
        p = subprocess.Popen(['pbpaste', 'r'], stdout=subprocess.PIPE, close_fds=True)
        stdout, stderr = p.communicate()
        return stdout.decode('utf-8')

    return copy_osx, paste_osx


def init_gtk_clipboard():
    import gtk

    def copy_gtk(text):
        global cb
        cb = gtk.Clipboard()
        cb.set_text(text)
        cb.store()

    def paste_gtk():
        clipboardContents = gtk.Clipboard().wait_for_text()  # for python 2, returns None if the clipboard is blank.
        if clipboardContents is None:
            return ''
        else:
            return clipboardContents

    return copy_gtk, paste_gtk


def init_qt_clipboard():
    # We must not import PyQt4 at the top,
    # because `import PyQt4` sys.exit()s if $DISPLAY is not set on unix systems.
    import PyQt4.QtGui

    # FIXME: This segfaults on one of my systems?
    app = PyQt4.QtGui.QApplication([])  # TODO: is this required?
    cb = PyQt4.QtGui.QApplication.clipboard()

    def copy_qt(text):
        cb.setText(text)

    def paste_qt():
        return str(cb.text())  # TODO: should str() be replaced with STRING_FUNCTION()?

    return copy_qt, paste_qt


def init_xclip_clipboard():
    def copy_xclip(text):
        p = subprocess.Popen(['xclip', '-selection', 'c'], stdin=subprocess.PIPE, close_fds=True)
        p.communicate(input=text.encode('utf-8'))

    def paste_xclip():
        p = subprocess.Popen(['xclip', '-selection', 'c', '-o'], stdout=subprocess.PIPE, close_fds=True)
        stdout, stderr = p.communicate()
        return stdout.decode('utf-8')

    return copy_xclip, paste_xclip


def init_xsel_clipboard():
    def copy_xsel(text):
        p = subprocess.Popen(['xsel', '-b', '-i'], stdin=subprocess.PIPE, close_fds=True)
        p.communicate(input=text.encode('utf-8'))

    def paste_xsel():
        p = subprocess.Popen(['xsel', '-b', '-o'], stdout=subprocess.PIPE, close_fds=True)
        stdout, stderr = p.communicate()
        return stdout.decode('utf-8')

    return copy_xsel, paste_xsel


def init_klipper_clipboard():
    def copy_klipper(text):
        p = subprocess.Popen(
            ['qdbus', 'org.kde.klipper', '/klipper', 'setClipboardContents', text.encode('utf-8')],
            stdin=subprocess.PIPE,
            close_fds=True
        )
        p.communicate(input=None)

    def paste_klipper():
        p = subprocess.Popen(
            ['qdbus', 'org.kde.klipper', '/klipper', 'getClipboardContents'],
            stdout=subprocess.PIPE,
            close_fds=True
        )
        stdout, stderr = p.communicate()

        # Apparently Klipper has a bug that adds a newline to the end. It was reported in Klipper version 0.20.3 but I've
        # seen it in 0.9.7. The bug is unfixed. This function will remove a newline if the string has one.
        # TODO: In the future, once Klipper is fixed, check the version number to decided whether or not to strip the
        # newline.
        # https://bugs.kde.org/show_bug.cgi?id=342874

        clipboardContents = stdout.decode('utf-8')
        assert len(clipboardContents) > 0  # even if blank, Klipper will append a newline at the end
        assert clipboardContents.endswith('\n')  # make sure that newline is there
        if clipboardContents.endswith('\n'):
            clipboardContents = clipboardContents[:-1]
        return clipboardContents

    return copy_klipper, paste_klipper


{'fraction_non_alphanumeric': 0.11821170282708744, 'fraction_numerical': 0.008678500986193294, 'mean_word_length': 2.765346534653465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323627', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2461, 'n_words': 631}