1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-19 12:43:35 +02: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 return htmlpath, toc
def _read_file(self, name): def _read_file(self, name):
with lopen(name, 'rb') as f: with open(name, 'rb') as f:
data = f.read() data = f.read()
return data return data

View File

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

View File

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

View File

@@ -189,7 +189,7 @@ class FB2Output(OutputFormatPlugin):
close = True close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '': if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
os.makedirs(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: else:
out_stream = output_path out_stream = output_path

View File

@@ -113,7 +113,7 @@ class HTMLZOutput(OutputFormatPlugin):
if cover_data: if cover_data:
from ebook_converter.utils.img import save_cover_data_to from ebook_converter.utils.img import save_cover_data_to
cover_path = os.path.join(tdir, u'cover.jpg') 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('') cf.write('')
save_cover_data_to(cover_data, cover_path) save_cover_data_to(cover_data, cover_path)
except: except:

View File

@@ -51,14 +51,14 @@ class MOBIInput(InputFormatPlugin):
if raw: if raw:
if isinstance(raw, str): if isinstance(raw, str):
raw = raw.encode('utf-8') raw = raw.encode('utf-8')
with lopen('debug-raw.html', 'wb') as f: with open('debug-raw.html', 'wb') as f:
f.write(raw) f.write(raw)
from ebook_converter.ebooks.oeb.base import close_self_closing_tags from ebook_converter.ebooks.oeb.base import close_self_closing_tags
for f, root in parse_cache.items(): for f, root in parse_cache.items():
raw = html.tostring(root, encoding='utf-8', method='xml', raw = html.tostring(root, encoding='utf-8', method='xml',
include_meta_content_type=False) include_meta_content_type=False)
raw = close_self_closing_tags(raw) raw = close_self_closing_tags(raw)
with lopen(f, 'wb') as q: with open(f, 'wb') as q:
q.write(raw) q.write(raw)
accelerators['pagebreaks'] = '//h:div[@class="mbp_pagebreak"]' accelerators['pagebreaks'] = '//h:div[@class="mbp_pagebreak"]'
return mr.created_opf_path 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 # Needed as I can't get lxml to output opf:role and
# not output <opf:metadata> as well # not output <opf:metadata> as well
raw = re.sub(br'(<[/]{0,1})opf:', br'\1', raw) 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) f.write(raw)
for item in oeb_book.manifest: for item in oeb_book.manifest:
@@ -65,7 +65,7 @@ class OEBOutput(OutputFormatPlugin):
dir = os.path.dirname(path) dir = os.path.dirname(path)
if not os.path.exists(dir): if not os.path.exists(dir):
os.makedirs(dir) os.makedirs(dir)
with lopen(path, 'wb') as f: with open(path, 'wb') as f:
f.write(item.bytes_representation) f.write(item.bytes_representation)
item.unload_data_from_memory(memory=path) item.unload_data_from_memory(memory=path)

View File

@@ -39,7 +39,7 @@ class PDBOutput(OutputFormatPlugin):
close = True close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path): if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path):
os.makedirs(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: else:
out_stream = output_path out_stream = output_path

View File

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

View File

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

View File

@@ -40,7 +40,7 @@ class PMLOutput(OutputFormatPlugin):
with TemporaryDirectory('_pmlz_output') as tdir: with TemporaryDirectory('_pmlz_output') as tdir:
pmlmlizer = PMLMLizer(log) pmlmlizer = PMLMLizer(log)
pml = str(pmlmlizer.extract_content(oeb_book, opts)) 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')) out.write(pml.encode(opts.pml_output_encoding, 'replace'))
img_path = os.path.join(tdir, 'index_img') 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]) 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) out.write(data)

View File

