2015-02-21 18:30:32 -08:00
|
|
|
"""A class to represent a single comic, be it file or folder of images"""
|
2015-02-16 04:27:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# Copyright 2012-2014 Anthony Beville
|
2015-02-16 04:27:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# 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
|
2015-02-16 04:27:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-16 04:27:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# 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.
|
2015-02-16 04:27:21 -08:00
|
|
|
|
2022-03-18 12:14:42 -07:00
|
|
|
import py7zr
|
2015-02-16 04:27:21 -08:00
|
|
|
import zipfile
|
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import subprocess
|
|
|
|
import platform
|
2015-02-21 18:30:32 -08:00
|
|
|
import time
|
2018-09-19 13:05:39 -07:00
|
|
|
import io
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2021-10-21 21:58:20 -07:00
|
|
|
import natsort
|
2021-12-12 18:46:28 -08:00
|
|
|
try:
|
|
|
|
from unrar.cffi import rarfile
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
try:
|
|
|
|
import Image
|
|
|
|
pil_available = True
|
|
|
|
except ImportError:
|
|
|
|
pil_available = False
|
|
|
|
|
2018-09-19 13:05:39 -07:00
|
|
|
from .comicinfoxml import ComicInfoXml
|
|
|
|
from .comicbookinfo import ComicBookInfo
|
|
|
|
from .comet import CoMet
|
|
|
|
from .genericmetadata import GenericMetadata, PageType
|
|
|
|
from .filenameparser import FileNameParser
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
sys.path.insert(0, os.path.abspath("."))
|
|
|
|
|
2015-02-16 04:27:21 -08:00
|
|
|
class MetaDataStyle:
|
2015-02-16 05:09:21 -08:00
|
|
|
CBI = 0
|
|
|
|
CIX = 1
|
|
|
|
COMET = 2
|
2015-02-21 18:30:32 -08:00
|
|
|
name = ['ComicBookLover', 'ComicRack', 'CoMet']
|
|
|
|
|
2022-03-18 12:14:42 -07:00
|
|
|
class SevenZipArchiver:
|
|
|
|
|
|
|
|
"""7Z implementation"""
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
# @todo: Implement Comment?
|
|
|
|
def getArchiveComment(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def setArchiveComment(self, comment):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def readArchiveFile(self, archive_file):
|
|
|
|
data = ""
|
|
|
|
try:
|
|
|
|
with py7zr.SevenZipFile(self.path, 'r') as zf:
|
|
|
|
data = zf.read(archive_file)[archive_file].read()
|
|
|
|
except py7zr.Bad7zFile as e:
|
2022-03-26 09:42:33 -07:00
|
|
|
print("bad 7zip file [{0}]: {1} :: {2}".format(e, self.path,
|
2022-03-18 12:14:42 -07:00
|
|
|
archive_file), file=sys.stderr)
|
|
|
|
raise IOError
|
|
|
|
except Exception as e:
|
2022-03-26 09:42:33 -07:00
|
|
|
print("bad 7zip file [{0}]: {1} :: {2}".format(e, self.path,
|
2022-03-18 12:14:42 -07:00
|
|
|
archive_file), file=sys.stderr)
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def removeArchiveFile(self, archive_file):
|
|
|
|
try:
|
|
|
|
self.rebuildSevenZipFile([archive_file])
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def writeArchiveFile(self, archive_file, data):
|
|
|
|
# At the moment, no other option but to rebuild the whole
|
|
|
|
# zip archive w/o the indicated file. Very sucky, but maybe
|
|
|
|
# another solution can be found
|
|
|
|
try:
|
|
|
|
files = self.getArchiveFilenameList()
|
|
|
|
if archive_file in files:
|
|
|
|
self.rebuildSevenZipFile([archive_file])
|
|
|
|
|
|
|
|
# now just add the archive file as a new one
|
|
|
|
with py7zr.SevenZipFile(self.path, 'a') as zf:
|
|
|
|
zf.writestr(data, archive_file)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getArchiveFilenameList(self):
|
|
|
|
try:
|
|
|
|
with py7zr.SevenZipFile(self.path, 'r') as zf:
|
|
|
|
namelist = zf.getnames()
|
|
|
|
|
|
|
|
return namelist
|
|
|
|
except Exception as e:
|
|
|
|
print("Unable to get zipfile list [{0}]: {1}".format(
|
|
|
|
e, self.path), file=sys.stderr)
|
|
|
|
return []
|
|
|
|
|
|
|
|
def rebuildSevenZipFile(self, exclude_list):
|
|
|
|
"""Zip helper func
|
|
|
|
|
|
|
|
This recompresses the zip archive, without the files in the exclude_list
|
|
|
|
"""
|
|
|
|
tmp_fd, tmp_name = tempfile.mkstemp(dir=os.path.dirname(self.path))
|
|
|
|
os.close(tmp_fd)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with py7zr.SevenZipFile(self.path, 'r') as zip:
|
|
|
|
targets = [f for f in zip.getnames() if f not in exclude_list]
|
|
|
|
with py7zr.SevenZipFile(self.path, 'r') as zin:
|
|
|
|
with py7zr.SevenZipFile(tmp_name, 'w') as zout:
|
|
|
|
for fname, bio in zin.read(targets).items():
|
|
|
|
zout.writef(bio, fname)
|
|
|
|
except Exception as e:
|
|
|
|
print("Exception[{0}]: {1}".format(e, self.path))
|
|
|
|
return []
|
|
|
|
|
|
|
|
# replace with the new file
|
|
|
|
os.remove(self.path)
|
|
|
|
os.rename(tmp_name, self.path)
|
|
|
|
|
|
|
|
def copyFromArchive(self, otherArchive):
|
|
|
|
"""Replace the current zip with one copied from another archive"""
|
|
|
|
try:
|
|
|
|
with py7zr.SevenZipFile(self.path, 'w') as zout:
|
|
|
|
for fname in otherArchive.getArchiveFilenameList():
|
|
|
|
data = otherArchive.readArchiveFile(fname)
|
|
|
|
if data is not None:
|
|
|
|
zout.writestr(data, fname)
|
|
|
|
except Exception as e:
|
|
|
|
print("Error while copying to {0}: {1}".format(
|
|
|
|
self.path, e), file=sys.stderr)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-02-16 04:27:21 -08:00
|
|
|
class ZipArchiver:
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
"""ZIP implementation"""
|
|
|
|
|
|
|
|
def __init__(self, path):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.path = path
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveComment(self):
|
|
|
|
zf = zipfile.ZipFile(self.path, 'r')
|
2015-02-16 05:09:21 -08:00
|
|
|
comment = zf.comment
|
|
|
|
zf.close()
|
|
|
|
return comment
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def setArchiveComment(self, comment):
|
2018-09-19 13:05:39 -07:00
|
|
|
zf = zipfile.ZipFile(self.path, 'a')
|
|
|
|
zf.comment = bytes(comment, 'utf-8')
|
|
|
|
zf.close()
|
|
|
|
return True
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
data = ""
|
2015-02-21 18:30:32 -08:00
|
|
|
zf = zipfile.ZipFile(self.path, 'r')
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
data = zf.read(archive_file)
|
2015-02-16 05:09:21 -08:00
|
|
|
except zipfile.BadZipfile as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("bad zipfile [{0}]: {1} :: {2}".format(e, self.path, archive_file), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
zf.close()
|
|
|
|
raise IOError
|
|
|
|
except Exception as e:
|
|
|
|
zf.close()
|
2018-09-19 13:05:39 -07:00
|
|
|
print("bad zipfile [{0}]: {1} :: {2}".format(
|
|
|
|
e, self.path, archive_file), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
raise IOError
|
|
|
|
finally:
|
|
|
|
zf.close()
|
|
|
|
return data
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.rebuildZipFile([archive_file])
|
2015-02-16 05:09:21 -08:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def writeArchiveFile(self, archive_file, data):
|
2015-02-16 05:09:21 -08:00
|
|
|
# At the moment, no other option but to rebuild the whole
|
|
|
|
# zip archive w/o the indicated file. Very sucky, but maybe
|
|
|
|
# another solution can be found
|
|
|
|
try:
|
2022-03-18 12:14:42 -07:00
|
|
|
files = self.getArchiveFilenameList()
|
|
|
|
if archive_file in files:
|
|
|
|
self.rebuildZipFile([archive_file])
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
# now just add the archive file as a new one
|
|
|
|
zf = zipfile.ZipFile(
|
|
|
|
self.path,
|
|
|
|
mode='a',
|
2018-09-19 15:09:24 -07:00
|
|
|
allowZip64=True,
|
2015-02-21 18:30:32 -08:00
|
|
|
compression=zipfile.ZIP_DEFLATED)
|
|
|
|
zf.writestr(archive_file, data)
|
2015-02-16 05:09:21 -08:00
|
|
|
zf.close()
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveFilenameList(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
try:
|
2015-03-06 02:26:47 -08:00
|
|
|
zf = zipfile.ZipFile(self.path, 'r')
|
2015-02-16 05:09:21 -08:00
|
|
|
namelist = zf.namelist()
|
|
|
|
zf.close()
|
|
|
|
return namelist
|
|
|
|
except Exception as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Unable to get zipfile list [{0}]: {1}".format(
|
|
|
|
e, self.path), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
return []
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def rebuildZipFile(self, exclude_list):
|
|
|
|
"""Zip helper func
|
|
|
|
|
|
|
|
This recompresses the zip archive, without the files in the exclude_list
|
|
|
|
"""
|
|
|
|
tmp_fd, tmp_name = tempfile.mkstemp(dir=os.path.dirname(self.path))
|
|
|
|
os.close(tmp_fd)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
zin = zipfile.ZipFile(self.path, 'r')
|
2018-09-19 15:09:24 -07:00
|
|
|
zout = zipfile.ZipFile(tmp_name, 'w', allowZip64=True)
|
2015-02-16 05:09:21 -08:00
|
|
|
for item in zin.infolist():
|
|
|
|
buffer = zin.read(item.filename)
|
2015-02-21 18:30:32 -08:00
|
|
|
if (item.filename not in exclude_list):
|
2015-02-16 05:09:21 -08:00
|
|
|
zout.writestr(item, buffer)
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# preserve the old comment
|
2015-02-16 05:09:21 -08:00
|
|
|
zout.comment = zin.comment
|
|
|
|
|
|
|
|
zout.close()
|
|
|
|
zin.close()
|
|
|
|
|
|
|
|
# replace with the new file
|
2015-02-21 18:30:32 -08:00
|
|
|
os.remove(self.path)
|
|
|
|
os.rename(tmp_name, self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def writeZipComment(self, filename, comment):
|
2015-02-16 05:09:21 -08:00
|
|
|
"""
|
|
|
|
This is a custom function for writing a comment to a zip file,
|
|
|
|
since the built-in one doesn't seem to work on Windows and Mac OS/X
|
|
|
|
|
|
|
|
Fortunately, the zip comment is at the end of the file, and it's
|
|
|
|
easy to manipulate. See this website for more info:
|
|
|
|
see: http://en.wikipedia.org/wiki/Zip_(file_format)#Structure
|
|
|
|
"""
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# get file size
|
2015-02-16 05:09:21 -08:00
|
|
|
statinfo = os.stat(filename)
|
|
|
|
file_length = statinfo.st_size
|
|
|
|
|
|
|
|
try:
|
|
|
|
fo = open(filename, "r+b")
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# the starting position, relative to EOF
|
2015-02-16 05:09:21 -08:00
|
|
|
pos = -4
|
|
|
|
|
|
|
|
found = False
|
|
|
|
value = bytearray()
|
2022-03-18 12:14:42 -07:00
|
|
|
|
2015-02-16 05:09:21 -08:00
|
|
|
# walk backwards to find the "End of Central Directory" record
|
2015-02-21 18:30:32 -08:00
|
|
|
while (not found) and (-pos != file_length):
|
2015-02-16 05:09:21 -08:00
|
|
|
# seek, relative to EOF
|
2015-02-21 18:30:32 -08:00
|
|
|
fo.seek(pos, 2)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
value = fo.read(4)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# look for the end of central directory signature
|
|
|
|
if bytearray(value) == bytearray([0x50, 0x4b, 0x05, 0x06]):
|
2015-02-16 05:09:21 -08:00
|
|
|
found = True
|
|
|
|
else:
|
|
|
|
# not found, step back another byte
|
|
|
|
pos = pos - 1
|
2015-02-21 18:30:32 -08:00
|
|
|
# print pos,"{1} int: {0:x}".format(bytearray(value)[0], value)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if found:
|
|
|
|
|
|
|
|
# now skip forward 20 bytes to the comment length word
|
|
|
|
pos += 20
|
2015-02-21 18:30:32 -08:00
|
|
|
fo.seek(pos, 2)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# Pack the length of the comment string
|
|
|
|
format = "H" # one 2-byte integer
|
2015-02-21 18:30:32 -08:00
|
|
|
comment_length = struct.pack(
|
|
|
|
format,
|
|
|
|
len(comment)) # pack integer in a binary string
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# write out the length
|
2015-02-21 18:30:32 -08:00
|
|
|
fo.write(comment_length)
|
|
|
|
fo.seek(pos + 2, 2)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# write out the comment itself
|
2018-09-19 13:05:39 -07:00
|
|
|
fo.write(bytes(comment))
|
2015-02-16 05:09:21 -08:00
|
|
|
fo.truncate()
|
|
|
|
fo.close()
|
|
|
|
else:
|
|
|
|
raise Exception('Failed to write comment to zip file!')
|
2018-09-19 13:05:39 -07:00
|
|
|
except Exception as e:
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def copyFromArchive(self, otherArchive):
|
|
|
|
"""Replace the current zip with one copied from another archive"""
|
2015-02-16 05:09:21 -08:00
|
|
|
try:
|
2018-09-19 15:09:24 -07:00
|
|
|
zout = zipfile.ZipFile(self.path, 'w', allowZip64=True)
|
2015-02-16 05:09:21 -08:00
|
|
|
for fname in otherArchive.getArchiveFilenameList():
|
2015-02-21 18:30:32 -08:00
|
|
|
data = otherArchive.readArchiveFile(fname)
|
2015-02-16 05:09:21 -08:00
|
|
|
if data is not None:
|
2015-02-21 18:30:32 -08:00
|
|
|
zout.writestr(fname, data)
|
2015-02-16 05:09:21 -08:00
|
|
|
zout.close()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# preserve the old comment
|
2015-02-16 05:09:21 -08:00
|
|
|
comment = otherArchive.getArchiveComment()
|
|
|
|
if comment is not None:
|
2015-02-21 18:30:32 -08:00
|
|
|
if not self.writeZipComment(self.path, comment):
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
2015-02-21 18:30:32 -08:00
|
|
|
except Exception as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Error while copying to {0}: {1}".format(
|
|
|
|
self.path, e), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-02-16 04:27:21 -08:00
|
|
|
class RarArchiver:
|
2015-02-21 18:30:32 -08:00
|
|
|
"""RAR implementation"""
|
2015-02-16 05:09:21 -08:00
|
|
|
devnull = None
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def __init__(self, path, rar_exe_path):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.path = path
|
|
|
|
self.rar_exe_path = rar_exe_path
|
|
|
|
|
|
|
|
if RarArchiver.devnull is None:
|
|
|
|
RarArchiver.devnull = open(os.devnull, "w")
|
|
|
|
|
|
|
|
# windows only, keeps the cmd.exe from popping up
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
self.startupinfo = subprocess.STARTUPINFO()
|
2018-09-19 13:05:39 -07:00
|
|
|
self.startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
2015-02-16 05:09:21 -08:00
|
|
|
else:
|
|
|
|
self.startupinfo = None
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveComment(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
rarc = self.getRARObj()
|
|
|
|
return rarc.comment
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def setArchiveComment(self, comment):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.rar_exe_path is not None:
|
|
|
|
try:
|
|
|
|
# write comment to temp file
|
|
|
|
tmp_fd, tmp_name = tempfile.mkstemp()
|
2018-09-19 13:05:39 -07:00
|
|
|
f = os.fdopen(tmp_fd, 'w+')
|
2015-02-21 18:30:32 -08:00
|
|
|
f.write(comment)
|
2015-02-16 05:09:21 -08:00
|
|
|
f.close()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
working_dir = os.path.dirname(os.path.abspath(self.path))
|
2022-03-18 12:14:42 -07:00
|
|
|
|
2015-02-16 05:09:21 -08:00
|
|
|
# use external program to write comment to Rar archive
|
2018-09-19 13:05:39 -07:00
|
|
|
proc_args = [self.rar_exe_path,
|
2015-02-21 18:30:32 -08:00
|
|
|
'c',
|
|
|
|
'-w' + working_dir,
|
|
|
|
'-c-',
|
|
|
|
'-z' + tmp_name,
|
2018-09-19 13:05:39 -07:00
|
|
|
self.path]
|
|
|
|
subprocess.call(proc_args,
|
2015-02-21 18:30:32 -08:00
|
|
|
startupinfo=self.startupinfo,
|
2018-09-19 13:05:39 -07:00
|
|
|
stdout=RarArchiver.devnull,
|
|
|
|
stdin=RarArchiver.devnull,
|
|
|
|
stderr=RarArchiver.devnull)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if platform.system() == "Darwin":
|
|
|
|
time.sleep(1)
|
2015-02-21 18:30:32 -08:00
|
|
|
os.remove(tmp_name)
|
2018-09-19 13:05:39 -07:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
entries = []
|
|
|
|
|
|
|
|
rarc = self.getRARObj()
|
|
|
|
|
|
|
|
tries = 0
|
|
|
|
while tries < 7:
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
tries = tries + 1
|
2022-03-18 12:14:42 -07:00
|
|
|
data = rarc.open(archive_file).read()
|
2015-02-16 05:09:21 -08:00
|
|
|
entries = [(rarc.getinfo(archive_file), data)]
|
|
|
|
|
|
|
|
if entries[0][0].file_size != len(entries[0][1]):
|
2018-09-19 13:05:39 -07:00
|
|
|
print("readArchiveFile(): [file is not expected size: {0} vs {1}] {2}:{3} [attempt # {4}]".format(
|
2015-02-21 18:30:32 -08:00
|
|
|
entries[0][0].file_size, len(
|
2018-09-19 13:05:39 -07:00
|
|
|
entries[0][1]), self.path, archive_file, tries), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
continue
|
|
|
|
except (OSError, IOError) as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("readArchiveFile(): [{0}] {1}:{2} attempt#{3}".format(
|
|
|
|
str(e), self.path, archive_file, tries), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
time.sleep(1)
|
|
|
|
except Exception as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Unexpected exception in readArchiveFile(): [{0}] for {1}:{2} attempt#{3}".format(
|
|
|
|
str(e), self.path, archive_file, tries), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
# Success"
|
|
|
|
# entries is a list of of tuples: ( rarinfo, filedata)
|
2015-02-16 05:09:21 -08:00
|
|
|
if tries > 1:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Attempted read_files() {0} times".format(
|
|
|
|
tries), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
if (len(entries) == 1):
|
|
|
|
return entries[0][1]
|
|
|
|
else:
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
raise IOError
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def writeArchiveFile(self, archive_file, data):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if self.rar_exe_path is not None:
|
|
|
|
try:
|
|
|
|
tmp_folder = tempfile.mkdtemp()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
tmp_file = os.path.join(tmp_folder, archive_file)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
working_dir = os.path.dirname(os.path.abspath(self.path))
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# TODO: will this break if 'archive_file' is in a subfolder. i.e. "foo/bar.txt"
|
|
|
|
# will need to create the subfolder above, I guess...
|
|
|
|
f = open(tmp_file, 'w')
|
2015-02-21 18:30:32 -08:00
|
|
|
f.write(data)
|
2015-02-16 05:09:21 -08:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
# use external program to write file to Rar archive
|
2015-02-21 18:30:32 -08:00
|
|
|
subprocess.call([self.rar_exe_path,
|
|
|
|
'a',
|
|
|
|
'-w' + working_dir,
|
|
|
|
'-c-',
|
|
|
|
'-ep',
|
|
|
|
self.path,
|
|
|
|
tmp_file],
|
|
|
|
startupinfo=self.startupinfo,
|
2018-09-19 13:05:39 -07:00
|
|
|
stdout=RarArchiver.devnull,
|
|
|
|
stdin=RarArchiver.devnull,
|
|
|
|
stderr=RarArchiver.devnull)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if platform.system() == "Darwin":
|
|
|
|
time.sleep(1)
|
2015-02-21 18:30:32 -08:00
|
|
|
os.remove(tmp_file)
|
|
|
|
os.rmdir(tmp_folder)
|
2015-02-16 05:09:21 -08:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.rar_exe_path is not None:
|
|
|
|
try:
|
|
|
|
# use external program to remove file from Rar archive
|
2015-02-21 18:30:32 -08:00
|
|
|
subprocess.call([self.rar_exe_path,
|
|
|
|
'd',
|
|
|
|
'-c-',
|
|
|
|
self.path,
|
|
|
|
archive_file],
|
|
|
|
startupinfo=self.startupinfo,
|
2018-09-19 13:05:39 -07:00
|
|
|
stdout=RarArchiver.devnull,
|
|
|
|
stdin=RarArchiver.devnull,
|
|
|
|
stderr=RarArchiver.devnull)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if platform.system() == "Darwin":
|
|
|
|
time.sleep(1)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveFilenameList(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
rarc = self.getRARObj()
|
|
|
|
tries = 0
|
|
|
|
while tries < 7:
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
tries = tries + 1
|
2015-02-16 05:09:21 -08:00
|
|
|
namelist = []
|
|
|
|
for item in rarc.infolist():
|
|
|
|
if item.file_size != 0:
|
2015-02-21 18:30:32 -08:00
|
|
|
namelist.append(item.filename)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
except (OSError, IOError) as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("getArchiveFilenameList(): [{0}] {1} attempt#{2}".format(
|
|
|
|
str(e), self.path, tries), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
# Success"
|
2015-02-16 05:09:21 -08:00
|
|
|
return namelist
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getRARObj(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
tries = 0
|
|
|
|
while tries < 7:
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
tries = tries + 1
|
2019-10-05 14:59:52 -07:00
|
|
|
rarc = rarfile.RarFile(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
except (OSError, IOError) as e:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("getRARObj(): [{0}] {1} attempt#{2}".format(
|
|
|
|
str(e), self.path, tries), file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
# Success"
|
2015-02-16 05:09:21 -08:00
|
|
|
return rarc
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
|
2015-02-16 04:27:21 -08:00
|
|
|
class FolderArchiver:
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
"""Folder implementation"""
|
|
|
|
|
|
|
|
def __init__(self, path):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.path = path
|
|
|
|
self.comment_file_name = "ComicTaggerFolderComment.txt"
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveComment(self):
|
|
|
|
return self.readArchiveFile(self.comment_file_name)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def setArchiveComment(self, comment):
|
|
|
|
return self.writeArchiveFile(self.comment_file_name, comment)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
data = ""
|
2015-02-21 18:30:32 -08:00
|
|
|
fname = os.path.join(self.path, archive_file)
|
2015-02-16 05:09:21 -08:00
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
with open(fname, 'rb') as f:
|
2015-02-16 05:09:21 -08:00
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
except IOError as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def writeArchiveFile(self, archive_file, data):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
fname = os.path.join(self.path, archive_file)
|
2015-02-16 05:09:21 -08:00
|
|
|
try:
|
|
|
|
with open(fname, 'w+') as f:
|
2015-02-21 18:30:32 -08:00
|
|
|
f.write(data)
|
2015-02-16 05:09:21 -08:00
|
|
|
f.close()
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
fname = os.path.join(self.path, archive_file)
|
2015-02-16 05:09:21 -08:00
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
os.remove(fname)
|
2015-02-16 05:09:21 -08:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveFilenameList(self):
|
|
|
|
return self.listFiles(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def listFiles(self, folder):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
itemlist = list()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
for item in os.listdir(folder):
|
|
|
|
itemlist.append(item)
|
|
|
|
if os.path.isdir(item):
|
|
|
|
itemlist.extend(self.listFiles(os.path.join(folder, item)))
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
return itemlist
|
2015-02-16 04:27:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
|
2015-02-16 04:27:21 -08:00
|
|
|
class UnknownArchiver:
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
"""Unknown implementation"""
|
|
|
|
|
|
|
|
def __init__(self, path):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.path = path
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getArchiveComment(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
return ""
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def setArchiveComment(self, comment):
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def readArchiveFile(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
return ""
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def writeArchiveFile(self, archive_file, data):
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def removeArchiveFile(self, archive_file):
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def getArchiveFilenameList(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
return []
|
2015-02-16 04:27:21 -08:00
|
|
|
|
|
|
|
class ComicArchive:
|
2015-02-16 05:09:21 -08:00
|
|
|
logo_data = None
|
|
|
|
class ArchiveType:
|
2022-03-18 12:14:42 -07:00
|
|
|
SevenZip, Zip, Rar, Folder, Pdf, Unknown = list(range(6))
|
2015-02-21 18:30:32 -08:00
|
|
|
|
|
|
|
def __init__(self, path, rar_exe_path=None, default_image_path=None):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.path = path
|
|
|
|
|
|
|
|
self.rar_exe_path = rar_exe_path
|
|
|
|
self.ci_xml_filename = 'ComicInfo.xml'
|
|
|
|
self.comet_default_filename = 'CoMet.xml'
|
|
|
|
self.resetCache()
|
|
|
|
self.default_image_path = default_image_path
|
|
|
|
|
|
|
|
# Use file extension to decide which archive test we do first
|
|
|
|
ext = os.path.splitext(path)[1].lower()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
self.archive_type = self.ArchiveType.Unknown
|
|
|
|
self.archiver = UnknownArchiver(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if ext == ".cbr" or ext == ".rar":
|
|
|
|
if self.rarTest():
|
2015-02-21 18:30:32 -08:00
|
|
|
self.archive_type = self.ArchiveType.Rar
|
|
|
|
self.archiver = RarArchiver(
|
|
|
|
self.path,
|
|
|
|
rar_exe_path=self.rar_exe_path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
elif self.zipTest():
|
2015-02-21 18:30:32 -08:00
|
|
|
self.archive_type = self.ArchiveType.Zip
|
|
|
|
self.archiver = ZipArchiver(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
else:
|
2022-03-18 12:14:42 -07:00
|
|
|
if self.sevenZipTest():
|
|
|
|
self.archive_type = self.ArchiveType.SevenZip
|
|
|
|
self.archiver = SevenZipArchiver(self.path)
|
|
|
|
|
|
|
|
elif self.zipTest():
|
2015-02-21 18:30:32 -08:00
|
|
|
self.archive_type = self.ArchiveType.Zip
|
|
|
|
self.archiver = ZipArchiver(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
elif self.rarTest():
|
2015-02-21 18:30:32 -08:00
|
|
|
self.archive_type = self.ArchiveType.Rar
|
|
|
|
self.archiver = RarArchiver(
|
|
|
|
self.path,
|
|
|
|
rar_exe_path=self.rar_exe_path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if ComicArchive.logo_data is None:
|
|
|
|
#fname = ComicTaggerSettings.getGraphic('nocover.png')
|
|
|
|
fname = self.default_image_path
|
|
|
|
with open(fname, 'rb') as fd:
|
|
|
|
ComicArchive.logo_data = fd.read()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def resetCache(self):
|
|
|
|
"""Clears the cached data"""
|
|
|
|
|
2015-02-16 05:09:21 -08:00
|
|
|
self.has_cix = None
|
|
|
|
self.has_cbi = None
|
|
|
|
self.has_comet = None
|
|
|
|
self.comet_filename = None
|
2015-02-21 18:30:32 -08:00
|
|
|
self.page_count = None
|
|
|
|
self.page_list = None
|
|
|
|
self.cix_md = None
|
|
|
|
self.cbi_md = None
|
|
|
|
self.comet_md = None
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def loadCache(self, style_list):
|
2015-02-16 05:09:21 -08:00
|
|
|
for style in style_list:
|
|
|
|
self.readMetadata(style)
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def rename(self, path):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.path = path
|
|
|
|
self.archiver.path = path
|
|
|
|
|
2022-03-18 12:14:42 -07:00
|
|
|
def sevenZipTest(self):
|
|
|
|
return py7zr.is_7zfile(self.path)
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def zipTest(self):
|
|
|
|
return zipfile.is_zipfile(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def rarTest(self):
|
2021-12-12 18:46:28 -08:00
|
|
|
try:
|
|
|
|
return rarfile.is_rarfile(self.path)
|
|
|
|
except:
|
|
|
|
return False
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2022-03-18 12:14:42 -07:00
|
|
|
def isSevenZip(self):
|
|
|
|
return self.archive_type == self.ArchiveType.SevenZip
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def isZip(self):
|
|
|
|
return self.archive_type == self.ArchiveType.Zip
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def isRar(self):
|
|
|
|
return self.archive_type == self.ArchiveType.Rar
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
def isPdf(self):
|
2015-02-21 18:30:32 -08:00
|
|
|
return self.archive_type == self.ArchiveType.Pdf
|
|
|
|
|
|
|
|
def isFolder(self):
|
|
|
|
return self.archive_type == self.ArchiveType.Folder
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def isWritable(self, check_rar_status=True):
|
|
|
|
if self.archive_type == self.ArchiveType.Unknown:
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
|
|
|
|
2018-09-19 13:05:39 -07:00
|
|
|
elif check_rar_status and self.isRar() and not self.rar_exe_path:
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
|
|
|
|
|
|
|
elif not os.access(self.path, os.W_OK):
|
|
|
|
return False
|
|
|
|
|
|
|
|
elif ((self.archive_type != self.ArchiveType.Folder) and
|
2015-02-21 18:30:32 -08:00
|
|
|
(not os.access(os.path.dirname(os.path.abspath(self.path)), os.W_OK))):
|
2015-02-16 05:09:21 -08:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def isWritableForStyle(self, data_style):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if self.isRar() and data_style == MetaDataStyle.CBI:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.isWritable()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def seemsToBeAComicArchive(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
# Do we even care about extensions??
|
|
|
|
ext = os.path.splitext(self.path)[1].lower()
|
|
|
|
|
|
|
|
if (
|
2015-02-21 18:30:32 -08:00
|
|
|
# or self.isFolder() )
|
2022-03-18 12:14:42 -07:00
|
|
|
(self.isSevenZip() or self.isZip() or self.isRar())
|
2015-02-21 18:30:32 -08:00
|
|
|
and
|
|
|
|
(self.getNumberOfPages() > 0)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
):
|
2015-02-16 05:09:21 -08:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readMetadata(self, style):
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if style == MetaDataStyle.CIX:
|
|
|
|
return self.readCIX()
|
|
|
|
elif style == MetaDataStyle.CBI:
|
|
|
|
return self.readCBI()
|
|
|
|
elif style == MetaDataStyle.COMET:
|
|
|
|
return self.readCoMet()
|
|
|
|
else:
|
|
|
|
return GenericMetadata()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def writeMetadata(self, metadata, style):
|
2015-02-16 05:09:21 -08:00
|
|
|
retcode = None
|
|
|
|
if style == MetaDataStyle.CIX:
|
2015-02-21 18:30:32 -08:00
|
|
|
retcode = self.writeCIX(metadata)
|
2015-02-16 05:09:21 -08:00
|
|
|
elif style == MetaDataStyle.CBI:
|
2015-02-21 18:30:32 -08:00
|
|
|
retcode = self.writeCBI(metadata)
|
2015-02-16 05:09:21 -08:00
|
|
|
elif style == MetaDataStyle.COMET:
|
2015-02-21 18:30:32 -08:00
|
|
|
retcode = self.writeCoMet(metadata)
|
2015-02-16 05:09:21 -08:00
|
|
|
return retcode
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def hasMetadata(self, style):
|
2015-02-16 05:09:21 -08:00
|
|
|
if style == MetaDataStyle.CIX:
|
|
|
|
return self.hasCIX()
|
|
|
|
elif style == MetaDataStyle.CBI:
|
|
|
|
return self.hasCBI()
|
|
|
|
elif style == MetaDataStyle.COMET:
|
|
|
|
return self.hasCoMet()
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeMetadata(self, style):
|
2015-02-16 05:09:21 -08:00
|
|
|
retcode = True
|
|
|
|
if style == MetaDataStyle.CIX:
|
|
|
|
retcode = self.removeCIX()
|
|
|
|
elif style == MetaDataStyle.CBI:
|
|
|
|
retcode = self.removeCBI()
|
|
|
|
elif style == MetaDataStyle.COMET:
|
|
|
|
retcode = self.removeCoMet()
|
|
|
|
return retcode
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getPage(self, index):
|
2015-02-16 05:09:21 -08:00
|
|
|
image_data = None
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
filename = self.getPageName(index)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if filename is not None:
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
image_data = self.archiver.readArchiveFile(filename)
|
2015-02-16 05:09:21 -08:00
|
|
|
except IOError:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Error reading in page. Substituting logo page.", file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
image_data = ComicArchive.logo_data
|
|
|
|
|
|
|
|
return image_data
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getPageName(self, index):
|
2015-02-16 05:09:21 -08:00
|
|
|
if index is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
page_list = self.getPageNameList()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
num_pages = len(page_list)
|
2015-02-16 05:09:21 -08:00
|
|
|
if num_pages == 0 or index >= num_pages:
|
|
|
|
return None
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
return page_list[index]
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getScannerPageIndex(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
scanner_page_index = None
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# make a guess at the scanner page
|
2015-02-16 05:09:21 -08:00
|
|
|
name_list = self.getPageNameList()
|
|
|
|
count = self.getNumberOfPages()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# too few pages to really know
|
2015-02-16 05:09:21 -08:00
|
|
|
if count < 5:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# count the length of every filename, and count occurences
|
|
|
|
length_buckets = dict()
|
|
|
|
for name in name_list:
|
2015-02-21 18:30:32 -08:00
|
|
|
fname = os.path.split(name)[1]
|
2015-02-16 05:09:21 -08:00
|
|
|
length = len(fname)
|
2015-02-21 18:30:32 -08:00
|
|
|
if length in length_buckets:
|
|
|
|
length_buckets[length] += 1
|
2015-02-16 05:09:21 -08:00
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
length_buckets[length] = 1
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# sort by most common
|
2015-02-21 18:30:32 -08:00
|
|
|
sorted_buckets = sorted(
|
2018-09-19 13:05:39 -07:00
|
|
|
iter(length_buckets.items()),
|
2015-02-21 18:30:32 -08:00
|
|
|
key=lambda k_v: (
|
|
|
|
k_v[1],
|
|
|
|
k_v[0]),
|
|
|
|
reverse=True)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# statistical mode occurence is first
|
|
|
|
mode_length = sorted_buckets[0][0]
|
|
|
|
|
|
|
|
# we are only going to consider the final image file:
|
2015-02-21 18:30:32 -08:00
|
|
|
final_name = os.path.split(name_list[count - 1])[1]
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
common_length_list = list()
|
|
|
|
for name in name_list:
|
|
|
|
if len(os.path.split(name)[1]) == mode_length:
|
2015-02-21 18:30:32 -08:00
|
|
|
common_length_list.append(os.path.split(name)[1])
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
prefix = os.path.commonprefix(common_length_list)
|
|
|
|
|
|
|
|
if mode_length <= 7 and prefix == "":
|
2015-02-21 18:30:32 -08:00
|
|
|
# probably all numbers
|
2015-02-16 05:09:21 -08:00
|
|
|
if len(final_name) > mode_length:
|
2015-02-21 18:30:32 -08:00
|
|
|
scanner_page_index = count - 1
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# see if the last page doesn't start with the same prefix as most
|
|
|
|
# others
|
2015-02-16 05:09:21 -08:00
|
|
|
elif not final_name.startswith(prefix):
|
2015-02-21 18:30:32 -08:00
|
|
|
scanner_page_index = count - 1
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
return scanner_page_index
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getPageNameList(self, sort_list=True):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.page_list is None:
|
|
|
|
# get the list file names in the archive, and sort
|
|
|
|
files = self.archiver.getArchiveFilenameList()
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# seems like some archive creators are on Windows, and don't know
|
|
|
|
# about case-sensitivity!
|
2015-02-16 05:09:21 -08:00
|
|
|
if sort_list:
|
|
|
|
def keyfunc(k):
|
|
|
|
return k.lower()
|
|
|
|
|
2022-03-14 10:27:03 -07:00
|
|
|
files = natsort.natsorted(files, alg=natsort.ns.IC | natsort.ns.I | natsort.ns.U)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
# make a sub-list of image files
|
|
|
|
self.page_list = []
|
|
|
|
for name in files:
|
2015-02-21 18:30:32 -08:00
|
|
|
if (name[-4:].lower() in [".jpg",
|
|
|
|
"jpeg",
|
|
|
|
".png",
|
|
|
|
".gif",
|
|
|
|
"webp"] and os.path.basename(name)[0] != "."):
|
2015-02-16 05:09:21 -08:00
|
|
|
self.page_list.append(name)
|
|
|
|
|
|
|
|
return self.page_list
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def getNumberOfPages(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.page_count is None:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.page_count = len(self.getPageNameList())
|
2015-02-16 05:09:21 -08:00
|
|
|
return self.page_count
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readCBI(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.cbi_md is None:
|
|
|
|
raw_cbi = self.readRawCBI()
|
|
|
|
if raw_cbi is None:
|
|
|
|
self.cbi_md = GenericMetadata()
|
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.cbi_md = ComicBookInfo().metadataFromString(raw_cbi)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
self.cbi_md.setDefaultPageList(self.getNumberOfPages())
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
return self.cbi_md
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readRawCBI(self):
|
|
|
|
if (not self.hasCBI()):
|
2015-02-16 05:09:21 -08:00
|
|
|
return None
|
|
|
|
|
|
|
|
return self.archiver.getArchiveComment()
|
|
|
|
|
|
|
|
def hasCBI(self):
|
|
|
|
if self.has_cbi is None:
|
|
|
|
|
2022-03-18 12:14:42 -07:00
|
|
|
# if ( not (self.isSevenZip() or self.isZip() or self.isRar()) or not
|
2015-02-21 18:30:32 -08:00
|
|
|
# self.seemsToBeAComicArchive() ):
|
2015-02-16 05:09:21 -08:00
|
|
|
if not self.seemsToBeAComicArchive():
|
|
|
|
self.has_cbi = False
|
|
|
|
else:
|
|
|
|
comment = self.archiver.getArchiveComment()
|
2015-02-21 18:30:32 -08:00
|
|
|
self.has_cbi = ComicBookInfo().validateString(comment)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
return self.has_cbi
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def writeCBI(self, metadata):
|
2015-02-16 05:09:21 -08:00
|
|
|
if metadata is not None:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.applyArchiveInfoToMetadata(metadata)
|
|
|
|
cbi_string = ComicBookInfo().stringFromMetadata(metadata)
|
|
|
|
write_success = self.archiver.setArchiveComment(cbi_string)
|
2015-02-16 05:09:21 -08:00
|
|
|
if write_success:
|
|
|
|
self.has_cbi = True
|
|
|
|
self.cbi_md = metadata
|
|
|
|
self.resetCache()
|
|
|
|
return write_success
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeCBI(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.hasCBI():
|
2015-02-21 18:30:32 -08:00
|
|
|
write_success = self.archiver.setArchiveComment("")
|
2015-02-16 05:09:21 -08:00
|
|
|
if write_success:
|
|
|
|
self.has_cbi = False
|
|
|
|
self.cbi_md = None
|
|
|
|
self.resetCache()
|
|
|
|
return write_success
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readCIX(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.cix_md is None:
|
|
|
|
raw_cix = self.readRawCIX()
|
|
|
|
if raw_cix is None or raw_cix == "":
|
|
|
|
self.cix_md = GenericMetadata()
|
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.cix_md = ComicInfoXml().metadataFromString(raw_cix)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# validate the existing page list (make sure count is correct)
|
|
|
|
if len(self.cix_md.pages) != 0:
|
|
|
|
if len(self.cix_md.pages) != self.getNumberOfPages():
|
2015-02-16 05:09:21 -08:00
|
|
|
# pages array doesn't match the actual number of images we're seeing
|
|
|
|
# in the archive, so discard the data
|
|
|
|
self.cix_md.pages = []
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
if len(self.cix_md.pages) == 0:
|
|
|
|
self.cix_md.setDefaultPageList(self.getNumberOfPages())
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
return self.cix_md
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readRawCIX(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if not self.hasCIX():
|
|
|
|
return None
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
raw_cix = self.archiver.readArchiveFile(self.ci_xml_filename)
|
2015-02-16 05:09:21 -08:00
|
|
|
except IOError:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Error reading in raw CIX!")
|
2015-02-16 05:09:21 -08:00
|
|
|
raw_cix = ""
|
2015-02-21 18:30:32 -08:00
|
|
|
return raw_cix
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
def writeCIX(self, metadata):
|
|
|
|
if metadata is not None:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.applyArchiveInfoToMetadata(metadata, calc_page_sizes=True)
|
2021-10-03 23:25:37 -07:00
|
|
|
rawCIX = self.readRawCIX()
|
|
|
|
if rawCIX == "":
|
|
|
|
rawCIX = None
|
|
|
|
cix_string = ComicInfoXml().stringFromMetadata(metadata, xml=rawCIX)
|
2015-02-21 18:30:32 -08:00
|
|
|
write_success = self.archiver.writeArchiveFile(
|
|
|
|
self.ci_xml_filename,
|
|
|
|
cix_string)
|
2015-02-16 05:09:21 -08:00
|
|
|
if write_success:
|
|
|
|
self.has_cix = True
|
|
|
|
self.cix_md = metadata
|
|
|
|
self.resetCache()
|
|
|
|
return write_success
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeCIX(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.hasCIX():
|
2015-02-21 18:30:32 -08:00
|
|
|
write_success = self.archiver.removeArchiveFile(
|
|
|
|
self.ci_xml_filename)
|
2015-02-16 05:09:21 -08:00
|
|
|
if write_success:
|
|
|
|
self.has_cix = False
|
|
|
|
self.cix_md = None
|
|
|
|
self.resetCache()
|
|
|
|
return write_success
|
|
|
|
return True
|
|
|
|
|
|
|
|
def hasCIX(self):
|
|
|
|
if self.has_cix is None:
|
|
|
|
|
|
|
|
if not self.seemsToBeAComicArchive():
|
|
|
|
self.has_cix = False
|
|
|
|
elif self.ci_xml_filename in self.archiver.getArchiveFilenameList():
|
|
|
|
self.has_cix = True
|
|
|
|
else:
|
|
|
|
self.has_cix = False
|
|
|
|
return self.has_cix
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readCoMet(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.comet_md is None:
|
|
|
|
raw_comet = self.readRawCoMet()
|
|
|
|
if raw_comet is None or raw_comet == "":
|
|
|
|
self.comet_md = GenericMetadata()
|
|
|
|
else:
|
2015-02-21 18:30:32 -08:00
|
|
|
self.comet_md = CoMet().metadataFromString(raw_comet)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
self.comet_md.setDefaultPageList(self.getNumberOfPages())
|
|
|
|
# use the coverImage value from the comet_data to mark the cover in this struct
|
2015-02-16 05:09:21 -08:00
|
|
|
# walk through list of images in file, and find the matching one for md.coverImage
|
|
|
|
# need to remove the existing one in the default
|
|
|
|
if self.comet_md.coverImage is not None:
|
|
|
|
cover_idx = 0
|
2015-02-21 18:30:32 -08:00
|
|
|
for idx, f in enumerate(self.getPageNameList()):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.comet_md.coverImage == f:
|
|
|
|
cover_idx = idx
|
|
|
|
break
|
|
|
|
if cover_idx != 0:
|
2015-02-21 18:30:32 -08:00
|
|
|
del (self.comet_md.pages[0]['Type'])
|
|
|
|
self.comet_md.pages[cover_idx][
|
|
|
|
'Type'] = PageType.FrontCover
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
return self.comet_md
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def readRawCoMet(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if not self.hasCoMet():
|
2018-09-19 13:05:39 -07:00
|
|
|
print(self.path, "doesn't have CoMet data!", file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
raw_comet = self.archiver.readArchiveFile(self.comet_filename)
|
2015-02-16 05:09:21 -08:00
|
|
|
except IOError:
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Error reading in raw CoMet!", file=sys.stderr)
|
2015-02-16 05:09:21 -08:00
|
|
|
raw_comet = ""
|
2015-02-21 18:30:32 -08:00
|
|
|
return raw_comet
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
def writeCoMet(self, metadata):
|
|
|
|
|
|
|
|
if metadata is not None:
|
|
|
|
if not self.hasCoMet():
|
|
|
|
self.comet_filename = self.comet_default_filename
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
self.applyArchiveInfoToMetadata(metadata)
|
2015-02-16 05:09:21 -08:00
|
|
|
# Set the coverImage value, if it's not the first page
|
|
|
|
cover_idx = int(metadata.getCoverPageIndexList()[0])
|
|
|
|
if cover_idx != 0:
|
2015-02-21 18:30:32 -08:00
|
|
|
metadata.coverImage = self.getPageName(cover_idx)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
comet_string = CoMet().stringFromMetadata(metadata)
|
|
|
|
write_success = self.archiver.writeArchiveFile(
|
|
|
|
self.comet_filename,
|
|
|
|
comet_string)
|
2015-02-16 05:09:21 -08:00
|
|
|
if write_success:
|
|
|
|
self.has_comet = True
|
|
|
|
self.comet_md = metadata
|
|
|
|
self.resetCache()
|
|
|
|
return write_success
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def removeCoMet(self):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.hasCoMet():
|
2015-02-21 18:30:32 -08:00
|
|
|
write_success = self.archiver.removeArchiveFile(
|
|
|
|
self.comet_filename)
|
2015-02-16 05:09:21 -08:00
|
|
|
if write_success:
|
|
|
|
self.has_comet = False
|
|
|
|
self.comet_md = None
|
|
|
|
self.resetCache()
|
|
|
|
return write_success
|
|
|
|
return True
|
|
|
|
|
|
|
|
def hasCoMet(self):
|
|
|
|
if self.has_comet is None:
|
|
|
|
self.has_comet = False
|
|
|
|
if not self.seemsToBeAComicArchive():
|
|
|
|
return self.has_comet
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
# look at all xml files in root, and search for CoMet data, get
|
|
|
|
# first
|
2015-02-16 05:09:21 -08:00
|
|
|
for n in self.archiver.getArchiveFilenameList():
|
2015-02-21 18:30:32 -08:00
|
|
|
if (os.path.dirname(n) == "" and
|
|
|
|
os.path.splitext(n)[1].lower() == '.xml'):
|
2015-02-16 05:09:21 -08:00
|
|
|
# read in XML file, and validate it
|
|
|
|
try:
|
2015-02-21 18:30:32 -08:00
|
|
|
data = self.archiver.readArchiveFile(n)
|
2015-02-16 05:09:21 -08:00
|
|
|
except:
|
|
|
|
data = ""
|
2018-09-19 13:05:39 -07:00
|
|
|
print("Error reading in Comet XML for validation!", file=sys.stderr)
|
2015-02-21 18:30:32 -08:00
|
|
|
if CoMet().validateString(data):
|
2015-02-16 05:09:21 -08:00
|
|
|
# since we found it, save it!
|
|
|
|
self.comet_filename = n
|
|
|
|
self.has_comet = True
|
|
|
|
break
|
|
|
|
|
|
|
|
return self.has_comet
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def applyArchiveInfoToMetadata(self, md, calc_page_sizes=False):
|
2015-02-16 05:09:21 -08:00
|
|
|
md.pageCount = self.getNumberOfPages()
|
|
|
|
|
|
|
|
if calc_page_sizes:
|
|
|
|
for p in md.pages:
|
2015-02-21 18:30:32 -08:00
|
|
|
idx = int(p['Image'])
|
2015-02-16 05:09:21 -08:00
|
|
|
if pil_available:
|
|
|
|
if 'ImageSize' not in p or 'ImageHeight' not in p or 'ImageWidth' not in p:
|
2015-02-21 18:30:32 -08:00
|
|
|
data = self.getPage(idx)
|
2015-02-16 05:09:21 -08:00
|
|
|
if data is not None:
|
|
|
|
try:
|
2022-03-26 09:42:33 -07:00
|
|
|
im = Image.open(io.StringIO(data))
|
2015-02-21 18:30:32 -08:00
|
|
|
w, h = im.size
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
p['ImageSize'] = str(len(data))
|
|
|
|
p['ImageHeight'] = str(h)
|
|
|
|
p['ImageWidth'] = str(w)
|
|
|
|
except IOError:
|
|
|
|
p['ImageSize'] = str(len(data))
|
|
|
|
|
|
|
|
else:
|
|
|
|
if 'ImageSize' not in p:
|
2015-02-21 18:30:32 -08:00
|
|
|
data = self.getPage(idx)
|
2015-02-16 05:09:21 -08:00
|
|
|
p['ImageSize'] = str(len(data))
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def metadataFromFilename(self, parse_scan_info=True):
|
2015-02-16 05:09:21 -08:00
|
|
|
metadata = GenericMetadata()
|
|
|
|
|
|
|
|
fnp = FileNameParser()
|
2015-02-21 18:30:32 -08:00
|
|
|
fnp.parseFilename(self.path)
|
2015-02-16 05:09:21 -08:00
|
|
|
|
|
|
|
if fnp.issue != "":
|
|
|
|
metadata.issue = fnp.issue
|
|
|
|
if fnp.series != "":
|
|
|
|
metadata.series = fnp.series
|
|
|
|
if fnp.volume != "":
|
|
|
|
metadata.volume = fnp.volume
|
|
|
|
if fnp.year != "":
|
|
|
|
metadata.year = fnp.year
|
|
|
|
if fnp.issue_count != "":
|
|
|
|
metadata.issueCount = fnp.issue_count
|
|
|
|
if parse_scan_info:
|
|
|
|
if fnp.remainder != "":
|
|
|
|
metadata.scanInfo = fnp.remainder
|
|
|
|
|
|
|
|
metadata.isEmpty = False
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
def exportAsZip(self, zipfilename):
|
2015-02-16 05:09:21 -08:00
|
|
|
if self.archive_type == self.ArchiveType.Zip:
|
|
|
|
# nothing to do, we're already a zip
|
|
|
|
return True
|
|
|
|
|
2015-02-21 18:30:32 -08:00
|
|
|
zip_archiver = ZipArchiver(zipfilename)
|
|
|
|
return zip_archiver.copyFromArchive(self.archiver)
|