Skip to content

Commit

Permalink
siegfried - use lists for SiegfriedMatch.basis and .warning
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoCampinoti94 committed Oct 17, 2023
1 parent a357478 commit 109745e
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions acacore/siegfried/siegfried.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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]:
"""
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 109745e

Please sign in to comment.