Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using pydantic for SoftwareVersion type and fixing edge case for datetime type #6

Merged
merged 2 commits into from
Sep 10, 2024
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
50 changes: 16 additions & 34 deletions pa_api/xmlapi/types/operations/software.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
from dataclasses import dataclass
from datetime import datetime
from typing import Optional

from pydantic import ConfigDict, Field

from pa_api.utils import (
first,
)
from pa_api.xmlapi.types.utils import (
mksx,
pd,
Bool,
Datetime,
String,
XMLBaseModel,
)


@dataclass
class SoftwareVersion:
# TODO: Use pydantic
version: str
filename: str
released_on: datetime
downloaded: bool
current: bool
latest: bool
uploaded: bool
class SoftwareVersion(XMLBaseModel):
model_config = ConfigDict(extra="ignore")

@staticmethod
def from_xml(xml):
# TODO: Use correct pydantic functionalities
if isinstance(xml, (list, tuple)):
xml = first(xml)
if xml is None:
return None
p = mksx(xml)
return SoftwareVersion(
p("./version/text()"),
p("./filename/text()"),
p("./released-on/text()", parser=pd),
p("./downloaded/text()") != "no",
p("./current/text()") != "no",
p("./latest/text()") != "no",
p("./uploaded/text()") != "no",
)
version: String
filename: String
released_on: Optional[Datetime] = Field(alias="released-on")
downloaded: Bool
current: Bool
latest: Bool
uploaded: Bool

@property
def base_minor_version(self) -> str:
Expand Down
9 changes: 8 additions & 1 deletion pa_api/xmlapi/types/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def from_xml(cls, xml) -> Self:

def parse_datetime(d):
try:
if d is None or d == "none":
if d is None or d in ("none", "Unknown"):
return None
return datetime.strptime(d, DATETIME_FORMAT)
except Exception as e:
Expand Down Expand Up @@ -102,6 +102,12 @@ def ensure_list(v: Any) -> typing.List[Any]:
# print(ta.validate_python())


def xml_text(v: Any):
if isinstance(v, dict) and "#text" in v:
return v["#text"]
return v


def ensure_str(v: Any) -> str:
if v is None:
return ""
Expand All @@ -119,6 +125,7 @@ def validate_ip(v: Any) -> str:


String = Annotated[str, BeforeValidator(ensure_str)]
Bool = Annotated[bool, BeforeValidator(xml_text)]
Ip = Annotated[str, BeforeValidator(validate_ip)]


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pa-api-sdk"
version = "0.1.1"
version = "0.1.2"
description = ""
authors = ["David Gallay <[email protected]>"]
readme = "README.md"
Expand Down
Loading