88from platform import python_version
99from tempfile import TemporaryDirectory
1010
11+ from pydantic import ValidationError
12+
1113from perdoo import ARCHIVE_EXTENSIONS , IMAGE_EXTENSIONS , __version__ , setup_logging
1214from perdoo .archives import BaseArchive , CB7Archive , CBTArchive , CBZArchive , get_archive
1315from perdoo .console import CONSOLE
@@ -53,14 +55,33 @@ def convert_collection(path: Path, output: OutputFormat) -> None:
5355def read_archive (archive : BaseArchive ) -> tuple [Metadata , MetronInfo , ComicInfo ]:
5456 filenames = archive .list_filenames ()
5557 metadata = None
56- if "Metadata.xml" in filenames :
57- metadata = Metadata .from_bytes (content = archive .read_file (filename = "Metadata.xml" ))
58+ try :
59+ if "/Metadata.xml" in filenames :
60+ metadata = Metadata .from_bytes (content = archive .read_file (filename = "/Metadata.xml" ))
61+ elif "Metadata.xml" in filenames :
62+ metadata = Metadata .from_bytes (content = archive .read_file (filename = "Metadata.xml" ))
63+ except ValidationError :
64+ LOGGER .error ("%s contains an invalid Metadata file" , archive .path .name ) # noqa: TRY400
5865 metron_info = None
59- if "MetronInfo.xml" in filenames :
60- metron_info = MetronInfo .from_bytes (content = archive .read_file (filename = "MetronInfo.xml" ))
66+ try :
67+ if "/MetronInfo.xml" in filenames :
68+ metron_info = MetronInfo .from_bytes (
69+ content = archive .read_file (filename = "/MetronInfo.xml" )
70+ )
71+ elif "MetronInfo.xml" in filenames :
72+ metron_info = MetronInfo .from_bytes (
73+ content = archive .read_file (filename = "MetronInfo.xml" )
74+ )
75+ except ValidationError :
76+ LOGGER .error ("%s contains an invalid MetronInfo file" , archive .path .name ) # noqa: TRY400
6177 comic_info = None
62- if "ComicInfo.xml" in filenames :
63- comic_info = ComicInfo .from_bytes (content = archive .read_file (filename = "ComicInfo.xml" ))
78+ try :
79+ if "/ComicInfo.xml" in filenames :
80+ comic_info = ComicInfo .from_bytes (content = archive .read_file (filename = "/ComicInfo.xml" ))
81+ elif "ComicInfo.xml" in filenames :
82+ comic_info = ComicInfo .from_bytes (content = archive .read_file (filename = "ComicInfo.xml" ))
83+ except ValidationError :
84+ LOGGER .error ("%s contains an invalid ComicInfo file" , archive .path .name ) # noqa: TRY400
6485
6586 if not metadata :
6687 if metron_info :
@@ -80,7 +101,7 @@ def fetch_from_services(
80101 settings : Settings , metainfo : tuple [Metadata , MetronInfo , ComicInfo ]
81102) -> None :
82103 marvel = None
83- if settings .marvel and settings .marvel .public_key and settings .marve .private_key :
104+ if settings .marvel and settings .marvel .public_key and settings .marvel .private_key :
84105 marvel = Marvel (settings = settings .marvel )
85106 metron = None
86107 if settings .metron and settings .metron .username and settings .metron .password :
@@ -115,7 +136,7 @@ def generate_filename(root: Path, extension: str, metadata: Metadata) -> Path:
115136 )
116137
117138 number_str = (
118- f"_#{ metadata .issue .number .zfill (3 if metadata .issue .format_ == Format .COMIC else 2 )} "
139+ f"_#{ metadata .issue .number .zfill (3 if metadata .issue .format == Format .COMIC else 2 )} "
119140 if metadata .issue .number
120141 else ""
121142 )
@@ -125,10 +146,10 @@ def generate_filename(root: Path, extension: str, metadata: Metadata) -> Path:
125146 Format .GRAPHIC_NOVEL : "_GN" ,
126147 Format .HARDCOVER : "_HC" ,
127148 Format .TRADE_PAPERBACK : "_TP" ,
128- }.get (metadata .issue .format_ , "" )
129- if metadata .issue .format_ in {Format .ANNUAL , Format .DIGITAL_CHAPTER }:
149+ }.get (metadata .issue .format , "" )
150+ if metadata .issue .format in {Format .ANNUAL , Format .DIGITAL_CHAPTER }:
130151 issue_filename = sanitize (value = series_filename ) + format_str + number_str
131- elif metadata .issue .format_ in {Format .GRAPHIC_NOVEL , Format .HARDCOVER , Format .TRADE_PAPERBACK }:
152+ elif metadata .issue .format in {Format .GRAPHIC_NOVEL , Format .HARDCOVER , Format .TRADE_PAPERBACK }:
132153 issue_filename = sanitize (value = series_filename ) + number_str + format_str
133154 else :
134155 issue_filename = sanitize (value = series_filename ) + number_str
@@ -145,7 +166,7 @@ def rename_images(folder: Path, filename: str) -> None:
145166 image_list = list_files (folder , * IMAGE_EXTENSIONS )
146167 pad_count = len (str (len (image_list )))
147168 for index , img_file in enumerate (image_list ):
148- new_filename = f"{ filename } - { str (index ).zfill (pad_count )} { img_file .suffix } "
169+ new_filename = f"{ filename } _ { str (index ).zfill (pad_count )} { img_file .suffix } "
149170 if img_file .name != new_filename :
150171 LOGGER .info ("Renamed %s to %s" , img_file .name , new_filename )
151172 img_file .rename (folder / f"{ filename } -{ str (index ).zfill (pad_count )} { img_file .suffix } " )
0 commit comments