@@ -28,7 +28,7 @@ class RBOutput(OutputFormatPlugin):
close = True close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path): if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path):
os.makedirs(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: else:
out_stream = output_path out_stream = output_path

View File

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

View File

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

View File

@@ -26,7 +26,7 @@ class RTFOutput(OutputFormatPlugin):
close = True close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '': if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
os.makedirs(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: else:
out_stream = output_path out_stream = output_path

View File

@@ -242,7 +242,7 @@ class SNBOutput(OutputFormatPlugin):
# img = img.rotate(90) # img = img.rotate(90)
# x,y = y,x # x,y = y,x
img = resize_image(img, x // scale, y // scale) 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])) f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))

View File

@@ -31,7 +31,7 @@ class TCROutput(OutputFormatPlugin):
close = True close = True
if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path): if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path):
os.makedirs(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: else:
out_stream = output_path out_stream = output_path

View File

@@ -945,7 +945,7 @@ OptionRecommendation(name='search_replace',
if self.opts.read_metadata_from_opf is not None: 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 = os.path.abspath(
self.opts.read_metadata_from_opf) 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)) opf = OPF(stream, os.path.dirname(self.opts.read_metadata_from_opf))
mi = opf.to_book_metadata() mi = opf.to_book_metadata()
self.opts_to_mi(mi) self.opts_to_mi(mi)
@@ -955,7 +955,7 @@ OptionRecommendation(name='search_replace',
ext = mi.cover.rpartition('.')[-1].lower().strip() ext = mi.cover.rpartition('.')[-1].lower().strip()
if ext not in ('png', 'jpg', 'jpeg', 'gif'): if ext not in ('png', 'jpg', 'jpeg', 'gif'):
ext = 'jpg' ext = 'jpg'
with lopen(mi.cover, 'rb') as stream: with open(mi.cover, 'rb') as stream:
mi.cover_data = (ext, stream.read()) mi.cover_data = (ext, stream.read())
mi.cover = None mi.cover = None
self.user_metadata = mi self.user_metadata = mi
@@ -1061,7 +1061,7 @@ OptionRecommendation(name='search_replace',
self.opts.debug_pipeline = os.path.abspath(self.opts.debug_pipeline) self.opts.debug_pipeline = os.path.abspath(self.opts.debug_pipeline)
if not os.path.exists(self.opts.debug_pipeline): if not os.path.exists(self.opts.debug_pipeline):
os.makedirs(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) f.write(DEBUG_README)
for x in ('input', 'parsed', 'structure', 'processed'): for x in ('input', 'parsed', 'structure', 'processed'):
x = os.path.join(self.opts.debug_pipeline, x) x = os.path.join(self.opts.debug_pipeline, x)
@@ -1079,7 +1079,7 @@ OptionRecommendation(name='search_replace',
tdir = PersistentTemporaryDirectory('_plumber') tdir = PersistentTemporaryDirectory('_plumber')
stream = self.input if self.input_fmt == 'recipe' else \ stream = self.input if self.input_fmt == 'recipe' else \
lopen(self.input, 'rb') open(self.input, 'rb')
if self.input_fmt == 'recipe': if self.input_fmt == 'recipe':
self.opts.original_recipe_input_arg = self.original_input_arg 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: if os.path.exists(path) and before_count(root, img, limit=10) < 5:
from ebook_converter.utils.imghdr import identify from ebook_converter.utils.imghdr import identify
try: try:
with lopen(path, 'rb') as imf: with open(path, 'rb') as imf:
fmt, width, height = identify(imf) fmt, width, height = identify(imf)
except: except:
width, height, fmt = 0, 0, None # noqa width, height, fmt = 0, 0, None # noqa

View File

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

View File

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

View File

@@ -230,7 +230,7 @@ class TOC(list):
def read_html_toc(self, toc): def read_html_toc(self, toc):
self.base_path = os.path.dirname(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()) parsed_toc = parse_html_toc(f.read())
for href, fragment, txt in parsed_toc: for href, fragment, txt in parsed_toc:
add = True add = True

View File

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

View File

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

View File

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

View File

@@ -367,7 +367,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)
with lopen(path, 'wb') as f: with open(path, 'wb') as f:
if hasattr(data, 'read'): if hasattr(data, 'read'):
shutil.copyfileobj(data, f) shutil.copyfileobj(data, f)
else: else:
@@ -579,7 +579,7 @@ class Container(ContainerBase): # {{{
return set() return set()
def parse(self, path, mime): def parse(self, path, mime):
with lopen(path, 'rb') as src: with open(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))
@@ -959,7 +959,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)
lopen(path, 'wb').close() open(path, 'wb').close()
return item return item
def format_opf(self): def format_opf(self):
@@ -1014,7 +1014,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 lopen(dest, 'wb') as f: with open(dest, 'wb') as f:
f.write(data) f.write(data)
def filesize(self, name): 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 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 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): def commit(self, outpath=None, keep_parsed=False):
''' '''
@@ -1073,7 +1073,7 @@ class Container(ContainerBase): # {{{
mismatches = [] mismatches = []
for name, path in iteritems(self.name_path_map): for name, path in iteritems(self.name_path_map):
opath = other.name_path_map[name] 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(): 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)
@@ -1146,7 +1146,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 lopen(self.pathtoepub, 'rb') as stream: with open(self.pathtoepub, 'rb') as stream:
try: try:
zf = ZipFile(stream) zf = ZipFile(stream)
zf.extractall(tdir) zf.extractall(tdir)
@@ -1363,12 +1363,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 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) shutil.copyfileobj(src, dest)
else: else:
from ebook_converter.ebooks.tweak import zip_rebuilder 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') et = guess_type('a.epub')
if not isinstance(et, bytes): if not isinstance(et, bytes):
et = et.encode('ascii') et = et.encode('ascii')
@@ -1398,7 +1398,7 @@ class InvalidMobi(InvalidBook):
def do_explode(path, dest): def do_explode(path, dest):
from ebook_converter.ebooks.mobi.reader.mobi6 import MobiReader from ebook_converter.ebooks.mobi.reader.mobi6 import MobiReader
from ebook_converter.ebooks.mobi.reader.mobi8 import Mobi8Reader 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) mr = MobiReader(stream, default_log, None, None)
with CurrentDir(dest): with CurrentDir(dest):
@@ -1456,7 +1456,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 lopen(pathtoazw3, 'rb') as stream: with open(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.'))

View File

@@ -412,7 +412,7 @@ class Reader(FormatReader):
for col in row: for col in row:
if col not in images: if col not in images:
raise Exception('Image with uid: %s missing.' % col) 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 row_width += w
if col_height < h: if col_height < h:
col_height = h col_height = h
@@ -427,14 +427,14 @@ class Reader(FormatReader):
x_off = 0 x_off = 0
largest_height = 0 largest_height = 0
for col in row: 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) canvas.compose(im, x_off, y_off)
w, h = im.width(), im.height() w, h = im.width(), im.height()
x_off += w x_off += w
if largest_height < h: if largest_height < h:
largest_height = h largest_height = h
y_off += largest_height 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)) out.write(canvas.export(compression_quality=70))
self.log.debug('Wrote composite image with uid %s to images/%s.jpg' % (uid, uid)) self.log.debug('Wrote composite image with uid %s to images/%s.jpg' % (uid, uid))
except Exception as e: 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') pdfsrc = os.path.join(output_dir, 'src.pdf')
index = os.path.join(output_dir, 'index.'+('xml' if as_xml else 'html')) 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) shutil.copyfileobj(src, dest)
with CurrentDir(output_dir): 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) ret = eintr_retry_call(p.wait)
logf.flush() logf.flush()
logf.close() 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: if ret != 0:
raise ConversionError('pdftohtml failed with return code: %d\n%s' % (ret, out)) raise ConversionError('pdftohtml failed with return code: %d\n%s' % (ret, out))
if out: if out:
@@ -89,7 +89,7 @@ def pdftohtml(output_dir, pdf_path, no_images, as_xml=False):
raise DRMError() raise DRMError()
if not as_xml: 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 = i.read().decode('utf-8', 'replace')
raw = flip_images(raw) raw = flip_images(raw)
raw = raw.replace('<head', '<!-- created by ebook-converter\'s pdftohtml -->\n <head', 1) 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): def flip_image(img, flip):
from ebook_converter.utils.img import flip_image, image_and_format_from_data, image_to_data 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, fmt = image_and_format_from_data(f.read())
img = flip_image(img, horizontal='x' in flip, vertical='y' in flip) img = flip_image(img, horizontal='x' in flip, vertical='y' in flip)
f.seek(0), f.truncate() 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 = OPFCreator(path, mi)
opf.create_manifest(manifest) opf.create_manifest(manifest)
opf.create_spine(spine) 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) opf.render(opffile)

