1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-01-01 07:22:26 +01:00

Removed superfluous lopen builtin

This commit is contained in:
2020-04-20 21:12:22 +02:00
parent 3ca3f08054
commit 9e076e0af4
38 changed files with 107 additions and 195 deletions

View File

@@ -175,7 +175,7 @@ class CHMInput(InputFormatPlugin):
return htmlpath, toc
def _read_file(self, name):
with lopen(name, 'rb') as f:
with open(name, 'rb') as f:
data = f.read()
return data

View File

@@ -22,7 +22,7 @@ def decrypt_font_data(key, data, algorithm):
def decrypt_font(key, path, algorithm):
with lopen(path, 'r+b') as f:
with open(path, 'r+b') as f:
data = decrypt_font_data(key, f.read(), algorithm)
f.seek(0), f.truncate(), f.write(data)
@@ -221,7 +221,7 @@ class EPUBInput(InputFormatPlugin):
if os.path.exists(guide_cover):
renderer = render_html_svg_workaround(guide_cover, log)
if renderer is not None:
with lopen('calibre_raster_cover.jpg', 'wb') as f:
with open('calibre_raster_cover.jpg', 'wb') as f:
f.write(renderer)
# Set the titlepage guide entry
@@ -236,7 +236,7 @@ class EPUBInput(InputFormatPlugin):
if k.endswith(attr):
return v
try:
with lopen('META-INF/container.xml', 'rb') as f:
with open('META-INF/container.xml', 'rb') as f:
root = safe_xml_fromstring(f.read())
for r in root.xpath('//*[local-name()="rootfile"]'):
if attr(r, 'media-type') != "application/oebps-package+xml":
@@ -343,7 +343,7 @@ class EPUBInput(InputFormatPlugin):
if len(list(opf.iterspine())) == 0:
raise ValueError('No valid entries in the spine of this EPUB')
with lopen('content.opf', 'wb') as nopf:
with open('content.opf', 'wb') as nopf:
nopf.write(opf.render())
return os.path.abspath('content.opf')
@@ -356,7 +356,7 @@ class EPUBInput(InputFormatPlugin):
from ebook_converter.ebooks.oeb.polish.toc import first_child
from ebook_converter.utils.xml_parse import safe_xml_fromstring
from tempfile import NamedTemporaryFile
with lopen(nav_path, 'rb') as f:
with open(nav_path, 'rb') as f:
raw = f.read()
raw = xml_to_unicode(raw, strip_encoding_pats=True, assume_utf8=True)[0]
root = parse(raw, log=log)
@@ -420,7 +420,7 @@ class EPUBInput(InputFormatPlugin):
changed = True
elem.set('data-calibre-removed-titlepage', '1')
if changed:
with lopen(nav_path, 'wb') as f:
with open(nav_path, 'wb') as f:
f.write(serialize(root, 'application/xhtml+xml'))
def postprocess_book(self, oeb, opts, log):

View File

@@ -328,7 +328,7 @@ class EPUBOutput(OutputFormatPlugin):
uris.pop(uri)
continue
self.log.debug('Encrypting font:', uri)
with lopen(path, 'r+b') as f:
with open(path, 'r+b') as f:
data = f.read(1024)
if len(data) >= 1024:
data = bytearray(data)

View File

@@ -189,7 +189,7 @@ class FB2Output(OutputFormatPlugin):
close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
os.makedirs(os.path.dirname(output_path))
out_stream = lopen(output_path, 'wb')
out_stream = open(output_path, 'wb')
else:
out_stream = output_path

View File

@@ -113,7 +113,7 @@ class HTMLZOutput(OutputFormatPlugin):
if cover_data:
from ebook_converter.utils.img import save_cover_data_to
cover_path = os.path.join(tdir, u'cover.jpg')
with lopen(cover_path, 'w') as cf:
with open(cover_path, 'w') as cf:
cf.write('')
save_cover_data_to(cover_data, cover_path)
except:

View File

@@ -51,14 +51,14 @@ class MOBIInput(InputFormatPlugin):
if raw:
if isinstance(raw, str):
raw = raw.encode('utf-8')
with lopen('debug-raw.html', 'wb') as f:
with open('debug-raw.html', 'wb') as f:
f.write(raw)
from ebook_converter.ebooks.oeb.base import close_self_closing_tags
for f, root in parse_cache.items():
raw = html.tostring(root, encoding='utf-8', method='xml',
include_meta_content_type=False)
raw = close_self_closing_tags(raw)
with lopen(f, 'wb') as q:
with open(f, 'wb') as q:
q.write(raw)
accelerators['pagebreaks'] = '//h:div[@class="mbp_pagebreak"]'
return mr.created_opf_path

View File

