From c8507c08a91aee6a8c2b8ee3e012879edb663506 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sat, 23 Dec 2023 23:50:58 -0800 Subject: [PATCH] Ensure ComicRack and CoMet metadata preserve unknown xml tags --- comicapi/metadata/comet.py | 22 +++++++++++++--------- comicapi/metadata/comicrack.py | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/comicapi/metadata/comet.py b/comicapi/metadata/comet.py index 67a42a3..e87616b 100644 --- a/comicapi/metadata/comet.py +++ b/comicapi/metadata/comet.py @@ -121,7 +121,8 @@ class CoMet(Metadata): success = True if self.file != self.comet_filename: success = self.remove_metadata(archive) - return archive.write_file(self.comet_filename, self._bytes_from_metadata(metadata)) and success + xml = self._bytes_from_metadata(metadata, archive.read_file(self.comet_filename)) + return success and archive.write_file(self.comet_filename, xml) else: logger.warning(f"Archive ({archive.name()}) does not support {self.name()} metadata") return False @@ -145,19 +146,22 @@ class CoMet(Metadata): tree = ET.ElementTree(ET.fromstring(string)) return self._convert_xml_to_metadata(tree, archive) - def _bytes_from_metadata(self, metadata: GenericMetadata) -> bytes: - tree = self._convert_metadata_to_xml(metadata) + def _bytes_from_metadata(self, metadata: GenericMetadata, xml: bytes = b"") -> bytes: + tree = self._convert_metadata_to_xml(metadata, xml) return ET.tostring(tree.getroot(), encoding="utf-8", xml_declaration=True) - def _convert_metadata_to_xml(self, metadata: GenericMetadata) -> ET.ElementTree: + def _convert_metadata_to_xml(self, metadata: GenericMetadata, xml: bytes = b"") -> ET.ElementTree: # shorthand for the metadata md = metadata - # build a tree structure - root = ET.Element("comet") - root.attrib["xmlns:comet"] = "http://www.denvog.com/comet/" - root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance" - root.attrib["xsi:schemaLocation"] = "http://www.denvog.com http://www.denvog.com/comet/comet.xsd" + if xml: + root = ET.fromstring(xml) + else: + # build a tree structure + root = ET.Element("comet") + root.attrib["xmlns:comet"] = "http://www.denvog.com/comet/" + root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance" + root.attrib["xsi:schemaLocation"] = "http://www.denvog.com http://www.denvog.com/comet/comet.xsd" # helper func def assign(comet_entry: str, md_entry: Any) -> None: diff --git a/comicapi/metadata/comicrack.py b/comicapi/metadata/comicrack.py index 96744ec..e1faca0 100644 --- a/comicapi/metadata/comicrack.py +++ b/comicapi/metadata/comicrack.py @@ -118,7 +118,7 @@ class ComicRack(Metadata): def set_metadata(self, metadata: GenericMetadata, archive: Archiver) -> bool: if self.supports_metadata(archive): - return archive.write_file(self.file, self._bytes_from_metadata(metadata)) + return archive.write_file(self.file, self._bytes_from_metadata(metadata, archive.read_file(self.file))) else: logger.warning(f"Archive ({archive.name()}) does not support {self.name()} metadata") return False