-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
img - more tests, refactors, fixes. snip DONE
- Loading branch information
Showing
7 changed files
with
223 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ pythonpath = [ | |
testpaths = [ | ||
"tests", | ||
] | ||
tmp_path_retention_count = 0 | ||
|
||
|
||
[tool.mypy] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import os | ||
from typing import Any | ||
from typing import Callable | ||
|
||
import tapper | ||
from tapper.helper._util.image import base | ||
from tapper.helper.model_types import BboxT | ||
from tapper.helper.model_types import ImagePixelMatrixT | ||
from tapper.helper.model_types import XyCoordsT | ||
|
||
snip_start_coords: XyCoordsT | None = None | ||
|
||
|
||
def toggle_snip( | ||
prefix: str | None = None, | ||
bbox_to_name: bool = True, | ||
override_existing: bool = True, | ||
bbox_callback: Callable[[tuple[int, int, int, int]], Any] | None = None, | ||
picture_callback: Callable[[ImagePixelMatrixT], Any] | None = None, | ||
) -> None: | ||
global snip_start_coords | ||
if not snip_start_coords: | ||
start_snip() | ||
else: | ||
stop_coords = tapper.mouse.get_pos() | ||
x1 = min(snip_start_coords[0], stop_coords[0]) | ||
x2 = max(snip_start_coords[0], stop_coords[0]) | ||
y1 = min(snip_start_coords[1], stop_coords[1]) | ||
y2 = max(snip_start_coords[1], stop_coords[1]) | ||
snip_start_coords = None | ||
finish_snip_with_callback( | ||
prefix, | ||
bbox_to_name, | ||
(x1, y1, x2, y2), | ||
override_existing, | ||
bbox_callback, | ||
picture_callback, | ||
) | ||
|
||
|
||
def start_snip() -> None: | ||
global snip_start_coords | ||
snip_start_coords = tapper.mouse.get_pos() | ||
|
||
|
||
def finish_snip_with_callback( | ||
prefix: str | None = None, | ||
bbox_to_name: bool = True, | ||
bbox: BboxT | None = None, | ||
override_existing: bool = True, | ||
bbox_callback: Callable[[tuple[int, int, int, int]], Any] | None = None, | ||
picture_callback: Callable[[ImagePixelMatrixT], Any] | None = None, | ||
) -> None: | ||
nd_sct, bbox = finish_snip(prefix, bbox, bbox_to_name, override_existing) | ||
if bbox and bbox_callback: | ||
bbox_callback(bbox) | ||
if picture_callback: | ||
picture_callback(nd_sct) | ||
|
||
|
||
def finish_snip( | ||
prefix: str | None, | ||
bbox: BboxT | None, | ||
bbox_to_name: bool, | ||
override_existing: bool, | ||
) -> tuple[ImagePixelMatrixT, BboxT | None]: | ||
sct = base.get_screenshot_if_none_and_cut(None, bbox) | ||
if prefix is not None: | ||
bbox_str = ( | ||
f"-BBOX({bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]})" | ||
if bbox and bbox_to_name | ||
else "" | ||
) | ||
ending = bbox_str + ".png" | ||
full_name = "" | ||
if override_existing or not os.path.exists(prefix + ending): | ||
full_name = prefix + ending | ||
else: | ||
for i in range(1, 100): | ||
potential_name = prefix + f"({i})" + ending | ||
if not os.path.exists(potential_name): | ||
full_name = potential_name | ||
break | ||
base.save_to_disk(sct, full_name) | ||
return sct, bbox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,97 @@ | ||
import os | ||
import random | ||
import shutil | ||
from pathlib import Path | ||
from string import ascii_uppercase | ||
from unittest.mock import call | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
import img_test_util | ||
import numpy | ||
import pytest | ||
from tapper.helper import img | ||
|
||
absolutes = img_test_util.absolutes() | ||
|
||
|
||
@pytest.fixture | ||
def mock_get_sct() -> MagicMock: | ||
with patch( | ||
"tapper.helper._util.image.base.get_screenshot_if_none_and_cut" | ||
) as mock_sct: | ||
yield mock_sct | ||
|
||
|
||
@pytest.fixture | ||
def temp_dir(tmpdir_factory) -> Path: | ||
temp_name = "".join(random.choice(ascii_uppercase) for i in range(12)) | ||
my_tmpdir = tmpdir_factory.mktemp(temp_name) | ||
yield my_tmpdir | ||
shutil.rmtree(str(my_tmpdir)) | ||
|
||
|
||
@pytest.fixture | ||
def mock_save_to_disk() -> MagicMock: | ||
with patch("tapper.helper._util.image.base.save_to_disk") as mock_save: | ||
yield mock_save | ||
|
||
|
||
@pytest.fixture | ||
def mock_mouse_pos() -> MagicMock: | ||
with patch("tapper.mouse.get_pos") as mock_mouse_get_pos: | ||
yield mock_mouse_get_pos | ||
|
||
|
||
class TestSnip: | ||
def test_simplest(self) -> None: | ||
pass | ||
def test_simplest(self, mock_get_sct) -> None: | ||
mock_get_sct.return_value = absolutes | ||
snipped = img.get_snip(None) | ||
assert numpy.array_equal(snipped, absolutes) | ||
|
||
def test_saved_image_same_as_on_disk(self, temp_dir, mock_get_sct) -> None: | ||
mock_get_sct.return_value = absolutes | ||
get_name = lambda name: str(Path(temp_dir / name)) | ||
img.get_snip(bbox=None, prefix=get_name("qwe")) | ||
on_disk = img.from_path(get_name("qwe.png"), cache=False) | ||
assert numpy.array_equal(on_disk, absolutes) | ||
|
||
def test_bbox_to_name(self, mock_get_sct, mock_save_to_disk) -> None: | ||
mock_get_sct.return_value = absolutes | ||
img.get_snip(bbox=(0, 0, 20, 20), prefix="qwe", bbox_to_name=True) | ||
assert mock_save_to_disk.call_count == 1 | ||
assert mock_save_to_disk.call_args == call(absolutes, "qwe-BBOX(0,0,20,20).png") | ||
|
||
def test_no_override_creates_different_file(self, temp_dir, mock_get_sct) -> None: | ||
mock_get_sct.return_value = absolutes | ||
get_name = lambda name: str(Path(temp_dir / name)) | ||
img.get_snip(bbox=None, prefix=get_name("qwe")) | ||
on_disk_0 = img.from_path(get_name("qwe.png"), cache=False) | ||
img.get_snip(bbox=None, prefix=get_name("qwe"), override_existing=False) | ||
on_disk_1 = img.from_path(get_name("qwe(1).png"), cache=False) | ||
assert numpy.array_equal(on_disk_0, on_disk_1) | ||
assert not os.path.exists(get_name("qwe(2).png")) | ||
|
||
def test_override(self, temp_dir, mock_get_sct) -> None: | ||
get_name = lambda name: str(Path(temp_dir / name)) | ||
mock_get_sct.return_value = absolutes | ||
img.get_snip(bbox=None, prefix=get_name("qwe")) | ||
on_disk_0 = img.from_path(get_name("qwe.png"), cache=False) | ||
|
||
mock_get_sct.return_value = img_test_util.btn_yellow() | ||
img.get_snip(bbox=None, prefix=get_name("qwe"), override_existing=True) | ||
on_disk_1 = img.from_path(get_name("qwe.png"), cache=False) | ||
assert not os.path.exists(get_name("qwe(1).png")) | ||
assert len([name for name in os.listdir(temp_dir)]) == 1 | ||
assert numpy.array_equal(on_disk_0, absolutes) | ||
assert numpy.array_equal(on_disk_1, img_test_util.btn_yellow()) | ||
|
||
def test_saved_image_same_as_on_disk(self) -> None: | ||
pass | ||
def test_bbox_is_correct( | ||
self, mock_get_sct, mock_save_to_disk, mock_mouse_pos | ||
) -> None: | ||
snip_fn = img.snip() | ||
mock_mouse_pos.return_value = 100, 450 | ||
snip_fn() | ||
mock_mouse_pos.return_value = 300, 0 | ||
snip_fn() | ||
assert mock_get_sct.call_args == call(None, (100, 0, 300, 450)) |