@@ -53,7 +53,7 @@ class OEBOutput(OutputFormatPlugin):
# Needed as I can't get lxml to output opf:role and
# not output <opf:metadata> as well
raw = re.sub(br'(<[/]{0,1})opf:', br'\1', raw)
with lopen(href, 'wb') as f:
with open(href, 'wb') as f:
f.write(raw)
for item in oeb_book.manifest:
@@ -65,7 +65,7 @@ class OEBOutput(OutputFormatPlugin):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
with lopen(path, 'wb') as f:
with open(path, 'wb') as f:
f.write(item.bytes_representation)
item.unload_data_from_memory(memory=path)

View File

@@ -39,7 +39,7 @@ class PDBOutput(OutputFormatPlugin):
close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path):
os.makedirs(os.path.dirname(output_path))
out_stream = lopen(output_path, 'wb')
out_stream = open(output_path, 'wb')
else:
out_stream = output_path

View File

@@ -34,7 +34,7 @@ class PDFInput(InputFormatPlugin):
from ebook_converter.ebooks.pdf.reflow import PDFDocument
pdftohtml(os.getcwd(), stream.name, self.opts.no_images, as_xml=True)
with lopen('index.xml', 'rb') as f:
with open('index.xml', 'rb') as f:
xml = clean_ascii_chars(f.read())
PDFDocument(xml, self.opts, self.log)
return os.path.join(os.getcwd(), 'metadata.opf')
@@ -66,12 +66,12 @@ class PDFInput(InputFormatPlugin):
opf.create_spine(['index.html'])
log.debug('Rendering manifest...')
with lopen('metadata.opf', 'wb') as opffile:
with open('metadata.opf', 'wb') as opffile:
opf.render(opffile)
if os.path.exists('toc.ncx'):
ncxid = opf.manifest.id_for_path('toc.ncx')
if ncxid:
with lopen('metadata.opf', 'r+b') as f:
with open('metadata.opf', 'r+b') as f:
raw = f.read().replace(b'<spine', b'<spine toc="%s"' % as_bytes(ncxid))
f.seek(0)
f.write(raw)

View File

@@ -27,14 +27,14 @@ class PMLInput(InputFormatPlugin):
hclose = False
if not hasattr(pml_path, 'read'):
pml_stream = lopen(pml_path, 'rb')
pml_stream = open(pml_path, 'rb')
pclose = True
else:
pml_stream = pml_path
pml_stream.seek(0)
if not hasattr(html_path, 'write'):
html_stream = lopen(html_path, 'wb')
html_stream = open(html_path, 'wb')
hclose = True
else:
html_stream = html_path
@@ -135,8 +135,8 @@ class PMLInput(InputFormatPlugin):
opf.create_manifest(manifest_items)
opf.create_spine(pages)
opf.set_toc(toc)
with lopen('metadata.opf', 'wb') as opffile:
with lopen('toc.ncx', 'wb') as tocfile:
with open('metadata.opf', 'wb') as opffile:
with open('toc.ncx', 'wb') as tocfile:
opf.render(opffile, tocfile, 'toc.ncx')
return os.path.join(os.getcwd(), 'metadata.opf')

View File

@@ -40,7 +40,7 @@ class PMLOutput(OutputFormatPlugin):
with TemporaryDirectory('_pmlz_output') as tdir:
pmlmlizer = PMLMLizer(log)
pml = str(pmlmlizer.extract_content(oeb_book, opts))
with lopen(os.path.join(tdir, 'index.pml'), 'wb') as out:
with open(os.path.join(tdir, 'index.pml'), 'wb') as out:
out.write(pml.encode(opts.pml_output_encoding, 'replace'))
img_path = os.path.join(tdir, 'index_img')
@@ -70,5 +70,5 @@ class PMLOutput(OutputFormatPlugin):
path = os.path.join(out_dir, image_hrefs[item.href])
with lopen(path, 'wb') as out:
with open(path, 'wb') as out:
out.write(data)

View File

@@ -28,7 +28,7 @@ class RBOutput(OutputFormatPlugin):
close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path):
os.makedirs(os.path.dirname(output_path))
out_stream = lopen(output_path, 'wb')
out_stream = open(output_path, 'wb')
else:
out_stream = output_path

View File

@@ -61,7 +61,7 @@ class RecipeInput(InputFormatPlugin):
zf = ZipFile(recipe_or_file, 'r')
zf.extractall()
zf.close()
with lopen('download.recipe', 'rb') as f:
with open('download.recipe', 'rb') as f:
self.recipe_source = f.read()
recipe = compile_recipe(self.recipe_source)
recipe.needs_subscription = False
@@ -84,7 +84,7 @@ class RecipeInput(InputFormatPlugin):
self.recipe_source = self.recipe_source.encode('utf-8')
recipe = compile_recipe(self.recipe_source)
elif os.access(recipe_or_file, os.R_OK):
with lopen(recipe_or_file, 'rb') as f:
with open(recipe_or_file, 'rb') as f:
self.recipe_source = f.read()
recipe = compile_recipe(self.recipe_source)
log('Using custom recipe')

