Skip to content

Commit

Permalink
refactor(clipboard): use protocols instead of abstract base class
Browse files Browse the repository at this point in the history
  • Loading branch information
dynobo committed Jan 19, 2024
1 parent a7c0f22 commit a0ddc9e
Show file tree
Hide file tree
Showing 21 changed files with 598 additions and 531 deletions.
10 changes: 8 additions & 2 deletions normcap/clipboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from normcap.clipboard.main import ClipboardHandlers, copy, copy_with_handler
from normcap.clipboard.main import (
Handler,
copy,
copy_with_handler,
get_available_handlers,
)

__all__ = [
"Handler",
"copy",
"copy_with_handler",
"ClipboardHandlers",
"get_available_handlers",
]
109 changes: 0 additions & 109 deletions normcap/clipboard/handlers/base.py

This file was deleted.

50 changes: 25 additions & 25 deletions normcap/clipboard/handlers/pbcopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import subprocess
import sys

from .base import ClipboardHandlerBase

logger = logging.getLogger(__name__)

install_instructions = "" # pbcopy is pre-installed on macOS


def copy(text: str) -> None:
subprocess.run(
["pbcopy", "w"], # noqa: S607
shell=False, # noqa: S603
input=text.encode("utf-8"),
check=True,
timeout=30,
env={"LC_CTYPE": "UTF-8"},
)


def is_compatible() -> bool:
if sys.platform != "darwin":
logger.debug("%s is incompatible on non-macOS systems", __name__)
return False

logger.debug("%s is compatible", __name__)
return True


class PbCopyHandler(ClipboardHandlerBase):
@staticmethod
def _copy(text: str) -> None:
subprocess.run(
["pbcopy", "w"], # noqa: S607
shell=False, # noqa: S603
input=text.encode("utf-8"),
check=True,
timeout=30,
env={"LC_CTYPE": "UTF-8"},
)

def is_compatible(self) -> bool:
if sys.platform != "darwin":
logger.debug("%s is incompatible on non-macOS systems", self.name)
return False

logger.debug("%s is compatible", self.name)
return True

def is_installed(self) -> bool:
logger.debug("%s requires no dependencies", self.name)
return True
def is_installed() -> bool:
logger.debug("%s requires no dependencies", __name__)
return True
47 changes: 26 additions & 21 deletions normcap/clipboard/handlers/qtclipboard.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import logging
from typing import Any, cast

from normcap.clipboard import system_info

try:
from PySide6 import QtGui

except ImportError:
QtGui = cast(Any, None)

from .base import ClipboardHandlerBase

logger = logging.getLogger(__name__)

install_instructions = (
"Please install the Python package PySide6 with your preferred package manager."
)


def copy(text: str) -> None:
"""Use QtWidgets.QApplication.clipboard to copy text to system clipboard."""
app = QtGui.QGuiApplication.instance() or QtGui.QGuiApplication()

cb = app.clipboard() # type: ignore # Type hint wrong in PySide6?
cb.setText(text)
app.processEvents()

class QtCopyHandler(ClipboardHandlerBase):
@staticmethod
def _copy(text: str) -> None:
"""Use QtWidgets.QApplication.clipboard to copy text to system clipboard."""
app = QtGui.QGuiApplication.instance() or QtGui.QGuiApplication()

cb = app.clipboard() # type: ignore # Type hint wrong in PySide6?
cb.setText(text)
app.processEvents()
def is_compatible() -> bool:
if not QtGui:
logger.debug("%s is not compatible on systems w/o PySide6", __name__)
return False

def is_compatible(self) -> bool:
if not QtGui:
logger.debug("%s is not compatible on systems w/o PySide6", self.name)
return False
if system_info.os_has_wayland_display_manager():
logger.debug("%s is not compatible with Wayland", __name__)
return False

if self._os_has_wayland_display_manager():
logger.debug("%s is not compatible with Wayland", self.name)
return False
logger.debug("%s is compatible", __name__)
return True

logger.debug("%s is compatible", self.name)
return True

def is_installed(self) -> bool:
logger.debug("%s requires no dependencies", self.name)
return True
def is_installed() -> bool:
logger.debug("%s requires no dependencies", __name__)
return True
Loading

0 comments on commit a0ddc9e

Please sign in to comment.