Skip to content

Commit

Permalink
refactor(screengrab): remove custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dynobo committed Jan 23, 2024
1 parent 071cb41 commit 043202a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 44 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -208,7 +208,8 @@ If NormCap doesn't fit your needs, try those alternatives (no particular order):
- [Textinator](https://github.com/RhetTbull/textinator) (macOS)
- [Text-Grab](https://github.com/TheJoeFin/Text-Grab) (Windows)
- [dpScreenOCR](https://danpla.github.io/dpscreenocr/) (Linux, Windows)
- [PowerToys Text Extractor](https://learn.microsoft.com/en-us/windows/powertoys/text-extractor) (Windows)
- [PowerToys Text Extractor](https://learn.microsoft.com/en-us/windows/powertoys/text-extractor)
(Windows)

## Certification

Expand Down
16 changes: 5 additions & 11 deletions normcap/screengrab/handlers/dbus_portal.py
Expand Up @@ -11,12 +11,6 @@

from normcap.screengrab import system_info
from normcap.screengrab.post_processing import split_full_desktop_to_screens
from normcap.screengrab.structures import (
ScreenshotPermissionError,
ScreenshotRequestError,
ScreenshotResponseError,
ScreenshotTimeoutError,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,7 +99,7 @@ def grab_full_desktop(self) -> None:
else:
msg = "No object path received from xdg-portal!"
logger.error(msg)
self.on_exception.emit(ScreenshotRequestError(msg))
self.on_exception.emit(RuntimeError(msg))

def _get_timeout_timer(self, timeout_sec: int) -> QtCore.QTimer:
def _timeout_triggered() -> None:
Expand All @@ -130,13 +124,13 @@ def got_signal(self, message: QtDBus.QDBusMessage) -> None:
if code == permission_denied_code:
msg = f"Permission denied for Screenshot via xdg-portal! Message: {message}"
logger.error(msg)
self.on_exception.emit(ScreenshotPermissionError(msg))
self.on_exception.emit(PermissionError(msg))
return

if code != all_okay_code:
msg = f"Error code {code} received from xdg-portal!"
logger.error(msg)
self.on_exception.emit(ScreenshotResponseError(msg))
self.on_exception.emit(RuntimeError(msg))
return

logger.debug("Parse response")
Expand Down Expand Up @@ -166,7 +160,7 @@ def got_signal(self, message: QtDBus.QDBusMessage) -> None:
if not result:
msg = f"Couldn't parse URI from message: {message}"
logger.error(msg)
self.on_exception.emit(ScreenshotResponseError(message))
self.on_exception.emit(RuntimeError(message))
return

uri = result.group(1)
Expand Down Expand Up @@ -231,6 +225,6 @@ def capture() -> list[QtGui.QImage]:
try:
image = _synchronized_capture(interactive=False)
except TimeoutError as exc:
raise ScreenshotTimeoutError("Timeout when taking screenshot!") from exc
raise TimeoutError("Timeout when taking screenshot!") from exc
else:
return split_full_desktop_to_screens(image)
7 changes: 2 additions & 5 deletions normcap/screengrab/permissions.py
Expand Up @@ -7,7 +7,7 @@

from PySide6 import QtGui, QtWidgets

from normcap.screengrab import structures, system_info
from normcap.screengrab import system_info

try:
from normcap.screengrab.handlers import dbus_portal
Expand Down Expand Up @@ -198,10 +198,7 @@ def _dbus_portal_has_screenshot_permission() -> bool:
result = []
try:
result = dbus_portal.capture()
except (
structures.ScreenshotPermissionError,
structures.ScreenshotTimeoutError,
) as exc:
except (PermissionError, TimeoutError) as exc:
logger.warning("Screenshot permissions on Wayland seem missing.", exc_info=exc)
return len(result) > 0

Expand Down
20 changes: 0 additions & 20 deletions normcap/screengrab/structures.py
Expand Up @@ -50,23 +50,3 @@ class Handler(enum.IntEnum):

# For linux with wayland, old variant, e.g. for Ubuntu 20.04
DBUS_SHELL = enum.auto()


class ScreenshotError(Exception):
...


class ScreenshotResponseError(ScreenshotError):
...


class ScreenshotRequestError(ScreenshotError):
...


class ScreenshotPermissionError(ScreenshotError):
...


class ScreenshotTimeoutError(ScreenshotError):
...
8 changes: 2 additions & 6 deletions tests/tests_screengrab/test_handlers/test_dbus_portal.py
Expand Up @@ -5,10 +5,6 @@
import pytest

from normcap.screengrab.permissions import has_screenshot_permission
from normcap.screengrab.structures import (
ScreenshotRequestError,
ScreenshotResponseError,
)


@pytest.mark.gui()
Expand All @@ -32,7 +28,7 @@ def _mocked_interface_call(*args):
monkeypatch.setattr(
dbus_portal.QtDBus.QDBusInterface, "call", _mocked_interface_call
)
with pytest.raises(ScreenshotRequestError):
with pytest.raises(RuntimeError, match=r"[Nn]o object path"):
_ = dbus_portal._synchronized_capture(interactive=False)


Expand All @@ -54,7 +50,7 @@ def decorated_msg(cls, msg):
"got_signal",
_decorated_got_signal(dbus_portal.OrgFreedesktopPortalScreenshot.got_signal),
)
with pytest.raises(ScreenshotResponseError):
with pytest.raises(RuntimeError, match=r"[Ee]rror code 1 received .* xdg-portal"):
_ = dbus_portal._synchronized_capture(interactive=False)


Expand Down
2 changes: 1 addition & 1 deletion tests/tests_screengrab/test_main.py
Expand Up @@ -7,7 +7,7 @@
from normcap.screengrab.structures import Handler


def test_capture():
def test_capture(qapp):
# GIVEN any system (with display server)
# WHEN screenshots are taken
images = screengrab.capture()
Expand Down

0 comments on commit 043202a

Please sign in to comment.