Skip to content

Commit

Permalink
Let's not write a date if it has a bad year (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpepple authored Nov 25, 2024
1 parent 9b82ab4 commit 1ab0950
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions darkseid/metroninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from darkseid.utils import cast_id_as_str

METRON_INFO_XSD = Path("darkseid") / "schemas" / "MetronInfo" / "v1" / "MetronInfo.xsd"
EARLIEST_YEAR = 1900


class MetronInfo:
Expand Down Expand Up @@ -190,15 +190,15 @@ def _get_or_create_element(parent: ET.Element, tag: str) -> ET.Element:
return element

@staticmethod
def _assign(root: ET.Element, element: str, val: str | int | date | None = None) -> None:
def _assign(root: ET.Element, element: str, val: str | int | 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.strftime("%Y-%m-%d") if isinstance(val, date) else str(val)
et_entry.text = str(val) if isinstance(val, int) else val

@staticmethod
def _assign_datetime(root: ET.Element, element: str, val: datetime | None = None) -> None:
Expand All @@ -224,6 +224,19 @@ def _assign_basic_children(
if id_ := val.id_:
child_node.attrib["id"] = cast_id_as_str(id_)

@staticmethod
def _assign_date(root: ET.Element, element: str, val: date | None = None) -> None:
et_entry = root.find(element)
if val is None:
if et_entry is not None:
root.remove(et_entry)
else:
if val.year < EARLIEST_YEAR: # Info source has a bad year
return
if et_entry is None:
et_entry = ET.SubElement(root, element)
et_entry.text = val.strftime("%Y-%m-%d")

@staticmethod
def _assign_arc(root: ET.Element, vals: list[Arc]) -> None:
parent_node = MetronInfo._get_or_create_element(root, "Arcs")
Expand Down Expand Up @@ -395,8 +408,8 @@ def convert_metadata_to_xml(self, md: Metadata, xml=None) -> ET.ElementTree: #
self._assign(root, "Summary", md.comments)
if md.prices:
self._assign_price(root, md.prices)
self._assign(root, "CoverDate", md.cover_date)
self._assign(root, "StoreDate", md.store_date)
self._assign_date(root, "CoverDate", md.cover_date)
self._assign_date(root, "StoreDate", md.store_date)
self._assign(root, "PageCount", md.page_count)
if md.notes is not None and md.notes.metron_info:
self._assign(root, "Notes", md.notes.metron_info)
Expand Down Expand Up @@ -691,8 +704,9 @@ def write_xml(self, filename: Path, md: Metadata, xml=None) -> None:
xml (optional): Optional XML bytes to include.
"""
tree = self.convert_metadata_to_xml(md, xml)
mi_xsd = Path("darkseid") / "schemas" / "MetronInfo" / "v1" / "MetronInfo.xsd"
schema = XMLSchema11(mi_xsd)
# Let's validate the xml
schema = XMLSchema11(METRON_INFO_XSD)
try:
schema.validate(tree)
except XMLSchemaValidationError as e:
Expand Down

0 comments on commit 1ab0950

Please sign in to comment.