-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomicinfoxml.py
More file actions
408 lines (344 loc) · 15.2 KB
/
comicinfoxml.py
File metadata and controls
408 lines (344 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""A class to encapsulate ComicRack's ComicInfo.xml data."""
# Copyright 2012-2014 ComicTagger Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
import xml.etree.ElementTree as ET
from typing import Any
from typing import TYPE_CHECKING
from comicapi import utils
from comicapi.genericmetadata import FileHash
from comicapi.genericmetadata import GenericMetadata
from comicapi.genericmetadata import PageMetadata
from comicapi.tags import Tag
if TYPE_CHECKING:
from comicapi.archivers import Archiver
logger = logging.getLogger(f'comicapi.metadata.{__name__}')
class ComicInfoXml(Tag):
enabled = True
id = 'cix'
def __init__(self, version: str) -> None:
super().__init__(version)
self.file = 'ComicInfo.xml'
self.supported_attributes = {
'original_hash',
'series',
'issue',
'issue_count',
'title',
'gtin',
'volume',
'genres',
'description',
'notes',
'alternate_series',
'alternate_number',
'alternate_count',
'story_arcs',
'series_groups',
'publisher',
'imprint',
'day',
'month',
'year',
'language',
'web_links',
'format',
'manga',
'black_and_white',
'maturity_rating',
'critical_rating',
'scan_info',
'tags',
'pages',
'pages.bookmark',
'pages.double_page',
'pages.height',
'pages.image_index',
'pages.size',
'pages.type',
'pages.width',
'page_count',
'characters',
'teams',
'locations',
'credits',
'credits.person',
'credits.role',
}
def supports_credit_role(self, role: str) -> bool:
return role.casefold() in self._get_parseable_credits()
def supports_tags(self, archive: Archiver) -> bool:
return archive.supports_files()
def has_tags(self, archive: Archiver) -> bool:
return (
self.supports_tags(archive)
and self.file in archive.get_filename_list()
and self._validate_bytes(archive.read_file(self.file))
)
def remove_tags(self, archive: Archiver) -> bool:
return self.has_tags(archive) and archive.remove_file(self.file)
def read_tags(self, archive: Archiver) -> GenericMetadata:
if self.has_tags(archive):
metadata = archive.read_file(self.file) or b''
if self._validate_bytes(metadata):
return self._metadata_from_bytes(metadata)
return GenericMetadata()
def read_raw_tags(self, archive: Archiver) -> str:
if self.has_tags(archive):
return ET.tostring(ET.fromstring(archive.read_file(self.file)), encoding='unicode', xml_declaration=True)
return ''
def write_tags(self, metadata: GenericMetadata, archive: Archiver) -> bool:
if self.supports_tags(archive):
xml = b''
if self.has_tags(archive):
xml = archive.read_file(self.file)
return archive.write_file(self.file, self._bytes_from_metadata(metadata, xml))
logger.warning('Archive (%s) does not support %s metadata', archive.name(), self.name())
return False
def name(self) -> str:
return 'Comic Info XML'
@classmethod
def _get_parseable_credits(cls) -> list[str]:
parsable_credits: list[str] = []
parsable_credits.extend(GenericMetadata.writer_synonyms)
parsable_credits.extend(GenericMetadata.penciller_synonyms)
parsable_credits.extend(GenericMetadata.inker_synonyms)
parsable_credits.extend(GenericMetadata.colorist_synonyms)
parsable_credits.extend(GenericMetadata.letterer_synonyms)
parsable_credits.extend(GenericMetadata.cover_synonyms)
parsable_credits.extend(GenericMetadata.editor_synonyms)
parsable_credits.extend(GenericMetadata.translator_synonyms)
return parsable_credits
def _metadata_from_bytes(self, string: bytes) -> GenericMetadata:
root = ET.fromstring(string)
return self._convert_xml_to_metadata(root)
def _bytes_from_metadata(self, metadata: GenericMetadata, xml: bytes = b'') -> bytes:
root = self._convert_metadata_to_xml(metadata, xml)
return ET.tostring(root, encoding='utf-8', xml_declaration=True)
def _convert_metadata_to_xml(self, metadata: GenericMetadata, xml: bytes = b'') -> ET.Element:
# shorthand for the metadata
md = metadata
if xml:
root = ET.fromstring(xml)
else:
# build a tree structure
root = ET.Element('ComicInfo')
root.attrib['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'
root.attrib['xmlns:xsd'] = 'http://www.w3.org/2001/XMLSchema'
# helper func
def assign(cr_entry: str, md_entry: Any) -> None:
if md_entry:
text = str(md_entry)
if isinstance(md_entry, (list, set)):
text = ','.join(md_entry)
et_entry = root.find(cr_entry)
if et_entry is not None:
et_entry.text = text
else:
ET.SubElement(root, cr_entry).text = text
else:
et_entry = root.find(cr_entry)
if et_entry is not None:
root.remove(et_entry)
# need to specially process the credits, since they are structured
# differently than CIX
credit_writer_list = []
credit_penciller_list = []
credit_inker_list = []
credit_colorist_list = []
credit_letterer_list = []
credit_cover_list = []
credit_editor_list = []
credit_translator_list = []
# first, loop thru credits, and build a list for each role that CIX
# supports
for credit in metadata.credits:
if credit.role.casefold() in GenericMetadata.writer_synonyms:
credit_writer_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.penciller_synonyms:
credit_penciller_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.inker_synonyms:
credit_inker_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.colorist_synonyms:
credit_colorist_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.letterer_synonyms:
credit_letterer_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.cover_synonyms:
credit_cover_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.editor_synonyms:
credit_editor_list.append(credit.person.replace(',', ''))
if credit.role.casefold() in GenericMetadata.translator_synonyms:
credit_translator_list.append(credit.person.replace(',', ''))
assign('Series', md.series)
assign('Number', md.issue)
assign('Count', md.issue_count)
assign('Title', md.title)
assign('GTIN', md.gtin)
assign('Volume', md.volume)
assign('Genre', md.genres)
assign('Summary', md.description)
assign('Notes', md.notes)
assign('AlternateSeries', md.alternate_series)
assign('AlternateNumber', md.alternate_number)
assign('AlternateCount', md.alternate_count)
assign('StoryArc', md.story_arcs)
assign('SeriesGroup', md.series_groups)
assign('Publisher', md.publisher)
assign('Imprint', md.imprint)
assign('Day', md.day)
assign('Month', md.month)
assign('Year', md.year)
assign('LanguageISO', md.language)
assign('Web', ' '.join(u.url for u in md.web_links))
assign('Format', md.format)
assign('Manga', md.manga)
assign('BlackAndWhite', 'Yes' if md.black_and_white else None)
assign('AgeRating', md.maturity_rating)
assign('CommunityRating', md.critical_rating)
scan_info = md.scan_info or ''
if md.original_hash:
scan_info += f" sum:{md.original_hash}"
assign('ScanInformation', scan_info)
assign('Tags', md.tags)
assign('PageCount', md.page_count)
assign('Characters', md.characters)
assign('Teams', md.teams)
assign('Locations', md.locations)
assign('Writer', credit_writer_list)
assign('Penciller', credit_penciller_list)
assign('Inker', credit_inker_list)
assign('Colorist', credit_colorist_list)
assign('Letterer', credit_letterer_list)
assign('CoverArtist', credit_cover_list)
assign('Editor', credit_editor_list)
assign('Translator', credit_translator_list)
# loop and add the page entries under pages node
pages_node = root.find('Pages')
if pages_node is not None:
pages_node.clear()
else:
pages_node = ET.SubElement(root, 'Pages')
for page in md.pages:
page_node = ET.SubElement(pages_node, 'Page')
page_node.attrib = {'Image': str(page.display_index)}
if page.bookmark:
page_node.attrib['Bookmark'] = page.bookmark
if page.type:
page_node.attrib['Type'] = page.type
if page.double_page is not None:
page_node.attrib['DoublePage'] = str(page.double_page)
if page.height is not None:
page_node.attrib['ImageHeight'] = str(page.height)
if page.byte_size is not None:
page_node.attrib['ImageSize'] = str(page.byte_size)
if page.width is not None:
page_node.attrib['ImageWidth'] = str(page.width)
page_node.attrib = dict(sorted(page_node.attrib.items()))
ET.indent(root)
return root
def _convert_xml_to_metadata(self, root: ET.Element) -> GenericMetadata:
if root.tag != 'ComicInfo':
raise Exception('Not a ComicInfo file')
def get(name: str) -> str | None:
tag = root.find(name)
if tag is None:
return None
return tag.text
md = GenericMetadata()
md.series = utils.xlate(get('Series'))
md.issue = utils.xlate(get('Number'))
md.issue_count = utils.xlate_int(get('Count'))
md.title = utils.xlate(get('Title'))
md.gtin = utils.xlate(get('GTIN'))
md.volume = utils.xlate_int(get('Volume'))
md.genres = set(utils.split(get('Genre'), ','))
md.description = utils.xlate(get('Summary'))
md.notes = utils.xlate(get('Notes'))
md.alternate_series = utils.xlate(get('AlternateSeries'))
md.alternate_number = utils.xlate(get('AlternateNumber'))
md.alternate_count = utils.xlate_int(get('AlternateCount'))
md.story_arcs = utils.split(get('StoryArc'), ',')
md.series_groups = utils.split(get('SeriesGroup'), ',')
md.publisher = utils.xlate(get('Publisher'))
md.imprint = utils.xlate(get('Imprint'))
md.day = utils.xlate_int(get('Day'))
md.month = utils.xlate_int(get('Month'))
md.year = utils.xlate_int(get('Year'))
md.language = utils.xlate(get('LanguageISO'))
md.web_links = utils.split_urls(utils.xlate(get('Web')))
md.format = utils.xlate(get('Format'))
md.manga = utils.xlate(get('Manga'))
md.maturity_rating = utils.xlate(get('AgeRating'))
md.critical_rating = utils.xlate_float(get('CommunityRating'))
scan_info_list = (utils.xlate(get('ScanInformation')) or '').split()
for word in scan_info_list.copy():
if not word.startswith('sum:'):
continue
original_hash = FileHash.parse(word[4:])
if original_hash:
md.original_hash = original_hash
scan_info_list.remove(word)
break
if scan_info_list:
md.scan_info = ' '.join(scan_info_list)
md.tags = set(utils.split(get('Tags'), ','))
md.page_count = utils.xlate_int(get('PageCount'))
md.characters = set(utils.split(get('Characters'), ','))
md.teams = set(utils.split(get('Teams'), ','))
md.locations = set(utils.split(get('Locations'), ','))
tmp = utils.xlate(get('BlackAndWhite'))
if tmp is not None:
md.black_and_white = tmp.casefold() in {'yes', 'true', '1'}
# Now extract the credit info
for n in root:
if n.tag in {'Writer', 'Penciller', 'Inker', 'Colorist', 'Letterer', 'Editor'} and n.text is not None:
for name in utils.split(n.text, ','):
md.add_credit(name.strip(), n.tag)
if n.tag == 'CoverArtist' and n.text is not None:
for name in utils.split(n.text, ','):
md.add_credit(name.strip(), 'Cover')
# parse page data now
pages_node = root.find('Pages')
if pages_node is not None:
for i, page in enumerate(pages_node):
p: dict[str, Any] = page.attrib
md_page = PageMetadata(
filename='', # cix doesn't record the filename it just assumes it's always ordered the same
display_index=int(p.get('Image', i)),
archive_index=i,
bookmark=p.get('Bookmark', ''),
type='',
)
md_page.set_type(p.get('Type', ''))
if isinstance(p.get('DoublePage', None), str):
md_page.double_page = p['DoublePage'].casefold() in ('yes', 'true', '1')
if p.get('ImageHeight', '').isnumeric():
md_page.height = int(float(p['ImageHeight']))
if p.get('ImageWidth', '').isnumeric():
md_page.width = int(float(p['ImageWidth']))
if p.get('ImageSize', '').isnumeric():
md_page.byte_size = int(float(p['ImageSize']))
md.pages.append(md_page)
md.is_empty = False
return md
def _validate_bytes(self, string: bytes) -> bool:
"""Verify that the string actually contains CIX data in XML format."""
try:
root = ET.fromstring(string)
if root.tag != 'ComicInfo':
return False
except ET.ParseError:
return False
return True