View File

@@ -173,7 +173,7 @@ class RTFInput(InputFormatPlugin):
' Use Microsoft Word or OpenOffice to save this RTF file'
' as HTML and convert that in calibre.')
name = name.replace('.wmf', '.jpg')
with lopen(name, 'wb') as f:
with open(name, 'wb') as f:
f.write(self.default_img)
return name

View File

@@ -26,7 +26,7 @@ class RTFOutput(OutputFormatPlugin):
close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
os.makedirs(os.path.dirname(output_path))
out_stream = lopen(output_path, 'wb')
out_stream = open(output_path, 'wb')
else:
out_stream = output_path

View File

@@ -242,7 +242,7 @@ class SNBOutput(OutputFormatPlugin):
# img = img.rotate(90)
# x,y = y,x
img = resize_image(img, x // scale, y // scale)
with lopen(imagePath, 'wb') as f:
with open(imagePath, 'wb') as f:
f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))

View File

@@ -31,7 +31,7 @@ class TCROutput(OutputFormatPlugin):
close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path):
os.makedirs(os.path.dirname(output_path))
out_stream = lopen(output_path, 'wb')
out_stream = open(output_path, 'wb')
else:
out_stream = output_path

View File

@@ -945,7 +945,7 @@ OptionRecommendation(name='search_replace',
if self.opts.read_metadata_from_opf is not None:
self.opts.read_metadata_from_opf = os.path.abspath(
self.opts.read_metadata_from_opf)
with lopen(self.opts.read_metadata_from_opf, 'rb') as stream:
with open(self.opts.read_metadata_from_opf, 'rb') as stream:
opf = OPF(stream, os.path.dirname(self.opts.read_metadata_from_opf))
mi = opf.to_book_metadata()
self.opts_to_mi(mi)
@@ -955,7 +955,7 @@ OptionRecommendation(name='search_replace',
ext = mi.cover.rpartition('.')[-1].lower().strip()
if ext not in ('png', 'jpg', 'jpeg', 'gif'):
ext = 'jpg'
with lopen(mi.cover, 'rb') as stream:
with open(mi.cover, 'rb') as stream:
mi.cover_data = (ext, stream.read())
mi.cover = None
self.user_metadata = mi
@@ -1061,7 +1061,7 @@ OptionRecommendation(name='search_replace',
self.opts.debug_pipeline = os.path.abspath(self.opts.debug_pipeline)
if not os.path.exists(self.opts.debug_pipeline):
os.makedirs(self.opts.debug_pipeline)
with lopen(os.path.join(self.opts.debug_pipeline, 'README.txt'), 'wb') as f:
with open(os.path.join(self.opts.debug_pipeline, 'README.txt'), 'wb') as f:
f.write(DEBUG_README)
for x in ('input', 'parsed', 'structure', 'processed'):
x = os.path.join(self.opts.debug_pipeline, x)
@@ -1079,7 +1079,7 @@ OptionRecommendation(name='search_replace',
tdir = PersistentTemporaryDirectory('_plumber')
stream = self.input if self.input_fmt == 'recipe' else \
lopen(self.input, 'rb')
open(self.input, 'rb')
if self.input_fmt == 'recipe':
self.opts.original_recipe_input_arg = self.original_input_arg

View File

@@ -217,7 +217,7 @@ def cleanup_markup(log, root, styles, dest_dir, detect_cover, XPath):
if os.path.exists(path) and before_count(root, img, limit=10) < 5:
from ebook_converter.utils.imghdr import identify
try:
with lopen(path, 'rb') as imf:
with open(path, 'rb') as imf:
fmt, width, height = identify(imf)
except:
width, height, fmt = 0, 0, None # noqa

View File

@@ -372,11 +372,11 @@ class Convert(object):
def write(self, doc):
toc = create_toc(doc, self.body, self.resolved_link_map, self.styles, self.object_map, self.log, self.namespace)
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with lopen(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx, self.notes_nopb, self.nosupsub)
if css:
with lopen(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8'))
opf = OPFCreator(self.dest_dir, self.mi)
@@ -394,7 +394,7 @@ class Convert(object):
guide.append(E.reference(
href='index.html#' + self.toc_anchor, title=_('Table of Contents'), type='toc'))
toc_file = os.path.join(self.dest_dir, 'toc.ncx')
with lopen(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx', process_guide=process_guide)
if os.path.getsize(toc_file) == 0:
os.remove(toc_file)

View File

@@ -48,7 +48,7 @@ def _metadata_from_formats(formats, force_read_metadata=False, pattern=None):
return mi2
for path, ext in zip(formats, extensions):
with lopen(path, 'rb') as stream:
with open(path, 'rb') as stream:
try:
newmi = get_metadata(stream, stream_type=ext,
use_libprs_metadata=True,
@@ -223,19 +223,19 @@ def opf_metadata(opfpath):
def forked_read_metadata(path, tdir):
from ebook_converter.ebooks.metadata.opf2 import metadata_to_opf
with lopen(path, 'rb') as f:
with open(path, 'rb') as f:
fmt = os.path.splitext(path)[1][1:].lower()
f.seek(0, 2)
sz = f.tell()
with lopen(os.path.join(tdir, 'size.txt'), 'wb') as s:
with open(os.path.join(tdir, 'size.txt'), 'wb') as s:
s.write(str(sz).encode('ascii'))
f.seek(0)
mi = get_metadata(f, fmt)
if mi.cover_data and mi.cover_data[1]:
with lopen(os.path.join(tdir, 'cover.jpg'), 'wb') as f:
with open(os.path.join(tdir, 'cover.jpg'), 'wb') as f:
f.write(mi.cover_data[1])
mi.cover_data = (None, None)
mi.cover = 'cover.jpg'
opf = metadata_to_opf(mi, default_lang='und')
with lopen(os.path.join(tdir, 'metadata.opf'), 'wb') as f:
with open(os.path.join(tdir, 'metadata.opf'), 'wb') as f:
f.write(opf)

View File

@@ -230,7 +230,7 @@ class TOC(list):
def read_html_toc(self, toc):
self.base_path = os.path.dirname(toc)
with lopen(toc, 'rb') as f:
with open(toc, 'rb') as f:
parsed_toc = parse_html_toc(f.read())
for href, fragment, txt in parsed_toc:
add = True

View File

@@ -289,7 +289,7 @@ class MobiReader(object):
def write_as_utf8(path, data):
if isinstance(data, str):
data = data.encode('utf-8')
with lopen(path, 'wb') as f:
with open(path, 'wb') as f:
f.write(data)
parse_cache[htmlfile] = root
@@ -297,7 +297,7 @@ class MobiReader(object):
ncx = io.BytesIO()
opf, ncx_manifest_entry = self.create_opf(htmlfile, guide, root)
self.created_opf_path = os.path.splitext(htmlfile)[0] + '.opf'
opf.render(lopen(self.created_opf_path, 'wb'), ncx,
opf.render(open(self.created_opf_path, 'wb'), ncx,
ncx_manifest_entry=ncx_manifest_entry)
ncx = ncx.getvalue()
if ncx:

View File

@@ -60,7 +60,7 @@ class Resources(object):
try:
from ebook_converter.utils.img import optimize_png
optimize_png(pt.name)
data = lopen(pt.name, 'rb').read()
data = open(pt.name, 'rb').read()
finally:
os.remove(pt.name)
return func(data)

View File

@@ -575,7 +575,7 @@ class DirContainer(object):
if path is None:
path = self.opfname
path = os.path.join(self.rootdir, self._unquote(path))
with lopen(path, 'rb') as f:
with open(path, 'rb') as f:
return f.read()
def write(self, path, data):
@@ -583,7 +583,7 @@ class DirContainer(object):
dir = os.path.dirname(path)
if not os.path.isdir(dir):
os.makedirs(dir)
with lopen(path, 'wb') as f:
with open(path, 'wb') as f:
return f.write(data)
def exists(self, path):

View File

@@ -367,7 +367,7 @@ class Container(ContainerBase): # {{{
base = os.path.dirname(path)
if not os.path.exists(base):
os.makedirs(base)
with lopen(path, 'wb') as f:
with open(path, 'wb') as f:
if hasattr(data, 'read'):
shutil.copyfileobj(data, f)
else:
@@ -579,7 +579,7 @@ class Container(ContainerBase): # {{{
return set()
def parse(self, path, mime):
with lopen(path, 'rb') as src:
with open(path, 'rb') as src:
data = src.read()
if mime in OEB_DOCS:
data = self.parse_xhtml(data, self.relpath(path))
@@ -959,7 +959,7 @@ class Container(ContainerBase): # {{{
base = os.path.dirname(path)
if not os.path.exists(base):
os.makedirs(base)
lopen(path, 'wb').close()
open(path, 'wb').close()
return item
def format_opf(self):
@@ -1014,7 +1014,7 @@ class Container(ContainerBase): # {{{
if self.cloned and nlinks_file(dest) > 1:
# Decouple this file from its links
os.unlink(dest)
with lopen(dest, 'wb') as f:
with open(dest, 'wb') as f:
f.write(data)
def filesize(self, name):
@@ -1055,7 +1055,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 lopen(self.get_file_path_for_processing(name, mode not in {'r', 'rb'}), mode)
return open(self.get_file_path_for_processing(name, mode not in {'r', 'rb'}), mode)
def commit(self, outpath=None, keep_parsed=False):
'''
@@ -1073,7 +1073,7 @@ class Container(ContainerBase): # {{{
mismatches = []
for name, path in iteritems(self.name_path_map):
opath = other.name_path_map[name]
with lopen(path, 'rb') as f1, lopen(opath, 'rb') as f2:
with open(path, 'rb') as f1, open(opath, 'rb') as f2:
if f1.read() != f2.read():
mismatches.append('The file %s is not the same'%name)
return '\n'.join(mismatches)
@@ -1146,7 +1146,7 @@ class EpubContainer(Container):
if fname is not None:
shutil.copy(os.path.join(dirpath, fname), os.path.join(base, fname))
else:
with lopen(self.pathtoepub, 'rb') as stream:
with open(self.pathtoepub, 'rb') as stream:
try:
zf = ZipFile(stream)
zf.extractall(tdir)
@@ -1363,12 +1363,12 @@ class EpubContainer(Container):
if err.errno != errno.EEXIST:
raise
for fname in filenames:
with lopen(os.path.join(dirpath, fname), 'rb') as src, lopen(os.path.join(base, fname), 'wb') as dest:
with open(os.path.join(dirpath, fname), 'rb') as src, open(os.path.join(base, fname), 'wb') as dest:
shutil.copyfileobj(src, dest)
else:
from ebook_converter.ebooks.tweak import zip_rebuilder
with lopen(join(self.root, 'mimetype'), 'wb') as f:
with open(join(self.root, 'mimetype'), 'wb') as f:
et = guess_type('a.epub')
if not isinstance(et, bytes):
et = et.encode('ascii')
@@ -1398,7 +1398,7 @@ class InvalidMobi(InvalidBook):
def do_explode(path, dest):
from ebook_converter.ebooks.mobi.reader.mobi6 import MobiReader
from ebook_converter.ebooks.mobi.reader.mobi8 import Mobi8Reader
with lopen(path, 'rb') as stream:
with open(path, 'rb') as stream:
mr = MobiReader(stream, default_log, None, None)
with CurrentDir(dest):
@@ -1456,7 +1456,7 @@ class AZW3Container(Container):
tdir = PersistentTemporaryDirectory('_azw3_container')
tdir = os.path.abspath(os.path.realpath(tdir))
self.root = tdir
with lopen(pathtoazw3, 'rb') as stream:
with open(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.'))

View File

@@ -412,7 +412,7 @@ class Reader(FormatReader):
for col in row:
if col not in images:
raise Exception('Image with uid: %s missing.' % col)
w, h = identify(lopen('%s.jpg' % col, 'rb'))[1:]
w, h = identify(open('%s.jpg' % col, 'rb'))[1:]
row_width += w
if col_height < h:
col_height = h
@@ -427,14 +427,14 @@ class Reader(FormatReader):
x_off = 0
largest_height = 0
for col in row:
im = image_from_data(lopen('%s.jpg' % col, 'rb').read())
im = image_from_data(open('%s.jpg' % col, 'rb').read())
canvas.compose(im, x_off, y_off)
w, h = im.width(), im.height()
x_off += w
if largest_height < h:
largest_height = h
y_off += largest_height
with lopen('%s.jpg' % uid) as out:
with open('%s.jpg' % uid) as out:
out.write(canvas.export(compression_quality=70))
self.log.debug('Wrote composite image with uid %s to images/%s.jpg' % (uid, uid))
except Exception as e:

View File

@@ -47,7 +47,7 @@ def pdftohtml(output_dir, pdf_path, no_images, as_xml=False):
pdfsrc = os.path.join(output_dir, 'src.pdf')
index = os.path.join(output_dir, 'index.'+('xml' if as_xml else 'html'))
with lopen(pdf_path, 'rb') as src, lopen(pdfsrc, 'wb') as dest:
with open(pdf_path, 'rb') as src, open(pdfsrc, 'wb') as dest:
shutil.copyfileobj(src, dest)
with CurrentDir(output_dir):
@@ -79,7 +79,7 @@ def pdftohtml(output_dir, pdf_path, no_images, as_xml=False):
ret = eintr_retry_call(p.wait)
logf.flush()
logf.close()
out = lopen(logf.name, 'rb').read().decode('utf-8', 'replace').strip()
out = open(logf.name, 'rb').read().decode('utf-8', 'replace').strip()
if ret != 0:
raise ConversionError('pdftohtml failed with return code: %d\n%s' % (ret, out))
if out:
@@ -89,7 +89,7 @@ def pdftohtml(output_dir, pdf_path, no_images, as_xml=False):
raise DRMError()
if not as_xml:
with lopen(index, 'r+b') as i:
with open(index, 'r+b') as i:
raw = i.read().decode('utf-8', 'replace')
raw = flip_images(raw)
raw = raw.replace('<head', '<!-- created by ebook-converter\'s pdftohtml -->\n <head', 1)
@@ -151,7 +151,7 @@ def parse_outline(raw, output_dir):
def flip_image(img, flip):
from ebook_converter.utils.img import flip_image, image_and_format_from_data, image_to_data
with lopen(img, 'r+b') as f:
with open(img, 'r+b') as f:
img, fmt = image_and_format_from_data(f.read())
img = flip_image(img, horizontal='x' in flip, vertical='y' in flip)
f.seek(0), f.truncate()

View File

@@ -228,7 +228,7 @@ def opf_writer(path, opf_name, manifest, spine, mi):
opf = OPFCreator(path, mi)
opf.create_manifest(manifest)
opf.create_spine(spine)
with lopen(os.path.join(path, opf_name), 'wb') as opffile:
with open(os.path.join(path, opf_name), 'wb') as opffile:
opf.render(opffile)

View File

@@ -1,9 +1,7 @@
import importlib
import os
import sys
is_py3 = sys.version_info.major >= 3
native_string_type = str
iterkeys = iter

View File

@@ -9,10 +9,10 @@ Perform various initialization tasks.
import builtins
import locale
import sys
import os
from ebook_converter import constants
# Default translation is NOOP
from ebook_converter.polyglot.builtins import is_py3
builtins.__dict__['_'] = lambda s: s
# For strings which belong in the translation tables, but which shouldn't be
@@ -23,7 +23,6 @@ builtins.__dict__['__'] = lambda s: s
builtins.__dict__['dynamic_property'] = lambda func: func(None)
from ebook_converter.constants import iswindows, preferred_encoding, plugins, isosx, islinux, isfrozen, DEBUG, isfreebsd, ispy3
_run_once = False
winutil = winutilerror = None
@@ -32,74 +31,30 @@ if not _run_once:
_run_once = True
from importlib import import_module
if not isfrozen and not ispy3:
# Prevent PyQt4 from being loaded
class PyQt4Ban(object):
def find_module(self, fullname, path=None):
if fullname.startswith('PyQt4'):
return self
def load_module(self, fullname):
raise ImportError('Importing PyQt4 is not allowed as calibre uses PyQt5')
sys.meta_path.insert(0, PyQt4Ban())
class DeVendor(object):
if ispy3:
def find_spec(self, fullname, path, target=None):
spec = None
if fullname == 'calibre.web.feeds.feedparser':
m = import_module('feedparser')
spec = m.__spec__
elif fullname.startswith('calibre.ebooks.markdown'):
m = import_module(fullname[len('calibre.ebooks.'):])
spec = m.__spec__
return spec
def find_spec(self, fullname, path, target=None):
spec = None
if fullname == 'calibre.web.feeds.feedparser':
m = import_module('feedparser')
spec = m.__spec__
elif fullname.startswith('calibre.ebooks.markdown'):
m = import_module(fullname[len('calibre.ebooks.'):])
spec = m.__spec__
return spec
else:
def find_module(self, fullname, path=None):
if fullname == 'calibre.web.feeds.feedparser' or fullname.startswith('calibre.ebooks.markdown'):
return self
def load_module(self, fullname):
if fullname == 'calibre.web.feeds.feedparser':
return import_module('feedparser')
return import_module(fullname[len('calibre.ebooks.'):])
sys.meta_path.insert(0, DeVendor())
#
# Platform specific modules
if iswindows:
winutil, winutilerror = plugins['winutil']
if constants.iswindows:
winutil, winutilerror = constants.plugins['winutil']
if not winutil:
raise RuntimeError('Failed to load the winutil plugin: %s'%winutilerror)
if len(sys.argv) > 1 and not isinstance(sys.argv[1], str):
sys.argv[1:] = winutil.argv()[1-len(sys.argv):]
if not ispy3:
# Python2's expanduser is broken for non-ASCII usernames
# and unicode paths
def expanduser(path):
if isinstance(path, bytes):
path = path.decode('mbcs')
if path[:1] != '~':
return path
i, n = 1, len(path)
while i < n and path[i] not in '/\\':
i += 1
userhome = winutil.special_folder_path(winutil.CSIDL_PROFILE)
if i != 1: # ~user
userhome = os.path.join(os.path.dirname(userhome), path[1:i])
return userhome + path[i:]
os.path.expanduser = expanduser
# Ensure that all temp files/dirs are created under a calibre tmp dir
from ebook_converter.ptempfile import base_dir
try:
@@ -109,8 +64,8 @@ if not _run_once:
#
# Convert command line arguments to unicode
enc = preferred_encoding
if isosx:
enc = constants.preferred_encoding
if constants.isosx:
enc = 'utf-8'
for i in range(1, len(sys.argv)):
if not isinstance(sys.argv[i], str):
@@ -118,7 +73,7 @@ if not _run_once:
#
# Ensure that the max number of open files is at least 1024
if iswindows:
if constants.iswindows:
# See https://msdn.microsoft.com/en-us/library/6e3b887c.aspx
if hasattr(winutil, 'setmaxstdio'):
winutil.setmaxstdio(max(1024, winutil.getmaxstdio()))
@@ -129,7 +84,7 @@ if not _run_once:
try:
resource.setrlimit(resource.RLIMIT_NOFILE, (min(1024, hard), hard))
except Exception:
if DEBUG:
if constants.DEBUG:
import traceback
traceback.print_exc()
@@ -162,47 +117,6 @@ if not _run_once:
except:
pass
# local_open() opens a file that wont be inherited by child processes
if is_py3:
local_open = open # PEP 446
elif iswindows:
def local_open(name, mode='r', bufsize=-1):
mode += 'N'
return open(name, mode, bufsize)
elif isosx:
import fcntl
FIOCLEX = 0x20006601
def local_open(name, mode='r', bufsize=-1):
ans = open(name, mode, bufsize)
try:
fcntl.ioctl(ans.fileno(), FIOCLEX)
except EnvironmentError:
fcntl.fcntl(ans, fcntl.F_SETFD, fcntl.fcntl(ans, fcntl.F_GETFD) | fcntl.FD_CLOEXEC)
return ans
else:
import fcntl
try:
cloexec_flag = fcntl.FD_CLOEXEC
except AttributeError:
cloexec_flag = 1
supports_mode_e = False
def local_open(name, mode='r', bufsize=-1):
global supports_mode_e
mode += 'e'
ans = open(name, mode, bufsize)
if supports_mode_e:
return ans
old = fcntl.fcntl(ans, fcntl.F_GETFD)
if not (old & cloexec_flag):
fcntl.fcntl(ans, fcntl.F_SETFD, old | cloexec_flag)
else:
supports_mode_e = True
return ans
builtins.__dict__['lopen'] = local_open
from ebook_converter.utils.icu import lower as icu_lower, upper as icu_upper
builtins.__dict__['icu_lower'] = icu_lower
builtins.__dict__['icu_upper'] = icu_upper
@@ -226,7 +140,7 @@ if not _run_once:
bound_signal.connect(slot, **kw)
builtins.__dict__['connect_lambda'] = connect_lambda
if islinux or isosx or isfreebsd:
if constants.islinux or constants.isosx or constants.isfreebsd:
# Name all threads at the OS level created using the threading module, see
# http://bugs.python.org/issue15500
import threading
@@ -244,7 +158,7 @@ if not _run_once:
if name:
if isinstance(name, str):
name = name.encode('ascii', 'replace').decode('ascii')
plugins['speedup'][0].set_thread_name(name[:15])
constants.plugins['speedup'][0].set_thread_name(name[:15])
except Exception:
pass # Don't care about failure to set name
threading.Thread.start = new_start
@@ -254,9 +168,9 @@ def test_lopen():
from ebook_converter.ptempfile import TemporaryDirectory
from ebook_converter import CurrentDir
n = 'f\xe4llen'
print('testing lopen()')
print('testing open()')
if iswindows:
if constants.iswindows:
import msvcrt, win32api
def assert_not_inheritable(f):
@@ -270,7 +184,7 @@ def test_lopen():
raise SystemExit('File handle is inheritable!')
def copen(*args):
ans = lopen(*args)
ans = open(*args)
assert_not_inheritable(ans)
return ans

View File

@@ -195,7 +195,7 @@ def case_preserving_open_file(path, mode='wb', mkdir_mode=0o777):
ans = fpath = cpath
else:
fname = components[-1]
ans = lopen(os.path.join(cpath, fname), mode)
ans = open(os.path.join(cpath, fname), mode)
# Ensure file and all its metadata is written to disk so that subsequent
# listdir() has file name in it. I don't know if this is actually
# necessary, but given the diversity of platforms, best to be safe.
@@ -434,7 +434,7 @@ class WindowsAtomicFolderMove(object):
pass
win32file.SetFilePointer(handle, 0, win32file.FILE_BEGIN)
with lopen(dest, 'wb') as f:
with open(dest, 'wb') as f:
while True:
hr, raw = win32file.ReadFile(handle, 1024*1024)
if hr != 0:

View File

@@ -245,7 +245,7 @@ class FontScanner(Thread):
path = font_or_path
if isinstance(font_or_path, dict):
path = font_or_path['path']
with lopen(path, 'rb') as f:
with open(path, 'rb') as f:
return f.read()
def find_font_for_text(self, text, allowed_families={'serif', 'sans-serif'},
@@ -366,7 +366,7 @@ class FontScanner(Thread):
self.write_cache()
def read_font_metadata(self, path, fileid):
with lopen(path, 'rb') as f:
with open(path, 'rb') as f:
try:
fm = FontMetadata(f)
except UnsupportedFont:

View File

@@ -441,7 +441,7 @@ def get_font_for_text(text, candidate_font_data=None):
from ebook_converter.utils.fonts.scanner import font_scanner
family, faces = font_scanner.find_font_for_text(text)
if faces:
with lopen(faces[0]['path'], 'rb') as f:
with open(faces[0]['path'], 'rb') as f:
candidate_font_data = f.read()
return candidate_font_data

View File

@@ -49,11 +49,11 @@ def load_jxr_data(data):
with TemporaryDirectory() as tdir:
if iswindows and isinstance(tdir, str):
tdir = tdir.encode('mbcs')
with lopen(os.path.join(tdir, 'input.jxr'), 'wb') as f:
with open(os.path.join(tdir, 'input.jxr'), 'wb') as f:
f.write(data)
cmd = [get_exe_path('JxrDecApp'), '-i', 'input.jxr', '-o', 'output.tif']
creationflags = 0x08 if iswindows else 0
subprocess.Popen(cmd, cwd=tdir, stdout=lopen(os.devnull, 'wb'), stderr=subprocess.STDOUT, creationflags=creationflags).wait()
subprocess.Popen(cmd, cwd=tdir, stdout=open(os.devnull, 'wb'), stderr=subprocess.STDOUT, creationflags=creationflags).wait()
i = QImage()
if not i.load(os.path.join(tdir, 'output.tif')):
raise NotImage('Failed to convert JPEG-XR image')
@@ -124,7 +124,7 @@ def image_from_data(data):
def image_from_path(path):
' Load an image from the specified path. '
with lopen(path, 'rb') as f:
with open(path, 'rb') as f:
return image_from_data(f.read())
@@ -200,7 +200,7 @@ def save_image(img, path, **kw):
`image_to_data()` function. '''
fmt = path.rpartition('.')[-1]
kw['fmt'] = kw.get('fmt', fmt)
with lopen(path, 'wb') as f:
with open(path, 'wb') as f:
f.write(image_to_data(image_from_data(img), **kw))
@@ -286,7 +286,7 @@ def save_cover_data_to(
changed = True
if path is None:
return image_to_data(img, compression_quality, fmt, compression_quality // 10) if changed else data
with lopen(path, 'wb') as f:
with open(path, 'wb') as f:
f.write(image_to_data(img, compression_quality, fmt, compression_quality // 10) if changed else data)
# }}}
@@ -667,7 +667,7 @@ def test(): # {{{
if __name__ == '__main__': # {{{
args = sys.argv[1:]
infile = args.pop(0)
img = image_from_data(lopen(infile, 'rb').read())
img = image_from_data(open(infile, 'rb').read())
func = globals()[args[0]]
kw = {}
args.pop(0)
@@ -692,6 +692,6 @@ if __name__ == '__main__': # {{{
bn = os.path.basename(infile)
outf = bn.rpartition('.')[0] + '.' + '-output' + bn.rpartition('.')[-1]
img = func(img, **kw)
with lopen(outf, 'wb') as f:
with open(outf, 'wb') as f:
f.write(image_to_data(img, fmt=outf.rpartition('.')[-1]))
# }}}

View File

@@ -15,7 +15,7 @@ def what(file, h=None):
' Recognize image headers '
if h is None:
if isinstance(file, (str, bytes)):
with lopen(file, 'rb') as f:
with open(file, 'rb') as f:
h = f.read(HSIZE)
else:
location = file.tell()
@@ -41,7 +41,7 @@ def identify(src):
width = height = -1
if isinstance(src, str):
stream = lopen(src, 'rb')
stream = open(src, 'rb')
elif isinstance(src, bytes):
stream = ReadOnlyFileBuffer(src)
else:

View File

@@ -7,7 +7,7 @@ import json
from gettext import GNUTranslations, NullTranslations
import pkg_resources
from ebook_converter.polyglot.builtins import is_py3, iteritems
from ebook_converter.polyglot.builtins import iteritems
_available_translations = None

View File

@@ -190,7 +190,7 @@ else:
def create_single_instance_mutex(name, per_user=True):
from ebook_converter.utils.ipc import eintr_retry_call
path = singleinstance_path(name, per_user)
f = lopen(path, 'w')
f = open(path, 'w')
try:
eintr_retry_call(fcntl.lockf, f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
return partial(_clean_lock_file, f)