1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-23 14:41:30 +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
@@ -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
@@ -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):
@@ -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)
@@ -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
@@ -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:
@@ -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
@@ -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)
@@ -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
@@ -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)
@@ -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')
@@ -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)
@@ -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
@@ -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')
@@ -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
@@ -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
@@ -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]))
@@ -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