View File

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

View File

@@ -9,10 +9,10 @@ Perform various initialization tasks.
import builtins import builtins
import locale import locale
import sys import sys
import os
from ebook_converter import constants
# Default translation is NOOP # Default translation is NOOP
from ebook_converter.polyglot.builtins import is_py3
builtins.__dict__['_'] = lambda s: s builtins.__dict__['_'] = lambda s: s
# For strings which belong in the translation tables, but which shouldn't be # 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) 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 _run_once = False
winutil = winutilerror = None winutil = winutilerror = None
@@ -32,74 +31,30 @@ if not _run_once:
_run_once = True _run_once = True
from importlib import import_module 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): 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()) sys.meta_path.insert(0, DeVendor())
# #
# Platform specific modules # Platform specific modules
if iswindows: if constants.iswindows:
winutil, winutilerror = plugins['winutil'] winutil, winutilerror = constants.plugins['winutil']
if not winutil: if not winutil:
raise RuntimeError('Failed to load the winutil plugin: %s'%winutilerror) raise RuntimeError('Failed to load the winutil plugin: %s'%winutilerror)
if len(sys.argv) > 1 and not isinstance(sys.argv[1], str): if len(sys.argv) > 1 and not isinstance(sys.argv[1], str):
sys.argv[1:] = winutil.argv()[1-len(sys.argv):] 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 # Ensure that all temp files/dirs are created under a calibre tmp dir
from ebook_converter.ptempfile import base_dir from ebook_converter.ptempfile import base_dir
try: try:
@@ -109,8 +64,8 @@ if not _run_once:
# #
# Convert command line arguments to unicode # Convert command line arguments to unicode
enc = preferred_encoding enc = constants.preferred_encoding
if isosx: if constants.isosx:
enc = 'utf-8' enc = 'utf-8'
for i in range(1, len(sys.argv)): for i in range(1, len(sys.argv)):
if not isinstance(sys.argv[i], str): 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 # 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 # See https://msdn.microsoft.com/en-us/library/6e3b887c.aspx
if hasattr(winutil, 'setmaxstdio'): if hasattr(winutil, 'setmaxstdio'):
winutil.setmaxstdio(max(1024, winutil.getmaxstdio())) winutil.setmaxstdio(max(1024, winutil.getmaxstdio()))
@@ -129,7 +84,7 @@ if not _run_once:
try: try:
resource.setrlimit(resource.RLIMIT_NOFILE, (min(1024, hard), hard)) resource.setrlimit(resource.RLIMIT_NOFILE, (min(1024, hard), hard))
except Exception: except Exception:
if DEBUG: if constants.DEBUG:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
@@ -162,47 +117,6 @@ if not _run_once:
except: except:
pass 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 from ebook_converter.utils.icu import lower as icu_lower, upper as icu_upper
builtins.__dict__['icu_lower'] = icu_lower builtins.__dict__['icu_lower'] = icu_lower
builtins.__dict__['icu_upper'] = icu_upper builtins.__dict__['icu_upper'] = icu_upper
@@ -226,7 +140,7 @@ if not _run_once:
bound_signal.connect(slot, **kw) bound_signal.connect(slot, **kw)
builtins.__dict__['connect_lambda'] = connect_lambda 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 # Name all threads at the OS level created using the threading module, see
# http://bugs.python.org/issue15500 # http://bugs.python.org/issue15500
import threading import threading
@@ -244,7 +158,7 @@ if not _run_once:
if name: if name:
if isinstance(name, str): if isinstance(name, str):
name = name.encode('ascii', 'replace').decode('ascii') 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: except Exception:
pass # Don't care about failure to set name pass # Don't care about failure to set name
threading.Thread.start = new_start threading.Thread.start = new_start
@@ -254,9 +168,9 @@ def test_lopen():
from ebook_converter.ptempfile import TemporaryDirectory from ebook_converter.ptempfile import TemporaryDirectory
from ebook_converter import CurrentDir from ebook_converter import CurrentDir
n = 'f\xe4llen' n = 'f\xe4llen'
print('testing lopen()') print('testing open()')
if iswindows: if constants.iswindows:
import msvcrt, win32api import msvcrt, win32api
def assert_not_inheritable(f): def assert_not_inheritable(f):
@@ -270,7 +184,7 @@ def test_lopen():
raise SystemExit('File handle is inheritable!') raise SystemExit('File handle is inheritable!')
def copen(*args): def copen(*args):
ans = lopen(*args) ans = open(*args)
assert_not_inheritable(ans) assert_not_inheritable(ans)
return ans return ans

