Skip to content

Commit

Permalink
Merge pull request #225 from stephenfin/remove-deprecated-libraries
Browse files Browse the repository at this point in the history
Remove deprecated libraries (pyqt4, pygtk)
  • Loading branch information
asweigart authored Jun 18, 2024
2 parents eb92ba6 + 2856acf commit 0d187a0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ On Mac, this module makes use of the pbcopy and pbpaste commands, which should c

On Linux, this module makes use of the xclip or xsel commands, which should come with the os. Otherwise run "sudo apt-get install xclip" or "sudo apt-get install xsel" (Note: xsel does not always seem to work.)

Otherwise on Linux, you will need the gtk or PyQt4 modules installed.
Otherwise on Linux, you will need the qtpy or PyQT5 modules installed.

Support
-------
Expand Down
75 changes: 18 additions & 57 deletions src/pyperclip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
sudo apt-get install xsel
sudo apt-get install wl-clipboard
Otherwise on Linux, you will need the gtk or PyQt5/PyQt4 modules installed.
Otherwise on Linux, you will need the qtpy or PyQt5 modules installed.
gtk and PyQt4 modules are not available for Python 3,
and this module does not work with PyGObject yet.
This module does not work with PyGObject yet.
Note: There seems to be a way to get gtk on Python 3, according to:
https://askubuntu.com/questions/697397/python3-is-not-supporting-gtk-module
Expand Down Expand Up @@ -133,40 +132,15 @@ def paste_osx_pyobjc():
return copy_osx_pyobjc, paste_osx_pyobjc


def init_gtk_clipboard():
global gtk
import gtk

def copy_gtk(text):
global cb
text = _PYTHON_STR_TYPE(text) # Converts non-str values to str.
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():
global QApplication
# $DISPLAY should exist

# Try to import from qtpy, but if that fails try PyQt5 then PyQt4
# Try to import from qtpy, but if that fails try PyQt5
try:
from qtpy.QtWidgets import QApplication
except:
try:
from PyQt5.QtWidgets import QApplication
except:
from PyQt4.QtGui import QApplication
from PyQt5.QtWidgets import QApplication

app = QApplication.instance()
if app is None:
Expand Down Expand Up @@ -529,7 +503,7 @@ def determine_clipboard():
accordingly.
'''

global Foundation, AppKit, gtk, qtpy, PyQt4, PyQt5
global Foundation, AppKit, gtk, qtpy, PyQt5

# Setup for the CYGWIN platform:
if 'cygwin' in platform.system().lower(): # Cygwin has a variety of values returned by platform.system(), such as 'CYGWIN_NT-6.1'
Expand Down Expand Up @@ -564,13 +538,6 @@ def determine_clipboard():
# Thus, we need to detect the presence of $DISPLAY manually
# and not load PyQt4 if it is absent.
if os.getenv("DISPLAY"):
try:
import gtk # check if gtk is installed
except ImportError:
pass # We want to fail fast for all non-ImportError exceptions.
else:
return init_gtk_clipboard()

if (
os.getenv("WAYLAND_DISPLAY") and
_executable_exists("wl-copy")
Expand All @@ -585,25 +552,20 @@ def determine_clipboard():
return init_klipper_clipboard()

try:
# qtpy is a small abstraction layer that lets you write applications using a single api call to either PyQt or PySide.
# 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:
# 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()
except ImportError:
pass

# If qtpy isn't installed, fall back on importing PyQt5
try:
import PyQt5 # check if PyQt5 is installed
return init_qt_clipboard()
except ImportError:
pass

return init_no_clipboard()

Expand All @@ -628,8 +590,7 @@ def set_clipboard(clipboard):
clipboard_types = {
"pbcopy": init_osx_pbcopy_clipboard,
"pyobjc": init_osx_pyobjc_clipboard,
"gtk": init_gtk_clipboard,
"qt": init_qt_clipboard, # TODO - split this into 'qtpy', 'pyqt4', and 'pyqt5'
"qt": init_qt_clipboard, # TODO - split this into 'qtpy' and 'pyqt5'
"xclip": init_xclip_clipboard,
"xsel": init_xsel_clipboard,
"wl-clipboard": init_wl_clipboard,
Expand All @@ -652,7 +613,7 @@ def lazy_load_stub_copy(text):
This allows users to import pyperclip without having determine_clipboard()
automatically run, which will automatically select a clipboard mechanism.
This could be a problem if it selects, say, the memory-heavy PyQt4 module
This could be a problem if it selects, say, the memory-heavy PyQt5 module
but the user was just going to immediately call set_clipboard() to use a
different clipboard mechanism.
Expand All @@ -674,7 +635,7 @@ def lazy_load_stub_paste():
This allows users to import pyperclip without having determine_clipboard()
automatically run, which will automatically select a clipboard mechanism.
This could be a problem if it selects, say, the memory-heavy PyQt4 module
This could be a problem if it selects, say, the memory-heavy PyQt5 module
but the user was just going to immediately call set_clipboard() to use a
different clipboard mechanism.
Expand Down
19 changes: 2 additions & 17 deletions tests/test_pyperclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pyperclip import _executable_exists, HAS_DISPLAY
from pyperclip import (init_osx_pbcopy_clipboard, init_osx_pyobjc_clipboard,
init_dev_clipboard_clipboard,
init_gtk_clipboard, init_qt_clipboard,
init_qt_clipboard,
init_xclip_clipboard, init_xsel_clipboard,
init_wl_clipboard,
init_klipper_clipboard, init_no_clipboard)
Expand Down Expand Up @@ -134,27 +134,12 @@ class TestOSX(_TestClipboard):
clipboard = init_osx_pyobjc_clipboard()


class TestGtk(_TestClipboard):
if HAS_DISPLAY:
try:
import gtk
except ImportError:
pass
else:
clipboard = init_gtk_clipboard()


class TestQt(_TestClipboard):
if HAS_DISPLAY:
try:
import PyQt5.QtWidgets
except ImportError:
try:
import PyQt4.QtGui
except ImportError:
pass
else:
clipboard = init_qt_clipboard()
pass
else:
clipboard = init_qt_clipboard()

Expand Down

0 comments on commit 0d187a0

Please sign in to comment.