diff --git a/src/calibre/ebooks/metadata/sources/overdrive.py b/src/calibre/ebooks/metadata/sources/overdrive.py
index 514fb9b98e..18aa6b03bd 100644
--- a/src/calibre/ebooks/metadata/sources/overdrive.py
+++ b/src/calibre/ebooks/metadata/sources/overdrive.py
@@ -150,7 +150,7 @@ class OverDrive(Source):
fix_slashes = re.compile(r'\\/')
thumbimage = fix_slashes.sub('/', thumbimage)
worldcatlink = fix_slashes.sub('/', worldcatlink)
- cover_url = re.sub('(?P
(Ima?g(eType-)?))200', '\\g
100', thumbimage)
+ cover_url = re.sub(r'(?P
(Ima?g(eType-)?))200', r'\g
100', thumbimage)
social_metadata_url = base_url+'TitleInfo.aspx?ReserveID='+reserveid+'&FormatID='+formatid
series_num = ''
if not series:
@@ -235,7 +235,7 @@ class OverDrive(Source):
xreq.add_header('Referer', q_init_search)
xreq.add_header('Accept', 'application/json, text/javascript, */*')
raw = br.open_novisit(xreq).read()
- for m in re.finditer(type(u'')(r'"iTotalDisplayRecords":(?P\d+).*?"iTotalRecords":(?P\d+)'), raw):
+ for m in re.finditer(type('')(r'"iTotalDisplayRecords":(?P\d+).*?"iTotalRecords":(?P\d+)'), raw):
if int(m.group('totalrecords')) == 0:
return ''
elif int(m.group('displayrecords')) >= 1:
@@ -256,9 +256,9 @@ class OverDrive(Source):
def sort_ovrdrv_results(self, raw, log, title=None, title_tokens=None, author=None, author_tokens=None, ovrdrv_id=None):
close_matches = []
- raw = re.sub('.*?\\[\\[(?P.*?)\\]\\].*', '[[\\g]]', raw)
+ raw = re.sub(r'.*?\[\[(?P.*?)\]\].*', r'[[\g]]', raw)
results = json.loads(raw)
- # log.error('raw results are:'+str(results))
+ # log.error('raw results are:'+type('')(results))
# The search results are either from a keyword search or a multi-format list from a single ID,
# sort through the results for closest match/format
if results:
@@ -336,7 +336,7 @@ class OverDrive(Source):
req.add_header('Referer', search_url)
req.add_header('Accept', 'application/json, text/javascript, */*')
raw = br.open_novisit(req)
- raw = str(list(raw))
+ raw = type('')(list(raw))
clean_cj = mechanize.CookieJar()
br.set_cookiejar(clean_cj)
return self.sort_ovrdrv_results(raw, log, None, None, None, ovrdrv_id)
@@ -443,7 +443,7 @@ class OverDrive(Source):
mi.language = lang
if ebook_isbn:
- # print "ebook isbn is "+str(ebook_isbn[0])
+ # print("ebook isbn is "+type('')(ebook_isbn[0]))
isbn = check_isbn(ebook_isbn[0].strip())
if isbn:
self.cache_isbn_to_identifier(isbn, ovrdrv_id)
diff --git a/src/calibre/ebooks/mobi/__init__.py b/src/calibre/ebooks/mobi/__init__.py
index add994ba37..bea6a1736c 100644
--- a/src/calibre/ebooks/mobi/__init__.py
+++ b/src/calibre/ebooks/mobi/__init__.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
+from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal '
diff --git a/src/calibre/ebooks/mobi/debug/containers.py b/src/calibre/ebooks/mobi/debug/containers.py
index a14194db0b..bfd3d2cc42 100644
--- a/src/calibre/ebooks/mobi/debug/containers.py
+++ b/src/calibre/ebooks/mobi/debug/containers.py
@@ -8,7 +8,7 @@ __copyright__ = '2014, Kovid Goyal '
from struct import unpack_from
from calibre.ebooks.mobi.debug.headers import EXTHHeader
-from polyglot.builtins import filter
+from polyglot.builtins import filter, unicode_type
class ContainerHeader(object):
@@ -63,4 +63,4 @@ class ContainerHeader(object):
a('Null bytes after EXTH: %d' % self.null_bytes_after_exth)
if len(self.bytes_after_exth) != self.null_bytes_after_exth:
a('Non-null bytes present after EXTH header!!!!')
- return '\n'.join(ans) + '\n\n' + str(self.exth) + '\n\n' + ('Title: %s' % self.title)
+ return '\n'.join(ans) + '\n\n' + unicode_type(self.exth) + '\n\n' + ('Title: %s' % self.title)
diff --git a/src/calibre/ebooks/mobi/debug/headers.py b/src/calibre/ebooks/mobi/debug/headers.py
index d263ffbb6e..b931b4b1da 100644
--- a/src/calibre/ebooks/mobi/debug/headers.py
+++ b/src/calibre/ebooks/mobi/debug/headers.py
@@ -13,7 +13,7 @@ from calibre.ebooks.mobi.reader.headers import NULL_INDEX
from calibre.ebooks.mobi.langcodes import main_language, sub_language
from calibre.ebooks.mobi.debug import format_bytes
from calibre.ebooks.mobi.utils import get_trailing_data
-from polyglot.builtins import iteritems, range, as_bytes
+from polyglot.builtins import as_bytes, iteritems, range, unicode_type
# PalmDB {{{
@@ -42,7 +42,7 @@ class PalmDOCAttributes(object):
self.val))
def __str__(self):
- attrs = '\n\t'.join([str(x) for x in self.attributes])
+ attrs = '\n\t'.join([unicode_type(x) for x in self.attributes])
return 'PalmDOC Attributes: %s\n\t%s'%(bin(self.val), attrs)
@@ -84,7 +84,7 @@ class PalmDB(object):
def __str__(self):
ans = ['*'*20 + ' PalmDB Header '+ '*'*20]
ans.append('Name: %r'%self.name)
- ans.append(str(self.attributes))
+ ans.append(unicode_type(self.attributes))
ans.append('Version: %s'%self.version)
ans.append('Creation date: %s (%s)'%(self.creation_date.isoformat(),
self.creation_date_raw))
@@ -255,7 +255,7 @@ class EXTHHeader(object):
ans.append('Number of EXTH records: %d'%self.count)
ans.append('EXTH records...')
for r in self.records:
- ans.append(str(r))
+ ans.append(unicode_type(r))
return '\n'.join(ans)
# }}}
@@ -496,7 +496,7 @@ class MOBIHeader(object): # {{{
ans = '\n'.join(ans)
if self.has_exth:
- ans += '\n\n' + str(self.exth)
+ ans += '\n\n' + unicode_type(self.exth)
ans += '\n\nBytes after EXTH (%d bytes): %s'%(
len(self.bytes_after_exth),
format_bytes(self.bytes_after_exth))
diff --git a/src/calibre/ebooks/mobi/debug/mobi6.py b/src/calibre/ebooks/mobi/debug/mobi6.py
index 37d445399d..b57067f6f2 100644
--- a/src/calibre/ebooks/mobi/debug/mobi6.py
+++ b/src/calibre/ebooks/mobi/debug/mobi6.py
@@ -368,7 +368,7 @@ class IndexEntry(object): # {{{
self.index, len(self.tags))]
for tag in self.tags:
if tag.value is not None:
- ans.append('\t'+str(tag))
+ ans.append('\t'+unicode_type(tag))
if self.first_child_index != -1:
ans.append('\tNumber of children: %d'%(self.last_child_index -
self.first_child_index + 1))
@@ -421,7 +421,7 @@ class IndexRecord(object): # {{{
len(w), not bool(w.replace(b'\0', b''))))
for entry in self.indices:
offset = entry.offset
- a(str(entry))
+ a(unicode_type(entry))
t = self.alltext
if offset is not None and self.alltext is not None:
a('\tHTML before offset: %r'%t[offset-50:offset])
@@ -608,7 +608,7 @@ class TBSIndexing(object): # {{{
return as_bytes('0'*(4-len(ans)) + ans)
def repr_extra(x):
- return str({bin4(k):v for k, v in iteritems(extra)})
+ return unicode_type({bin4(k):v for k, v in iteritems(extra)})
tbs_type = 0
is_periodical = self.doc_type in (257, 258, 259)
@@ -788,14 +788,14 @@ class MOBIFile(object): # {{{
self.index_record.indices, self.mobi_header.type_raw)
def print_header(self, f=sys.stdout):
- print(str(self.palmdb).encode('utf-8'), file=f)
+ print(unicode_type(self.palmdb).encode('utf-8'), file=f)
print(file=f)
print('Record headers:', file=f)
for i, r in enumerate(self.records):
print('%6d. %s'%(i, r.header), file=f)
print(file=f)
- print(str(self.mobi_header).encode('utf-8'), file=f)
+ print(unicode_type(self.mobi_header).encode('utf-8'), file=f)
# }}}
@@ -820,19 +820,19 @@ def inspect_mobi(mobi_file, ddir):
if f.index_header is not None:
f.index_record.alltext = alltext
with open(os.path.join(ddir, 'index.txt'), 'wb') as out:
- print(str(f.index_header), file=out)
+ print(unicode_type(f.index_header), file=out)
print('\n\n', file=out)
if f.secondary_index_header is not None:
- print(str(f.secondary_index_header).encode('utf-8'), file=out)
+ print(unicode_type(f.secondary_index_header).encode('utf-8'), file=out)
print('\n\n', file=out)
if f.secondary_index_record is not None:
- print(str(f.secondary_index_record).encode('utf-8'), file=out)
+ print(unicode_type(f.secondary_index_record).encode('utf-8'), file=out)
print('\n\n', file=out)
- print(str(f.cncx).encode('utf-8'), file=out)
+ print(unicode_type(f.cncx).encode('utf-8'), file=out)
print('\n\n', file=out)
- print(str(f.index_record), file=out)
+ print(unicode_type(f.index_record), file=out)
with open(os.path.join(ddir, 'tbs_indexing.txt'), 'wb') as out:
- print(str(f.tbs_indexing), file=out)
+ print(unicode_type(f.tbs_indexing), file=out)
f.tbs_indexing.dump(ddir)
for tdir, attr in [('text', 'text_records'), ('images', 'image_records'),
diff --git a/src/calibre/ebooks/mobi/debug/mobi8.py b/src/calibre/ebooks/mobi/debug/mobi8.py
index 3e2524584a..ad3246d1b6 100644
--- a/src/calibre/ebooks/mobi/debug/mobi8.py
+++ b/src/calibre/ebooks/mobi/debug/mobi8.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import absolute_import, division, print_function, unicode_literals
-from polyglot.builtins import map
__license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal '
@@ -18,7 +17,7 @@ from calibre.ebooks.mobi.utils import read_font_record, decode_tbs, RECORD_SIZE
from calibre.ebooks.mobi.debug import format_bytes
from calibre.ebooks.mobi.reader.headers import NULL_INDEX
from calibre.utils.imghdr import what
-from polyglot.builtins import zip, iteritems, itervalues
+from polyglot.builtins import iteritems, itervalues, map, unicode_type, zip
class FDST(object):
@@ -95,14 +94,14 @@ class MOBIFile(object):
self.read_tbs()
def print_header(self, f=sys.stdout):
- print(str(self.mf.palmdb).encode('utf-8'), file=f)
+ print(unicode_type(self.mf.palmdb).encode('utf-8'), file=f)
print(file=f)
print('Record headers:', file=f)
for i, r in enumerate(self.mf.records):
print('%6d. %s'%(i, r.header), file=f)
print(file=f)
- print(str(self.mf.mobi8_header).encode('utf-8'), file=f)
+ print(unicode_type(self.mf.mobi8_header).encode('utf-8'), file=f)
def read_fdst(self):
self.fdst = None
@@ -314,23 +313,23 @@ def inspect_mobi(mobi_file, ddir):
for i, container in enumerate(f.containers):
with open(os.path.join(ddir, 'container%d.txt' % (i + 1)), 'wb') as cf:
- cf.write(str(container).encode('utf-8'))
+ cf.write(unicode_type(container).encode('utf-8'))
if f.fdst:
with open(os.path.join(ddir, 'fdst.record'), 'wb') as fo:
- fo.write(str(f.fdst).encode('utf-8'))
+ fo.write(unicode_type(f.fdst).encode('utf-8'))
with open(os.path.join(ddir, 'skel.record'), 'wb') as fo:
- fo.write(str(f.skel_index).encode('utf-8'))
+ fo.write(unicode_type(f.skel_index).encode('utf-8'))
with open(os.path.join(ddir, 'chunks.record'), 'wb') as fo:
- fo.write(str(f.sect_index).encode('utf-8'))
+ fo.write(unicode_type(f.sect_index).encode('utf-8'))
with open(os.path.join(ddir, 'ncx.record'), 'wb') as fo:
- fo.write(str(f.ncx_index).encode('utf-8'))
+ fo.write(unicode_type(f.ncx_index).encode('utf-8'))
with open(os.path.join(ddir, 'guide.record'), 'wb') as fo:
- fo.write(str(f.guide_index).encode('utf-8'))
+ fo.write(unicode_type(f.guide_index).encode('utf-8'))
with open(os.path.join(ddir, 'tbs.txt'), 'wb') as fo:
fo.write(('\n'.join(f.indexing_data)).encode('utf-8'))
diff --git a/src/calibre/ebooks/mobi/langcodes.py b/src/calibre/ebooks/mobi/langcodes.py
index 16be630f18..be34fc1384 100644
--- a/src/calibre/ebooks/mobi/langcodes.py
+++ b/src/calibre/ebooks/mobi/langcodes.py
@@ -1,4 +1,6 @@
#!/usr/bin/env python2
+from __future__ import absolute_import, division, print_function, unicode_literals
+
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
diff --git a/src/calibre/ebooks/mobi/mobiml.py b/src/calibre/ebooks/mobi/mobiml.py
index 51bff9c57a..94c8114e85 100644
--- a/src/calibre/ebooks/mobi/mobiml.py
+++ b/src/calibre/ebooks/mobi/mobiml.py
@@ -1,8 +1,8 @@
+from __future__ import absolute_import, division, print_function, unicode_literals
+
'''
Transform XHTML/OPS-ish content into Mobipocket HTML 3.2.
'''
-from __future__ import with_statement
-from __future__ import print_function
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift '
@@ -54,7 +54,7 @@ def asfloat(value):
def isspace(text):
if not text:
return True
- if u'\xa0' in text:
+ if '\xa0' in text:
return False
return text.isspace()
@@ -139,7 +139,7 @@ class MobiMLizer(object):
self.mobimlize_elem(body, stylizer, BlockState(nbody),
[FormatState()])
item.data = nroot
- # print etree.tostring(nroot)
+ # print(etree.tostring(nroot))
def mobimlize_font(self, ptsize):
return self.fnums[self.fmap[ptsize]]
@@ -156,9 +156,9 @@ class MobiMLizer(object):
text = unicode_type(text)
if pre_wrap:
# Replace n consecutive spaces with n-1 NBSP + space
- text = re.sub(r' {2,}', lambda m:(u'\xa0'*(len(m.group())-1) + u' '), text)
+ text = re.sub(r' {2,}', lambda m:('\xa0'*(len(m.group())-1) + ' '), text)
else:
- text = text.replace(u' ', u'\xa0')
+ text = text.replace(' ', '\xa0')
text = text.replace('\r\n', '\n')
text = text.replace('\r', '\n')
@@ -201,7 +201,7 @@ class MobiMLizer(object):
bstate.nested.append(para)
if tag == 'li' and len(istates) > 1:
istates[-2].list_num += 1
- para.attrib['value'] = str(istates[-2].list_num)
+ para.attrib['value'] = unicode_type(istates[-2].list_num)
elif tag in NESTABLE_TAGS and istate.rendered:
para = wrapper = bstate.nested[-1]
elif not self.opts.mobi_ignore_margins and left > 0 and indent >= 0:
@@ -210,7 +210,7 @@ class MobiMLizer(object):
para = wrapper
emleft = int(round(left / self.profile.fbase)) - ems
emleft = min((emleft, 10))
- while emleft > ems/2.0:
+ while emleft > ems / 2:
para = etree.SubElement(para, XHTML('blockquote'))
emleft -= ems
else:
@@ -287,7 +287,7 @@ class MobiMLizer(object):
if fsize != 3:
inline = etree.SubElement(inline, XHTML('font'),
- size=str(fsize))
+ size=unicode_type(fsize))
if istate.family == 'monospace':
inline = etree.SubElement(inline, XHTML('tt'))
if istate.italic:
@@ -390,17 +390,17 @@ class MobiMLizer(object):
lspace = margin + padding
if lspace > 0:
spaces = int(round((lspace * 3) / style['font-size']))
- elem.text = (u'\xa0' * spaces) + (elem.text or '')
+ elem.text = ('\xa0' * spaces) + (elem.text or '')
margin = asfloat(style['margin-right'])
padding = asfloat(style['padding-right'])
rspace = margin + padding
if rspace > 0:
spaces = int(round((rspace * 3) / style['font-size']))
if len(elem) == 0:
- elem.text = (elem.text or '') + (u'\xa0' * spaces)
+ elem.text = (elem.text or '') + ('\xa0' * spaces)
else:
last = elem[-1]
- last.text = (last.text or '') + (u'\xa0' * spaces)
+ last.text = (last.text or '') + ('\xa0' * spaces)
if bstate.content and style['page-break-before'] in PAGE_BREAKS:
bstate.pbreak = True
istate.fsize = self.mobimlize_font(style['font-size'])
@@ -446,10 +446,10 @@ class MobiMLizer(object):
# See #7520 for test case
try:
pixs = int(round(float(value) /
- (72./self.profile.dpi)))
+ (72/self.profile.dpi)))
except:
continue
- result = str(pixs)
+ result = unicode_type(pixs)
istate.attrib[prop] = result
if 'width' not in istate.attrib or 'height' not in istate.attrib:
href = self.current_spine_item.abshref(elem.attrib['src'])
@@ -466,22 +466,22 @@ class MobiMLizer(object):
else:
if 'width' not in istate.attrib and 'height' not in \
istate.attrib:
- istate.attrib['width'] = str(width)
- istate.attrib['height'] = str(height)
+ istate.attrib['width'] = unicode_type(width)
+ istate.attrib['height'] = unicode_type(height)
else:
- ar = float(width)/float(height)
+ ar = width / height
if 'width' not in istate.attrib:
try:
width = int(istate.attrib['height'])*ar
except:
pass
- istate.attrib['width'] = str(int(width))
+ istate.attrib['width'] = unicode_type(int(width))
else:
try:
height = int(istate.attrib['width'])/ar
except:
pass
- istate.attrib['height'] = str(int(height))
+ istate.attrib['height'] = unicode_type(int(height))
item.unload_data_from_memory()
elif tag == 'hr' and asfloat(style['width']) > 0 and style._get('width') not in {'100%', 'auto'}:
raww = style._get('width')
@@ -515,11 +515,11 @@ class MobiMLizer(object):
t = elem.text
if not t:
t = ''
- elem.text = u'\u201c' + t
+ elem.text = '\u201c' + t
t = elem.tail
if not t:
t = ''
- elem.tail = u'\u201d' + t
+ elem.tail = '\u201d' + t
text = None
if elem.text:
if istate.preserve or istate.pre_wrap:
@@ -602,7 +602,7 @@ class MobiMLizer(object):
bstate.pbreak = True
if isblock:
para = bstate.para
- if para is not None and para.text == u'\xa0' and len(para) < 1:
+ if para is not None and para.text == '\xa0' and len(para) < 1:
if style.height > 2:
para.getparent().replace(para, etree.Element(XHTML('br')))
else:
diff --git a/src/calibre/ebooks/mobi/reader/headers.py b/src/calibre/ebooks/mobi/reader/headers.py
index fd82f6509d..f34b49af92 100644
--- a/src/calibre/ebooks/mobi/reader/headers.py
+++ b/src/calibre/ebooks/mobi/reader/headers.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
-from __future__ import (absolute_import, print_function)
+from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal '
@@ -294,14 +294,14 @@ class MetadataHeader(BookHeader):
def kf8_type(self):
if (self.mobi_version == 8 and getattr(self, 'skelidx', NULL_INDEX) !=
NULL_INDEX):
- return u'standalone'
+ return 'standalone'
kf8_header_index = getattr(self.exth, 'kf8_header', None)
if kf8_header_index is None:
return None
try:
if self.section_data(kf8_header_index-1) == b'BOUNDARY':
- return u'joint'
+ return 'joint'
except:
pass
return None
diff --git a/src/calibre/ebooks/mobi/reader/mobi6.py b/src/calibre/ebooks/mobi/reader/mobi6.py
index 60e4f89aaf..abc648f806 100644
--- a/src/calibre/ebooks/mobi/reader/mobi6.py
+++ b/src/calibre/ebooks/mobi/reader/mobi6.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
-from __future__ import (absolute_import, print_function)
+from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal '
@@ -169,7 +169,7 @@ class MobiReader(object):
self.processed_html = self.processed_html.replace('', '')
self.processed_html = re.sub(r'([a-zA-Z]+)<', r'\1><',
self.processed_html)
- self.processed_html = self.processed_html.replace(u'\ufeff', '')
+ self.processed_html = self.processed_html.replace('\ufeff', '')
# Remove tags of the form as they can cause issues further
# along the pipeline
self.processed_html = re.sub(r'{0,1}[a-zA-Z]+:\s+[^>]*>', '',
@@ -356,15 +356,15 @@ class MobiReader(object):
# Swap inline and block level elements, and order block level elements according to priority
# - lxml and beautifulsoup expect/assume a specific order based on xhtml spec
self.processed_html = re.sub(
- r'(?i)(?P(<(h\d+|i|b|u|em|small|big|strong|tt)>\s*){1,})(?P]*>)', '\\g'+'\\g', self.processed_html)
+ r'(?i)(?P(<(h\d+|i|b|u|em|small|big|strong|tt)>\s*){1,})(?P]*>)', r'\g'+r'\g', self.processed_html)
self.processed_html = re.sub(
- r'(?i)(?P
]*>)\s*(?P((h\d+|i|b|u|em|small|big|strong|tt)>\s*){1,})', '\\g'+'\\g', self.processed_html)
+ r'(?i)(?P
]*>)\s*(?P((h\d+|i|b|u|em|small|big|strong|tt)>\s*){1,})', r'\g'+r'\g', self.processed_html)
self.processed_html = re.sub(
- r'(?i)(?P((blockquote|div)[^>]*>\s*){1,})(?P
]*>)', '\\g'+'\\g', self.processed_html)
+ r'(?i)(?P((blockquote|div)[^>]*>\s*){1,})(?P]*>)', r'\g'+r'\g', self.processed_html)
self.processed_html = re.sub(
- r'(?i)(?P]*>)\s*(?P
(<(blockquote|div)[^>]*>\s*){1,})', '\\g'+'\\g', self.processed_html)
+ r'(?i)(?P]*>)\s*(?P
(<(blockquote|div)[^>]*>\s*){1,})', r'\g'+r'\g', self.processed_html)
bods = htmls = 0
- for x in re.finditer(u'