py3: more __future__s

This commit is contained in:
Eli Schwartz 2019-05-28 09:48:46 -04:00
parent 0cee082978
commit e521e770f3
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
33 changed files with 232 additions and 205 deletions

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
@ -177,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)
@ -231,14 +232,14 @@ class ComicInput(InputFormatPlugin):
_('Page')+' %d'%(i+1), play_order=po)
po += 1
opf.set_toc(toc)
with open(u'metadata.opf', 'wb') as m, open('toc.ncx', 'wb') as n:
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('''\
<html xmlns="%s">
<head>
<meta charset="utf-8"/>
@ -259,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)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -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('''
<enc:EncryptedData>
<enc:EncryptionMethod Algorithm="http://ns.adobe.com/pdf/enc#RC"/>
<enc:CipherData>
@ -347,13 +347,13 @@ class EPUBOutput(OutputFormatPlugin):
</enc:EncryptedData>
'''%(uri.replace('"', '\\"')))
if fonts:
ans = '''<encryption
ans = b'''<encryption
xmlns="urn:oasis:names:tc:opendocument:xmlns:container"
xmlns:enc="http://www.w3.org/2001/04/xmlenc#"
xmlns:deenc="http://ns.adobe.com/digitaleditions/enc">
'''
ans += (u'\n'.join(fonts)).encode('utf-8')
ans += '\n</encryption>'
ans += '\n'.join(fonts).encode('utf-8')
ans += b'\n</encryption>'
return ans
# }}}
@ -431,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')
@ -484,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

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Anatoly Shipitsin <norguhtar at gmail.com>'
"""
@ -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

View File

@ -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:

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
__docformat__ = 'restructuredtext en'

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
@ -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'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s</title></head><body>\n%s\n</body></html>'
HTML_TEMPLATE = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s</title></head><body>\n%s\n</body></html>'
def html_encode(s):
return s.replace(u'&', u'&amp;').replace(u'<', u'&lt;').replace(u'>', u'&gt;').replace(u'"', u'&quot;').replace(u"'", u'&apos;').replace(u'\n', u'<br/>').replace(u' ', u'&nbsp;') # noqa
return s.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&apos;').replace('\n', '<br/>').replace(' ', '&nbsp;') # noqa
class SNBInput(InputFormatPlugin):
@ -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:
@ -99,11 +101,11 @@ class SNBInput(InputFormatPlugin):
lines = []
for line in snbc.find('.//body'):
if line.tag == 'text':
lines.append(u'<p>%s</p>' % html_encode(line.text))
lines.append('<p>%s</p>' % html_encode(line.text))
elif line.tag == 'img':
lines.append(u'<p><img src="%s" /></p>' % html_encode(line.text))
lines.append('<p><img src="%s" /></p>' % html_encode(line.text))
with open(os.path.join(tdir, fname), 'wb') as f:
f.write((HTML_TEMPLATE % (chapterName, u'\n'.join(lines))).encode('utf-8', 'replace'))
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))

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
@ -243,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]))

View File

@ -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 <john@nachtimwald.com>'

View File

@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
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

View File

@ -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)

View File

@ -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))

View File

@ -18,7 +18,7 @@ from calibre.utils.date import utcnow
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
from calibre.utils.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

View File

@ -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:

View File

@ -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

View File

@ -8,7 +8,7 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
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))

View File

@ -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

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
@ -18,7 +19,7 @@ def rules(stylesheets):
def simple_container_xml(opf_path, extra_entries=''):
return u'''\
return '''\
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
@ -36,7 +37,7 @@ def initialize_container(path_to_container, opf_name='metadata.opf',
'''
rootfiles = ''
for path, mimetype, _ in extra_entries:
rootfiles += u'<rootfile full-path="{0}" media-type="{1}"/>'.format(
rootfiles += '<rootfile full-path="{0}" media-type="{1}"/>'.format(
path, mimetype)
CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8')
zf = ZipFile(path_to_container, 'w')

View File

@ -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 <llasram@gmail.com>'
@ -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):

View File

@ -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 <kovid@kovidgoyal.net>'
@ -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 = '''\
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcterms="http://purl.org/dc/terms/"
@ -32,7 +33,7 @@ SONY_METADATA = u'''\
</rdf:RDF>
'''
SONY_ATOM = u'''\
SONY_ATOM = '''\
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:dc="http://purl.org/dc/elements/1.1/"
@ -49,7 +50,7 @@ SONY_ATOM = u'''\
</feed>
'''
SONY_ATOM_SECTION = u'''\
SONY_ATOM_SECTION = '''\
<entry rdf:ID="{title}">
<title>{title}</title>
<link href="{href}"/>
@ -63,7 +64,7 @@ SONY_ATOM_SECTION = u'''\
</entry>
'''
SONY_ATOM_ENTRY = u'''\
SONY_ATOM_ENTRY = '''\
<entry>
<title>{title}</title>
<author><name>{author}</name></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
))

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
@ -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'<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding='unicode', pretty_print=True)
return '<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding='unicode', pretty_print=True)
else:
return u'<?xml version="1.0" encoding="UTF-8"?>' + output
return '<?xml version="1.0" encoding="UTF-8"?>' + 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'] += '<last-name>%s</last-name>' % prepare_string_for_xml(author_last)
metadata['author'] += '</author>'
if not metadata['author']:
metadata['author'] = u'<author><first-name></first-name><last-name></last-name></author>'
metadata['author'] = '<author><first-name></first-name><last-name></last-name></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'] = '<keywords>%s</keywords>'%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'<sequence name="%s" number="%s" />' % (prepare_string_for_xml(u'%s' % self.oeb_book.metadata.series[0]), index)
metadata['sequence'] = '<sequence name="%s" number="%s" />' % (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'] = '<annotation>{}</annotation>'.format(prepare_string_for_xml(html2text(comments.value.strip())))
return textwrap.dedent(u'''
return textwrap.dedent('''
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<description>
<title-info>
@ -222,7 +223,7 @@ class FB2MLizer(object):
</description>\n''') % metadata
def fb2_footer(self):
return u'\n</FictionBook>'
return '\n</FictionBook>'
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'<coverpage><image xlink:href="#%s" /></coverpage>' % self.image_hrefs[cover_href]
return '<coverpage><image xlink:href="#%s" /></coverpage>' % self.image_hrefs[cover_href]
return u''
return ''
def get_text(self):
from calibre.ebooks.oeb.base import XHTML

View File

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

View File

@ -1,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 <kovid@kovidgoyal.net>'
@ -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):

View File

@ -1,4 +1,5 @@
from __future__ import with_statement
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL 3'
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
__docformat__ = 'restructuredtext en'

View File

@ -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 <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -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 <llasram@gmail.com>'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

View File

@ -1,3 +1,5 @@
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
''''''
@ -83,45 +85,45 @@ class LRFDocument(LRFMetaFile):
f.write(obj.stream)
def to_xml(self, write_files=True):
bookinfo = u'<BookInformation>\n<Info version="1.1">\n<BookInfo>\n'
bookinfo += u'<Title reading="%s">%s</Title>\n'%(self.metadata.title_reading, self.metadata.title)
bookinfo += u'<Author reading="%s">%s</Author>\n'%(self.metadata.author_reading, self.metadata.author)
bookinfo += u'<BookID>%s</BookID>\n'%(self.metadata.book_id,)
bookinfo += u'<Publisher reading="">%s</Publisher>\n'%(self.metadata.publisher,)
bookinfo += u'<Label reading="">%s</Label>\n'%(self.metadata.label,)
bookinfo += u'<Category reading="">%s</Category>\n'%(self.metadata.category,)
bookinfo += u'<Classification reading="">%s</Classification>\n'%(self.metadata.classification,)
bookinfo += u'<FreeText reading="">%s</FreeText>\n</BookInfo>\n<DocInfo>\n'%(self.metadata.free_text,)
bookinfo = '<BookInformation>\n<Info version="1.1">\n<BookInfo>\n'
bookinfo += '<Title reading="%s">%s</Title>\n'%(self.metadata.title_reading, self.metadata.title)
bookinfo += '<Author reading="%s">%s</Author>\n'%(self.metadata.author_reading, self.metadata.author)
bookinfo += '<BookID>%s</BookID>\n'%(self.metadata.book_id,)
bookinfo += '<Publisher reading="">%s</Publisher>\n'%(self.metadata.publisher,)
bookinfo += '<Label reading="">%s</Label>\n'%(self.metadata.label,)
bookinfo += '<Category reading="">%s</Category>\n'%(self.metadata.category,)
bookinfo += '<Classification reading="">%s</Classification>\n'%(self.metadata.classification,)
bookinfo += '<FreeText reading="">%s</FreeText>\n</BookInfo>\n<DocInfo>\n'%(self.metadata.free_text,)
th = self.doc_info.thumbnail
if th:
prefix = ascii_filename(self.metadata.title)
bookinfo += u'<CThumbnail file="%s" />\n'%(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension,)
bookinfo += '<CThumbnail file="%s" />\n'%(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension,)
if write_files:
with open(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension, 'wb') as f:
f.write(th)
bookinfo += u'<Language reading="">%s</Language>\n'%(self.doc_info.language,)
bookinfo += u'<Creator reading="">%s</Creator>\n'%(self.doc_info.creator,)
bookinfo += u'<Producer reading="">%s</Producer>\n'%(self.doc_info.producer,)
bookinfo += u'<SumPage>%s</SumPage>\n</DocInfo>\n</Info>\n%s</BookInformation>\n'%(self.doc_info.page,self.toc)
pages = u''
bookinfo += '<Language reading="">%s</Language>\n'%(self.doc_info.language,)
bookinfo += '<Creator reading="">%s</Creator>\n'%(self.doc_info.creator,)
bookinfo += '<Producer reading="">%s</Producer>\n'%(self.doc_info.producer,)
bookinfo += '<SumPage>%s</SumPage>\n</DocInfo>\n</Info>\n%s</BookInformation>\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'<Main>\n'
close = u'</Main>\n'
pages += '<Main>\n'
close = '</Main>\n'
pt_id = page_tree.id
else:
pages += u'<PageTree objid="%d">\n'%(page_tree.id,)
close = u'</PageTree>\n'
pages += '<PageTree objid="%d">\n'%(page_tree.id,)
close = '</PageTree>\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<Objects>\n'
styles = u'\n<Style>\n'
objects = '\n<Objects>\n'
styles = '\n<Style>\n'
for obj in self.objects:
obj = self.objects[obj]
if obj.id in traversed_objects:

View File

@ -1,4 +1,5 @@
from __future__ import print_function
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
@ -9,7 +10,7 @@ Just create an L{LRFMetaFile} object and use its properties
to get and set meta information. For example:
>>> lrf = LRFMetaFile("mybook.lrf")
>>> print lrf.title, lrf.author
>>> print(lrf.title, lrf.author)
>>> lrf.category = "History"
"""
@ -50,8 +51,8 @@ class field(object):
def __repr__(self):
typ = {DWORD: 'unsigned int', 'QWORD': 'unsigned long long', BYTE: 'unsigned char', WORD: 'unsigned short'}.get(self._fmt, '')
return "An " + typ + " stored in " + \
str(struct.calcsize(self._fmt)) + \
" bytes starting at byte " + str(self._start)
unicode_type(struct.calcsize(self._fmt)) + \
" bytes starting at byte " + unicode_type(self._start)
class versioned_field(field):
@ -92,20 +93,20 @@ class fixed_stringfield(object):
self._start = start
def __get__(self, obj, typ=None):
length = str(self._length)
length = unicode_type(self._length)
return obj.unpack(start=self._start, fmt="<"+length+"s")[0]
def __set__(self, obj, val):
if val.__class__.__name__ != 'str':
val = str(val)
if not isinstance(val, unicode_type):
val = unicode_type(val)
if len(val) != self._length:
raise LRFException("Trying to set fixed_stringfield with a " +
"string of incorrect length")
obj.pack(val, start=self._start, fmt="<"+str(len(val))+"s")
obj.pack(val, start=self._start, fmt="<"+unicode_type(len(val))+"s")
def __repr__(self):
return "A string of length " + str(self._length) + \
" starting at byte " + str(self._start)
return "A string of length " + unicode_type(self._length) + \
" starting at byte " + unicode_type(self._start)
class xml_attr_field(object):
@ -191,7 +192,7 @@ class xml_field(object):
return elem
if not val:
val = u''
val = ''
if not isinstance(val, unicode_type):
val = val.decode('utf-8')
@ -721,8 +722,8 @@ def main(args=sys.argv):
fields = LRFMetaFile.__dict__.items()
fields.sort()
for f in fields:
if "XML" in str(f):
print(str(f[1]) + ":", lrf.__getattribute__(f[0]).encode('utf-8'))
if "XML" in unicode_type(f):
print(unicode_type(f[1]) + ":", lrf.__getattribute__(f[0]).encode('utf-8'))
if options.get_thumbnail:
print("Thumbnail:", td)
if options.get_cover:

View File

@ -1,4 +1,5 @@
from __future__ import print_function
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import struct, array, zlib, io, collections, re
@ -127,7 +128,7 @@ class LRFContentObject(LRFObject):
func, args = action[0], (action[1],)
getattr(self, func)(tag, *args)
else:
raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, str(tag)))
raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, unicode_type(tag)))
def __iter__(self):
for i in self._contents:
@ -195,17 +196,17 @@ class PageTree(LRFObject):
class StyleObject(object):
def _tags_to_xml(self):
s = u''
s = ''
for h in self.tag_map.values():
attr = h[0]
if hasattr(self, attr):
s += u'%s="%s" '%(attr, getattr(self, attr))
s += '%s="%s" '%(attr, getattr(self, attr))
return s
def __str__(self):
s = u'<%s objid="%s" stylelabel="%s" '%(self.__class__.__name__.replace('Attr', 'Style'), self.id, self.id)
s = '<%s objid="%s" stylelabel="%s" '%(self.__class__.__name__.replace('Attr', 'Style'), self.id, self.id)
s += self._tags_to_xml()
s += u'/>\n'
s += '/>\n'
return s
if not ispy3:
@ -254,7 +255,7 @@ class Color(object):
self.a, self.r, self.g, self.b = val & 0xFF, (val>>8)&0xFF, (val>>16)&0xFF, (val>>24)&0xFF
def __str__(self):
return u'0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b)
return '0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b)
if not ispy3:
__unicode__ = __str__
@ -286,7 +287,7 @@ class PageDiv(EmptyPageElement):
self.linecolor = Color(linecolor)
def __str__(self):
return u'\n<PageDiv pain="%s" spacesize="%s" linewidth="%s" linecolor="%s" />\n'%\
return '\n<PageDiv pain="%s" spacesize="%s" linewidth="%s" linecolor="%s" />\n'%\
(self.pain, self.spacesize, self.linewidth, self.color)
if not ispy3:
@ -304,7 +305,7 @@ class RuledLine(EmptyPageElement):
self.id = -1
def __str__(self):
return u'\n<RuledLine linelength="%s" linetype="%s" linewidth="%s" linecolor="%s" />\n'%\
return '\n<RuledLine linelength="%s" linetype="%s" linewidth="%s" linecolor="%s" />\n'%\
(self.linelength, self.linetype, self.linewidth, self.linecolor)
if not ispy3:
@ -317,7 +318,7 @@ class Wait(EmptyPageElement):
self.time = time
def __str__(self):
return u'\n<Wait time="%d" />\n'%(self.time)
return '\n<Wait time="%d" />\n'%(self.time)
if not ispy3:
__unicode__ = __str__
@ -331,7 +332,7 @@ class Locate(EmptyPageElement):
self.pos = self.pos_map[pos]
def __str__(self):
return u'\n<Locate pos="%s" />\n'%(self.pos)
return '\n<Locate pos="%s" />\n'%(self.pos)
if not ispy3:
__unicode__ = __str__
@ -343,7 +344,7 @@ class BlockSpace(EmptyPageElement):
self.xspace, self.yspace = xspace, yspace
def __str__(self):
return u'\n<BlockSpace xspace="%d" yspace="%d" />\n'%\
return '\n<BlockSpace xspace="%d" yspace="%d" />\n'%\
(self.xspace, self.yspace)
if not ispy3:
@ -444,7 +445,7 @@ class Page(LRFStream):
yield i
def __str__(self):
s = u'\n<Page pagestyle="%d" objid="%d">\n'%(self.style_id, self.id)
s = '\n<Page pagestyle="%d" objid="%d">\n'%(self.style_id, self.id)
for i in self:
s += unicode_type(i)
s += '\n</Page>\n'
@ -454,7 +455,7 @@ class Page(LRFStream):
__unicode__ = __str__
def to_html(self):
s = u''
s = ''
for i in self:
s += i.to_html()
return s
@ -629,7 +630,7 @@ class Block(LRFStream, TextCSS):
self.attrs[attr] = getattr(self, attr)
def __str__(self):
s = u'\n<%s objid="%d" blockstyle="%d" '%(self.name, self.id, self.style_id)
s = '\n<%s objid="%d" blockstyle="%d" '%(self.name, self.id, self.style_id)
if hasattr(self, 'textstyle_id'):
s += 'textstyle="%d" '%(self.textstyle_id,)
for attr in self.attrs:
@ -646,8 +647,8 @@ class Block(LRFStream, TextCSS):
def to_html(self):
if self.name == 'TextBlock':
return u'<div class="block%s text%s">%s</div>'%(self.style_id, self.textstyle_id, self.content.to_html())
return u''
return '<div class="block%s text%s">%s</div>'%(self.style_id, self.textstyle_id, self.content.to_html())
return ''
class MiniPage(LRFStream):
@ -668,7 +669,7 @@ class Text(LRFStream):
style = property(fget=lambda self : self._document.objects[self.style_id])
text_map = {0x22: u'"', 0x26: u'&amp;', 0x27: u'\'', 0x3c: u'&lt;', 0x3e: u'&gt;'}
text_map = {0x22: '"', 0x26: '&amp;', 0x27: '\'', 0x3c: '&lt;', 0x3e: '&gt;'}
entity_pattern = re.compile(r'&amp;(\S+?);')
text_tags = {
@ -717,20 +718,20 @@ class Text(LRFStream):
self.self_closing = self_closing
def __str__(self):
s = u'<%s '%(self.name,)
s = '<%s '%(self.name,)
for name, val in self.attrs.items():
s += '%s="%s" '%(name, val)
return s.rstrip() + (u' />' if self.self_closing else u'>')
return s.rstrip() + (' />' if self.self_closing else '>')
if not ispy3:
__unicode__ = __str__
def to_html(self):
s = u''
s = ''
return s
def close_html(self):
return u''
return ''
class Span(TextTag):
pass
@ -901,7 +902,7 @@ class Text(LRFStream):
self.stream = None
def __str__(self):
s = u''
s = ''
open_containers = collections.deque()
for c in self.content:
if isinstance(c, string_or_bytes):
@ -909,7 +910,7 @@ class Text(LRFStream):
elif c is None:
if open_containers:
p = open_containers.pop()
s += u'</%s>'%(p.name,)
s += '</%s>'%(p.name,)
else:
s += unicode_type(c)
if not c.self_closing:
@ -917,7 +918,7 @@ class Text(LRFStream):
if len(open_containers) > 0:
if len(open_containers) == 1:
s += u'</%s>'%(open_containers[0].name,)
s += '</%s>'%(open_containers[0].name,)
else:
raise LRFParseError('Malformed text stream %s'%([i.name for i in open_containers if isinstance(i, Text.TextTag)],))
return s
@ -926,7 +927,7 @@ class Text(LRFStream):
__unicode__ = __str__
def to_html(self):
s = u''
s = ''
open_containers = collections.deque()
in_p = False
for c in self.content:
@ -970,7 +971,7 @@ class Image(LRFObject):
data = property(fget=lambda self : self._document.objects[self.refstream].stream)
def __str__(self):
return u'<Image objid="%s" x0="%d" y0="%d" x1="%d" y1="%d" xsize="%d" ysize="%d" refstream="%d" />\n'%\
return '<Image objid="%s" x0="%d" y0="%d" x1="%d" y1="%d" xsize="%d" ysize="%d" refstream="%d" />\n'%\
(self.id, self.x0, self.y0, self.x1, self.y1, self.xsize, self.ysize, self.refstream)
if not ispy3:
@ -984,7 +985,7 @@ class PutObj(EmptyPageElement):
self.object = objects[refobj]
def __str__(self):
return u'<PutObj x1="%d" y1="%d" refobj="%d" />'%(self.x1, self.y1, self.refobj)
return '<PutObj x1="%d" y1="%d" refobj="%d" />'%(self.x1, self.y1, self.refobj)
if not ispy3:
__unicode__ = __str__
@ -1069,12 +1070,12 @@ class ImageStream(LRFStream):
def end_stream(self, *args):
LRFStream.end_stream(self, *args)
self.file = str(self.id) + '.' + self.encoding.lower()
self.file = unicode_type(self.id) + '.' + self.encoding.lower()
if self._document is not None:
self._document.image_map[self.id] = self
def __str__(self):
return u'<ImageStream objid="%s" encoding="%s" file="%s" />\n'%\
return '<ImageStream objid="%s" encoding="%s" file="%s" />\n'%\
(self.id, self.encoding, self.file)
if not ispy3:
@ -1156,7 +1157,7 @@ class Button(LRFObject):
return (None, None)
def __str__(self):
s = u'<Button objid="%s">\n'%(self.id,)
s = '<Button objid="%s">\n'%(self.id,)
if self.button_flags & 0x10 != 0:
s += '<PushButton '
if 2 in self.refimage:
@ -1233,10 +1234,10 @@ class BookAttr(StyleObject, LRFObject):
self.font_link_list.append(tag.dword)
def __str__(self):
s = u'<BookStyle objid="%s" stylelabel="%s">\n'%(self.id, self.id)
s += u'<SetDefault %s />\n'%(self._tags_to_xml(),)
s = '<BookStyle objid="%s" stylelabel="%s">\n'%(self.id, self.id)
s += '<SetDefault %s />\n'%(self._tags_to_xml(),)
doc = self._document
s += u'<BookSetting bindingdirection="%s" dpi="%s" screenwidth="%s" screenheight="%s" colordepth="%s" />\n'%\
s += '<BookSetting bindingdirection="%s" dpi="%s" screenwidth="%s" screenheight="%s" colordepth="%s" />\n'%\
(self.binding_map[doc.binding], doc.dpi, doc.width, doc.height, doc.color_depth)
for font in self._document.font_map.values():
s += unicode_type(font)
@ -1257,7 +1258,7 @@ class TocLabel(object):
self.refpage, self.refobject, self.label = refpage, refobject, label
def __str__(self):
return u'<TocLabel refpage="%s" refobj="%s">%s</TocLabel>\n'%(self.refpage, self.refobject, self.label)
return '<TocLabel refpage="%s" refobj="%s">%s</TocLabel>\n'%(self.refpage, self.refobject, self.label)
if not ispy3:
__unicode__ = __str__
@ -1284,7 +1285,7 @@ class TOCObject(LRFStream):
yield i
def __str__(self):
s = u'<TOC>\n'
s = '<TOC>\n'
for i in self:
s += unicode_type(i)
return s + '</TOC>\n'