diff --git a/.gitignore b/.gitignore index 0a1f91b24..1a50fb9a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.coverage .debug .env .logs diff --git a/ai_diffusion/connection.py b/ai_diffusion/connection.py index ddbcc7453..b92fb0448 100644 --- a/ai_diffusion/connection.py +++ b/ai_diffusion/connection.py @@ -120,9 +120,6 @@ async def _handle_messages(self): self.error = "" else: self.message_received.emit(msg) - # model = self._find_model(msg.job_id) - # if model is not None: - # model.handle_message(msg) except asyncio.CancelledError: break except Exception as e: @@ -131,14 +128,6 @@ async def _handle_messages(self): except asyncio.CancelledError: pass # shutdown - # def _report_error(self, message: str): - # self.error = message - # self.error_reported.emit(message) - - # def _clear_error(self): - # self.error = None - # self.error_reported.emit("") - def apply_performance_preset(settings: Settings, device: DeviceInfo): if settings.performance_preset is PerformancePreset.auto: diff --git a/ai_diffusion/document.py b/ai_diffusion/document.py index 5bd379ab9..18f5f71bf 100644 --- a/ai_diffusion/document.py +++ b/ai_diffusion/document.py @@ -163,11 +163,6 @@ def create_mask_from_layer(self, padding: float, is_inpaint: bool): data: QByteArray = layer.projectionPixelData(*mask_bounds) assert data is not None and data.size() >= mask_bounds.extent.pixel_count mask = Mask(mask_bounds, data) - - log.debug( - f"Using experimental selection mask {self.active_layer.name()}, mask bounds:" - f" {mask.bounds}, image bounds: {image_bounds}" - ) return mask, image_bounds, None def get_image( diff --git a/ai_diffusion/util.py b/ai_diffusion/util.py index 77d1e7ecd..91a5f1a7b 100644 --- a/ai_diffusion/util.py +++ b/ai_diffusion/util.py @@ -6,7 +6,7 @@ import logging import logging.handlers import zipfile -from typing import Optional, TypeVar +from typing import Optional, Sequence, TypeVar T = TypeVar("T") @@ -57,7 +57,7 @@ def encode_json(obj): raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable") -def get_path_dict(paths: list[str | Path]) -> dict: +def get_path_dict(paths: Sequence[str | Path]) -> dict: """Builds a tree like structure out of a list of paths""" def _recurse(dic: dict, chain: tuple[str, ...] | list[str]): diff --git a/tests/test_image.py b/tests/test_image.py index 604eca160..0687709f0 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -118,6 +118,21 @@ def test_clamp_bounds(input, expected): assert result == expected +@pytest.mark.parametrize( + "input,bounds,expected", + [ + (Bounds(0, 0, 1, 2), Bounds(0, 0, 2, 2), Bounds(0, 0, 1, 2)), + (Bounds(0, 0, 1, 2), Bounds(0, 0, 1, 1), Bounds(0, 0, 1, 1)), + (Bounds(2, 4, 1, 2), Bounds(1, 4, 2, 2), Bounds(2, 4, 1, 2)), + (Bounds(2, 4, 7, 9), Bounds(1, 4, 2, 2), Bounds(2, 4, 1, 2)), + (Bounds(-1, 5, 3, 3), Bounds(0, 6, 5, 2), Bounds(0, 6, 2, 2)), + ], +) +def test_restrict_bounds(input: Bounds, bounds: Bounds, expected: Bounds): + result = Bounds.restrict(input, bounds) + assert result == expected + + @pytest.mark.parametrize( "input,target,expected", [ diff --git a/tests/test_util.py b/tests/test_util.py index deaa8f3c2..8754fb1fc 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,5 +1,7 @@ import pytest -from ai_diffusion.util import batched +from tempfile import TemporaryDirectory +from pathlib import Path +from ai_diffusion.util import batched, get_path_dict, ZipFile def test_batched(): @@ -7,3 +9,39 @@ def test_batched(): n = 3 expected_output = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] assert list(batched(iterable, n)) == expected_output + + +def test_path_dict(): + paths = [ + "f1.txt", + "f2.txxt", + "d1/f1.txt", + "d2/f1.txt", + "d3/d4/d5/f1.txt", + "d1/f2.txt", + "w1\\f1.txt", + "w1\\noext", + "w1\\w2\\.noext", + ] + expected = { + "f1.txt": None, + "f2.txxt": None, + "d1": {"f1.txt": None, "f2.txt": None}, + "d2": {"f1.txt": None}, + "d3": {"d4": {"d5": {"f1.txt": None}}}, + "w1": {"f1.txt": None, "noext": None, "w2": {".noext": None}}, + } + assert get_path_dict(paths) == expected + + +def test_long_path_zip_file(): + with TemporaryDirectory() as dir: + file = Path(dir) / "test.zip" + with ZipFile(file, "w") as zip: + zip.writestr("test.txt", "test") + zip.writestr("test2.txt", "test2") + long_path = Path(dir) / ("l" + "o" * 150 + "ng") / ("l" + "o" * 150 + "ng") + with ZipFile(file, "r") as zip: + zip.extractall(long_path) + assert (long_path / "test.txt").read_text() == "test" + assert (long_path / "test2.txt").read_text() == "test2"