From 109745e05199db44fbfbff1f16d120c327c2d853 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Tue, 17 Oct 2023 12:39:29 +0200 Subject: [PATCH] siegfried - use lists for SiegfriedMatch.basis and .warning --- acacore/siegfried/siegfried.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/acacore/siegfried/siegfried.py b/acacore/siegfried/siegfried.py index 4b07b23..dbec98c 100644 --- a/acacore/siegfried/siegfried.py +++ b/acacore/siegfried/siegfried.py @@ -11,7 +11,7 @@ from pydantic import BaseModel from pydantic import ConfigDict from pydantic import Field -from pydantic import field_validator +from pydantic import model_validator from pydantic.networks import AnyUrl from pydantic.networks import HttpUrl @@ -48,8 +48,8 @@ class SiegfriedMatch(BaseModel): version: Optional[str] = None mime: str match_class: Optional[str] = Field(None, alias="class") - basis: str - warning: str + basis: list[str] + warning: list[str] URI: Optional[AnyUrl] = None permalink: Optional[HttpUrl] = None @@ -60,8 +60,10 @@ def byte_match(self) -> Optional[int]: Returns: The length of the byte match or None, if the match was not on the basis of bytes. """ - match = _byte_match_regexp_single.match(self.basis) or _byte_match_regexp_multi.match(self.basis) - return (int(match.group(3)) - int(match.group(2))) if match else None + for basis in self.basis: + match = _byte_match_regexp_single.match(basis) or _byte_match_regexp_multi.match(basis) + return (int(match.group(3)) - int(match.group(2))) if match else None + return None def extension_match(self) -> Optional[str]: """ @@ -70,8 +72,10 @@ def extension_match(self) -> Optional[str]: Returns: The matched extension or None, if the match was not on the basis of the extension. """ - match = _extension_match.match(self.basis) - return match.group(2) if match else None + for basis in self.basis: + match = _extension_match.match(basis) + return match.group(2) if match else None + return None def extension_mismatch(self) -> bool: """ @@ -108,11 +112,17 @@ def sort_tuple(self) -> tuple[int, int, int, int, int]: ) # noinspection PyNestedDecorators - @field_validator("id") + @model_validator(mode="before") @classmethod - def unknown_id(cls, _id: Optional[str]): - _id = (_id or "").strip() - return None if _id.lower() == "unknown" else _id or None + def unknown_id(cls, data: dict | object): + if isinstance(data, dict): + return { + **data, + "id": None if data["id"].lower().strip() == "unknown" else data["id"].strip() or None, + "basis": data["basis"].strip().split(";"), + "warning": data["warning"].strip().split(";"), + } + return data class SiegfriedFile(BaseModel):