mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
IGN:Proper error messages when mimagemagick fails to load. Also workaround occasional IOError when sendingfiles to devices on linux
This commit is contained in:
parent
00c32f1a2a
commit
207a9728c0
@ -24,6 +24,12 @@ class File(object):
|
|||||||
self.name = os.path.basename(path)
|
self.name = os.path.basename(path)
|
||||||
|
|
||||||
|
|
||||||
|
def check_transfer(infile, dest):
|
||||||
|
infile.seek(0)
|
||||||
|
dest.seek(0)
|
||||||
|
return infile.read() == dest.read()
|
||||||
|
|
||||||
|
|
||||||
class CLI(object):
|
class CLI(object):
|
||||||
|
|
||||||
def get_file(self, path, outfile, end_session=True):
|
def get_file(self, path, outfile, end_session=True):
|
||||||
@ -45,7 +51,15 @@ class CLI(object):
|
|||||||
if not os.path.exists(d):
|
if not os.path.exists(d):
|
||||||
os.makedirs(d)
|
os.makedirs(d)
|
||||||
with open(path, 'wb') as dest:
|
with open(path, 'wb') as dest:
|
||||||
shutil.copyfileobj(infile, dest)
|
try:
|
||||||
|
shutil.copyfileobj(infile, dest)
|
||||||
|
except IOError:
|
||||||
|
print 'WARNING: First attempt to send file to device failed'
|
||||||
|
infile.seek(0)
|
||||||
|
dest.seek(0)
|
||||||
|
dest.truncate()
|
||||||
|
shutil.copyfileobj(infile, dest)
|
||||||
|
#if not check_transfer(infile, dest): raise Exception('Transfer failed')
|
||||||
if close:
|
if close:
|
||||||
infile.close()
|
infile.close()
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ and save it to a new file.
|
|||||||
|
|
||||||
>>> MagickWriteImage(wand,"out.png") #doctest: +ELLIPSIS
|
>>> MagickWriteImage(wand,"out.png") #doctest: +ELLIPSIS
|
||||||
<MagickBooleanType object at ...>
|
<MagickBooleanType object at ...>
|
||||||
>>>
|
>>>
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -82,14 +82,15 @@ else:
|
|||||||
_lib = util.find_library('MagickWand')
|
_lib = util.find_library('MagickWand')
|
||||||
if _lib is None:
|
if _lib is None:
|
||||||
_lib = util.find_library('Wand')
|
_lib = util.find_library('Wand')
|
||||||
|
|
||||||
_magick = _magick_error = None
|
_magick = _magick_error = None
|
||||||
try:
|
try:
|
||||||
_magick = ctypes.CDLL(_lib)
|
_magick = ctypes.CDLL(_lib)
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
_magick_erorr = str(err)
|
global _magick_error
|
||||||
|
_magick_error = str(err)
|
||||||
|
|
||||||
_initialized = False
|
_initialized = False
|
||||||
def initialize():
|
def initialize():
|
||||||
global _initialized
|
global _initialized
|
||||||
if not _initialized:
|
if not _initialized:
|
||||||
@ -97,22 +98,22 @@ def initialize():
|
|||||||
_magick.MagickWandGenesis()
|
_magick.MagickWandGenesis()
|
||||||
else:
|
else:
|
||||||
raise RuntimeError('Failed to load ImageMagick: %s'%_magick_error)
|
raise RuntimeError('Failed to load ImageMagick: %s'%_magick_error)
|
||||||
|
|
||||||
def finalize():
|
def finalize():
|
||||||
global _initialized
|
global _initialized
|
||||||
if _initialized:
|
if _initialized:
|
||||||
_magick.MagickWandTerminus()
|
_magick.MagickWandTerminus()
|
||||||
_initialized = False
|
_initialized = False
|
||||||
|
|
||||||
class ImageMagick(object):
|
class ImageMagick(object):
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
initialize()
|
initialize()
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
finalize()
|
finalize()
|
||||||
|
|
||||||
|
|
||||||
class MetricType(ctypes.c_int): pass
|
class MetricType(ctypes.c_int): pass
|
||||||
UndefinedMetric = MetricType(0)
|
UndefinedMetric = MetricType(0)
|
||||||
AbsoluteErrorMetric = MetricType(1)
|
AbsoluteErrorMetric = MetricType(1)
|
||||||
@ -489,7 +490,7 @@ SetEvaluateOperator = MagickEvaluateOperator(10)
|
|||||||
SubtractEvaluateOperator = MagickEvaluateOperator(11)
|
SubtractEvaluateOperator = MagickEvaluateOperator(11)
|
||||||
XorEvaluateOperator = MagickEvaluateOperator(12)
|
XorEvaluateOperator = MagickEvaluateOperator(12)
|
||||||
|
|
||||||
class ExceptionType(ctypes.c_int):
|
class ExceptionType(ctypes.c_int):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
UndefinedException = ExceptionType(0)
|
UndefinedException = ExceptionType(0)
|
||||||
@ -4360,9 +4361,8 @@ try:
|
|||||||
except AttributeError,e:
|
except AttributeError,e:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
PixelSetColor = _magick.PixelSetColor
|
PixelSetColor = _magick.PixelSetColor
|
||||||
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user