Skip to content

Commit

Permalink
Reject a calendar mcf file more gracefully than with a list index out…
Browse files Browse the repository at this point in the history
… of range error!
  • Loading branch information
AnEnglishmanInNorway committed Nov 19, 2024
1 parent 9c4d091 commit 2af477f
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions cewe2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,12 +1283,7 @@ def convertMcf(albumname, keepDoublePages: bool, pageNumbers=None, mcfxTmpDir=No
sys.exit(1)

fotobook = mcf.getroot()
if fotobook.tag != 'fotobook':
invalidmsg = f"Cannot process invalid mcf file (root tag is not 'fotobook'): {mcfxmlname}"
if mcfxFormat:
invalidmsg = invalidmsg + f" (unpacked from {albumname})"
logging.error(invalidmsg)
sys.exit(1)
ensureAcceptableAlbumMcf(fotobook, albumname, mcfxmlname, mcfxFormat)

# check output file is acceptable before we do any processing
outputFileName = getOutputFileName(albumname)
Expand Down Expand Up @@ -1655,15 +1650,19 @@ def convertMcf(albumname, keepDoublePages: bool, pageNumbers=None, mcfxTmpDir=No
try:
if (n == 0) or (n == pageCount - 1): # numeric comparisons read best like this so pylint: disable=consider-using-in
pageNumber = 0
page = [i for i in
fotobook.findall("./page[@pagenr='0'][@type='FULLCOVER']")
+ fotobook.findall("./page[@pagenr='0'][@type='fullcover']")
if (i.find("./area") is not None)
][0]
oddpage = (n == 0) # bool assign is clearer with parens so pylint: disable=superfluous-parens
pagetype = 'cover'
# for double-page-layout: the last page is already the left side of the book cover. So skip rendering the last page
if ((keepDoublePages is True) and (n == (pageCount - 1))):
fullcoverpages = [i for i in
fotobook.findall("./page[@pagenr='0'][@type='FULLCOVER']")
+ fotobook.findall("./page[@pagenr='0'][@type='fullcover']")
if (i.find("./area") is not None)]
if len(fullcoverpages) == 1: # in a well-formed album there are two fullcover pages, but only one with an area
page = fullcoverpages[0]
oddpage = (n == 0) # bool assign is clearer with parens so pylint: disable=superfluous-parens
pagetype = 'cover'
# for double-page-layout: the last page is already the left side of the book cover. So skip rendering the last page
if ((keepDoublePages is True) and (n == (pageCount - 1))):
page = None
else:
logging.warning(f"Cannot locate a cover page, is this really an album?")
page = None
elif n == 1:
pageNumber = 1
Expand Down Expand Up @@ -1769,6 +1768,23 @@ def convertMcf(albumname, keepDoublePages: bool, pageNumbers=None, mcfxTmpDir=No
return True


def ensureAcceptableAlbumMcf(fotobook, albumname, mcfxmlname, mcfxFormat):
if fotobook.tag != 'fotobook':
invalidmsg = f"Cannot process invalid mcf file (root tag is not 'fotobook'): {mcfxmlname}"
if mcfxFormat:
invalidmsg = invalidmsg + f" (unpacked from {albumname})"
logging.error(invalidmsg)
sys.exit(1)

startdatecalendarium = fotobook.attrib['startdatecalendarium']
if startdatecalendarium is not None and len(startdatecalendarium) > 0:
invalidmsg = f"Cannot process calendar mcf files (yet!): {mcfxmlname}"
if mcfxFormat:
invalidmsg = invalidmsg + f" (unpacked from {albumname})"
logging.error(invalidmsg)
sys.exit(1)


def getHpsDataFolder():
# linux + macosx
dotMcfFolder = os.path.expanduser("~/.mcf/hps/")
Expand Down

0 comments on commit 2af477f

Please sign in to comment.