Edit book: Check book: Auto fix package identifier being empty. Fixes #1963748 [Add automatic solution for empty dc:identifier](https://bugs.launchpad.net/calibre/+bug/1963748)

This commit is contained in:
Kovid Goyal 2022-03-16 12:15:59 +05:30
parent 9ab58a73c6
commit 852b9d8cd2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -223,7 +223,7 @@ class MultipleCovers(BaseError):
class NoUID(BaseError): class NoUID(BaseError):
HELP = xml(_( HELP = xml(_(
'The OPF must have a unique identifier, i.e. a <dc:identifier> element whose id is referenced' 'The OPF must have an unique identifier, i.e. a <dc:identifier> element whose id is referenced'
' by the <package> element')) ' by the <package> element'))
INDIVIDUAL_FIX = _('Auto-generate a unique identifier') INDIVIDUAL_FIX = _('Auto-generate a unique identifier')
@ -251,10 +251,18 @@ class NoUID(BaseError):
class EmptyIdentifier(BaseError): class EmptyIdentifier(BaseError):
HELP = xml(_('The <dc:identifier> element must not be empty.')) HELP = xml(_('The <dc:identifier> element must not be empty.'))
INDIVIDUAL_FIX = _('Remove empty identifiers')
def __init__(self, name, lnum): def __init__(self, name, lnum):
BaseError.__init__(self, _('Empty identifier element'), name, lnum) BaseError.__init__(self, _('Empty identifier element'), name, lnum)
def __call__(self, container):
for dcid in container.opf_xpath('/opf:package/opf:metadata/dc:identifier'):
if not dcid.text or not dcid.text.strip():
container.remove_from_xml(dcid)
container.dirty(container.opf_name)
return True
class BadSpineMime(BaseError): class BadSpineMime(BaseError):
@ -387,8 +395,12 @@ def check_opf(container):
errors.append(NookCover(container.opf_name, cover.sourceline)) errors.append(NookCover(container.opf_name, cover.sourceline))
uid = container.opf.get('unique-identifier', None) uid = container.opf.get('unique-identifier', None)
if uid is None or not container.opf_xpath('/opf:package/opf:metadata/dc:identifier[@id=%r]' % uid): if uid is None:
errors.append(NoUID(container.opf_name)) errors.append(NoUID(container.opf_name))
else:
dcid = container.opf_xpath('/opf:package/opf:metadata/dc:identifier[@id=%r]' % uid)
if not dcid or not dcid[0].text or not dcid[0].text.strip():
errors.append(NoUID(container.opf_name))
for elem in container.opf_xpath('/opf:package/opf:metadata/dc:identifier'): for elem in container.opf_xpath('/opf:package/opf:metadata/dc:identifier'):
if not elem.text or not elem.text.strip(): if not elem.text or not elem.text.strip():
errors.append(EmptyIdentifier(container.opf_name, elem.sourceline)) errors.append(EmptyIdentifier(container.opf_name, elem.sourceline))