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', '\\g100', thumbimage) + cover_url = re.sub(r'(?P(Ima?g(eType-)?))200', r'\g100', 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) - 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']*>', '', @@ -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(\s*){1,})', '\\g'+'\\g', self.processed_html) + r'(?i)(?P]*>)\s*(?P(\s*){1,})', r'\g'+r'\g', self.processed_html) self.processed_html = re.sub( - r'(?i)(?P
(]*>\s*){1,})(?P]*>)', '\\g'+'\\g
', self.processed_html) + r'(?i)(?P
(]*>\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'|', self.processed_html): + for x in re.finditer('|', self.processed_html): if x == '': bods +=1 else: @@ -442,7 +442,7 @@ class MobiReader(object): # Paragraph spacer # Insert nbsp so that the element is never # discarded by a renderer - tag.text = u'\u00a0' # nbsp + tag.text = '\u00a0' # nbsp styles.append('height: %s' % self.ensure_unit(height)) else: @@ -642,11 +642,11 @@ class MobiReader(object): mi = MetaInformation(self.book_header.title, [_('Unknown')]) opf = OPFCreator(os.path.dirname(htmlfile), mi) if hasattr(self.book_header.exth, 'cover_offset'): - opf.cover = u'images/%05d.jpg' % (self.book_header.exth.cover_offset + 1) + opf.cover = 'images/%05d.jpg' % (self.book_header.exth.cover_offset + 1) elif mi.cover is not None: opf.cover = mi.cover else: - opf.cover = u'images/%05d.jpg' % 1 + opf.cover = 'images/%05d.jpg' % 1 if not os.path.exists(os.path.join(os.path.dirname(htmlfile), * opf.cover.split('/'))): opf.cover = None @@ -656,7 +656,7 @@ class MobiReader(object): if cover is not None: cover = cover.replace('/', os.sep) if os.path.exists(cover): - ncover = u'images'+os.sep+u'calibre_cover.jpg' + ncover = 'images'+os.sep+'calibre_cover.jpg' if os.path.exists(ncover): os.remove(ncover) shutil.copyfile(cover, ncover) @@ -664,9 +664,9 @@ class MobiReader(object): opf.cover = ncover.replace(os.sep, '/') manifest = [(htmlfile, 'application/xhtml+xml'), - (os.path.abspath(u'styles.css'), 'text/css')] + (os.path.abspath('styles.css'), 'text/css')] bp = os.path.dirname(htmlfile) - added = set([]) + added = set() for i in getattr(self, 'image_names', []): path = os.path.join(bp, 'images', i) added.add(path) @@ -699,9 +699,9 @@ class MobiReader(object): continue if reached and x.tag == 'a': href = x.get('href', '') - if href and re.match('\\w+://', href) is None: + if href and re.match(r'\w+://', href) is None: try: - text = u' '.join([t.strip() for t in + text = ' '.join([t.strip() for t in x.xpath('descendant::text()')]) except: text = '' diff --git a/src/calibre/ebooks/mobi/writer8/exth.py b/src/calibre/ebooks/mobi/writer8/exth.py index 3d76721e85..d997b3ae29 100644 --- a/src/calibre/ebooks/mobi/writer8/exth.py +++ b/src/calibre/ebooks/mobi/writer8/exth.py @@ -107,7 +107,7 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, break if uuid is None: from uuid import uuid4 - uuid = str(uuid4()) + uuid = unicode_type(uuid4()) if isinstance(uuid, unicode_type): uuid = uuid.encode('utf-8') @@ -137,9 +137,9 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, # Add a publication date entry if metadata['date']: - datestr = str(metadata['date'][0]) + datestr = unicode_type(metadata['date'][0]) elif metadata['timestamp']: - datestr = str(metadata['timestamp'][0]) + datestr = unicode_type(metadata['timestamp'][0]) if datestr is None: raise ValueError("missing date or timestamp") diff --git a/src/calibre/ebooks/mobi/writer8/mobi.py b/src/calibre/ebooks/mobi/writer8/mobi.py index ac843b5ce1..da589456af 100644 --- a/src/calibre/ebooks/mobi/writer8/mobi.py +++ b/src/calibre/ebooks/mobi/writer8/mobi.py @@ -289,7 +289,7 @@ class KF8Book(object): self.extra_data_flags |= 0b10 self.uid = random.randint(0, 0xffffffff) - self.language_code = iana2mobi(str(metadata.language[0])) + self.language_code = iana2mobi(unicode_type(metadata.language[0])) self.exth_flags = 0b1010000 if writer.opts.mobi_periodical: self.exth_flags |= 0b1000 diff --git a/src/calibre/ebooks/odt/__init__.py b/src/calibre/ebooks/odt/__init__.py index da901ade3e..8a2513c8cc 100644 --- a/src/calibre/ebooks/odt/__init__.py +++ b/src/calibre/ebooks/odt/__init__.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' @@ -6,4 +8,3 @@ __docformat__ = 'restructuredtext en' ''' Handle the Open Document Format ''' - diff --git a/src/calibre/ebooks/oeb/__init__.py b/src/calibre/ebooks/oeb/__init__.py index 4f8588535f..57af3199b4 100644 --- a/src/calibre/ebooks/oeb/__init__.py +++ b/src/calibre/ebooks/oeb/__init__.py @@ -1,2 +1,4 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 24560c49d7..1f87dcbe45 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -920,7 +920,7 @@ class Manifest(object): """ def __init__(self, oeb, id, href, media_type, - fallback=None, loader=str, data=None): + fallback=None, loader=unicode_type, data=None): if href: href = unicode_type(href) self.oeb = oeb @@ -937,7 +937,7 @@ class Manifest(object): self._data = data def __repr__(self): - return u'Item(id=%r, href=%r, media_type=%r)' \ + return 'Item(id=%r, href=%r, media_type=%r)' \ % (self.id, self.href, self.media_type) # Parsing {{{ @@ -1024,8 +1024,8 @@ class Manifest(object): - XML content is parsed and returned as an lxml.etree element. - CSS and CSS-variant content is parsed and returned as a css_parser CSS DOM stylesheet. - - All other content is returned as a :class:`str` object with no - special parsing. + - All other content is returned as a :class:`str` or :class:`bytes` + object with no special parsing. """ data = self._data if data is None: @@ -1642,7 +1642,7 @@ class TOC(object): po = node.play_order if po == 0: po = 1 - attrib = {'id': id, 'playOrder': str(po)} + attrib = {'id': id, 'playOrder': unicode_type(po)} if node.klass: attrib['class'] = node.klass point = element(parent, NCX('navPoint'), attrib=attrib) @@ -1925,7 +1925,7 @@ class OEBBook(object): for i, elem in enumerate(xpath(ncx, '//*[@playOrder and ./ncx:content[@src]]')): href = urlnormalize(selector(elem)[0]) order = playorder.get(href, i) - elem.attrib['playOrder'] = str(order) + elem.attrib['playOrder'] = unicode_type(order) return def _to_ncx(self): @@ -1938,12 +1938,12 @@ class OEBBook(object): etree.SubElement(head, NCX('meta'), name='dtb:uid', content=unicode_type(self.uid)) etree.SubElement(head, NCX('meta'), - name='dtb:depth', content=str(self.toc.depth())) + name='dtb:depth', content=unicode_type(self.toc.depth())) generator = ''.join(['calibre (', __version__, ')']) etree.SubElement(head, NCX('meta'), name='dtb:generator', content=generator) etree.SubElement(head, NCX('meta'), - name='dtb:totalPageCount', content=str(len(self.pages))) + name='dtb:totalPageCount', content=unicode_type(len(self.pages))) maxpnum = etree.SubElement(head, NCX('meta'), name='dtb:maxPageNumber', content='0') title = etree.SubElement(ncx, NCX('docTitle')) @@ -1954,7 +1954,7 @@ class OEBBook(object): if len(self.pages) > 0: plist = self.pages.to_ncx(ncx) value = max(int(x) for x in xpath(plist, '//@value')) - maxpnum.attrib['content'] = str(value) + maxpnum.attrib['content'] = unicode_type(value) self._update_playorder(ncx) return ncx diff --git a/src/calibre/ebooks/oeb/normalize_css.py b/src/calibre/ebooks/oeb/normalize_css.py index 14b1ca07f2..3b9ee27d28 100644 --- a/src/calibre/ebooks/oeb/normalize_css.py +++ b/src/calibre/ebooks/oeb/normalize_css.py @@ -6,13 +6,13 @@ __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' import numbers -from polyglot.builtins import iteritems, zip, string_or_bytes from functools import wraps from css_parser.css import PropertyValue from css_parser import profile as cssprofiles, CSSParser from tinycss.fonts3 import parse_font, serialize_font_family from calibre.ebooks.oeb.base import css_text +from polyglot.builtins import iteritems, string_or_bytes, unicode_type, zip DEFAULTS = {'azimuth': 'center', 'background-attachment': 'scroll', # {{{ 'background-color': 'transparent', 'background-image': 'none', @@ -393,7 +393,7 @@ def test_normalization(return_tests=False): # {{{ tuple('0 0 0 0'.split()) : '0', }): for prefix in ('margin', 'padding'): - css = {'%s-%s' % (prefix, x) : str(y)+'pt' if isinstance(y, numbers.Number) else y for x, y in zip(('left', 'top', 'right', 'bottom'), s)} + css = {'%s-%s' % (prefix, x) : unicode_type(y)+'pt' if isinstance(y, numbers.Number) else y for x, y in zip(('left', 'top', 'right', 'bottom'), s)} css = '; '.join(('%s:%s' % (k, v) for k, v in iteritems(css))) style = parseStyle(css) condense_rule(style) diff --git a/src/calibre/ebooks/oeb/polish/cover.py b/src/calibre/ebooks/oeb/polish/cover.py index a59b9b3cfe..98ae45277a 100644 --- a/src/calibre/ebooks/oeb/polish/cover.py +++ b/src/calibre/ebooks/oeb/polish/cover.py @@ -10,7 +10,7 @@ import shutil, re, os from calibre.ebooks.oeb.base import OPF, OEB_DOCS, XPath, XLINK, xml2text from calibre.ebooks.oeb.polish.replace import replace_links, get_recommended_folders from calibre.utils.imghdr import identify -from polyglot.builtins import iteritems +from polyglot.builtins import iteritems, unicode_type def set_azw3_cover(container, cover_path, report, options=None): @@ -384,8 +384,8 @@ def create_epub_cover(container, cover_path, existing_image, options=None): ar = 'xMidYMid meet' if keep_aspect else 'none' templ = CoverManager.SVG_TEMPLATE.replace('__ar__', ar) templ = templ.replace('__viewbox__', '0 0 %d %d'%(width, height)) - templ = templ.replace('__width__', str(width)) - templ = templ.replace('__height__', str(height)) + templ = templ.replace('__width__', unicode_type(width)) + templ = templ.replace('__height__', unicode_type(height)) folder = recommended_folders[tname] if folder: tname = folder + '/' + tname diff --git a/src/calibre/ebooks/oeb/polish/parsing.py b/src/calibre/ebooks/oeb/polish/parsing.py index 3771a1588b..1b94dce1aa 100644 --- a/src/calibre/ebooks/oeb/polish/parsing.py +++ b/src/calibre/ebooks/oeb/polish/parsing.py @@ -13,6 +13,7 @@ import html5_parser from calibre import xml_replace_entities from calibre.ebooks.chardet import xml_to_unicode, strip_encoding_declarations from calibre.utils.cleantext import clean_xml_chars +from polyglot.builtins import unicode_type XHTML_NS = 'http://www.w3.org/1999/xhtml' @@ -83,7 +84,7 @@ def parse(raw, decoder=None, log=None, line_numbers=True, linenumber_attribute=N if linenumber_attribute: for elem in ans.iter(LxmlElement): if elem.sourceline is not None: - elem.set(linenumber_attribute, str(elem.sourceline)) + elem.set(linenumber_attribute, unicode_type(elem.sourceline)) return ans except Exception: if log is not None: diff --git a/src/calibre/ebooks/oeb/polish/split.py b/src/calibre/ebooks/oeb/polish/split.py index 0acc603b71..88ddeeec04 100644 --- a/src/calibre/ebooks/oeb/polish/split.py +++ b/src/calibre/ebooks/oeb/polish/split.py @@ -12,7 +12,7 @@ from calibre.ebooks.oeb.base import barename, XPNSMAP, XPath, OPF, XHTML, OEB_DO from calibre.ebooks.oeb.polish.errors import MalformedMarkup from calibre.ebooks.oeb.polish.toc import node_from_loc from calibre.ebooks.oeb.polish.replace import LinkRebaser -from polyglot.builtins import iteritems +from polyglot.builtins import iteritems, unicode_type from polyglot.urllib import urlparse @@ -282,7 +282,7 @@ def multisplit(container, name, xpath, before=True): raise AbortError('Cannot split on the tag') for i, tag in enumerate(nodes): - tag.set('calibre-split-point', str(i)) + tag.set('calibre-split-point', unicode_type(i)) current = name all_names = [name] diff --git a/src/calibre/ebooks/oeb/polish/toc.py b/src/calibre/ebooks/oeb/polish/toc.py index c752a0735e..1e7b224070 100644 --- a/src/calibre/ebooks/oeb/polish/toc.py +++ b/src/calibre/ebooks/oeb/polish/toc.py @@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en' import re from collections import Counter, OrderedDict from functools import partial -from polyglot.builtins import iteritems, map, unicode_type from operator import itemgetter from lxml import etree @@ -24,6 +23,7 @@ from calibre.ebooks.oeb.polish.opf import set_guide_item, get_book_language from calibre.ebooks.oeb.polish.pretty import pretty_html_tree from calibre.translations.dynamic import translate from calibre.utils.localization import get_lang, canonicalize_lang, lang_as_iso639_1 +from polyglot.builtins import iteritems, map, unicode_type from polyglot.urllib import urlparse ns = etree.FunctionNamespace('calibre_xpath_extensions') @@ -104,7 +104,7 @@ class TOC(object): def get_lines(self, lvl=0): frag = ('#'+self.frag) if self.frag else '' - ans = [(u'\t'*lvl) + u'TOC: %s --> %s%s'%(self.title, self.dest, frag)] + ans = [('\t'*lvl) + 'TOC: %s --> %s%s'%(self.title, self.dest, frag)] for child in self: ans.extend(child.get_lines(lvl+1)) return ans @@ -580,7 +580,7 @@ def create_ncx(toc, to_href, btitle, lang, uid): etree.SubElement(head, NCX('meta'), name='dtb:uid', content=unicode_type(uid)) etree.SubElement(head, NCX('meta'), - name='dtb:depth', content=str(toc.depth)) + name='dtb:depth', content=unicode_type(toc.depth)) generator = ''.join(['calibre (', __version__, ')']) etree.SubElement(head, NCX('meta'), name='dtb:generator', content=generator) @@ -598,7 +598,7 @@ def create_ncx(toc, to_href, btitle, lang, uid): for child in toc_parent: play_order['c'] += 1 point = etree.SubElement(xml_parent, NCX('navPoint'), id='num_%d' % play_order['c'], - playOrder=str(play_order['c'])) + playOrder=unicode_type(play_order['c'])) label = etree.SubElement(point, NCX('navLabel')) title = child.title if title: @@ -764,7 +764,7 @@ def commit_nav_toc(container, toc, lang=None, landmarks=None, previous_nav=None) for entry in toc.page_list: if container.has_name(entry['dest']) and container.mime_map[entry['dest']] in OEB_DOCS: a = create_li(ol, entry) - a.text = str(entry['pagenum']) + a.text = unicode_type(entry['pagenum']) pretty_xml_tree(nav) collapse_li(nav) container.replace(tocname, root) diff --git a/src/calibre/ebooks/oeb/reader.py b/src/calibre/ebooks/oeb/reader.py index 467b0113c1..c1961ed0e2 100644 --- a/src/calibre/ebooks/oeb/reader.py +++ b/src/calibre/ebooks/oeb/reader.py @@ -1,8 +1,7 @@ """ Container-/OPF-based input OEBBook reader. """ -from __future__ import with_statement -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' @@ -150,7 +149,7 @@ class OEBReader(object): dict(a=__appname__, v=__version__) meta_info_to_oeb_metadata(mi, self.oeb.metadata, self.logger) m = self.oeb.metadata - m.add('identifier', str(uuid.uuid4()), id='uuid_id', scheme='uuid') + m.add('identifier', unicode_type(uuid.uuid4()), id='uuid_id', scheme='uuid') self.oeb.uid = self.oeb.metadata.identifier[-1] if not m.title: m.add('title', self.oeb.translate(__('Unknown'))) @@ -198,8 +197,8 @@ class OEBReader(object): try: data = item.data except: - self.oeb.log.exception(u'Failed to read from manifest ' - u'entry with id: %s, ignoring'%item.id) + self.oeb.log.exception('Failed to read from manifest ' + 'entry with id: %s, ignoring'%item.id) invalid.add(item) continue if data is None: @@ -234,7 +233,7 @@ class OEBReader(object): if not scheme and href not in known: new.add(href) unchecked.clear() - warned = set([]) + warned = set() for href in new: known.add(href) is_invalid = False @@ -276,13 +275,13 @@ class OEBReader(object): media_type = media_type.lower() fallback = elem.get('fallback') if href in manifest.hrefs: - self.logger.warn(u'Duplicate manifest entry for %r' % href) + self.logger.warn('Duplicate manifest entry for %r' % href) continue if not self.oeb.container.exists(href): - self.logger.warn(u'Manifest item %r not found' % href) + self.logger.warn('Manifest item %r not found' % href) continue if id in manifest.ids: - self.logger.warn(u'Duplicate manifest id %r' % id) + self.logger.warn('Duplicate manifest id %r' % id) id, href = manifest.generate(id, href) manifest.add(id, href, media_type, fallback) invalid = self._manifest_prune_invalid() @@ -333,7 +332,7 @@ class OEBReader(object): for elem in xpath(opf, '/o2:package/o2:spine/o2:itemref'): idref = elem.get('idref') if idref not in manifest.ids: - self.logger.warn(u'Spine item %r not found' % idref) + self.logger.warn('Spine item %r not found' % idref) continue item = manifest.ids[idref] if item.media_type.lower() in OEB_DOCS and hasattr(item.data, 'xpath') and not getattr(item.data, 'tag', '').endswith('}ncx'): @@ -365,7 +364,7 @@ class OEBReader(object): corrected_href = href break if corrected_href is None: - self.logger.warn(u'Guide reference %r not found' % ref_href) + self.logger.warn('Guide reference %r not found' % ref_href) continue ref_href = corrected_href typ = elem.get('type') diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 9a5e8908d0..d834e26ad3 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -3,7 +3,7 @@ ''' CSS property propagation class. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' @@ -32,7 +32,8 @@ _html_css_stylesheet = None def html_css_stylesheet(): global _html_css_stylesheet if _html_css_stylesheet is None: - html_css = open(P('templates/html.css'), 'rb').read() + with open(P('templates/html.css'), 'rb') as f: + html_css = f.read() _html_css_stylesheet = parseString(html_css, validate=False) return _html_css_stylesheet @@ -74,7 +75,7 @@ def media_ok(raw): return mq.negated ^ matched try: - for mq in CSSMedia3Parser().parse_stylesheet(u'@media %s {}' % raw).rules[0].media: + for mq in CSSMedia3Parser().parse_stylesheet('@media %s {}' % raw).rules[0].media: if query_ok(mq): return True return False @@ -219,14 +220,14 @@ class Stylizer(object): log=logging.getLogger('calibre.css')) for elem in style_tags: if (elem.tag == XHTML('style') and elem.get('type', CSS_MIME) in OEB_STYLES and media_ok(elem.get('media'))): - text = elem.text if elem.text else u'' + text = elem.text if elem.text else '' for x in elem: t = getattr(x, 'text', None) if t: - text += u'\n\n' + force_unicode(t, u'utf-8') + text += '\n\n' + force_unicode(t, 'utf-8') t = getattr(x, 'tail', None) if t: - text += u'\n\n' + force_unicode(t, u'utf-8') + text += '\n\n' + force_unicode(t, 'utf-8') if text: text = oeb.css_preprocessor(text) # We handle @import rules separately @@ -295,7 +296,7 @@ class Stylizer(object): self.flatten_style = self.oeb.stylizer_rules.flatten_style self._styles = {} - pseudo_pat = re.compile(u':{1,2}(%s)' % ('|'.join(INAPPROPRIATE_PSEUDO_CLASSES)), re.I) + pseudo_pat = re.compile(':{1,2}(%s)' % ('|'.join(INAPPROPRIATE_PSEUDO_CLASSES)), re.I) select = Select(tree, ignore_inappropriate_pseudo_classes=True) for _, _, cssdict, text, _ in self.rules: @@ -309,7 +310,7 @@ class Stylizer(object): if fl is not None: fl = fl.group(1) if fl == 'first-letter' and getattr(self.oeb, - 'plumber_output_format', '').lower() in {u'mobi', u'docx'}: + 'plumber_output_format', '').lower() in {'mobi', 'docx'}: # Fake first-letter for elem in matches: for x in elem.iter('*'): @@ -323,8 +324,8 @@ class Stylizer(object): punctuation_chars.append(text[0]) text = text[1:] - special_text = u''.join(punctuation_chars) + \ - (text[0] if text else u'') + special_text = ''.join(punctuation_chars) + \ + (text[0] if text else '') span = x.makeelement('{%s}span' % XHTML_NS) span.text = special_text span.set('data-fake-first-letter', '1') @@ -486,7 +487,7 @@ class Style(object): return unit_convert(value, base, font, self._profile.dpi, body_font_size=self._stylizer.body_font_size) def pt_to_px(self, value): - return (self._profile.dpi / 72.0) * value + return (self._profile.dpi / 72) * value @property def backgroundColor(self): @@ -590,7 +591,7 @@ class Style(object): x = self._style.get(attr) if x is not None: if x == 'auto': - ans = self._unit_convert(str(img_size) + 'px', base=base) + ans = self._unit_convert(unicode_type(img_size) + 'px', base=base) else: x = self._unit_convert(x, base=base) if isinstance(x, numbers.Number): @@ -602,7 +603,7 @@ class Style(object): if isinstance(x, numbers.Number): ans = x if ans is None: - ans = self._unit_convert(str(img_size) + 'px', base=base) + ans = self._unit_convert(unicode_type(img_size) + 'px', base=base) maa = self._style.get('max-' + attr) if maa is not None: x = self._unit_convert(maa, base=base) diff --git a/src/calibre/ebooks/oeb/transforms/__init__.py b/src/calibre/ebooks/oeb/transforms/__init__.py index 4a7a9ab6f5..720ff711e6 100644 --- a/src/calibre/ebooks/oeb/transforms/__init__.py +++ b/src/calibre/ebooks/oeb/transforms/__init__.py @@ -1,10 +1,7 @@ #!/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 ' __docformat__ = 'restructuredtext en' - - - diff --git a/src/calibre/ebooks/oeb/transforms/cover.py b/src/calibre/ebooks/oeb/transforms/cover.py index 63b89c9b63..8fbb7bcc24 100644 --- a/src/calibre/ebooks/oeb/transforms/cover.py +++ b/src/calibre/ebooks/oeb/transforms/cover.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__ = '2010, Kovid Goyal ' @@ -16,7 +17,7 @@ from polyglot.urllib import unquote class CoverManager(object): - SVG_TEMPLATE = textwrap.dedent(u'''\ + SVG_TEMPLATE = textwrap.dedent('''\ @@ -40,7 +41,7 @@ class CoverManager(object): ''') - NONSVG_TEMPLATE = textwrap.dedent(u'''\ + NONSVG_TEMPLATE = textwrap.dedent('''\ @@ -103,7 +104,7 @@ class CoverManager(object): pass img_data = create_cover(title, authors, series, series_index) id, href = self.oeb.manifest.generate('cover', - u'cover_image.jpg') + 'cover_image.jpg') item = self.oeb.manifest.add(id, href, guess_type('t.jpg')[0], data=img_data) m.clear('cover') @@ -145,15 +146,15 @@ class CoverManager(object): self.svg_template = self.svg_template.replace('__viewbox__', '0 0 %d %d'%(width, height)) self.svg_template = self.svg_template.replace('__width__', - str(width)) + unicode_type(width)) self.svg_template = self.svg_template.replace('__height__', - str(height)) + unicode_type(height)) if href is not None: templ = self.non_svg_template if self.no_svg_cover \ else self.svg_template tp = templ%unquote(href) - id, href = m.generate('titlepage', u'titlepage.xhtml') + id, href = m.generate('titlepage', 'titlepage.xhtml') item = m.add(id, href, guess_type('t.xhtml')[0], data=etree.fromstring(tp)) else: diff --git a/src/calibre/ebooks/oeb/transforms/filenames.py b/src/calibre/ebooks/oeb/transforms/filenames.py index a7959e70fc..1539663370 100644 --- a/src/calibre/ebooks/oeb/transforms/filenames.py +++ b/src/calibre/ebooks/oeb/transforms/filenames.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__ = '2010, Kovid Goyal ' @@ -96,7 +97,7 @@ class UniqueFilenames(object): # {{{ self.opts = opts self.oeb = oeb - self.seen_filenames = set([]) + self.seen_filenames = set() self.rename_map = {} for item in list(oeb.manifest.items): diff --git a/src/calibre/ebooks/oeb/transforms/guide.py b/src/calibre/ebooks/oeb/transforms/guide.py index 104a2fc1d5..e935d79cd4 100644 --- a/src/calibre/ebooks/oeb/transforms/guide.py +++ b/src/calibre/ebooks/oeb/transforms/guide.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 with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' diff --git a/src/calibre/ebooks/oeb/transforms/htmltoc.py b/src/calibre/ebooks/oeb/transforms/htmltoc.py index a7a84dd8b3..bab65763b8 100644 --- a/src/calibre/ebooks/oeb/transforms/htmltoc.py +++ b/src/calibre/ebooks/oeb/transforms/htmltoc.py @@ -1,7 +1,7 @@ ''' HTML-TOC-adding transform. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' @@ -9,6 +9,7 @@ __copyright__ = '2008, Marshall T. Vandegrift ' from calibre.ebooks.oeb.base import XML, XHTML, XHTML_NS from calibre.ebooks.oeb.base import XHTML_MIME, CSS_MIME from calibre.ebooks.oeb.base import element, XPath +from polyglot.builtins import unicode_type __all__ = ['HTMLTOCAdder'] @@ -94,7 +95,7 @@ class HTMLTOCAdder(object): style = 'nested' id, css_href = oeb.manifest.generate('tocstyle', 'tocstyle.css') oeb.manifest.add(id, css_href, CSS_MIME, data=STYLE_CSS[style]) - language = str(oeb.metadata.language[0]) + language = unicode_type(oeb.metadata.language[0]) contents = element(None, XHTML('html'), nsmap={None: XHTML_NS}, attrib={XML('lang'): language}) head = element(contents, XHTML('head')) diff --git a/src/calibre/ebooks/oeb/transforms/linearize_tables.py b/src/calibre/ebooks/oeb/transforms/linearize_tables.py index 99d1cf8417..e86d4dd18e 100644 --- a/src/calibre/ebooks/oeb/transforms/linearize_tables.py +++ b/src/calibre/ebooks/oeb/transforms/linearize_tables.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 with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' diff --git a/src/calibre/ebooks/oeb/transforms/manglecase.py b/src/calibre/ebooks/oeb/transforms/manglecase.py index 9269a90084..c3a198949c 100644 --- a/src/calibre/ebooks/oeb/transforms/manglecase.py +++ b/src/calibre/ebooks/oeb/transforms/manglecase.py @@ -1,7 +1,7 @@ ''' CSS case-mangling transform. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/oeb/transforms/rasterize.py b/src/calibre/ebooks/oeb/transforms/rasterize.py index 35a96df60f..83651b23e5 100644 --- a/src/calibre/ebooks/oeb/transforms/rasterize.py +++ b/src/calibre/ebooks/oeb/transforms/rasterize.py @@ -1,7 +1,7 @@ ''' SVG rasterization transform. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' @@ -77,7 +77,7 @@ class SVGRasterizer(object): logger.info('Found SVG image height in %, trying to convert...') try: h = float(image.get('height').replace('%', ''))/100. - image.set('height', str(h*sizes[1])) + image.set('height', unicode_type(h*sizes[1])) except: logger.exception('Failed to convert percentage height:', image.get('height')) @@ -101,7 +101,7 @@ class SVGRasterizer(object): buffer = QBuffer(array) buffer.open(QIODevice.WriteOnly) image.save(buffer, format) - return str(array) + return array.data() def dataize_manifest(self): for item in self.oeb.manifest.values(): @@ -121,10 +121,10 @@ class SVGRasterizer(object): if abshref not in hrefs: continue linkee = hrefs[abshref] - data = str(linkee) + data = unicode_type(linkee) ext = what(None, data) or 'jpg' with PersistentTemporaryFile(suffix='.'+ext) as pt: - pt.write(data) + pt.write(data.encode('utf-8')) self.temp_files.append(pt.name) elem.attrib[XLINK('href')] = pt.name return svg @@ -182,7 +182,7 @@ class SVGRasterizer(object): height = style['height'] width = (width / 72) * self.profile.dpi height = (height / 72) * self.profile.dpi - data = QByteArray(str(svgitem)) + data = QByteArray(unicode_type(svgitem).encode('utf-8')) svg = QSvgRenderer(data) size = svg.defaultSize() size.scale(width, height, Qt.KeepAspectRatio) @@ -202,7 +202,7 @@ class SVGRasterizer(object): buffer = QBuffer(array) buffer.open(QIODevice.WriteOnly) image.save(buffer, 'PNG') - data = str(array) + data = array.data() manifest = self.oeb.manifest href = os.path.splitext(svgitem.href)[0] + '.png' id, href = manifest.generate(svgitem.id, href) diff --git a/src/calibre/ebooks/oeb/transforms/rescale.py b/src/calibre/ebooks/oeb/transforms/rescale.py index f3c1c95b37..214c89509e 100644 --- a/src/calibre/ebooks/oeb/transforms/rescale.py +++ b/src/calibre/ebooks/oeb/transforms/rescale.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 with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' @@ -30,8 +30,8 @@ class RescaleImages(object): page_width, page_height = self.opts.dest.comic_screen_size else: page_width, page_height = self.opts.dest.width, self.opts.dest.height - page_width -= (self.opts.margin_left + self.opts.margin_right) * self.opts.dest.dpi/72. - page_height -= (self.opts.margin_top + self.opts.margin_bottom) * self.opts.dest.dpi/72. + page_width -= (self.opts.margin_left + self.opts.margin_right) * self.opts.dest.dpi/72 + page_height -= (self.opts.margin_top + self.opts.margin_bottom) * self.opts.dest.dpi/72 for item in self.oeb.manifest: if item.media_type.startswith('image'): diff --git a/src/calibre/ebooks/oeb/writer.py b/src/calibre/ebooks/oeb/writer.py index 5f82a05011..953c4924da 100644 --- a/src/calibre/ebooks/oeb/writer.py +++ b/src/calibre/ebooks/oeb/writer.py @@ -1,7 +1,7 @@ ''' Directory output OEBBook writer. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/gui2/dialogs/plugin_updater.py b/src/calibre/gui2/dialogs/plugin_updater.py index 275e77dadf..fe16c4a5d3 100644 --- a/src/calibre/gui2/dialogs/plugin_updater.py +++ b/src/calibre/gui2/dialogs/plugin_updater.py @@ -357,7 +357,7 @@ class DisplayPluginModel(QAbstractTableModel): def _get_display_version(self, version): if version is None: return '' - return '.'.join([str(v) for v in list(version)]) + return '.'.join([unicode_type(v) for v in list(version)]) def _get_status(self, display_plugin): if not display_plugin.is_valid_platform(): diff --git a/src/calibre/utils/speedup.c b/src/calibre/utils/speedup.c index d270f3678c..86767d5aa0 100644 --- a/src/calibre/utils/speedup.c +++ b/src/calibre/utils/speedup.c @@ -139,7 +139,7 @@ speedup_fdopen(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "iss|i", &fd, &name, &mode, &bufsize)) return NULL; #if PY_MAJOR_VERSION >= 3 - ans = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1); + ans = PyFile_FromFd(fd, NULL, mode, bufsize, NULL, NULL, NULL, 1); #else fp = fdopen(fd, mode); if (fp == NULL) return PyErr_SetFromErrno(PyExc_OSError);