Skip to content

Commit

Permalink
Add LastModified to MetonInfo class
Browse files Browse the repository at this point in the history
  • Loading branch information
bpepple committed Sep 18, 2024
1 parent 0dda728 commit 24c373d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions darkseid/metroninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ def assign(root: ET.Element, element: str, val: str | int | date | None = None)
et_entry = ET.SubElement(root, element)
et_entry.text = val.strftime("%Y-%m-%d") if isinstance(val, date) else str(val)

@staticmethod
def assign_datetime(root: ET.Element, element: str, val: datetime | None = None) -> None:
et_entry = root.find(element)
if val is None:
if et_entry is not None:
root.remove(et_entry)
else:
if et_entry is None:
et_entry = ET.SubElement(root, element)
et_entry.text = val.isoformat(sep="T")

@staticmethod
def assign_basic_children(root: ET.Element, parent: str, child: str, vals: list[Basic]) -> None:
parent_node = MetronInfo.get_or_create_element(root, parent)
Expand Down Expand Up @@ -346,6 +357,7 @@ def convert_metadata_to_xml(self, md: Metadata, xml=None) -> ET.ElementTree: #
self.assign_gtin(root, md.gtin)
self.assign(root, "AgeRating", self.valid_age_rating(md.age_rating))
self.assign(root, "URL", md.web_link)
self.assign_datetime(root, "LastModified", md.modified)
if md.credits:
self.assign_credits(root, md.credits)

Expand Down Expand Up @@ -455,6 +467,12 @@ def get_publisher() -> Publisher | None:

return Publisher(publisher_name, publisher_id, imprint)

def get_modified() -> datetime | None:
resource = root.find("LastModified")
if resource is None:
return None
return datetime.fromisoformat(resource.text)

def _create_alt_name_list(element: ET.Element) -> list[AlternativeNames]:
return [
AlternativeNames(
Expand Down Expand Up @@ -565,6 +583,7 @@ def get_credits() -> list[Credit] | None:
md.gtin = get_gtin()
md.age_rating = get("AgeRating")
md.web_link = get("URL")
md.modified = get_modified()
md.credits = get_credits()

pages_node = root.find("Pages")
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import date
from datetime import date, datetime, timedelta, timezone
from decimal import Decimal

import pytest
Expand Down Expand Up @@ -39,6 +39,9 @@ def fake_metadata() -> Metadata:
md.comments = "Just some sample metadata."
md.black_and_white = True
md.is_empty = False
md.modified = datetime(
2024, 8, 12, 12, 13, 54, 87728, tzinfo=timezone(timedelta(days=-1, seconds=72000))
)

return md

Expand Down
6 changes: 5 additions & 1 deletion tests/test_metroninfo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import xml.etree.ElementTree as ET # noqa: N817
from datetime import date
from datetime import date, datetime, timedelta, timezone
from decimal import Decimal
from pathlib import Path

Expand Down Expand Up @@ -185,6 +185,7 @@ def test_metadata_from_string(metron_info):
<Name>Arc1</Name>
</Arc>
</Arcs>
<LastModified>2024-08-12T12:13:54.087728-04:00</LastModified>
<Credits>
<Credit>
<Creator id="123">Stan Lee</Creator>
Expand Down Expand Up @@ -224,6 +225,9 @@ def test_metadata_from_string(metron_info):
assert result.credits[0].person == "Stan Lee"
assert result.credits[0].id_ == 123
assert result.credits[0].role[0].name == "Writer"
assert result.modified == datetime(
2024, 8, 12, 12, 13, 54, 87728, tzinfo=timezone(timedelta(days=-1, seconds=72000))
)


def validate(xml_path: str, xsd_path: str) -> bool:
Expand Down

0 comments on commit 24c373d

Please sign in to comment.