diff --git a/src/calibre/ebooks/conversion/plugins/comic_input.py b/src/calibre/ebooks/conversion/plugins/comic_input.py index 13301d96f8..f47c200082 100644 --- a/src/calibre/ebooks/conversion/plugins/comic_input.py +++ b/src/calibre/ebooks/conversion/plugins/comic_input.py @@ -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' @@ -102,7 +103,8 @@ class ComicInput(InputFormatPlugin): '%s is not a valid comic collection' ' no comics.txt was found in the file') %stream.name) - raw = open('comics.txt', 'rb').read() + with open('comics.txt', 'rb') as f: + raw = f.read() if raw.startswith(codecs.BOM_UTF16_BE): raw = raw.decode('utf-16-be')[1:] elif raw.startswith(codecs.BOM_UTF16_LE): @@ -176,7 +178,7 @@ class ComicInput(InputFormatPlugin): comics = [] for i, x in enumerate(comics_): title, fname = x - cdir = u'comic_%d'%(i+1) if len(comics_) > 1 else u'.' + cdir = 'comic_%d'%(i+1) if len(comics_) > 1 else '.' cdir = os.path.abspath(cdir) if not os.path.exists(cdir): os.makedirs(cdir) @@ -230,14 +232,14 @@ class ComicInput(InputFormatPlugin): _('Page')+' %d'%(i+1), play_order=po) po += 1 opf.set_toc(toc) - m, n = open(u'metadata.opf', 'wb'), open('toc.ncx', 'wb') - opf.render(m, n, u'toc.ncx') - return os.path.abspath(u'metadata.opf') + with open('metadata.opf', 'wb') as m, open('toc.ncx', 'wb') as n: + opf.render(m, n, 'toc.ncx') + return os.path.abspath('metadata.opf') def create_wrappers(self, pages): from calibre.ebooks.oeb.base import XHTML_NS wrappers = [] - WRAPPER = textwrap.dedent(u'''\ + WRAPPER = textwrap.dedent('''\ @@ -258,7 +260,7 @@ class ComicInput(InputFormatPlugin): dir = os.path.dirname(pages[0]) for i, page in enumerate(pages): wrapper = WRAPPER%(XHTML_NS, i+1, os.path.basename(page), i+1) - page = os.path.join(dir, u'page_%d.xhtml'%(i+1)) + page = os.path.join(dir, 'page_%d.xhtml'%(i+1)) with open(page, 'wb') as f: f.write(wrapper.encode('utf-8')) wrappers.append(page) diff --git a/src/calibre/ebooks/conversion/plugins/epub_output.py b/src/calibre/ebooks/conversion/plugins/epub_output.py index 7af7f9ed4c..aaa8739225 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_output.py +++ b/src/calibre/ebooks/conversion/plugins/epub_output.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 ' @@ -233,7 +233,7 @@ class EPUBOutput(OutputFormatPlugin): if uuid is None: self.log.warn('No UUID identifier found') from uuid import uuid4 - uuid = str(uuid4()) + uuid = unicode_type(uuid4()) oeb.metadata.add('identifier', uuid, scheme='uuid', id=uuid) if encrypted_fonts and not uuid.startswith('urn:uuid:'): @@ -244,7 +244,7 @@ class EPUBOutput(OutputFormatPlugin): if unicode_type(x) == uuid: x.content = 'urn:uuid:'+uuid - with TemporaryDirectory(u'_epub_output') as tdir: + with TemporaryDirectory('_epub_output') as tdir: from calibre.customize.ui import plugin_for_output_format metadata_xml = None extra_entries = [] @@ -252,7 +252,7 @@ class EPUBOutput(OutputFormatPlugin): if self.opts.output_profile.epub_periodical_format == 'sony': from calibre.ebooks.epub.periodical import sony_metadata metadata_xml, atom_xml = sony_metadata(oeb) - extra_entries = [(u'atom.xml', 'application/atom+xml', atom_xml)] + extra_entries = [('atom.xml', 'application/atom+xml', atom_xml)] oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb, tdir, input_plugin, opts, log) opf = [x for x in os.listdir(tdir) if x.endswith('.opf')][0] @@ -338,7 +338,7 @@ class EPUBOutput(OutputFormatPlugin): self.log.warn('Font', path, 'is invalid, ignoring') if not isinstance(uri, unicode_type): uri = uri.decode('utf-8') - fonts.append(u''' + fonts.append(''' @@ -347,13 +347,13 @@ class EPUBOutput(OutputFormatPlugin): '''%(uri.replace('"', '\\"'))) if fonts: - ans = ''' ''' - ans += (u'\n'.join(fonts)).encode('utf-8') - ans += '\n' + ans += '\n'.join(fonts).encode('utf-8') + ans += b'\n' return ans # }}} @@ -367,7 +367,8 @@ class EPUBOutput(OutputFormatPlugin): if tag.tail: tag.tail = tag.tail.strip() compressed = etree.tostring(tree.getroot(), encoding='utf-8') - open(ncx_path, 'wb').write(compressed) + with open(ncx_path, 'wb') as f: + f.write(compressed) # }}} def workaround_ade_quirks(self): # {{{ @@ -430,7 +431,7 @@ class EPUBOutput(OutputFormatPlugin): if priortext: priortext = priortext.strip() br.tag = XHTML('p') - br.text = u'\u00a0' + br.text = '\u00a0' style = br.get('style', '').split(';') style = list(filter(None, map(lambda x: x.strip(), style))) style.append('margin:0pt; border:0pt') @@ -483,14 +484,14 @@ class EPUBOutput(OutputFormatPlugin): tag.tag = XHTML('div') # ADE fails to render non breaking hyphens/soft hyphens/zero width spaces - special_chars = re.compile(u'[\u200b\u00ad]') + special_chars = re.compile('[\u200b\u00ad]') for elem in root.iterdescendants('*'): if elem.text: elem.text = special_chars.sub('', elem.text) - elem.text = elem.text.replace(u'\u2011', '-') + elem.text = elem.text.replace('\u2011', '-') if elem.tail: elem.tail = special_chars.sub('', elem.tail) - elem.tail = elem.tail.replace(u'\u2011', '-') + elem.tail = elem.tail.replace('\u2011', '-') if stylesheet is not None: # ADE doesn't render lists correctly if they have left margins diff --git a/src/calibre/ebooks/conversion/plugins/fb2_input.py b/src/calibre/ebooks/conversion/plugins/fb2_input.py index 9ab3147e52..6ac300b655 100644 --- a/src/calibre/ebooks/conversion/plugins/fb2_input.py +++ b/src/calibre/ebooks/conversion/plugins/fb2_input.py @@ -1,4 +1,5 @@ -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Anatoly Shipitsin ' """ @@ -125,9 +126,9 @@ class FB2Input(InputFormatPlugin): src = img.get('src') img.set('src', self.binary_map.get(src, src)) index = transform.tostring(result) - with open(u'index.xhtml', 'wb') as f: + with open('index.xhtml', 'wb') as f: f.write(index.encode('utf-8')) - with open(u'inline-styles.css', 'wb') as f: + with open('inline-styles.css', 'wb') as f: f.write(css.encode('utf-8')) stream.seek(0) mi = get_metadata(stream, 'fb2') @@ -137,9 +138,9 @@ class FB2Input(InputFormatPlugin): mi.authors = [_('Unknown')] cpath = None if mi.cover_data and mi.cover_data[1]: - with open(u'fb2_cover_calibre_mi.jpg', 'wb') as f: + with open('fb2_cover_calibre_mi.jpg', 'wb') as f: f.write(mi.cover_data[1]) - cpath = os.path.abspath(u'fb2_cover_calibre_mi.jpg') + cpath = os.path.abspath('fb2_cover_calibre_mi.jpg') else: for img in doc.xpath('//f:coverpage/f:image', namespaces=NAMESPACES): href = img.get('{%s}href'%XLINK_NS, img.get('href', None)) @@ -152,12 +153,12 @@ class FB2Input(InputFormatPlugin): opf = OPFCreator(getcwd(), mi) entries = [(f2, guess_type(f2)[0]) for f2 in os.listdir(u'.')] opf.create_manifest(entries) - opf.create_spine([u'index.xhtml']) + opf.create_spine(['index.xhtml']) if cpath: opf.guide.set_cover(cpath) - with open(u'metadata.opf', 'wb') as f: + with open('metadata.opf', 'wb') as f: opf.render(f) - return os.path.join(getcwd(), u'metadata.opf') + return os.path.join(getcwd(), 'metadata.opf') def extract_embedded_content(self, doc): from calibre.ebooks.fb2 import base64_decode diff --git a/src/calibre/ebooks/conversion/plugins/html_input.py b/src/calibre/ebooks/conversion/plugins/html_input.py index a26e86be0a..a714036ef0 100644 --- a/src/calibre/ebooks/conversion/plugins/html_input.py +++ b/src/calibre/ebooks/conversion/plugins/html_input.py @@ -120,7 +120,7 @@ class HTMLInput(InputFormatPlugin): if not metadata.language: l = canonicalize_lang(getattr(opts, 'language', None)) if not l: - oeb.logger.warn(u'Language not specified') + oeb.logger.warn('Language not specified') l = get_lang().replace('_', '-') metadata.add('language', l) if not metadata.creator: @@ -135,7 +135,7 @@ class HTMLInput(InputFormatPlugin): if not metadata.title: oeb.logger.warn('Title not specified') metadata.add('title', self.oeb.translate(__('Unknown'))) - bookid = str(uuid.uuid4()) + bookid = unicode_type(uuid.uuid4()) metadata.add('identifier', bookid, id='uuid_id', scheme='uuid') for ident in metadata.identifier: if 'id' in ident.attrib: diff --git a/src/calibre/ebooks/conversion/plugins/html_output.py b/src/calibre/ebooks/conversion/plugins/html_output.py index ba2e922267..b45c3c61f0 100644 --- a/src/calibre/ebooks/conversion/plugins/html_output.py +++ b/src/calibre/ebooks/conversion/plugins/html_output.py @@ -1,4 +1,5 @@ -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL 3' __copyright__ = '2010, Fabian Grassl ' __docformat__ = 'restructuredtext en' @@ -91,17 +92,20 @@ class HTMLOutput(OutputFormatPlugin): # read template files if opts.template_html_index is not None: - template_html_index_data = open(opts.template_html_index, 'rb').read() + with open(opts.template_html_index, 'rb') as f: + template_html_index_data = f.read() else: template_html_index_data = P('templates/html_export_default_index.tmpl', data=True) if opts.template_html is not None: - template_html_data = open(opts.template_html, 'rb').read() + with open(opts.template_html, 'rb') as f: + template_html_data = f.read() else: template_html_data = P('templates/html_export_default.tmpl', data=True) if opts.template_css is not None: - template_css_data = open(opts.template_css, 'rb').read() + with open(opts.template_css, 'rb') as f: + template_css_data = f.read() else: template_css_data = P('templates/html_export_default.css', data=True) diff --git a/src/calibre/ebooks/conversion/plugins/snb_input.py b/src/calibre/ebooks/conversion/plugins/snb_input.py index c250466ff8..007adea1b9 100644 --- a/src/calibre/ebooks/conversion/plugins/snb_input.py +++ b/src/calibre/ebooks/conversion/plugins/snb_input.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL 3' __copyright__ = '2010, Li Fanxi ' @@ -9,12 +10,13 @@ import os from calibre.customize.conversion import InputFormatPlugin from calibre.ptempfile import TemporaryDirectory from calibre.utils.filenames import ascii_filename +from polyglot.builtins import unicode_type -HTML_TEMPLATE = u'%s\n%s\n' +HTML_TEMPLATE = '%s\n%s\n' def html_encode(s): - return s.replace(u'&', u'&').replace(u'<', u'<').replace(u'>', u'>').replace(u'"', u'"').replace(u"'", u''').replace(u'\n', u'
').replace(u' ', u' ') # noqa + return s.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''').replace('\n', '
').replace(' ', ' ') # noqa class SNBInput(InputFormatPlugin): @@ -43,7 +45,7 @@ class SNBInput(InputFormatPlugin): except: raise ValueError("Invalid SNB file") if not snbFile.IsValid(): - log.debug("Invaild SNB file") + log.debug("Invalid SNB file") raise ValueError("Invalid SNB file") log.debug("Handle meta data ...") from calibre.ebooks.conversion.plumber import create_oebbook @@ -74,7 +76,7 @@ class SNBInput(InputFormatPlugin): if d['cover'] != '': oeb.guide.add('cover', 'Cover', d['cover']) - bookid = str(uuid.uuid4()) + bookid = unicode_type(uuid.uuid4()) oeb.metadata.add('identifier', bookid, id='uuid_id', scheme='uuid') for ident in oeb.metadata.identifier: if 'id' in ident.attrib: @@ -96,15 +98,14 @@ class SNBInput(InputFormatPlugin): if data is None: continue snbc = etree.fromstring(data) - outputFile = open(os.path.join(tdir, fname), 'wb') lines = [] for line in snbc.find('.//body'): if line.tag == 'text': - lines.append(u'

%s

' % html_encode(line.text)) + lines.append('

%s

' % html_encode(line.text)) elif line.tag == 'img': - lines.append(u'

' % html_encode(line.text)) - outputFile.write((HTML_TEMPLATE % (chapterName, u'\n'.join(lines))).encode('utf-8', 'replace')) - outputFile.close() + lines.append('

' % html_encode(line.text)) + with open(os.path.join(tdir, fname), 'wb') as f: + f.write((HTML_TEMPLATE % (chapterName, '\n'.join(lines))).encode('utf-8', 'replace')) oeb.toc.add(ch.text, fname) id, href = oeb.manifest.generate(id='html', href=ascii_filename(fname)) diff --git a/src/calibre/ebooks/conversion/plugins/snb_output.py b/src/calibre/ebooks/conversion/plugins/snb_output.py index 5b4917fc7b..460e806999 100644 --- a/src/calibre/ebooks/conversion/plugins/snb_output.py +++ b/src/calibre/ebooks/conversion/plugins/snb_output.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL 3' __copyright__ = '2010, Li Fanxi ' @@ -113,9 +114,8 @@ class SNBOutput(OutputFormatPlugin): etree.SubElement(headTree, "cover").text = ProcessFileName(href) else: etree.SubElement(headTree, "cover") - bookInfoFile = open(os.path.join(snbfDir, 'book.snbf'), 'wb') - bookInfoFile.write(etree.tostring(bookInfoTree, pretty_print=True, encoding='utf-8')) - bookInfoFile.close() + with open(os.path.join(snbfDir, 'book.snbf'), 'wb') as f: + f.write(etree.tostring(bookInfoTree, pretty_print=True, encoding='utf-8')) # Output TOC tocInfoTree = etree.Element("toc-snbf") @@ -168,9 +168,8 @@ class SNBOutput(OutputFormatPlugin): etree.SubElement(tocHead, "chapters").text = '%d' % len(tocBody) - tocInfoFile = open(os.path.join(snbfDir, 'toc.snbf'), 'wb') - tocInfoFile.write(etree.tostring(tocInfoTree, pretty_print=True, encoding='utf-8')) - tocInfoFile.close() + with open(os.path.join(snbfDir, 'toc.snbf'), 'wb') as f: + f.write(etree.tostring(tocInfoTree, pretty_print=True, encoding='utf-8')) # Output Files oldTree = None @@ -185,9 +184,8 @@ class SNBOutput(OutputFormatPlugin): else: if oldTree is not None and mergeLast: log.debug('Output the modified chapter again: %s' % lastName) - outputFile = open(os.path.join(snbcDir, lastName), 'wb') - outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) - outputFile.close() + with open(os.path.join(snbcDir, lastName), 'wb') as f: + f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) mergeLast = False log.debug('Converting %s to snbc...' % item.href) @@ -201,9 +199,8 @@ class SNBOutput(OutputFormatPlugin): postfix = '_' + subName lastName = ProcessFileName(item.href + postfix + ".snbc") oldTree = snbcTrees[subName] - outputFile = open(os.path.join(snbcDir, lastName), 'wb') - outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) - outputFile.close() + with open(os.path.join(snbcDir, lastName), 'wb') as f: + f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) else: log.debug('Merge %s with last TOC item...' % item.href) snbwriter.merge_content(oldTree, oeb_book, item, [('', _("Start"))], opts) @@ -211,9 +208,8 @@ class SNBOutput(OutputFormatPlugin): # Output the last one if needed log.debug('Output the last modified chapter again: %s' % lastName) if oldTree is not None and mergeLast: - outputFile = open(os.path.join(snbcDir, lastName), 'wb') - outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) - outputFile.close() + with open(os.path.join(snbcDir, lastName), 'wb') as f: + f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8')) mergeLast = False for item in m: @@ -248,7 +244,7 @@ class SNBOutput(OutputFormatPlugin): # TODO : intelligent image rotation # img = img.rotate(90) # x,y = y,x - img = resize_image(img, x / scale, y / scale) + img = resize_image(img, x // scale, y // scale) with lopen(imagePath, 'wb') as f: f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1])) diff --git a/src/calibre/ebooks/conversion/plugins/txt_input.py b/src/calibre/ebooks/conversion/plugins/txt_input.py index e9f6fa54fe..66357c5fb7 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_input.py +++ b/src/calibre/ebooks/conversion/plugins/txt_input.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL 3' __copyright__ = '2009, John Schember ' diff --git a/src/calibre/ebooks/docx/fields.py b/src/calibre/ebooks/docx/fields.py index 3a6be921ef..7400bcde51 100644 --- a/src/calibre/ebooks/docx/fields.py +++ b/src/calibre/ebooks/docx/fields.py @@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal ' import re from calibre.ebooks.docx.index import process_index, polish_index_markup -from polyglot.builtins import iteritems +from polyglot.builtins import iteritems, native_string_type class Field(object): @@ -75,7 +75,7 @@ def parser(name, field_map, default_field_name=None): ans.pop(null, None) return ans - parse.__name__ = str('parse_' + name) + parse.__name__ = native_string_type('parse_' + name) return parse diff --git a/src/calibre/ebooks/docx/numbering.py b/src/calibre/ebooks/docx/numbering.py index a75ca830e1..da33d6a584 100644 --- a/src/calibre/ebooks/docx/numbering.py +++ b/src/calibre/ebooks/docx/numbering.py @@ -14,7 +14,7 @@ from lxml.html.builder import OL, UL, SPAN from calibre.ebooks.docx.block_styles import ParagraphStyle from calibre.ebooks.docx.char_styles import RunStyle, inherit from calibre.ebooks.metadata import roman -from polyglot.builtins import iteritems +from polyglot.builtins import iteritems, unicode_type STYLE_MAP = { 'aiueo': 'hiragana', @@ -291,7 +291,7 @@ class Numbering(object): seen_instances.add(num_id) p.tag = 'li' p.set('value', '%s' % counter[ilvl]) - p.set('list-lvl', str(ilvl)) + p.set('list-lvl', unicode_type(ilvl)) p.set('list-id', num_id) if lvl.num_template is not None: val = lvl.format_template(counter, ilvl, lvl.num_template) diff --git a/src/calibre/ebooks/docx/to_html.py b/src/calibre/ebooks/docx/to_html.py index 067e6d6bc9..42b55020e1 100644 --- a/src/calibre/ebooks/docx/to_html.py +++ b/src/calibre/ebooks/docx/to_html.py @@ -28,7 +28,7 @@ from calibre.ebooks.docx.fields import Fields from calibre.ebooks.docx.settings import Settings from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1 -from polyglot.builtins import iteritems, itervalues, filter, getcwd, map +from polyglot.builtins import iteritems, itervalues, filter, getcwd, map, unicode_type NBSP = '\xa0' @@ -480,7 +480,7 @@ class Convert(object): current_hyperlink = x elif x.tag.endswith('}instrText') and x.text and x.text.strip().startswith('TOC '): old_anchor = current_anchor - anchor = str(uuid.uuid4()) + anchor = unicode_type(uuid.uuid4()) self.anchor_map[anchor] = current_anchor = generate_anchor('toc', frozenset(itervalues(self.anchor_map))) self.toc_anchor = current_anchor if old_anchor is not None: @@ -693,9 +693,9 @@ class Convert(object): ans.append(text.elem) ans[-1].set('class', 'tab') elif self.namespace.is_tag(child, 'w:noBreakHyphen'): - text.buf.append(u'\u2011') + text.buf.append('\u2011') elif self.namespace.is_tag(child, 'w:softHyphen'): - text.buf.append(u'\u00ad') + text.buf.append('\u00ad') if text.buf: setattr(text.elem, text.attr, ''.join(text.buf)) diff --git a/src/calibre/ebooks/docx/writer/container.py b/src/calibre/ebooks/docx/writer/container.py index 791e7527da..e28e5c41cb 100644 --- a/src/calibre/ebooks/docx/writer/container.py +++ b/src/calibre/ebooks/docx/writer/container.py @@ -18,7 +18,7 @@ from calibre.utils.date import utcnow from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1 from calibre.utils.zipfile import ZipFile from calibre.ebooks.pdf.render.common import PAPER_SIZES -from polyglot.builtins import iteritems, map +from polyglot.builtins import iteritems, map, unicode_type def xml2str(root, pretty_print=False, with_tail=False): @@ -65,9 +65,9 @@ def create_skeleton(opts, namespaces=None): def margin(which): val = page_margin(opts, which) - return w(which), str(int(val * 20)) + return w(which), unicode_type(int(val * 20)) body.append(E.sectPr( - E.pgSz(**{w('w'):str(width), w('h'):str(height)}), + E.pgSz(**{w('w'):unicode_type(width), w('h'):unicode_type(height)}), E.pgMar(**dict(map(margin, 'left top right bottom'.split()))), E.cols(**{w('space'):'720'}), E.docGrid(**{w('linePitch'):"360"}), @@ -243,7 +243,7 @@ class DOCX(object): namespaces = self.namespace.namespaces 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')) - ts = utcnow().isoformat(str('T')).rpartition('.')[0] + 'Z' + ts = utcnow().isoformat(unicode_type('T')).rpartition('.')[0] + 'Z' for x in 'created modified'.split(): x = cp.makeelement('{%s}%s' % (namespaces['dcterms'], x), **{'{%s}type' % namespaces['xsi']:'dcterms:W3CDTF'}) x.text = ts diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index dea920d55b..a29ecef2f4 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -101,7 +101,7 @@ class TextRun(object): for text, preserve_whitespace, bookmark in self.texts: if bookmark is not None: bid = links_manager.bookmark_id - makeelement(r, 'w:bookmarkStart', w_id=str(bid), w_name=bookmark) + makeelement(r, 'w:bookmarkStart', w_id=unicode_type(bid), w_name=bookmark) if text is None: makeelement(r, 'w:br', w_clear=preserve_whitespace) elif hasattr(text, 'xpath'): @@ -112,7 +112,7 @@ class TextRun(object): if preserve_whitespace: t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve') if bookmark is not None: - makeelement(r, 'w:bookmarkEnd', w_id=str(bid)) + makeelement(r, 'w:bookmarkEnd', w_id=unicode_type(bid)) def __repr__(self): return repr(self.texts) @@ -207,7 +207,7 @@ class Block(object): p = makeelement(body, 'w:p') end_bookmarks = [] for bmark in self.bookmarks: - end_bookmarks.append(str(self.links_manager.bookmark_id)) + end_bookmarks.append(unicode_type(self.links_manager.bookmark_id)) makeelement(p, 'w:bookmarkStart', w_id=end_bookmarks[-1], w_name=bmark) if self.block_lang: rpr = makeelement(p, 'w:rPr') @@ -220,8 +220,8 @@ class Block(object): self.float_spec.serialize(self, ppr) if self.numbering_id is not None: numpr = makeelement(ppr, 'w:numPr') - makeelement(numpr, 'w:ilvl', w_val=str(self.numbering_id[1])) - makeelement(numpr, 'w:numId', w_val=str(self.numbering_id[0])) + makeelement(numpr, 'w:ilvl', w_val=unicode_type(self.numbering_id[1])) + makeelement(numpr, 'w:numId', w_val=unicode_type(self.numbering_id[0])) if self.linked_style is not None: makeelement(ppr, 'w:pStyle', w_val=self.linked_style.id) elif self.style.id: diff --git a/src/calibre/ebooks/docx/writer/links.py b/src/calibre/ebooks/docx/writer/links.py index bf89f1fc72..ba8bb8aeb4 100644 --- a/src/calibre/ebooks/docx/writer/links.py +++ b/src/calibre/ebooks/docx/writer/links.py @@ -9,6 +9,7 @@ import posixpath, re from uuid import uuid4 from calibre.utils.filenames import ascii_text +from polyglot.builtins import unicode_type from polyglot.urllib import urlparse @@ -35,7 +36,7 @@ class TOCItem(object): p = makeelement(body, 'w:p', append=False) ppr = makeelement(p, 'w:pPr') makeelement(ppr, 'w:pStyle', w_val="Normal") - makeelement(ppr, 'w:ind', w_left='0', w_firstLineChars='0', w_firstLine='0', w_leftChars=str(200 * self.level)) + makeelement(ppr, 'w:ind', w_left='0', w_firstLineChars='0', w_firstLine='0', w_leftChars=unicode_type(200 * self.level)) if self.is_first: makeelement(ppr, 'w:pageBreakBefore', w_val='off') r = makeelement(p, 'w:r') @@ -69,7 +70,7 @@ class LinksManager(object): self.namespace = namespace self.log = log self.document_relationships = document_relationships - self.top_anchor = type('')(uuid4().hex) + self.top_anchor = uuid4().hex self.anchor_map = {} self.used_bookmark_names = set() self.bmark_id = 0 diff --git a/src/calibre/ebooks/docx/writer/lists.py b/src/calibre/ebooks/docx/writer/lists.py index c71ebf9cb7..259cfb3539 100644 --- a/src/calibre/ebooks/docx/writer/lists.py +++ b/src/calibre/ebooks/docx/writer/lists.py @@ -8,7 +8,7 @@ __copyright__ = '2015, Kovid Goyal ' from collections import defaultdict from operator import attrgetter -from polyglot.builtins import iteritems, itervalues +from polyglot.builtins import iteritems, itervalues, unicode_type LIST_STYLES = frozenset( 'disc circle square decimal decimal-leading-zero lower-roman upper-roman' @@ -83,7 +83,7 @@ class NumberingDefinition(object): def serialize(self, parent): makeelement = self.namespace.makeelement - an = makeelement(parent, 'w:abstractNum', w_abstractNumId=str(self.num_id)) + an = makeelement(parent, 'w:abstractNum', w_abstractNumId=unicode_type(self.num_id)) makeelement(an, 'w:multiLevelType', w_val='hybridMultilevel') makeelement(an, 'w:name', w_val='List %d' % (self.num_id + 1)) for level in self.levels: @@ -114,12 +114,12 @@ class Level(object): return hash((self.start, self.num_fmt, self.lvl_text)) def serialize(self, parent, makeelement): - lvl = makeelement(parent, 'w:lvl', w_ilvl=str(self.ilvl)) - makeelement(lvl, 'w:start', w_val=str(self.start)) + lvl = makeelement(parent, 'w:lvl', w_ilvl=unicode_type(self.ilvl)) + makeelement(lvl, 'w:start', w_val=unicode_type(self.start)) makeelement(lvl, 'w:numFmt', w_val=self.num_fmt) makeelement(lvl, 'w:lvlText', w_val=self.lvl_text) makeelement(lvl, 'w:lvlJc', w_val='left') - makeelement(makeelement(lvl, 'w:pPr'), 'w:ind', w_hanging='360', w_left=str(1152 + self.ilvl * 360)) + makeelement(makeelement(lvl, 'w:pPr'), 'w:ind', w_hanging='360', w_left=unicode_type(1152 + self.ilvl * 360)) if self.num_fmt == 'bullet': ff = {'\uf0b7':'Symbol', '\uf0a7':'Wingdings'}.get(self.lvl_text, 'Courier New') makeelement(makeelement(lvl, 'w:rPr'), 'w:rFonts', w_ascii=ff, w_hAnsi=ff, w_hint="default") @@ -165,5 +165,5 @@ class ListsManager(object): defn.serialize(parent) makeelement = self.namespace.makeelement for defn in self.definitions: - n = makeelement(parent, 'w:num', w_numId=str(defn.num_id + 1)) - makeelement(n, 'w:abstractNumId', w_val=str(defn.num_id)) + n = makeelement(parent, 'w:num', w_numId=unicode_type(defn.num_id + 1)) + makeelement(n, 'w:abstractNumId', w_val=unicode_type(defn.num_id)) diff --git a/src/calibre/ebooks/docx/writer/styles.py b/src/calibre/ebooks/docx/writer/styles.py index 4cea15d599..5d5e617538 100644 --- a/src/calibre/ebooks/docx/writer/styles.py +++ b/src/calibre/ebooks/docx/writer/styles.py @@ -14,7 +14,7 @@ from lxml import etree from calibre.ebooks import parse_css_length from calibre.ebooks.docx.writer.utils import convert_color, int_or_zero from calibre.utils.localization import lang_as_iso639_1 -from polyglot.builtins import iteritems, filter +from polyglot.builtins import iteritems, filter, unicode_type from tinycss.css21 import CSS21Parser css_parser = CSS21Parser() @@ -76,7 +76,7 @@ class CombinedStyle(object): pPr = makeelement(block, 'w:pPr') self.bs.serialize_properties(pPr, normal_style.bs) if self.outline_level is not None: - makeelement(pPr, 'w:outlineLvl', w_val=str(self.outline_level + 1)) + makeelement(pPr, 'w:outlineLvl', w_val=unicode_type(self.outline_level + 1)) rPr = makeelement(block, 'w:rPr') self.rs.serialize_properties(rPr, normal_style.rs) @@ -109,16 +109,16 @@ class FloatSpec(object): def serialize(self, block, parent): if self.is_dropcaps: - attrs = dict(w_dropCap='drop', w_lines=str(self.dropcaps_lines), w_wrap='around', w_vAnchor='text', w_hAnchor='text') + attrs = dict(w_dropCap='drop', w_lines=unicode_type(self.dropcaps_lines), w_wrap='around', w_vAnchor='text', w_hAnchor='text') else: attrs = dict( w_wrap='around', w_vAnchor='text', w_hAnchor='text', w_xAlign=self.x_align, w_y='1', - w_hSpace=str(self.h_space), w_vSpace=str(self.v_space), w_hRule=self.h_rule + w_hSpace=unicode_type(self.h_space), w_vSpace=unicode_type(self.v_space), w_hRule=self.h_rule ) if self.w is not None: - attrs['w_w'] = str(self.w) + attrs['w_w'] = unicode_type(self.w) if self.h is not None: - attrs['w_h'] = str(self.h) + attrs['w_h'] = unicode_type(self.h) self.makeelement(parent, 'w:framePr', **attrs) # Margins are already applied by the frame style, so override them to # be zero on individual blocks @@ -137,7 +137,7 @@ class FloatSpec(object): padding = getattr(self, 'padding_' + edge) width = getattr(self, 'border_%s_width' % edge) bstyle = getattr(self, 'border_%s_style' % edge) - self.makeelement(bdr, 'w:'+edge, w_space=str(padding), w_val=bstyle, w_sz=str(width), w_color=getattr(self, 'border_%s_color' % edge)) + self.makeelement(bdr, 'w:'+edge, w_space=unicode_type(padding), w_val=bstyle, w_sz=unicode_type(width), w_color=getattr(self, 'border_%s_color' % edge)) class DOCXStyle(object): @@ -233,7 +233,7 @@ class TextStyle(DOCXStyle): self.spacing = None va = css.first_vertical_align if isinstance(va, numbers.Number): - self.vertical_align = str(int(va * 2)) + self.vertical_align = unicode_type(int(va * 2)) else: val = { 'top':'superscript', 'text-top':'superscript', 'sup':'superscript', 'super':'superscript', @@ -289,9 +289,9 @@ class TextStyle(DOCXStyle): w = self.w is_normal_style = self is normal_style if is_normal_style or self.padding != normal_style.padding: - bdr.set(w('space'), str(self.padding)) + bdr.set(w('space'), unicode_type(self.padding)) if is_normal_style or self.border_width != normal_style.border_width: - bdr.set(w('sz'), str(self.border_width)) + bdr.set(w('sz'), unicode_type(self.border_width)) if is_normal_style or self.border_style != normal_style.border_style: bdr.set(w('val'), self.border_style) if is_normal_style or self.border_color != normal_style.border_color: @@ -341,7 +341,7 @@ class TextStyle(DOCXStyle): if check_attr('shadow'): rPr.append(makeelement(rPr, 'shadow', val=bmap(self.shadow))) if check_attr('spacing'): - rPr.append(makeelement(rPr, 'spacing', val=str(self.spacing or 0))) + rPr.append(makeelement(rPr, 'spacing', val=unicode_type(self.spacing or 0))) if is_normal_style: rPr.append(makeelement(rPr, 'vertAlign', val=self.vertical_align if self.vertical_align in {'superscript', 'subscript'} else 'baseline')) elif self.vertical_align != normal_style.vertical_align: @@ -379,7 +379,7 @@ class DescendantTextStyle(object): for name, attr in (('sz', 'font_size'), ('b', 'bold'), ('i', 'italic')): pval, cval = vals(attr) if pval != cval: - val = 'on' if attr in {'bold', 'italic'} else str(cval) # bold, italic are toggle properties + val = 'on' if attr in {'bold', 'italic'} else unicode_type(cval) # bold, italic are toggle properties for suffix in ('', 'Cs'): add(name + suffix, val=val) @@ -400,7 +400,7 @@ class DescendantTextStyle(object): if check('shadow'): add('shadow', val='on') # toggle property if check('spacing'): - add('spacing', val=str(child_style.spacing or 0)) + add('spacing', val=unicode_type(child_style.spacing or 0)) if check('vertical_align'): val = child_style.vertical_align if val in {'superscript', 'subscript', 'baseline'}: @@ -410,9 +410,9 @@ class DescendantTextStyle(object): bdr = {} if check('padding'): - bdr['space'] = str(child_style.padding) + bdr['space'] = unicode_type(child_style.padding) if check('border_width'): - bdr['sz'] = str(child_style.border_width) + bdr['sz'] = unicode_type(child_style.border_width) if check('border_style'): bdr['val'] = child_style.border_style if check('border_color'): @@ -536,14 +536,14 @@ class BlockStyle(DOCXStyle): e = bdr.makeelement(w(edge)) padding = getattr(self, 'padding_' + edge) if (self is normal_style and padding > 0) or (padding != getattr(normal_style, 'padding_' + edge)): - e.set(w('space'), str(padding)) + e.set(w('space'), unicode_type(padding)) width = getattr(self, 'border_%s_width' % edge) bstyle = getattr(self, 'border_%s_style' % edge) if (self is normal_style and width > 0 and bstyle != 'none' ) or width != getattr(normal_style, 'border_%s_width' % edge ) or bstyle != getattr(normal_style, 'border_%s_style' % edge): e.set(w('val'), bstyle) - e.set(w('sz'), str(width)) + e.set(w('sz'), unicode_type(width)) e.set(w('color'), getattr(self, 'border_%s_color' % edge)) if e.attrib: bdr.append(e) @@ -567,15 +567,15 @@ class BlockStyle(DOCXStyle): if css_unit in ('em', 'ex'): lines = max(0, int(css_val * (50 if css_unit == 'ex' else 100))) if (self is normal_style and lines > 0) or getter(self) != getter(normal_style): - spacing.set(w(attr + 'Lines'), str(lines)) + spacing.set(w(attr + 'Lines'), unicode_type(lines)) else: getter = attrgetter('margin_' + edge) val = getter(self) if (self is normal_style and val > 0) or val != getter(normal_style): - spacing.set(w(attr), str(val)) + spacing.set(w(attr), unicode_type(val)) if self is normal_style or self.line_height != normal_style.line_height: - spacing.set(w('line'), str(self.line_height)) + spacing.set(w('line'), unicode_type(self.line_height)) spacing.set(w('lineRule'), 'atLeast') if spacing.attrib: @@ -588,31 +588,31 @@ class BlockStyle(DOCXStyle): if css_unit in ('em', 'ex'): chars = max(0, int(css_val * (50 if css_unit == 'ex' else 100))) if (self is normal_style and chars > 0) or getter(self) != getter(normal_style): - ind.set(w(edge + 'Chars'), str(chars)) + ind.set(w(edge + 'Chars'), unicode_type(chars)) else: getter = attrgetter('margin_' + edge) val = getter(self) if (self is normal_style and val > 0) or val != getter(normal_style): - ind.set(w(edge), str(val)) + ind.set(w(edge), unicode_type(val)) ind.set(w(edge + 'Chars'), '0') # This is needed to override any declaration in the parent style css_val, css_unit = parse_css_length(self.css_text_indent) if css_unit in ('em', 'ex'): chars = int(css_val * (50 if css_unit == 'ex' else 100)) if css_val >= 0: if (self is normal_style and chars > 0) or self.css_text_indent != normal_style.css_text_indent: - ind.set(w('firstLineChars'), str(chars)) + ind.set(w('firstLineChars'), unicode_type(chars)) else: if (self is normal_style and chars < 0) or self.css_text_indent != normal_style.css_text_indent: - ind.set(w('hangingChars'), str(abs(chars))) + ind.set(w('hangingChars'), unicode_type(abs(chars))) else: val = self.text_indent if val >= 0: if (self is normal_style and val > 0) or self.text_indent != normal_style.text_indent: - ind.set(w('firstLine'), str(val)) + ind.set(w('firstLine'), unicode_type(val)) ind.set(w('firstLineChars'), '0') # This is needed to override any declaration in the parent style else: if (self is normal_style and val < 0) or self.text_indent != normal_style.text_indent: - ind.set(w('hanging'), str(abs(val))) + ind.set(w('hanging'), unicode_type(abs(val))) ind.set(w('hangingChars'), '0') if ind.attrib: pPr.append(ind) @@ -686,7 +686,7 @@ class StylesManager(object): pure_block_styles.add(bs) self.pure_block_styles = sorted(pure_block_styles, key=block_counts.__getitem__) - bnum = len(str(max(1, len(pure_block_styles) - 1))) + bnum = len(unicode_type(max(1, len(pure_block_styles) - 1))) for i, bs in enumerate(self.pure_block_styles): bs.id = bs.name = '%0{}d Block'.format(bnum) % i bs.seq = i @@ -706,7 +706,7 @@ class StylesManager(object): heading_style = styles[-1] heading_style.outline_level = i - snum = len(str(max(1, len(counts) - 1))) + snum = len(unicode_type(max(1, len(counts) - 1))) heading_styles = [] for i, (style, count) in enumerate(counts.most_common()): if i == 0: @@ -734,7 +734,7 @@ class StylesManager(object): if run.descendant_style is None: run.descendant_style = descendant_style_map[ds] = ds ds_counts[run.descendant_style] += run.style_weight - rnum = len(str(max(1, len(ds_counts) - 1))) + rnum = len(unicode_type(max(1, len(ds_counts) - 1))) for i, (text_style, count) in enumerate(ds_counts.most_common()): text_style.id = 'Text%d' % i text_style.name = '%0{}d Text'.format(rnum) % i diff --git a/src/calibre/ebooks/epub/__init__.py b/src/calibre/ebooks/epub/__init__.py index dc85db1e36..a750203b81 100644 --- a/src/calibre/ebooks/epub/__init__.py +++ b/src/calibre/ebooks/epub/__init__.py @@ -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' @@ -18,7 +19,7 @@ def rules(stylesheets): def simple_container_xml(opf_path, extra_entries=''): - return u'''\ + return '''\ @@ -36,7 +37,7 @@ def initialize_container(path_to_container, opf_name='metadata.opf', ''' rootfiles = '' for path, mimetype, _ in extra_entries: - rootfiles += u''.format( + rootfiles += ''.format( path, mimetype) CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8') zf = ZipFile(path_to_container, 'w') diff --git a/src/calibre/ebooks/epub/pages.py b/src/calibre/ebooks/epub/pages.py index 200c5e03fe..613821337e 100644 --- a/src/calibre/ebooks/epub/pages.py +++ b/src/calibre/ebooks/epub/pages.py @@ -2,7 +2,7 @@ Add page mapping information to an EPUB book. ''' -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' @@ -13,6 +13,7 @@ from itertools import count from calibre.ebooks.oeb.base import XHTML_NS from calibre.ebooks.oeb.base import OEBBook from lxml.etree import XPath +from polyglot.builtins import unicode_type NSMAP = {'h': XHTML_NS, 'html': XHTML_NS, 'xhtml': XHTML_NS} PAGE_RE = re.compile(r'page', re.IGNORECASE) @@ -32,7 +33,7 @@ def filter_name(name): def build_name_for(expr): if not expr: counter = count(1) - return lambda elem: str(next(counter)) + return lambda elem: unicode_type(next(counter)) selector = XPath(expr, namespaces=NSMAP) def name_for(elem): diff --git a/src/calibre/ebooks/epub/periodical.py b/src/calibre/ebooks/epub/periodical.py index b5943b7dff..ebadc990dc 100644 --- a/src/calibre/ebooks/epub/periodical.py +++ b/src/calibre/ebooks/epub/periodical.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 ' @@ -13,7 +14,7 @@ from calibre import strftime, prepare_string_for_xml as xml from calibre.utils.date import parse_date from polyglot.builtins import unicode_type, filter -SONY_METADATA = u'''\ +SONY_METADATA = '''\ ''' -SONY_ATOM = u'''\ +SONY_ATOM = '''\ ''' -SONY_ATOM_SECTION = u'''\ +SONY_ATOM_SECTION = '''\ {title} @@ -63,7 +64,7 @@ SONY_ATOM_SECTION = u'''\ ''' -SONY_ATOM_ENTRY = u'''\ +SONY_ATOM_ENTRY = '''\ {title} {author} @@ -86,7 +87,7 @@ def sony_metadata(oeb): publisher = __appname__ + ' ' + __version__ try: pt = unicode_type(oeb.metadata.publication_type[0]) - short_title = u':'.join(pt.split(':')[2:]) + short_title = ':'.join(pt.split(':')[2:]) except: pass @@ -116,7 +117,7 @@ def sony_metadata(oeb): try: base_id = unicode_type(list(filter(cal_id, m.identifier))[0]) except: - base_id = str(uuid4()) + base_id = unicode_type(uuid4()) toc = oeb.toc @@ -144,7 +145,7 @@ def sony_metadata(oeb): d = 1 bsectitle = sectitle while sectitle in seen_titles: - sectitle = bsectitle + ' ' + str(d) + sectitle = bsectitle + ' ' + unicode_type(d) d += 1 seen_titles.add(sectitle) sectitle = xml(sectitle, True) @@ -163,7 +164,7 @@ def sony_metadata(oeb): btitle = atitle d = 1 while atitle in seen_titles: - atitle = btitle + ' ' + str(d) + atitle = btitle + ' ' + unicode_type(d) d += 1 auth = article.author if article.author else '' @@ -180,7 +181,7 @@ def sony_metadata(oeb): short_title=short_title, section_title=sectitle, href=article.href, - word_count=str(1), + word_count=unicode_type(1), id=xml(base_id)+'/'+secid+'/'+aid )) diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py index 9936ffad7e..a84cb84023 100644 --- a/src/calibre/ebooks/fb2/fb2ml.py +++ b/src/calibre/ebooks/fb2/fb2ml.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL 3' __copyright__ = '2009, John Schember ' @@ -62,12 +63,12 @@ class FB2MLizer(object): output.append(self.get_text()) output.append(self.fb2mlize_images()) output.append(self.fb2_footer()) - output = self.clean_text(u''.join(output)) + output = self.clean_text(''.join(output)) if self.opts.pretty_print: - return u'\n%s' % etree.tostring(etree.fromstring(output), encoding='unicode', pretty_print=True) + return '\n%s' % etree.tostring(etree.fromstring(output), encoding='unicode', pretty_print=True) else: - return u'' + output + return '' + output def clean_text(self, text): # Condense empty paragraphs into a line break. @@ -116,11 +117,11 @@ class FB2MLizer(object): metadata['cover'] = self.get_cover() metadata['genre'] = self.opts.fb2_genre - metadata['author'] = u'' + metadata['author'] = '' for auth in self.oeb_book.metadata.creator: - author_first = u'' - author_middle = u'' - author_last = u'' + author_first = '' + author_middle = '' + author_last = '' author_parts = auth.value.split(' ') if len(author_parts) == 1: author_last = author_parts[0] @@ -138,22 +139,22 @@ class FB2MLizer(object): metadata['author'] += '%s' % prepare_string_for_xml(author_last) metadata['author'] += '' if not metadata['author']: - metadata['author'] = u'' + metadata['author'] = '' - metadata['keywords'] = u'' + metadata['keywords'] = '' tags = list(map(unicode_type, self.oeb_book.metadata.subject)) if tags: tags = ', '.join(prepare_string_for_xml(x) for x in tags) metadata['keywords'] = '%s'%tags - metadata['sequence'] = u'' + metadata['sequence'] = '' if self.oeb_book.metadata.series: index = '1' if self.oeb_book.metadata.series_index: index = self.oeb_book.metadata.series_index[0] - metadata['sequence'] = u'' % (prepare_string_for_xml(u'%s' % self.oeb_book.metadata.series[0]), index) + metadata['sequence'] = '' % (prepare_string_for_xml('%s' % self.oeb_book.metadata.series[0]), index) - year = publisher = isbn = u'' + year = publisher = isbn = '' identifiers = self.oeb_book.metadata['identifier'] for x in identifiers: if x.get(OPF('scheme'), None).lower() == 'uuid' or unicode_type(x).startswith('urn:uuid:'): @@ -161,7 +162,7 @@ class FB2MLizer(object): break if metadata['id'] is None: self.log.warn('No UUID identifier found') - metadata['id'] = str(uuid.uuid4()) + metadata['id'] = unicode_type(uuid.uuid4()) try: date = self.oeb_book.metadata['date'][0] @@ -194,7 +195,7 @@ class FB2MLizer(object): from calibre.utils.html2text import html2text metadata['comments'] = '{}'.format(prepare_string_for_xml(html2text(comments.value.strip()))) - return textwrap.dedent(u''' + return textwrap.dedent(''' @@ -222,7 +223,7 @@ class FB2MLizer(object): \n''') % metadata def fb2_footer(self): - return u'\n' + return '\n' def get_cover(self): from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES @@ -255,9 +256,9 @@ class FB2MLizer(object): if cover_href in self.oeb_book.manifest.hrefs.keys(): if cover_href not in self.image_hrefs.keys(): self.image_hrefs[cover_href] = '_%s.jpg' % len(self.image_hrefs.keys()) - return u'' % self.image_hrefs[cover_href] + return '' % self.image_hrefs[cover_href] - return u'' + return '' def get_text(self): from calibre.ebooks.oeb.base import XHTML diff --git a/src/calibre/ebooks/html/__init__.py b/src/calibre/ebooks/html/__init__.py index ecd0329f82..720ff711e6 100644 --- a/src/calibre/ebooks/html/__init__.py +++ b/src/calibre/ebooks/html/__init__.py @@ -1,31 +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' - -import re - - -def tostring(root, strip_comments=False, pretty_print=False): - ''' - Serialize processed XHTML. - ''' - from lxml.etree import tostring as _tostring - - root.set('xmlns', 'http://www.w3.org/1999/xhtml') - root.set('{http://www.w3.org/1999/xhtml}xlink', 'http://www.w3.org/1999/xlink') - for x in root.iter(): - if hasattr(x.tag, 'rpartition') and x.tag.rpartition('}')[-1].lower() == 'svg': - x.set('xmlns', 'http://www.w3.org/2000/svg') - - ans = _tostring(root, encoding='utf-8', pretty_print=pretty_print) - if strip_comments: - ans = re.compile(r'', re.DOTALL).sub('', ans) - ans = '\n'+ans - - return ans - - diff --git a/src/calibre/ebooks/html/input.py b/src/calibre/ebooks/html/input.py index 1b4d644cd2..91d02d7297 100644 --- a/src/calibre/ebooks/html/input.py +++ b/src/calibre/ebooks/html/input.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 with_statement -from __future__ import print_function +from __future__ import absolute_import, division, print_function, unicode_literals __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' @@ -18,7 +17,7 @@ from calibre.ebooks.oeb.base import urlunquote from calibre.ebooks.chardet import detect_xml_encoding from calibre.constants import iswindows from calibre import unicode_path, as_unicode, replace_entities -from polyglot.builtins import unicode_type +from polyglot.builtins import is_py3, unicode_type from polyglot.urllib import urlparse, urlunparse @@ -68,6 +67,9 @@ class Link(object): def __str__(self): return u'Link: %s --> %s'%(self.url, self.path) + if not is_py3: + __unicode__ = __str__ + class IgnoreFile(Exception): @@ -149,10 +151,10 @@ class HTMLFile(object): return hash(self.path) def __str__(self): - return u'HTMLFile:%d:%s:%s'%(self.level, 'b' if self.is_binary else 'a', self.path) + return 'HTMLFile:%d:%s:%s'%(self.level, 'b' if self.is_binary else 'a', self.path) def __repr__(self): - return str(self) + return unicode_type(self) def find_links(self, src): for match in self.LINK_PAT.finditer(src): diff --git a/src/calibre/ebooks/html/meta.py b/src/calibre/ebooks/html/meta.py index eebf9cdd6d..7cc9010a38 100644 --- a/src/calibre/ebooks/html/meta.py +++ b/src/calibre/ebooks/html/meta.py @@ -1,4 +1,5 @@ -from __future__ import with_statement +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL 3' __copyright__ = '2010, Fabian Grassl ' __docformat__ = 'restructuredtext en' diff --git a/src/calibre/ebooks/iterator/__init__.py b/src/calibre/ebooks/iterator/__init__.py index 9a3d842248..4ddf412436 100644 --- a/src/calibre/ebooks/iterator/__init__.py +++ b/src/calibre/ebooks/iterator/__init__.py @@ -1,9 +1,7 @@ #!/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 ' __docformat__ = 'restructuredtext en' - - - diff --git a/src/calibre/ebooks/lit/__init__.py b/src/calibre/ebooks/lit/__init__.py index 5be3cb9b6a..3a4904f419 100644 --- a/src/calibre/ebooks/lit/__init__.py +++ b/src/calibre/ebooks/lit/__init__.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/lit/lzx.py b/src/calibre/ebooks/lit/lzx.py index a1e447d797..aea602f6f9 100644 --- a/src/calibre/ebooks/lit/lzx.py +++ b/src/calibre/ebooks/lit/lzx.py @@ -1,7 +1,8 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + ''' LZX compression/decompression wrapper. ''' -from __future__ import with_statement __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/lit/maps/__init__.py b/src/calibre/ebooks/lit/maps/__init__.py index b30974ba6b..e9997944e5 100644 --- a/src/calibre/ebooks/lit/maps/__init__.py +++ b/src/calibre/ebooks/lit/maps/__init__.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/lit/maps/html.py b/src/calibre/ebooks/lit/maps/html.py index c144d55ea8..9502092779 100644 --- a/src/calibre/ebooks/lit/maps/html.py +++ b/src/calibre/ebooks/lit/maps/html.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/lit/maps/opf.py b/src/calibre/ebooks/lit/maps/opf.py index 12dcaed4d8..8f5626842b 100644 --- a/src/calibre/ebooks/lit/maps/opf.py +++ b/src/calibre/ebooks/lit/maps/opf.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Marshall T. Vandegrift ' diff --git a/src/calibre/ebooks/lrf/fonts.py b/src/calibre/ebooks/lrf/fonts.py index 6729dfb5b2..c4300bec42 100644 --- a/src/calibre/ebooks/lrf/fonts.py +++ b/src/calibre/ebooks/lrf/fonts.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' diff --git a/src/calibre/ebooks/lrf/lrfparser.py b/src/calibre/ebooks/lrf/lrfparser.py index 102c4ad2d5..923630c9c1 100644 --- a/src/calibre/ebooks/lrf/lrfparser.py +++ b/src/calibre/ebooks/lrf/lrfparser.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' '''''' @@ -79,47 +81,49 @@ class LRFDocument(LRFMetaFile): def write_files(self): for obj in chain(itervalues(self.image_map), itervalues(self.font_map)): - open(obj.file, 'wb').write(obj.stream) + with open(obj.file, 'wb') as f: + f.write(obj.stream) def to_xml(self, write_files=True): - bookinfo = u'\n\n\n' - bookinfo += u'%s\n'%(self.metadata.title_reading, self.metadata.title) - bookinfo += u'%s\n'%(self.metadata.author_reading, self.metadata.author) - bookinfo += u'%s\n'%(self.metadata.book_id,) - bookinfo += u'%s\n'%(self.metadata.publisher,) - bookinfo += u'\n'%(self.metadata.label,) - bookinfo += u'%s\n'%(self.metadata.category,) - bookinfo += u'%s\n'%(self.metadata.classification,) - bookinfo += u'%s\n\n\n'%(self.metadata.free_text,) + bookinfo = '\n\n\n' + bookinfo += '%s\n'%(self.metadata.title_reading, self.metadata.title) + bookinfo += '%s\n'%(self.metadata.author_reading, self.metadata.author) + bookinfo += '%s\n'%(self.metadata.book_id,) + bookinfo += '%s\n'%(self.metadata.publisher,) + bookinfo += '\n'%(self.metadata.label,) + bookinfo += '%s\n'%(self.metadata.category,) + bookinfo += '%s\n'%(self.metadata.classification,) + bookinfo += '%s\n\n\n'%(self.metadata.free_text,) th = self.doc_info.thumbnail if th: prefix = ascii_filename(self.metadata.title) - bookinfo += u'\n'%(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension,) + bookinfo += '\n'%(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension,) if write_files: - open(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension, 'wb').write(th) - bookinfo += u'%s\n'%(self.doc_info.language,) - bookinfo += u'%s\n'%(self.doc_info.creator,) - bookinfo += u'%s\n'%(self.doc_info.producer,) - bookinfo += u'%s\n\n\n%s\n'%(self.doc_info.page,self.toc) - pages = u'' + with open(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension, 'wb') as f: + f.write(th) + bookinfo += '%s\n'%(self.doc_info.language,) + bookinfo += '%s\n'%(self.doc_info.creator,) + bookinfo += '%s\n'%(self.doc_info.producer,) + bookinfo += '%s\n\n\n%s\n'%(self.doc_info.page,self.toc) + pages = '' done_main = False pt_id = -1 for page_tree in self: if not done_main: done_main = True - pages += u'
\n' - close = u'
\n' + pages += '
\n' + close = '
\n' pt_id = page_tree.id else: - pages += u'\n'%(page_tree.id,) - close = u'\n' + pages += '\n'%(page_tree.id,) + close = '\n' for page in page_tree: pages += unicode_type(page) pages += close traversed_objects = [int(i) for i in re.findall(r'objid="(\w+)"', pages)] + [pt_id] - objects = u'\n\n' - styles = u'\n