mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use lopen() in the container class
This commit is contained in:
parent
c055ec3a15
commit
c2895cfbc0
@ -564,7 +564,7 @@ class Container(ContainerBase): # {{{
|
|||||||
return set()
|
return set()
|
||||||
|
|
||||||
def parse(self, path, mime):
|
def parse(self, path, mime):
|
||||||
with open(path, 'rb') as src:
|
with lopen(path, 'rb') as src:
|
||||||
data = src.read()
|
data = src.read()
|
||||||
if mime in OEB_DOCS:
|
if mime in OEB_DOCS:
|
||||||
data = self.parse_xhtml(data, self.relpath(path))
|
data = self.parse_xhtml(data, self.relpath(path))
|
||||||
@ -944,7 +944,7 @@ class Container(ContainerBase): # {{{
|
|||||||
base = os.path.dirname(path)
|
base = os.path.dirname(path)
|
||||||
if not os.path.exists(base):
|
if not os.path.exists(base):
|
||||||
os.makedirs(base)
|
os.makedirs(base)
|
||||||
open(path, 'wb').close()
|
lopen(path, 'wb').close()
|
||||||
return item
|
return item
|
||||||
|
|
||||||
def format_opf(self):
|
def format_opf(self):
|
||||||
@ -999,7 +999,7 @@ class Container(ContainerBase): # {{{
|
|||||||
if self.cloned and nlinks_file(dest) > 1:
|
if self.cloned and nlinks_file(dest) > 1:
|
||||||
# Decouple this file from its links
|
# Decouple this file from its links
|
||||||
os.unlink(dest)
|
os.unlink(dest)
|
||||||
with open(dest, 'wb') as f:
|
with lopen(dest, 'wb') as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
|
|
||||||
def filesize(self, name):
|
def filesize(self, name):
|
||||||
@ -1040,7 +1040,7 @@ class Container(ContainerBase): # {{{
|
|||||||
this will commit the file if it is dirtied and remove it from the parse
|
this will commit the file if it is dirtied and remove it from the parse
|
||||||
cache. You must finish with this file before accessing the parsed
|
cache. You must finish with this file before accessing the parsed
|
||||||
version of it again, or bad things will happen. '''
|
version of it again, or bad things will happen. '''
|
||||||
return open(self.get_file_path_for_processing(name, mode not in {'r', 'rb'}), mode)
|
return lopen(self.get_file_path_for_processing(name, mode not in {'r', 'rb'}), mode)
|
||||||
|
|
||||||
def commit(self, outpath=None, keep_parsed=False):
|
def commit(self, outpath=None, keep_parsed=False):
|
||||||
'''
|
'''
|
||||||
@ -1058,7 +1058,7 @@ class Container(ContainerBase): # {{{
|
|||||||
mismatches = []
|
mismatches = []
|
||||||
for name, path in self.name_path_map.iteritems():
|
for name, path in self.name_path_map.iteritems():
|
||||||
opath = other.name_path_map[name]
|
opath = other.name_path_map[name]
|
||||||
with open(path, 'rb') as f1, open(opath, 'rb') as f2:
|
with lopen(path, 'rb') as f1, lopen(opath, 'rb') as f2:
|
||||||
if f1.read() != f2.read():
|
if f1.read() != f2.read():
|
||||||
mismatches.append('The file %s is not the same'%name)
|
mismatches.append('The file %s is not the same'%name)
|
||||||
return '\n'.join(mismatches)
|
return '\n'.join(mismatches)
|
||||||
@ -1131,7 +1131,7 @@ class EpubContainer(Container):
|
|||||||
if fname is not None:
|
if fname is not None:
|
||||||
shutil.copy(os.path.join(dirpath, fname), os.path.join(base, fname))
|
shutil.copy(os.path.join(dirpath, fname), os.path.join(base, fname))
|
||||||
else:
|
else:
|
||||||
with open(self.pathtoepub, 'rb') as stream:
|
with lopen(self.pathtoepub, 'rb') as stream:
|
||||||
try:
|
try:
|
||||||
zf = ZipFile(stream)
|
zf = ZipFile(stream)
|
||||||
zf.extractall(tdir)
|
zf.extractall(tdir)
|
||||||
@ -1348,12 +1348,12 @@ class EpubContainer(Container):
|
|||||||
if err.errno != errno.EEXIST:
|
if err.errno != errno.EEXIST:
|
||||||
raise
|
raise
|
||||||
for fname in filenames:
|
for fname in filenames:
|
||||||
with open(os.path.join(dirpath, fname), 'rb') as src, open(os.path.join(base, fname), 'wb') as dest:
|
with lopen(os.path.join(dirpath, fname), 'rb') as src, lopen(os.path.join(base, fname), 'wb') as dest:
|
||||||
shutil.copyfileobj(src, dest)
|
shutil.copyfileobj(src, dest)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
from calibre.ebooks.tweak import zip_rebuilder
|
from calibre.ebooks.tweak import zip_rebuilder
|
||||||
with open(join(self.root, 'mimetype'), 'wb') as f:
|
with lopen(join(self.root, 'mimetype'), 'wb') as f:
|
||||||
f.write(guess_type('a.epub'))
|
f.write(guess_type('a.epub'))
|
||||||
zip_rebuilder(self.root, outpath)
|
zip_rebuilder(self.root, outpath)
|
||||||
for name, data in restore_fonts.iteritems():
|
for name, data in restore_fonts.iteritems():
|
||||||
@ -1439,7 +1439,7 @@ class AZW3Container(Container):
|
|||||||
tdir = PersistentTemporaryDirectory('_azw3_container')
|
tdir = PersistentTemporaryDirectory('_azw3_container')
|
||||||
tdir = os.path.abspath(os.path.realpath(tdir))
|
tdir = os.path.abspath(os.path.realpath(tdir))
|
||||||
self.root = tdir
|
self.root = tdir
|
||||||
with open(pathtoazw3, 'rb') as stream:
|
with lopen(pathtoazw3, 'rb') as stream:
|
||||||
raw = stream.read(3)
|
raw = stream.read(3)
|
||||||
if raw == b'TPZ':
|
if raw == b'TPZ':
|
||||||
raise InvalidMobi(_('This is not a MOBI file. It is a Topaz file.'))
|
raise InvalidMobi(_('This is not a MOBI file. It is a Topaz file.'))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user