Skip to content

Commit

Permalink
Merge pull request #6 from dga-nagra/fix_software_version
Browse files Browse the repository at this point in the history
Using pydantic for SoftwareVersion type and fixing edge case for datetime type
  • Loading branch information
divad1196 authored Sep 10, 2024
2 parents aed1b93 + b2c71bd commit 142f50a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
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

0 comments on commit 142f50a

Please sign in to comment.