Skip to content

Update clipboard Qt-bindings for flexiblity and Python3 compatibility #17723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions pandas/io/clipboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
On Linux, install xclip or xsel via package manager. For example, in Debian:
sudo apt-get install xclip

Otherwise on Linux, you will need the gtk or PyQt4 modules installed.
Otherwise on Linux, you will need the gtk or qtpy modules installed.
qtpy also requires a python-qt-bindings module: PyQt4, PyQt5, PySide, PySide2

gtk and PyQt4 modules are not available for Python 3,
and this module does not work with PyGObject yet.
Expand All @@ -34,9 +35,9 @@
init_klipper_clipboard, init_no_clipboard)
from .windows import init_windows_clipboard

# `import PyQt4` sys.exit()s if DISPLAY is not in the environment.
# `import qtpy` sys.exit()s if DISPLAY is not in the environment.
# Thus, we need to detect the presence of $DISPLAY manually
# and not load PyQt4 if it is absent.
# and not load qtpy if it is absent.
HAS_DISPLAY = os.getenv("DISPLAY", False)
CHECK_CMD = "where" if platform.system() == "Windows" else "which"

Expand Down Expand Up @@ -68,13 +69,25 @@ def determine_clipboard():
return init_gtk_clipboard()

try:
# Check if PyQt4 is installed
import PyQt4 # noqa
# qtpy is a small abstraction layer that lets you write applications using a single api call to either PyQt or PySide.
# https://pypi.python.org/pypi/QtPy
import qtpy # check if qtpy is installed
except ImportError:
pass
# If qtpy isn't installed, fall back on importing PyQt4.
try:
import PyQt5 # check if PyQt5 is installed
except ImportError:
try:
import PyQt4 # check if PyQt4 is installed
except ImportError:
pass # We want to fail fast for all non-ImportError exceptions.
else:
return init_qt_clipboard()
else:
return init_qt_clipboard()

else:
return init_qt_clipboard()

if _executable_exists("xclip"):
return init_xclip_clipboard()
if _executable_exists("xsel"):
Expand Down
20 changes: 12 additions & 8 deletions pandas/io/clipboard/clipboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ def paste_gtk():

def init_qt_clipboard():
# $DISPLAY should exist
from PyQt4.QtGui import QApplication

# use the global instance if it exists
app = QApplication.instance() or QApplication([])
# Try to import from qtpy, but if that fails try PyQt5 then PyQt4
try:
from qtpy.QtWidgets import QApplication
except:
try:
from PyQt5.QtWidgets import QApplication
except:
from PyQt4.QtGui import QApplication

app = QApplication.instance()
if app is None:
app = QApplication([])

def copy_qt(text):
cb = app.clipboard()
cb.setText(text)

def paste_qt():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this?

cb = app.clipboard()
return text_type(cb.text())

return copy_qt, paste_qt


def init_xclip_clipboard():
Expand Down