Skip to content
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

Feature/introduce protocols #597

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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