py3: more work towards universal __future__s

This commit is contained in:
Eli Schwartz 2019-05-27 14:17:06 -04:00
parent ac6bd5e0fd
commit b9a99c271b
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
27 changed files with 74 additions and 52 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2011, John Schember <john@nachtimwald.com>'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
''' CHM File decoding support '''
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>,' \
@ -64,7 +66,7 @@ class CHMInput(InputFormatPlugin):
metadata = Metadata(os.path.basename(chm_name))
encoding = self._chm_reader.get_encoding() or options.input_encoding or 'cp1252'
self._chm_reader.CloseCHM()
# print tdir, mainpath
# print((tdir, mainpath))
# from calibre import ipython
# ipython()
@ -117,10 +119,10 @@ class CHMInput(InputFormatPlugin):
strip_encoding_pats=True, resolve_entities=True)[0]
hhcroot = html.fromstring(hhcdata)
toc = self._process_nodes(hhcroot)
# print "============================="
# print "Printing hhcroot"
# print etree.tostring(hhcroot, pretty_print=True)
# print "============================="
# print("=============================")
# print("Printing hhcroot")
# print(etree.tostring(hhcroot, pretty_print=True))
# print("=============================")
log.debug('Found %d section nodes' % toc.count())
htmlpath = os.path.splitext(hhcpath)[0] + ".html"
base = os.path.dirname(os.path.abspath(htmlpath))
@ -183,7 +185,7 @@ class CHMInput(InputFormatPlugin):
p = node.xpath('ancestor::ul[1]/ancestor::li[1]/object[1]')
parent = p[0] if p else None
toc = ancestor_map.get(parent, toc)
title = href = u''
title = href = ''
for param in node.xpath('./param'):
if match_string(param.attrib['name'], 'name'):
title = param.attrib['value']

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
@ -44,7 +45,7 @@ class EPUBInput(InputFormatPlugin):
import uuid, hashlib
idpf_key = opf.raw_unique_identifier
if idpf_key:
idpf_key = re.sub(u'[\u0020\u0009\u000d\u000a]', u'', idpf_key)
idpf_key = re.sub('[\u0020\u0009\u000d\u000a]', '', idpf_key)
idpf_key = hashlib.sha1(idpf_key.encode('utf-8')).digest()
key = None
for item in opf.identifier_iter():
@ -269,7 +270,7 @@ class EPUBInput(InputFormatPlugin):
encfile = os.path.abspath(os.path.join('META-INF', 'encryption.xml'))
opf = self.find_opf()
if opf is None:
for f in walk(u'.'):
for f in walk('.'):
if f.lower().endswith('.opf') and '__MACOSX' not in f and \
not os.path.basename(f).startswith('.'):
opf = os.path.abspath(f)

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -85,4 +85,4 @@ class LRFInput(InputFormatPlugin):
with open('content.opf', 'wb') as f:
f.write(result)
styles.write()
return os.path.abspath(u'content.opf')
return os.path.abspath('content.opf')

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -188,7 +188,7 @@ class LRFOutput(OutputFormatPlugin):
self.flatten_toc()
from calibre.ptempfile import TemporaryDirectory
with TemporaryDirectory(u'_lrf_output') as tdir:
with TemporaryDirectory('_lrf_output') as tdir:
from calibre.customize.ui import plugin_for_output_format
oeb_output = plugin_for_output_format('oeb')
oeb_output.convert(oeb, tdir, input_plugin, opts, log)

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
@ -29,13 +30,13 @@ class MOBIInput(InputFormatPlugin):
mr = MobiReader(stream, log, options.input_encoding,
options.debug_pipeline)
if mr.kf8_type is None:
mr.extract_content(u'.', parse_cache)
mr.extract_content('.', parse_cache)
except:
mr = MobiReader(stream, log, options.input_encoding,
options.debug_pipeline, try_extra_data_fix=True)
if mr.kf8_type is None:
mr.extract_content(u'.', parse_cache)
mr.extract_content('.', parse_cache)
if mr.kf8_type is not None:
log('Found KF8 MOBI of type %r'%mr.kf8_type)
@ -52,7 +53,7 @@ class MOBIInput(InputFormatPlugin):
if raw:
if isinstance(raw, unicode_type):
raw = raw.encode('utf-8')
with open(u'debug-raw.html', 'wb') as f:
with open('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():

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
@ -38,7 +39,7 @@ class PDFInput(InputFormatPlugin):
with open(u'index.xml', 'rb') as f:
xml = clean_ascii_chars(f.read())
PDFDocument(xml, self.opts, self.log)
return os.path.join(getcwd(), u'metadata.opf')
return os.path.join(getcwd(), 'metadata.opf')
def convert(self, stream, options, file_ext, log,
accelerators):
@ -57,7 +58,7 @@ class PDFInput(InputFormatPlugin):
mi = get_metadata(stream, 'pdf')
opf = OPFCreator(getcwd(), mi)
manifest = [(u'index.html', None)]
manifest = [('index.html', None)]
images = os.listdir(getcwd())
images.remove('index.html')
@ -66,16 +67,16 @@ class PDFInput(InputFormatPlugin):
log.debug('Generating manifest...')
opf.create_manifest(manifest)
opf.create_spine([u'index.html'])
opf.create_spine(['index.html'])
log.debug('Rendering manifest...')
with open(u'metadata.opf', 'wb') as opffile:
with open('metadata.opf', 'wb') as opffile:
opf.render(opffile)
if os.path.exists(u'toc.ncx'):
if os.path.exists('toc.ncx'):
ncxid = opf.manifest.id_for_path('toc.ncx')
if ncxid:
with open(u'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)
return os.path.join(getcwd(), u'metadata.opf')
return os.path.join(getcwd(), 'metadata.opf')

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
@ -30,7 +31,7 @@ class PDFMetadata(object): # {{{
from calibre.ebooks.metadata import authors_to_string
self.title = _(u'Unknown')
self.author = _(u'Unknown')
self.tags = u''
self.tags = ''
self.mi = mi
if mi is not None:
@ -39,7 +40,7 @@ class PDFMetadata(object): # {{{
if mi.authors:
self.author = authors_to_string(mi.authors)
if mi.tags:
self.tags = u', '.join(mi.tags)
self.tags = ', '.join(mi.tags)
self.title = force_unicode(self.title)
self.author = force_unicode(self.author)
@ -242,16 +243,16 @@ class PDFOutput(OutputFormatPlugin):
elif iswindows and rule.type == rule.STYLE_RULE:
from tinycss.fonts3 import parse_font_family, serialize_font_family
s = rule.style
f = s.getProperty(u'font-family')
f = s.getProperty('font-family')
if f is not None:
font_families = parse_font_family(css_text(f.propertyValue))
ff = [x for x in font_families if x.lower() != u'courier']
ff = [x for x in font_families if x.lower() != 'courier']
if len(ff) != len(font_families):
if 'courier' not in self.filtered_font_warnings:
# See https://bugs.launchpad.net/bugs/1665835
self.filtered_font_warnings.add(u'courier')
self.log.warn(u'Removing courier font family as it does not render on windows')
f.propertyValue.cssText = serialize_font_family(ff or [u'monospace'])
self.filtered_font_warnings.add('courier')
self.log.warn('Removing courier font family as it does not render on windows')
f.propertyValue.cssText = serialize_font_family(ff or ['monospace'])
def convert_text(self, oeb_book):
from calibre.ebooks.metadata.opf2 import OPF

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
@ -73,9 +74,9 @@ class PMLInput(InputFormatPlugin):
imgs = glob.glob(os.path.join(tdir, os.path.splitext(os.path.basename(stream.name))[0] + '_img', '*.png'))
# No images in Dropbook location try generic images directory
if not imgs:
imgs = glob.glob(os.path.join(os.path.join(tdir, u'images'), u'*.png'))
imgs = glob.glob(os.path.join(os.path.join(tdir, 'images'), '*.png'))
if imgs:
os.makedirs(os.path.join(getcwd(), u'images'))
os.makedirs(os.path.join(getcwd(), 'images'))
for img in imgs:
pimg_name = os.path.basename(img)
pimg_path = os.path.join(getcwd(), 'images', pimg_name)
@ -99,11 +100,11 @@ class PMLInput(InputFormatPlugin):
if file_ext == 'pmlz':
log.debug('De-compressing content to temporary directory...')
with TemporaryDirectory(u'_unpmlz') as tdir:
with TemporaryDirectory('_unpmlz') as tdir:
zf = ZipFile(stream)
zf.extractall(tdir)
pmls = glob.glob(os.path.join(tdir, u'*.pml'))
pmls = glob.glob(os.path.join(tdir, '*.pml'))
for pml in pmls:
html_name = os.path.splitext(os.path.basename(pml))[0]+'.html'
html_path = os.path.join(getcwd(), html_name)
@ -114,8 +115,8 @@ class PMLInput(InputFormatPlugin):
toc += ttoc
images = self.get_images(stream, tdir, True)
else:
toc = self.process_pml(stream, u'index.html')
pages.append(u'index.html')
toc = self.process_pml(stream, 'index.html')
pages.append('index.html')
if hasattr(stream, 'name'):
images = self.get_images(stream, os.path.abspath(os.path.dirname(stream.name)))
@ -131,14 +132,14 @@ class PMLInput(InputFormatPlugin):
log.debug('Reading metadata from input file...')
mi = get_metadata(stream, 'pml')
if 'images/cover.png' in images:
mi.cover = u'images/cover.png'
mi.cover = 'images/cover.png'
opf = OPFCreator(getcwd(), mi)
log.debug('Generating manifest...')
opf.create_manifest(manifest_items)
opf.create_spine(pages)
opf.set_toc(toc)
with open(u'metadata.opf', 'wb') as opffile:
with open(u'toc.ncx', 'wb') as tocfile:
opf.render(opffile, tocfile, u'toc.ncx')
with open('metadata.opf', 'wb') as opffile:
with open('toc.ncx', 'wb') as tocfile:
opf.render(opffile, tocfile, 'toc.ncx')
return os.path.join(getcwd(), u'metadata.opf')
return os.path.join(getcwd(), 'metadata.opf')

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -142,11 +142,11 @@ class RecipeInput(InputFormatPlugin):
for key, val in self.recipe_object.conversion_options.items():
setattr(opts, key, val)
for f in os.listdir(u'.'):
for f in os.listdir('.'):
if f.endswith('.opf'):
return os.path.abspath(f)
for f in walk(u'.'):
for f in walk('.'):
if f.endswith('.opf'):
return os.path.abspath(f)

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

View File

@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
@ -848,7 +850,7 @@ OptionRecommendation(name='search_replace',
rec = self.get_option_by_name(name)
help = getattr(rec, 'help', None)
if help is not None:
return help.replace('%default', str(rec.recommended_value))
return help.replace('%default', unicode_type(rec.recommended_value))
def get_all_help(self):
ans = {}

View File

@ -164,7 +164,7 @@ class DocAnalysis(object):
for line in self.lines:
l = len(line)
if l > minLineLength and l < maxLineLength:
l = int(l/100)
l = int(l // 100)
# print("adding "+unicode_type(l))
hRaw[l]+=1