py3: Various fixes for the last py3 merge

This commit is contained in:
Kovid Goyal 2019-06-01 12:37:04 +05:30
parent d9fcdbe1a2
commit 20b065fb49
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
11 changed files with 31 additions and 28 deletions

View File

@ -12,7 +12,7 @@ from calibre.customize.conversion import (OutputFormatPlugin,
OptionRecommendation) OptionRecommendation)
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre import CurrentDir from calibre import CurrentDir
from polyglot.builtins import unicode_type, filter, map, zip from polyglot.builtins import unicode_type, filter, map, zip, range, as_bytes
block_level_tags = ( block_level_tags = (
'address', 'address',
@ -269,7 +269,7 @@ class EPUBOutput(OutputFormatPlugin):
extra_entries=extra_entries) as epub: extra_entries=extra_entries) as epub:
epub.add_dir(tdir) epub.add_dir(tdir)
if encryption is not None: if encryption is not None:
epub.writestr('META-INF/encryption.xml', encryption) epub.writestr('META-INF/encryption.xml', as_bytes(encryption))
if metadata_xml is not None: if metadata_xml is not None:
epub.writestr('META-INF/metadata.xml', epub.writestr('META-INF/metadata.xml',
metadata_xml.encode('utf-8')) metadata_xml.encode('utf-8'))
@ -331,9 +331,9 @@ class EPUBOutput(OutputFormatPlugin):
with lopen(path, 'r+b') as f: with lopen(path, 'r+b') as f:
data = f.read(1024) data = f.read(1024)
if len(data) >= 1024: if len(data) >= 1024:
data = bytearray(data)
f.seek(0) f.seek(0)
for i in range(1024): f.write(bytes(bytearray(data[i] ^ key[i%16] for i in range(1024))))
f.write(chr(ord(data[i]) ^ key[i%16]))
else: else:
self.log.warn('Font', path, 'is invalid, ignoring') self.log.warn('Font', path, 'is invalid, ignoring')
if not isinstance(uri, unicode_type): if not isinstance(uri, unicode_type):
@ -347,13 +347,13 @@ class EPUBOutput(OutputFormatPlugin):
</enc:EncryptedData> </enc:EncryptedData>
'''%(uri.replace('"', '\\"'))) '''%(uri.replace('"', '\\"')))
if fonts: if fonts:
ans = b'''<encryption ans = '''<encryption
xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"
xmlns:enc="http://www.w3.org/2001/04/xmlenc#" xmlns:enc="http://www.w3.org/2001/04/xmlenc#"
xmlns:deenc="http://ns.adobe.com/digitaleditions/enc"> xmlns:deenc="http://ns.adobe.com/digitaleditions/enc">
''' '''
ans += '\n'.join(fonts).encode('utf-8') ans += '\n'.join(fonts)
ans += b'\n</encryption>' ans += '\n</encryption>'
return ans return ans
# }}} # }}}

View File

