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
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ repos:
args: []
exclude: "tests/|examples/|docs/"
additional_dependencies:
- toml
- types-requests
- httpx
- types-setuptools
- importlib-metadata
- types-Pillow
Expand Down
10 changes: 5 additions & 5 deletions mindee/image/extracted_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ def save_to_file(self, output_path: Path | str, file_format: str | None = None):
else:
image.save(resolved_path)
logger.info("File saved successfully to '%s'.", resolved_path)
except TypeError as exc:
raise MindeeError("Invalid path/filename provided.") from exc
except Exception as exc:
print(exc)
raise MindeeError(f"Could not save file {Path(output_path).name}.") from exc
except TypeError as e:
raise MindeeError("Invalid path/filename provided.") from e
except Exception as e:
print(e)
raise MindeeError(f"Could not save file {Path(output_path).name}.") from e

def as_input_source(self) -> FileInput:
"""
Expand Down
14 changes: 7 additions & 7 deletions mindee/input/local_input_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def __init__(self) -> None:
try:
pdf = pdfium.PdfDocument(self.file_object)
self.page_count = len(pdf)
except pdfium.PdfiumError as exc:
except pdfium.PdfiumError as e:
logger.warning(
"Could not open PDF file: %s due to %s", self.filename, exc
"Could not open PDF file: %s due to %s", self.filename, e
)
self.page_count = 0
self.file_object.seek(0)
Expand Down Expand Up @@ -106,11 +106,11 @@ def fix_pdf(self, maximum_offset: int = 500) -> None:
f"PDF couldn't be fixed. PDF tag was found at position {pos}."
)
self.file_mimetype = "application/pdf"
except MimeTypeError as exc:
raise exc
except Exception as exc:
print(f"Attempt to fix pdf raised exception {exc}.")
raise exc
except MimeTypeError as e:
raise e
except Exception as e:
logger.error("Attempt to fix pdf raised exception %s.", e)
raise e

def is_pdf(self) -> bool:
""":return: True if the file is a PDF."""
Expand Down
12 changes: 6 additions & 6 deletions mindee/input/local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def as_dict(self) -> dict[str, Any]:
try:
self._file.seek(0)
out_json = json.loads(self._file.read())
except json.decoder.JSONDecodeError as exc:
raise MindeeError("File is not a valid dictionary.") from exc
except json.decoder.JSONDecodeError as e:
raise MindeeError("File is not a valid dictionary.") from e
return out_json

@staticmethod
Expand Down Expand Up @@ -87,8 +87,8 @@ def get_hmac_signature(self, secret_key: str | bytes | bytearray):
self._file.read(),
algorithm,
)
except (TypeError, ValueError) as exc:
raise MindeeError("Could not get HMAC signature from payload.") from exc
except (TypeError, ValueError) as e:
raise MindeeError("Could not get HMAC signature from payload.") from e

return mac.hexdigest()

Expand All @@ -114,5 +114,5 @@ def deserialize_response(self, response_class: type[ResponseT]) -> ResponseT:
"""
try:
return response_class(self.as_dict)
except KeyError as exc:
raise MindeeError("Invalid class specified for deserialization.") from exc
except KeyError as e:
raise MindeeError("Invalid class specified for deserialization.") from e
10 changes: 5 additions & 5 deletions mindee/input/url_input_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from urllib.parse import urlparse

import requests
import httpx

from mindee.error.mindee_error import MindeeSourceError
from mindee.input.bytes_input import BytesInput
Expand Down Expand Up @@ -153,7 +153,7 @@ def __get_file_extension(filename) -> str | None:
:return: The lowercase file extension or None if not found.
"""
ext = os.path.splitext(filename)[1]
return ext.lower() if ext else None
return str(ext).lower() if ext else None

def __fill_filename(self, filename=None) -> str:
"""
Expand All @@ -167,7 +167,7 @@ def __fill_filename(self, filename=None) -> str:

if not filename or not os.path.splitext(filename)[1]:
filename = self.__generate_file_name(
extension=URLInputSource.__get_file_extension(filename)
extension=URLInputSource.__get_file_extension(filename) or ""
)

return filename
Expand All @@ -185,15 +185,15 @@ def __make_request(url, auth, headers, redirects, max_redirects) -> bytes:
:return: The content of the response.
:raises MindeeSourceError: If max redirects are exceeded or the request fails.
"""
result = requests.get(url, headers=headers, timeout=120, auth=auth)
result = httpx.get(url, headers=headers, timeout=120, auth=auth)
if 299 < result.status_code < 400:
if redirects == max_redirects:
raise MindeeSourceError(
f"Can't reach URL after {redirects} out of {max_redirects} redirects, "
f"aborting operation."
)
return URLInputSource.__make_request(
redirects.location, auth, headers, redirects + 1, max_redirects
result.headers["Location"], auth, headers, redirects + 1, max_redirects
)

if result.status_code >= 400 or result.status_code < 200:
Expand Down
19 changes: 11 additions & 8 deletions mindee/mindee_http/response_validation.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import json

import requests
import httpx

from mindee.parsing.common import StringDict


def is_valid_sync_response(response: requests.Response) -> bool:
def is_valid_sync_response(response: httpx.Response) -> bool:
"""
Checks if the synchronous response is valid. Returns True if the response is valid.

:param response: a requests response object.
:return: bool
"""
if not response or not response.ok:
if not response or response.is_error:
return False
try:
response_json = response.json()
except httpx.DecodingError:
return False
response_json = json.loads(response.content)
# EXTREMELY rare edge case where raw html is sent instead of json.
return isinstance(response_json, dict)


def is_valid_async_response(response: requests.Response) -> bool:
def is_valid_async_response(response: httpx.Response) -> bool:
"""
Checks if the asynchronous response is valid. Also checks if it is a valid synchronous response.

Returns True if the response is valid.

:param response: a requests response object.
:param response: an httpx response object.
:return: bool
"""
if not is_valid_sync_response(response):
Expand All @@ -43,15 +46,15 @@ def is_valid_async_response(response: requests.Response) -> bool:
return False


def clean_request_json(response: requests.Response) -> StringDict:
def clean_request_json(response: httpx.Response) -> StringDict:
"""
Checks and correct the response error format depending on the two possible kind of returns.

:param response: Raw request response.
:return: Returns the job error if the error is due to parsing, returns the http error otherwise.
"""
response_json = response.json()
if response.status_code < 200 or response.status_code > 302:
if response.is_error:
response_json["status_code"] = response.status_code
return response_json
corrected_json = response_json
Expand Down
4 changes: 2 additions & 2 deletions mindee/pdf/extracted_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def get_page_count(self) -> int:
try:
pdf = pdfium.PdfDocument(self.pdf_bytes)
return len(pdf)
except Exception as exc:
except Exception as e:
raise MindeeError(
"Could not retrieve page count from Extracted PDF object."
) from exc
) from e

def save_to_file(self, output_path: Path | str):
"""
Expand Down
4 changes: 2 additions & 2 deletions mindee/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def load_prediction(
if local_response.as_dict.get("job"):
return AsyncPredictResponse(product_class, local_response.as_dict)
return PredictResponse(product_class, local_response.as_dict)
except KeyError as exc:
raise MindeeError("No prediction found in local response.") from exc
except KeyError as e:
raise MindeeError("No prediction found in local response.") from e

def parse_queued(
self,
Expand Down
Loading
Loading