View File

@@ -195,7 +195,7 @@ def case_preserving_open_file(path, mode='wb', mkdir_mode=0o777):
ans = fpath = cpath ans = fpath = cpath
else: else:
fname = components[-1] 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 # 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 # 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. # necessary, but given the diversity of platforms, best to be safe.
@@ -434,7 +434,7 @@ class WindowsAtomicFolderMove(object):
pass pass
win32file.SetFilePointer(handle, 0, win32file.FILE_BEGIN) win32file.SetFilePointer(handle, 0, win32file.FILE_BEGIN)
with lopen(dest, 'wb') as f: with open(dest, 'wb') as f:
while True: while True:
hr, raw = win32file.ReadFile(handle, 1024*1024) hr, raw = win32file.ReadFile(handle, 1024*1024)
if hr != 0: if hr != 0:

View File

@@ -245,7 +245,7 @@ class FontScanner(Thread):
path = font_or_path path = font_or_path
if isinstance(font_or_path, dict): if isinstance(font_or_path, dict):
path = font_or_path['path'] path = font_or_path['path']
with lopen(path, 'rb') as f: with open(path, 'rb') as f:
return f.read() return f.read()
def find_font_for_text(self, text, allowed_families={'serif', 'sans-serif'}, def find_font_for_text(self, text, allowed_families={'serif', 'sans-serif'},
@@ -366,7 +366,7 @@ class FontScanner(Thread):
self.write_cache() self.write_cache()
def read_font_metadata(self, path, fileid): def read_font_metadata(self, path, fileid):
with lopen(path, 'rb') as f: with open(path, 'rb') as f:
try: try:
fm = FontMetadata(f) fm = FontMetadata(f)
except UnsupportedFont: 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 from ebook_converter.utils.fonts.scanner import font_scanner
family, faces = font_scanner.find_font_for_text(text) family, faces = font_scanner.find_font_for_text(text)
if faces: if faces:
with lopen(faces[0]['path'], 'rb') as f: with open(faces[0]['path'], 'rb') as f:
candidate_font_data = f.read() candidate_font_data = f.read()
return candidate_font_data return candidate_font_data

View File

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

View File

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

View File

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

View File

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