@ -15,7 +15,7 @@ from calibre.customize.conversion import (InputFormatPlugin,
from calibre.utils.localization import get_lang from calibre.utils.localization import get_lang
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from calibre.utils.imghdr import what from calibre.utils.imghdr import what
from polyglot.builtins import unicode_type, zip, getcwd from polyglot.builtins import unicode_type, zip, getcwd, as_unicode
def sanitize_file_name(x): def sanitize_file_name(x):
@ -290,7 +290,7 @@ class HTMLInput(InputFormatPlugin):
# file, therefore we quote it here. # file, therefore we quote it here.
if isinstance(bhref, unicode_type): if isinstance(bhref, unicode_type):
bhref = bhref.encode('utf-8') bhref = bhref.encode('utf-8')
item.html_input_href = unicode_type(quote(bhref)) item.html_input_href = as_unicode(quote(bhref))
if guessed in self.OEB_STYLES: if guessed in self.OEB_STYLES:
item.override_css_fetch = partial( item.override_css_fetch = partial(
self.css_import_handler, os.path.dirname(link)) self.css_import_handler, os.path.dirname(link))

View File

@ -27,8 +27,7 @@ class SNBInput(InputFormatPlugin):
file_types = {'snb'} file_types = {'snb'}
commit_name = 'snb_input' commit_name = 'snb_input'
options = set([ options = set()
])
def convert(self, stream, options, file_ext, log, def convert(self, stream, options, file_ext, log,
accelerators): accelerators):

View File

@ -18,7 +18,7 @@ from calibre.utils.date import utcnow
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1 from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
from calibre.utils.zipfile import ZipFile from calibre.utils.zipfile import ZipFile
from calibre.ebooks.pdf.render.common import PAPER_SIZES from calibre.ebooks.pdf.render.common import PAPER_SIZES
from polyglot.builtins import iteritems, map, unicode_type from polyglot.builtins import iteritems, map, unicode_type, native_string_type
def xml2str(root, pretty_print=False, with_tail=False): def xml2str(root, pretty_print=False, with_tail=False):
@ -243,7 +243,7 @@ class DOCX(object):
namespaces = self.namespace.namespaces namespaces = self.namespace.namespaces
E = ElementMaker(namespace=namespaces['cp'], nsmap={x:namespaces[x] for x in 'cp dc dcterms xsi'.split()}) E = ElementMaker(namespace=namespaces['cp'], nsmap={x:namespaces[x] for x in 'cp dc dcterms xsi'.split()})
cp = E.coreProperties(E.revision("1"), E.lastModifiedBy('calibre')) cp = E.coreProperties(E.revision("1"), E.lastModifiedBy('calibre'))
ts = utcnow().isoformat(unicode_type('T')).rpartition('.')[0] + 'Z' ts = utcnow().isoformat(native_string_type('T')).rpartition('.')[0] + 'Z'
for x in 'created modified'.split(): for x in 'created modified'.split():
x = cp.makeelement('{%s}%s' % (namespaces['dcterms'], x), **{'{%s}type' % namespaces['xsi']:'dcterms:W3CDTF'}) x = cp.makeelement('{%s}%s' % (namespaces['dcterms'], x), **{'{%s}type' % namespaces['xsi']:'dcterms:W3CDTF'})
x.text = ts x.text = ts

View File

@ -70,7 +70,7 @@ class LinksManager(object):
self.namespace = namespace self.namespace = namespace
self.log = log self.log = log
self.document_relationships = document_relationships self.document_relationships = document_relationships
self.top_anchor = uuid4().hex self.top_anchor = unicode_type(uuid4().hex)
self.anchor_map = {} self.anchor_map = {}
self.used_bookmark_names = set() self.used_bookmark_names = set()
self.bmark_id = 0 self.bmark_id = 0

View File

@ -41,8 +41,8 @@ def initialize_container(path_to_container, opf_name='metadata.opf',
path, mimetype) path, mimetype)
CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8') CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8')
zf = ZipFile(path_to_container, 'w') zf = ZipFile(path_to_container, 'w')
zf.writestr('mimetype', 'application/epub+zip', compression=ZIP_STORED) zf.writestr('mimetype', b'application/epub+zip', compression=ZIP_STORED)
zf.writestr('META-INF/', '', 0o755) zf.writestr('META-INF/', b'', 0o755)
zf.writestr('META-INF/container.xml', CONTAINER) zf.writestr('META-INF/container.xml', CONTAINER)
for path, _, data in extra_entries: for path, _, data in extra_entries:
zf.writestr(path, data) zf.writestr(path, data)

View File

@ -134,7 +134,7 @@ def sony_metadata(oeb):
toc.nodes.append(section) toc.nodes.append(section)
entries = [] entries = []
seen_titles = set([]) seen_titles = set()
for i, section in enumerate(toc): for i, section in enumerate(toc):
if not section.href: if not section.href:
continue continue

View File

@ -65,7 +65,7 @@ class Link(object):
return self.path == getattr(other, 'path', other) return self.path == getattr(other, 'path', other)
def __str__(self): def __str__(self):
return u'Link: %s --> %s'%(self.url, self.path) return 'Link: %s --> %s'%(self.url, self.path)
if not is_py3: if not is_py3:
__unicode__ = __str__ __unicode__ = __str__
@ -176,8 +176,10 @@ class HTMLFile(object):
return Link(url, self.base) return Link(url, self.base)
def depth_first(root, flat, visited=set([])): def depth_first(root, flat, visited=None):
yield root yield root
if visited is None:
visited = set()
visited.add(root) visited.add(root)
for link in root.links: for link in root.links:
if link.path is not None and link not in visited: if link.path is not None and link not in visited:

View File

