Use lopen() in the container class

This commit is contained in:
Kovid Goyal 2018-11-16 08:46:32 +05:30
parent c055ec3a15
commit c2895cfbc0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -564,7 +564,7 @@ class Container(ContainerBase): # {{{
return set()
def parse(self, path, mime):
with open(path, 'rb') as src:
with lopen(path, 'rb') as src:
data = src.read()
if mime in OEB_DOCS:
data = self.parse_xhtml(data, self.relpath(path))
@ -944,7 +944,7 @@ class Container(ContainerBase): # {{{
base = os.path.dirname(path)
if not os.path.exists(base):
os.makedirs(base)
open(path, 'wb').close()
lopen(path, 'wb').close()
return item
def format_opf(self):
@ -999,7 +999,7 @@ class Container(ContainerBase): # {{{
if self.cloned and nlinks_file(dest) > 1:
# Decouple this file from its links
os.unlink(dest)
with open(dest, 'wb') as f:
with lopen(dest, 'wb') as f:
f.write(data)
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
cache. You must finish with this file before accessing the parsed
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):
'''
@ -1058,7 +1058,7 @@ class Container(ContainerBase): # {{{
mismatches = []
for name, path in self.name_path_map.iteritems():
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():
mismatches.append('The file %s is not the same'%name)
return '\n'.join(mismatches)
@ -1131,7 +1131,7 @@ class EpubContainer(Container):
if fname is not None:
shutil.copy(os.path.join(dirpath, fname), os.path.join(base, fname))
else:
with open(self.pathtoepub, 'rb') as stream:
with lopen(self.pathtoepub, 'rb') as stream:
try:
zf = ZipFile(stream)
zf.extractall(tdir)
@ -1348,12 +1348,12 @@ class EpubContainer(Container):
if err.errno != errno.EEXIST:
raise
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)
else:
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'))
zip_rebuilder(self.root, outpath)
for name, data in restore_fonts.iteritems():
@ -1439,7 +1439,7 @@ class AZW3Container(Container):
tdir = PersistentTemporaryDirectory('_azw3_container')
tdir = os.path.abspath(os.path.realpath(tdir))
self.root = tdir
with open(pathtoazw3, 'rb') as stream:
with lopen(pathtoazw3, 'rb') as stream:
raw = stream.read(3)
if raw == b'TPZ':
raise InvalidMobi(_('This is not a MOBI file. It is a Topaz file.'))