From 422537926da514e49efb58eeddcd539bd91d4d30 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 28 May 2019 09:13:21 +0530 Subject: [PATCH] py3: Calls to UUID(hexkey) require strings Also various misc cleanups I came across while reviewing the last py3 merge PR --- .../ebooks/conversion/plugins/chm_input.py | 5 ++-- .../ebooks/conversion/plugins/epub_input.py | 16 ++++++------- .../ebooks/conversion/plugins/fb2_output.py | 2 +- .../ebooks/conversion/plugins/mobi_input.py | 6 ++--- .../ebooks/conversion/plugins/oeb_output.py | 4 ++-- .../ebooks/conversion/plugins/pdb_output.py | 4 ++-- .../ebooks/conversion/plugins/pdf_input.py | 6 ++--- .../ebooks/conversion/plugins/pdf_output.py | 4 ++-- .../ebooks/conversion/plugins/pml_input.py | 8 +++---- .../ebooks/conversion/plugins/pml_output.py | 4 ++-- .../ebooks/conversion/plugins/rb_output.py | 4 ++-- .../ebooks/conversion/plugins/recipe_input.py | 4 ++-- .../ebooks/conversion/plugins/rtf_output.py | 2 +- .../ebooks/conversion/plugins/tcr_output.py | 4 ++-- src/calibre/ebooks/conversion/plumber.py | 23 ++++++++++--------- src/calibre/ebooks/oeb/polish/container.py | 4 ++-- 16 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/chm_input.py b/src/calibre/ebooks/conversion/plugins/chm_input.py index b87c347ca0..b28e31e74e 100644 --- a/src/calibre/ebooks/conversion/plugins/chm_input.py +++ b/src/calibre/ebooks/conversion/plugins/chm_input.py @@ -174,9 +174,8 @@ class CHMInput(InputFormatPlugin): return htmlpath, toc def _read_file(self, name): - f = open(name, 'rb') - data = f.read() - f.close() + with lopen(name, 'rb') as f: + data = f.read() return data def add_node(self, node, toc, ancestor_map): diff --git a/src/calibre/ebooks/conversion/plugins/epub_input.py b/src/calibre/ebooks/conversion/plugins/epub_input.py index 983d7b0d10..48723dc5b5 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_input.py +++ b/src/calibre/ebooks/conversion/plugins/epub_input.py @@ -8,7 +8,7 @@ import os, re, posixpath from itertools import cycle from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation -from polyglot.builtins import as_bytes, getcwd +from polyglot.builtins import getcwd ADOBE_OBFUSCATION = 'http://ns.adobe.com/pdf/enc#RC' IDPF_OBFUSCATION = 'http://www.idpf.org/2008/embedding' @@ -24,7 +24,7 @@ def decrypt_font_data(key, data, algorithm): def decrypt_font(key, path, algorithm): - with open(path, 'r+b') as f: + with lopen(path, 'r+b') as f: data = decrypt_font_data(key, f.read(), algorithm) f.seek(0), f.truncate(), f.write(data) @@ -57,7 +57,7 @@ class EPUBInput(InputFormatPlugin): (item.text and item.text.startswith('urn:uuid:')): try: key = item.text.rpartition(':')[-1] - key = uuid.UUID(as_bytes(key)).bytes + key = uuid.UUID(key).bytes except: import traceback traceback.print_exc() @@ -223,8 +223,8 @@ class EPUBInput(InputFormatPlugin): if os.path.exists(guide_cover): renderer = render_html_svg_workaround(guide_cover, log) if renderer is not None: - open('calibre_raster_cover.jpg', 'wb').write( - renderer) + with lopen('calibre_raster_cover.jpg', 'wb') as f: + f.write(renderer) # Set the titlepage guide entry self.set_guide_type(opf, 'titlepage', guide_cover, 'Title Page') @@ -238,7 +238,7 @@ class EPUBInput(InputFormatPlugin): if k.endswith(attr): return v try: - with open('META-INF/container.xml', 'rb') as f: + with lopen('META-INF/container.xml', 'rb') as f: root = etree.fromstring(f.read()) for r in root.xpath('//*[local-name()="rootfile"]'): if attr(r, 'media-type') != "application/oebps-package+xml": @@ -348,7 +348,7 @@ class EPUBInput(InputFormatPlugin): with lopen('content.opf', 'wb') as nopf: nopf.write(opf.render()) - return os.path.abspath(u'content.opf') + return os.path.abspath('content.opf') def convert_epub3_nav(self, nav_path, opf, log, opts): from lxml import etree @@ -421,7 +421,7 @@ class EPUBInput(InputFormatPlugin): changed = True elem.set('data-calibre-removed-titlepage', '1') if changed: - with open(nav_path, 'wb') as f: + with lopen(nav_path, 'wb') as f: f.write(serialize(root, 'application/xhtml+xml')) def postprocess_book(self, oeb, opts, log): diff --git a/src/calibre/ebooks/conversion/plugins/fb2_output.py b/src/calibre/ebooks/conversion/plugins/fb2_output.py index 0313086bb4..ce99457d0c 100644 --- a/src/calibre/ebooks/conversion/plugins/fb2_output.py +++ b/src/calibre/ebooks/conversion/plugins/fb2_output.py @@ -191,7 +191,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 = open(output_path, 'wb') + out_stream = lopen(output_path, 'wb') else: out_stream = output_path diff --git a/src/calibre/ebooks/conversion/plugins/mobi_input.py b/src/calibre/ebooks/conversion/plugins/mobi_input.py index 90a2dedd89..34e0f466ff 100644 --- a/src/calibre/ebooks/conversion/plugins/mobi_input.py +++ b/src/calibre/ebooks/conversion/plugins/mobi_input.py @@ -53,14 +53,14 @@ class MOBIInput(InputFormatPlugin): if raw: if isinstance(raw, unicode_type): raw = raw.encode('utf-8') - with open('debug-raw.html', 'wb') as f: + with lopen('debug-raw.html', 'wb') as f: f.write(raw) from calibre.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 open(f, 'wb') as q: + with lopen(f, 'wb') as q: q.write(raw) - accelerators['pagebreaks'] = '//h:div[@class="mbp_pagebreak"]' + accelerators['pagebreaks'] = '//h:div[@class="mbp_pagebreak"]' return mr.created_opf_path diff --git a/src/calibre/ebooks/conversion/plugins/oeb_output.py b/src/calibre/ebooks/conversion/plugins/oeb_output.py index 417701433d..df1aa4ef08 100644 --- a/src/calibre/ebooks/conversion/plugins/oeb_output.py +++ b/src/calibre/ebooks/conversion/plugins/oeb_output.py @@ -53,7 +53,7 @@ class OEBOutput(OutputFormatPlugin): # Needed as I can't get lxml to output opf:role and # not output as well raw = re.sub(br'(<[/]{0,1})opf:', br'\1', raw) - with open(href, 'wb') as f: + with lopen(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 open(path, 'wb') as f: + with lopen(path, 'wb') as f: f.write(item.bytes_representation) item.unload_data_from_memory(memory=path) diff --git a/src/calibre/ebooks/conversion/plugins/pdb_output.py b/src/calibre/ebooks/conversion/plugins/pdb_output.py index de450c175d..ec03e9706c 100644 --- a/src/calibre/ebooks/conversion/plugins/pdb_output.py +++ b/src/calibre/ebooks/conversion/plugins/pdb_output.py @@ -39,9 +39,9 @@ class PDBOutput(OutputFormatPlugin): close = False if not hasattr(output_path, 'write'): 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)) - out_stream = open(output_path, 'wb') + out_stream = lopen(output_path, 'wb') else: out_stream = output_path diff --git a/src/calibre/ebooks/conversion/plugins/pdf_input.py b/src/calibre/ebooks/conversion/plugins/pdf_input.py index 242d64b980..f5eab986e6 100644 --- a/src/calibre/ebooks/conversion/plugins/pdf_input.py +++ b/src/calibre/ebooks/conversion/plugins/pdf_input.py @@ -36,7 +36,7 @@ class PDFInput(InputFormatPlugin): from calibre.ebooks.pdf.reflow import PDFDocument pdftohtml(getcwd(), stream.name, self.opts.no_images, as_xml=True) - with open(u'index.xml', 'rb') as f: + with lopen('index.xml', 'rb') as f: xml = clean_ascii_chars(f.read()) PDFDocument(xml, self.opts, self.log) return os.path.join(getcwd(), 'metadata.opf') @@ -69,12 +69,12 @@ class PDFInput(InputFormatPlugin): opf.create_spine(['index.html']) log.debug('Rendering manifest...') - with open('metadata.opf', 'wb') as opffile: + with lopen('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 open('metadata.opf', 'r+b') as f: + with lopen('metadata.opf', 'r+b') as f: raw = f.read().replace(b'