@ -21,7 +21,7 @@ from functools import wraps
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.ebooks.metadata import MetaInformation, string_to_authors from calibre.ebooks.metadata import MetaInformation, string_to_authors
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
BYTE = "<B" #: Unsigned char little endian encoded in 1 byte BYTE = "<B" #: Unsigned char little endian encoded in 1 byte
WORD = "<H" #: Unsigned short little endian encoded in 2 bytes WORD = "<H" #: Unsigned short little endian encoded in 2 bytes
@ -97,8 +97,10 @@ class fixed_stringfield(object):
return obj.unpack(start=self._start, fmt="<"+length+"s")[0] return obj.unpack(start=self._start, fmt="<"+length+"s")[0]
def __set__(self, obj, val): def __set__(self, obj, val):
if not isinstance(val, unicode_type): if not isinstance(val, string_or_bytes):
val = unicode_type(val) val = unicode_type(val)
if isinstance(val, unicode_type):
val = val.encode('utf-8')
if len(val) != self._length: if len(val) != self._length:
raise LRFException("Trying to set fixed_stringfield with a " + raise LRFException("Trying to set fixed_stringfield with a " +
"string of incorrect length") "string of incorrect length")
@ -710,7 +712,7 @@ def main(args=sys.argv):
lrf.book_id = options.book_id lrf.book_id = options.book_id
if options.comment: if options.comment:
path = os.path.expanduser(os.path.expandvars(options.comment)) path = os.path.expanduser(os.path.expandvars(options.comment))
lrf.free_text = open(path).read() lrf.free_text = open(path, 'rb').read().decode('utf-8', 'replace')
if options.get_thumbnail: if options.get_thumbnail:
t = lrf.thumbnail t = lrf.thumbnail
td = "None" td = "None"

View File

@ -123,7 +123,7 @@ class LRFContentObject(LRFObject):
if tag.id in self.tag_map: if tag.id in self.tag_map:
action = self.tag_map[tag.id] action = self.tag_map[tag.id]
if isinstance(action, string_or_bytes): if isinstance(action, string_or_bytes):
func, args = action, tuple([]) func, args = action, ()
else: else:
func, args = action[0], (action[1],) func, args = action[0], (action[1],)
getattr(self, func)(tag, *args) getattr(self, func)(tag, *args)
@ -495,7 +495,7 @@ class BlockAttr(StyleObject, LRFObject):
return ans return ans
if hasattr(obj, 'sidemargin'): if hasattr(obj, 'sidemargin'):
margin = str(obj.sidemargin) + 'px' margin = unicode_type(obj.sidemargin) + 'px'
ans += item('margin-left: %(m)s; margin-right: %(m)s;'%dict(m=margin)) ans += item('margin-left: %(m)s; margin-right: %(m)s;'%dict(m=margin))
if hasattr(obj, 'topskip'): if hasattr(obj, 'topskip'):
ans += item('margin-top: %dpx;'%obj.topskip) ans += item('margin-top: %dpx;'%obj.topskip)
@ -1109,7 +1109,7 @@ class Button(LRFObject):
tag_map.update(LRFObject.tag_map) tag_map.update(LRFObject.tag_map)
def __init__(self, document, stream, id, scramble_key, boundary): def __init__(self, document, stream, id, scramble_key, boundary):
self.xml = u'' self.xml = ''
self.refimage = {} self.refimage = {}
self.actions = {} self.actions = {}
self.to_dump = True self.to_dump = True

View File

@ -14,7 +14,7 @@ from calibre import strftime
from calibre.constants import iswindows, isosx, plugins, preferred_encoding from calibre.constants import iswindows, isosx, plugins, preferred_encoding
from calibre.utils.iso8601 import utc_tz, local_tz, UNDEFINED_DATE from calibre.utils.iso8601 import utc_tz, local_tz, UNDEFINED_DATE
from calibre.utils.localization import lcdata from calibre.utils.localization import lcdata
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, native_string_type
_utc_tz = utc_tz _utc_tz = utc_tz
_local_tz = local_tz _local_tz = local_tz
@ -194,8 +194,8 @@ def isoformat(date_time, assume_utc=False, as_utc=True, sep='T'):
date_time = date_time.replace(tzinfo=_utc_tz if assume_utc else date_time = date_time.replace(tzinfo=_utc_tz if assume_utc else
_local_tz) _local_tz)
date_time = date_time.astimezone(_utc_tz if as_utc else _local_tz) date_time = date_time.astimezone(_utc_tz if as_utc else _local_tz)
# str(sep) because isoformat barfs with unicode sep on python 2.x # native_string_type(sep) because isoformat barfs with unicode sep on python 2.x
return unicode_type(date_time.isoformat(str(sep))) return unicode_type(date_time.isoformat(native_string_type(sep)))
def internal_iso_format_string(): def internal_iso_format_string():