Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix list index out of range for comicrack #604

Closed
wants to merge 7 commits into from
32 changes: 26 additions & 6 deletions kindlecomicconverter/comic2ebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ def buildEPUB(path, chapternames, tomenumber):
tomenumber), options.uuid))
# Overwrite chapternames if tree is flat and ComicInfo.xml has bookmarks
Copy link
Collaborator

@axu2 axu2 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this entire code section should be skipped if a 400 mb split is detected (as if the xml doesn't exist?)? If it can be logically separated, make it a separate PR.

if not chapternames and options.chapters:
filelen = len(filelist)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use len(filelist) per comment below.

chapterlist = []

global_diff = 0
Expand All @@ -525,16 +526,25 @@ def buildEPUB(path, chapternames, tomenumber):
elif options.splitter == 2:
diff_delta = 2

if options.spreadadjust and options.splitter != 1:
diff_delta -= 1

for aChapter in options.chapters:
pageid = aChapter[0]
cur_diff = global_diff
global_diff = 0

for x in range(0, pageid + cur_diff + 1):
if '-kcc-b' in filelist[x][1]:
pageid += diff_delta
global_diff += diff_delta

if x < filelen:
if '-kcc-b' in filelist[x][1]:
pageid += diff_delta
global_diff += diff_delta
if options.spreadadjust and options.splitter == 1 and '-kcc-a' in filelist[x][1]:
pageid -= 1
global_diff -= 1

if pageid >= filelen:
pageid = filelen - 1
Copy link
Collaborator

@axu2 axu2 Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pageid = min(pageid, len(filelist) -1)

I assume this is the code related to bookmarks near the end? Add a clarifying comment.

Moreover, probably should just use len(filelist) since you are using the var less often.

filename = filelist[pageid][1]
chapterlist.append((filelist[pageid][0].replace('Images', 'Text'), filename))
chapternames[filename] = aChapter[1]
Expand Down Expand Up @@ -576,7 +586,8 @@ def imgDirectoryProcessing(path):
raise UserWarning("Source directory is empty.")


def imgFileProcessingTick(output):
def imgFileProcessingTick(result):
output, spreadadjust = result
if isinstance(output, tuple):
workerOutput.append(output)
workerPool.terminate()
Expand All @@ -585,6 +596,9 @@ def imgFileProcessingTick(output):
if page is not None:
options.imgMetadata[page[0]] = page[1]
options.imgOld.append(page[2])
# options.doublepageattribute is True when there is a DoublePage attribute already found in the xml
if not options.doublepageattribute and not options.spreadadjust:
options.spreadadjust = spreadadjust
if GUI:
GUI.progressBarTick.emit('tick')
if not GUI.conversionAlive:
Expand All @@ -598,6 +612,7 @@ def imgFileProcessing(work):
opt = work[2]
output = []
workImg = image.ComicPageParser((dirpath, afile), opt)
spreadadjust = workImg.opt.spreadadjust
for i in workImg.payload:
img = image.ComicPage(opt, *i)
if opt.cropping == 2 and not opt.webtoon:
Expand All @@ -609,7 +624,7 @@ def imgFileProcessing(work):
if opt.forcepng and not opt.forcecolor:
img.quantizeImage()
output.append(img.saveToDir())
return output
return (output, spreadadjust)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for parentheses to return a tuple

except Exception:
return str(sys.exc_info()[1]), sanitizeTrace(sys.exc_info()[2])

Expand Down Expand Up @@ -691,6 +706,9 @@ def getComicInfo(path, originalpath):
xmlPath = os.path.join(path, 'ComicInfo.xml')
options.authors = ['KCC']
options.chapters = []
options.doublepageattribute = False
# toggled only when splitCheck finds spread pages in source files AND the ComicInfo.xml does not find a DoublePage attribute in Pages list
options.spreadadjust = False
options.summary = ''
titleSuffix = ''
if options.title == 'defaulttitle':
Expand Down Expand Up @@ -728,6 +746,8 @@ def getComicInfo(path, originalpath):
options.authors = ['KCC']
if xml.data['Bookmarks']:
options.chapters = xml.data['Bookmarks']
if xml.data['DoublePages']:
options.doublepageattribute = xml.data['DoublePages']
if xml.data['Summary']:
options.summary = hescape(xml.data['Summary'])
os.remove(xmlPath)
Expand Down
1 change: 1 addition & 0 deletions kindlecomicconverter/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def splitCheck(self):
if self.opt.splitter > 0:
self.payload.append(['R', self.source, self.image.rotate(90, Image.Resampling.BICUBIC, True),
self.color, self.fill])
self.opt.spreadadjust = True
else:
self.payload.append(['N', self.source, self.image, self.color, self.fill])

Expand Down
3 changes: 3 additions & 0 deletions kindlecomicconverter/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, source):
'Colorists': [],
'Summary': '',
'Bookmarks': [],
'DoublePages': False,
'Title': ''}
self.rawdata = None
self.format = None
Expand Down Expand Up @@ -72,6 +73,8 @@ def parseXML(self):
if 'Bookmark' in page.attributes and 'Image' in page.attributes:
self.data['Bookmarks'].append((int(page.attributes['Image'].value),
page.attributes['Bookmark'].value))
if 'DoublePage' in page.attributes:
self.data['DoublePages'] = True

def saveXML(self):
if self.rawdata:
Expand Down