using comicapi subtree classes

This commit is contained in:
Davide Romanini 2015-02-16 14:05:02 +01:00
parent d21657dc02
commit 5d641af4f2

View File

@ -26,7 +26,51 @@ import tempfile
import subprocess import subprocess
import platform import platform
import locale import locale
import shutil
from natsort import natsorted from natsort import natsorted
from unrar import rarfile
from unrar import unrarlib
import unrar.constants
import ctypes
import io
from unrar import constants
class OpenableRarFile(rarfile.RarFile):
def open(self, member):
#print "opening %s..." % member
# based on https://github.com/matiasb/python-unrar/pull/4/files
res = []
if isinstance(member, rarfile.RarInfo):
member = member.filename
archive = unrarlib.RAROpenArchiveDataEx(self.filename, mode=constants.RAR_OM_EXTRACT)
handle = self._open(archive)
found, buf = False, []
def _callback(msg, UserData, P1, P2):
if msg == constants.UCM_PROCESSDATA:
data = (ctypes.c_char*P2).from_address(P1).raw
buf.append(data)
return 1
c_callback = unrarlib.UNRARCALLBACK(_callback)
unrarlib.RARSetCallback(handle, c_callback, 1)
try:
rarinfo = self._read_header(handle)
while rarinfo is not None:
#print "checking rar archive %s against %s" % (rarinfo.filename, member)
if rarinfo.filename == member:
self._process_current(handle, constants.RAR_TEST)
found = True
else:
self._process_current(handle, constants.RAR_SKIP)
rarinfo = self._read_header(handle)
except unrarlib.UnrarException:
raise rarfile.BadRarFile("Bad RAR archive data.")
finally:
self._close(handle)
if not found:
raise KeyError('There is no item named %r in the archive' % member)
return ''.join(buf)
if platform.system() == "Windows": if platform.system() == "Windows":
import _subprocess import _subprocess
@ -40,8 +84,8 @@ except ImportError:
pil_available = False pil_available = False
sys.path.insert(0, os.path.abspath(".") ) sys.path.insert(0, os.path.abspath(".") )
import UnRAR2 #import UnRAR2
from UnRAR2.rar_exceptions import * #from UnRAR2.rar_exceptions import *
#from settings import ComicTaggerSettings #from settings import ComicTaggerSettings
from comicinfoxml import ComicInfoXml from comicinfoxml import ComicInfoXml
@ -296,7 +340,7 @@ class RarArchiver:
# Make sure to escape brackets, since some funky stuff is going on # Make sure to escape brackets, since some funky stuff is going on
# underneath with "fnmatch" # underneath with "fnmatch"
archive_file = archive_file.replace("[", '[[]') #archive_file = archive_file.replace("[", '[[]')
entries = [] entries = []
rarc = self.getRARObj() rarc = self.getRARObj()
@ -305,11 +349,21 @@ class RarArchiver:
while tries < 7: while tries < 7:
try: try:
tries = tries+1 tries = tries+1
entries = rarc.read_files( archive_file ) #tmp_folder = tempfile.mkdtemp()
#tmp_file = os.path.join(tmp_folder, archive_file)
#rarc.extract(archive_file, tmp_folder)
data = rarc.open(archive_file)
#data = open(tmp_file).read()
entries = [(rarc.getinfo(archive_file), data)]
if entries[0][0].size != len(entries[0][1]):
#shutil.rmtree(tmp_folder, ignore_errors=True)
#entries = rarc.read_files( archive_file )
if entries[0][0].file_size != len(entries[0][1]):
print >> sys.stderr, u"readArchiveFile(): [file is not expected size: {0} vs {1}] {2}:{3} [attempt # {4}]".format( print >> sys.stderr, u"readArchiveFile(): [file is not expected size: {0} vs {1}] {2}:{3} [attempt # {4}]".format(
entries[0][0].size,len(entries[0][1]), self.path, archive_file, tries) entries[0][0].file_size,len(entries[0][1]), self.path, archive_file, tries)
continue continue
except (OSError, IOError) as e: except (OSError, IOError) as e:
@ -395,7 +449,7 @@ class RarArchiver:
#namelist = [ item.filename for item in rarc.infolist() ] #namelist = [ item.filename for item in rarc.infolist() ]
namelist = [] namelist = []
for item in rarc.infolist(): for item in rarc.infolist():
if item.size != 0: if item.file_size != 0:
namelist.append( item.filename ) namelist.append( item.filename )
except (OSError, IOError) as e: except (OSError, IOError) as e:
@ -414,7 +468,8 @@ class RarArchiver:
while tries < 7: while tries < 7:
try: try:
tries = tries+1 tries = tries+1
rarc = UnRAR2.RarFile( self.path ) #rarc = UnRAR2.RarFile( self.path )
rarc = OpenableRarFile(self.path)
except (OSError, IOError) as e: except (OSError, IOError) as e:
print >> sys.stderr, u"getRARObj(): [{0}] {1} attempt#{2}".format(str(e), self.path, tries) print >> sys.stderr, u"getRARObj(): [{0}] {1} attempt#{2}".format(str(e), self.path, tries)
@ -604,7 +659,7 @@ class ComicArchive:
def rarTest( self ): def rarTest( self ):
try: try:
rarc = UnRAR2.RarFile( self.path ) rarc = rarfile.RarFile( self.path )
except: # InvalidRARArchive: except: # InvalidRARArchive:
return False return False
else: else: