Skip to content
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
7 changes: 7 additions & 0 deletions mindee/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import io
import logging
import sys
from typing import cast

from mindee.v2.commands.cli_parser import MindeeParser

Expand Down Expand Up @@ -68,6 +70,11 @@ def main() -> None:
Pass ``--verbose`` (or ``-v``) to enable diagnostic logging; repeat
the flag (``--verbose --verbose``) for debug-level output.
"""

stdout = cast(io.TextIOWrapper, sys.stdout)
stdout_encoding = str(stdout.encoding)
if stdout_encoding and stdout_encoding.lower() != "utf-8":
stdout.reconfigure(encoding="utf-8")
verbose_level, argv = _extract_verbose_level(sys.argv[1:])
_configure_logging(verbose_level)
sys.argv = [sys.argv[0], *argv]
Expand Down
33 changes: 33 additions & 0 deletions mindee/v2/parsing/failed_inference_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from datetime import datetime

from mindee.parsing.common.common_response import CommonResponse
from mindee.parsing.common.string_dict import StringDict
from mindee.v2.parsing import ErrorResponse


class FailedInferenceResponse(CommonResponse):
"""Webhook payload returned when an inference fails before producing a result."""

inference_id: str
"""UUID of the failed inference."""
model_id: str
"""UUID of the model used."""
file_name: str
"""Name of the input file."""
file_alias: str
"""Alias sent for the file, if any."""
error: ErrorResponse
"""Problem details for the failure, if available."""
created_at: datetime
"""Date and time when the inference was started."""

def __init__(self, raw_prediction: StringDict) -> None:
super().__init__(raw_prediction)
self.inference_id = raw_prediction["inference_id"]
self.model_id = raw_prediction["model_id"]
self.file_name = raw_prediction["file_name"]
self.file_alias = raw_prediction["file_alias"]
self.error = ErrorResponse(raw_prediction["error"])
self.created_at = datetime.fromisoformat(
raw_prediction["created_at"].replace("Z", "+00:00")
)
7 changes: 0 additions & 7 deletions tests/v1/input/test_url_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from mindee.mindee_http.response_validation import validate_url_for_source


@pytest.mark.v1
class TestValidateUrlScheme:
def test_rejects_http(self):
with pytest.raises(MindeeSourceError, match="HTTPS"):
Expand All @@ -18,7 +17,6 @@ def test_accepts_https(self):
validate_url_for_source("https://example.com/file.pdf")


@pytest.mark.v1
class TestValidateUrlUserinfo:
def test_rejects_username_and_password(self):
with pytest.raises(MindeeSourceError, match="credentials"):
Expand All @@ -29,7 +27,6 @@ def test_rejects_username_only(self):
validate_url_for_source("https://user@example.com/file.pdf")


@pytest.mark.v1
class TestValidateUrlLoopbackHostnames:
def test_rejects_localhost(self):
with pytest.raises(MindeeSourceError, match="Loopback"):
Expand All @@ -48,7 +45,6 @@ def test_rejects_ip6_loopback(self):
validate_url_for_source("https://ip6-loopback/file.pdf")


@pytest.mark.v1
class TestValidateUrlLoopbackIPs:
def test_rejects_ipv4_loopback(self):
with pytest.raises(MindeeSourceError, match="disallowed"):
Expand All @@ -63,7 +59,6 @@ def test_rejects_ipv6_loopback(self):
validate_url_for_source("https://[::1]/file.pdf")


@pytest.mark.v1
class TestValidateUrlPrivateIPs:
def test_rejects_rfc1918_10_block(self):
with pytest.raises(MindeeSourceError, match="disallowed"):
Expand All @@ -90,7 +85,6 @@ def test_rejects_multicast(self):
validate_url_for_source("https://224.0.0.1/file.pdf")


@pytest.mark.v1
class TestValidateUrlCgnat:
def test_rejects_cgnat_start(self):
with pytest.raises(MindeeSourceError, match="disallowed"):
Expand All @@ -105,7 +99,6 @@ def test_accepts_just_outside_cgnat(self):
validate_url_for_source("https://100.128.0.1/file.pdf")


@pytest.mark.v1
class TestValidateUrlIpv6UniqueLocal:
def test_rejects_ipv6_ula_fc(self):
with pytest.raises(MindeeSourceError, match="disallowed"):
Expand Down
26 changes: 26 additions & 0 deletions tests/v2/parsing/test_failed_inference_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
from datetime import datetime

import pytest

from mindee.v2.parsing.failed_inference_response import FailedInferenceResponse
from tests.utils import V2_DATA_DIR


@pytest.mark.v2
def test_should_load_when_failed():
"""Should load when the webhook didn't return a correct reply."""

json_path = V2_DATA_DIR / "errors" / "webhook_error_500_failed.json"
with json_path.open("r", encoding="utf-8") as fh:
json_sample = json.load(fh)
response = FailedInferenceResponse(json_sample)

assert response is not None
assert response.inference_id == "12345678-1234-1234-1234-123456789ABC"
assert response.file_name == "default_sample.jpg"
assert response.file_alias == "dummy-alias.jpg"
assert isinstance(response.created_at, datetime)
assert response.error is not None
assert response.error.status == 500
assert response.error.code == "500-012"