py3 porting cleanup: drop str proxy

We don't need unicode_type anymore, because the str/unicode split
disappeared.

In addition to converting all unicode_type to str, there are some cases
where string literals were converted to unicode_type, e.g. for r''
strings, since ur'' didn't work. These can now be dropped entirely.
This commit is contained in:
Eli Schwartz 2021-10-20 21:03:44 -04:00
parent e794b388b4
commit 39a22268b9
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
449 changed files with 2225 additions and 2423 deletions

View File

@ -5,7 +5,7 @@ __copyright__ = '2008, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import sys, os, re, time, random, warnings import sys, os, re, time, random, warnings
from polyglot.builtins import codepoint_to_chr, unicode_type, hasenv, native_string_type from polyglot.builtins import codepoint_to_chr, hasenv, native_string_type
from math import floor from math import floor
from functools import partial from functools import partial
@ -71,7 +71,7 @@ def get_types_map():
def to_unicode(raw, encoding='utf-8', errors='strict'): def to_unicode(raw, encoding='utf-8', errors='strict'):
if isinstance(raw, unicode_type): if isinstance(raw, str):
return raw return raw
return raw.decode(encoding, errors) return raw.decode(encoding, errors)
@ -259,7 +259,7 @@ def get_parsed_proxy(typ='http', debug=True):
traceback.print_exc() traceback.print_exc()
else: else:
if debug: if debug:
prints('Using http proxy', unicode_type(ans)) prints('Using http proxy', str(ans))
return ans return ans
@ -419,7 +419,7 @@ def strftime(fmt, t=None):
fmt = fmt.decode('mbcs' if iswindows else 'utf-8', 'replace') fmt = fmt.decode('mbcs' if iswindows else 'utf-8', 'replace')
ans = time.strftime(fmt, t) ans = time.strftime(fmt, t)
if early_year: if early_year:
ans = ans.replace('_early year hack##', unicode_type(orig_year)) ans = ans.replace('_early year hack##', str(orig_year))
return ans return ans
@ -531,7 +531,7 @@ def force_unicode(obj, enc=preferred_encoding):
def as_unicode(obj, enc=preferred_encoding): def as_unicode(obj, enc=preferred_encoding):
if not isbytestring(obj): if not isbytestring(obj):
try: try:
obj = unicode_type(obj) obj = str(obj)
except Exception: except Exception:
try: try:
obj = native_string_type(obj) obj = native_string_type(obj)
@ -554,7 +554,7 @@ def human_readable(size, sep=' '):
if size < (1 << ((i + 1) * 10)): if size < (1 << ((i + 1) * 10)):
divisor, suffix = (1 << (i * 10)), candidate divisor, suffix = (1 << (i * 10)), candidate
break break
size = unicode_type(float(size)/divisor) size = str(float(size)/divisor)
if size.find(".") > -1: if size.find(".") > -1:
size = size[:size.find(".")+2] size = size[:size.find(".")+2]
if size.endswith('.0'): if size.endswith('.0'):

View File

@ -1,12 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from polyglot.builtins import unicode_type, environ_item, hasenv from polyglot.builtins import environ_item, hasenv
import sys, locale, codecs, os, collections, collections.abc import sys, locale, codecs, os, collections, collections.abc
__appname__ = 'calibre' __appname__ = 'calibre'
numeric_version = (5, 29, 0) numeric_version = (5, 29, 0)
__version__ = '.'.join(map(unicode_type, numeric_version)) __version__ = '.'.join(map(str, numeric_version))
git_version = None git_version = None
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>" __author__ = "Kovid Goyal <kovid@kovidgoyal.net>"

View File

@ -6,7 +6,6 @@ import os, sys, zipfile, importlib, enum
from calibre.constants import numeric_version, iswindows, ismacos from calibre.constants import numeric_version, iswindows, ismacos
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from polyglot.builtins import unicode_type
if iswindows: if iswindows:
platform = 'windows' platform = 'windows'
@ -207,7 +206,7 @@ class Plugin: # {{{
config_dialog.exec_() config_dialog.exec_()
if config_dialog.result() == QDialog.DialogCode.Accepted: if config_dialog.result() == QDialog.DialogCode.Accepted:
sc = unicode_type(sc.text()).strip() sc = str(sc.text()).strip()
customize_plugin(self, sc) customize_plugin(self, sc)
geom = bytearray(config_dialog.saveGeometry()) geom = bytearray(config_dialog.saveGeometry())

View File

@ -7,7 +7,6 @@ import re, os, shutil, numbers
from calibre import CurrentDir from calibre import CurrentDir
from calibre.customize import Plugin from calibre.customize import Plugin
from polyglot.builtins import unicode_type
class ConversionOption: class ConversionOption:
@ -81,7 +80,7 @@ class OptionRecommendation:
self.option.choices: self.option.choices:
raise ValueError('OpRec: %s: Recommended value not in choices'% raise ValueError('OpRec: %s: Recommended value not in choices'%
self.option.name) self.option.name)
if not (isinstance(self.recommended_value, (numbers.Number, bytes, unicode_type)) or self.recommended_value is None): if not (isinstance(self.recommended_value, (numbers.Number, bytes, str)) or self.recommended_value is None):
raise ValueError('OpRec: %s:'%self.option.name + repr( raise ValueError('OpRec: %s:'%self.option.name + repr(
self.recommended_value) + ' is not a string or a number') self.recommended_value) + ' is not a string or a number')
@ -342,7 +341,7 @@ class OutputFormatPlugin(Plugin):
@property @property
def is_periodical(self): def is_periodical(self):
return self.oeb.metadata.publication_type and \ return self.oeb.metadata.publication_type and \
unicode_type(self.oeb.metadata.publication_type[0]).startswith('periodical:') str(self.oeb.metadata.publication_type[0]).startswith('periodical:')
def specialize_options(self, log, opts, input_fmt): def specialize_options(self, log, opts, input_fmt):
''' '''

View File

@ -22,7 +22,7 @@ from calibre.utils.config import (make_config_dir, Config, ConfigProxy,
plugin_dir, OptionParser) plugin_dir, OptionParser)
from calibre.ebooks.metadata.sources.base import Source from calibre.ebooks.metadata.sources.base import Source
from calibre.constants import DEBUG, numeric_version, system_plugins_loc from calibre.constants import DEBUG, numeric_version, system_plugins_loc
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
builtin_names = frozenset(p.name for p in builtin_plugins) builtin_names = frozenset(p.name for p in builtin_plugins)
BLACKLISTED_PLUGINS = frozenset({'Marvin XD', 'iOS reader applications'}) BLACKLISTED_PLUGINS = frozenset({'Marvin XD', 'iOS reader applications'})
@ -781,7 +781,7 @@ def build_plugin(path):
from calibre import prints from calibre import prints
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.zipfile import ZipFile, ZIP_STORED from calibre.utils.zipfile import ZipFile, ZIP_STORED
path = unicode_type(path) path = str(path)
names = frozenset(os.listdir(path)) names = frozenset(os.listdir(path))
if '__init__.py' not in names: if '__init__.py' not in names:
prints(path, ' is not a valid plugin') prints(path, ' is not a valid plugin')

View File

@ -22,7 +22,7 @@ from calibre import as_unicode
from calibre.customize import ( from calibre.customize import (
InvalidPlugin, Plugin, PluginNotFound, numeric_version, platform InvalidPlugin, Plugin, PluginNotFound, numeric_version, platform
) )
from polyglot.builtins import itervalues, reload, string_or_bytes, unicode_type from polyglot.builtins import itervalues, reload, string_or_bytes
# PEP 302 based plugin loading mechanism, works around the bug in zipimport in # PEP 302 based plugin loading mechanism, works around the bug in zipimport in
# python 2.x that prevents importing from zip files in locations whose paths # python 2.x that prevents importing from zip files in locations whose paths
@ -307,7 +307,7 @@ class CalibrePluginFinder:
if ans.minimum_calibre_version > numeric_version: if ans.minimum_calibre_version > numeric_version:
raise InvalidPlugin( raise InvalidPlugin(
'The plugin at %s needs a version of calibre >= %s' % 'The plugin at %s needs a version of calibre >= %s' %
(as_unicode(path_to_zip_file), '.'.join(map(unicode_type, (as_unicode(path_to_zip_file), '.'.join(map(str,
ans.minimum_calibre_version)))) ans.minimum_calibre_version))))
if platform not in ans.supported_platforms: if platform not in ans.supported_platforms:

View File

@ -16,7 +16,7 @@ from calibre import prints
from calibre.constants import filesystem_encoding, ismacos, iswindows from calibre.constants import filesystem_encoding, ismacos, iswindows
from calibre.ebooks import BOOK_EXTENSIONS from calibre.ebooks import BOOK_EXTENSIONS
from calibre.utils.filenames import make_long_path_useable from calibre.utils.filenames import make_long_path_useable
from polyglot.builtins import itervalues, unicode_type from polyglot.builtins import itervalues
def splitext(path): def splitext(path):
@ -71,7 +71,7 @@ def metadata_extensions():
# but not actually added) # but not actually added)
global _metadata_extensions global _metadata_extensions
if _metadata_extensions is None: if _metadata_extensions is None:
_metadata_extensions = frozenset(map(unicode_type, BOOK_EXTENSIONS)) | {'opf'} _metadata_extensions = frozenset(BOOK_EXTENSIONS) | {'opf'}
return _metadata_extensions return _metadata_extensions
@ -146,7 +146,7 @@ def find_books_in_directory(dirpath, single_book_per_directory, compiled_rules=(
for path in listdir_impl(dirpath, sort_by_mtime=True): for path in listdir_impl(dirpath, sort_by_mtime=True):
key, ext = splitext(path) key, ext = splitext(path)
if allow_path(path, ext, compiled_rules): if allow_path(path, ext, compiled_rules):
books[icu_lower(key) if isinstance(key, unicode_type) else key.lower()][ext] = path books[icu_lower(key) if isinstance(key, str) else key.lower()][ext] = path
for formats in itervalues(books): for formats in itervalues(books):
if formats_ok(formats): if formats_ok(formats):

View File

@ -50,8 +50,7 @@ from calibre.utils.formatter_functions import (
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.img import save_cover_data_to from calibre.utils.img import save_cover_data_to
from polyglot.builtins import ( from polyglot.builtins import (
cmp, iteritems, itervalues, native_string_type, reraise, string_or_bytes, cmp, iteritems, itervalues, native_string_type, reraise, string_or_bytes
unicode_type
) )
# }}} # }}}
@ -108,7 +107,7 @@ class DBPrefs(dict): # {{{
dict.__setitem__(self, key, val) dict.__setitem__(self, key, val)
def raw_to_object(self, raw): def raw_to_object(self, raw):
if not isinstance(raw, unicode_type): if not isinstance(raw, str):
raw = raw.decode(preferred_encoding) raw = raw.decode(preferred_encoding)
return json.loads(raw, object_hook=from_json) return json.loads(raw, object_hook=from_json)
@ -352,7 +351,7 @@ class Connection(apsw.Connection): # {{{
self.createscalarfunction('title_sort', title_sort, 1) self.createscalarfunction('title_sort', title_sort, 1)
self.createscalarfunction('author_to_author_sort', self.createscalarfunction('author_to_author_sort',
_author_to_author_sort, 1) _author_to_author_sort, 1)
self.createscalarfunction('uuid4', lambda: unicode_type(uuid.uuid4()), self.createscalarfunction('uuid4', lambda: str(uuid.uuid4()),
0) 0)
# Dummy functions for dynamically created filters # Dummy functions for dynamically created filters
@ -646,10 +645,10 @@ class DB:
prints('found user category case overlap', catmap[uc]) prints('found user category case overlap', catmap[uc])
cat = catmap[uc][0] cat = catmap[uc][0]
suffix = 1 suffix = 1
while icu_lower((cat + unicode_type(suffix))) in catmap: while icu_lower((cat + str(suffix))) in catmap:
suffix += 1 suffix += 1
prints('Renaming user category %s to %s'%(cat, cat+unicode_type(suffix))) prints('Renaming user category %s to %s'%(cat, cat+str(suffix)))
user_cats[cat + unicode_type(suffix)] = user_cats[cat] user_cats[cat + str(suffix)] = user_cats[cat]
del user_cats[cat] del user_cats[cat]
cats_changed = True cats_changed = True
if cats_changed: if cats_changed:
@ -755,25 +754,25 @@ class DB:
if d['is_multiple']: if d['is_multiple']:
if x is None: if x is None:
return [] return []
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
x = x.split(d['multiple_seps']['ui_to_list']) x = x.split(d['multiple_seps']['ui_to_list'])
x = [y.strip() for y in x if y.strip()] x = [y.strip() for y in x if y.strip()]
x = [y.decode(preferred_encoding, 'replace') if not isinstance(y, x = [y.decode(preferred_encoding, 'replace') if not isinstance(y,
unicode_type) else y for y in x] str) else y for y in x]
return [u' '.join(y.split()) for y in x] return [u' '.join(y.split()) for y in x]
else: else:
return x if x is None or isinstance(x, unicode_type) else \ return x if x is None or isinstance(x, str) else \
x.decode(preferred_encoding, 'replace') x.decode(preferred_encoding, 'replace')
def adapt_datetime(x, d): def adapt_datetime(x, d):
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
if isinstance(x, bytes): if isinstance(x, bytes):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
x = parse_date(x, assume_utc=False, as_utc=False) x = parse_date(x, assume_utc=False, as_utc=False)
return x return x
def adapt_bool(x, d): def adapt_bool(x, d):
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
if isinstance(x, bytes): if isinstance(x, bytes):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
x = x.lower() x = x.lower()
@ -796,7 +795,7 @@ class DB:
def adapt_number(x, d): def adapt_number(x, d):
if x is None: if x is None:
return None return None
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
if isinstance(x, bytes): if isinstance(x, bytes):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
if x.lower() == 'none': if x.lower() == 'none':
@ -888,7 +887,7 @@ class DB:
# account for the series index column. Field_metadata knows that # account for the series index column. Field_metadata knows that
# the series index is one larger than the series. If you change # the series index is one larger than the series. If you change
# it here, be sure to change it there as well. # it here, be sure to change it there as well.
self.FIELD_MAP[unicode_type(data['num'])+'_index'] = base = base+1 self.FIELD_MAP[str(data['num'])+'_index'] = base = base+1
self.field_metadata.set_field_record_index(label_+'_index', base, self.field_metadata.set_field_record_index(label_+'_index', base,
prefer_custom=True) prefer_custom=True)
@ -1311,7 +1310,7 @@ class DB:
if getattr(self, '_library_id_', None) is None: if getattr(self, '_library_id_', None) is None:
ans = self.conn.get('SELECT uuid FROM library_id', all=False) ans = self.conn.get('SELECT uuid FROM library_id', all=False)
if ans is None: if ans is None:
ans = unicode_type(uuid.uuid4()) ans = str(uuid.uuid4())
self.library_id = ans self.library_id = ans
else: else:
self._library_id_ = ans self._library_id_ = ans
@ -1319,7 +1318,7 @@ class DB:
@library_id.setter @library_id.setter
def library_id(self, val): def library_id(self, val):
self._library_id_ = unicode_type(val) self._library_id_ = str(val)
self.execute(''' self.execute('''
DELETE FROM library_id; DELETE FROM library_id;
INSERT INTO library_id (uuid) VALUES (?); INSERT INTO library_id (uuid) VALUES (?);
@ -2026,7 +2025,7 @@ class DB:
def map_data(x): def map_data(x):
if not isinstance(x, string_or_bytes): if not isinstance(x, string_or_bytes):
x = native_string_type(x) x = native_string_type(x)
x = x.encode('utf-8') if isinstance(x, unicode_type) else x x = x.encode('utf-8') if isinstance(x, str) else x
x = pickle_binary_string(x) x = pickle_binary_string(x)
return x return x
options = [(book_id, fmt.upper(), map_data(data)) for book_id, data in iteritems(options)] options = [(book_id, fmt.upper(), map_data(data)) for book_id, data in iteritems(options)]
@ -2067,7 +2066,7 @@ class DB:
copyfile_using_links(src, dest, dest_is_dir=False) copyfile_using_links(src, dest, dest_is_dir=False)
old_files.add(src) old_files.add(src)
x = path_map[x] x = path_map[x]
if not isinstance(x, unicode_type): if not isinstance(x, str):
x = x.decode(filesystem_encoding, 'replace') x = x.decode(filesystem_encoding, 'replace')
progress(x, i+1, total) progress(x, i+1, total)

View File

@ -47,7 +47,7 @@ from calibre.utils.config import prefs, tweaks
from calibre.utils.date import UNDEFINED_DATE, now as nowf, utcnow from calibre.utils.date import UNDEFINED_DATE, now as nowf, utcnow
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.localization import canonicalize_lang from calibre.utils.localization import canonicalize_lang
from polyglot.builtins import cmp, iteritems, itervalues, string_or_bytes, unicode_type from polyglot.builtins import cmp, iteritems, itervalues, string_or_bytes
def api(f): def api(f):
@ -579,14 +579,14 @@ class Cache:
@read_api @read_api
def get_item_id(self, field, item_name): def get_item_id(self, field, item_name):
' Return the item id for item_name (case-insensitive) ' ' Return the item id for item_name (case-insensitive) '
rmap = {icu_lower(v) if isinstance(v, unicode_type) else v:k for k, v in iteritems(self.fields[field].table.id_map)} rmap = {icu_lower(v) if isinstance(v, str) else v:k for k, v in iteritems(self.fields[field].table.id_map)}
return rmap.get(icu_lower(item_name) if isinstance(item_name, unicode_type) else item_name, None) return rmap.get(icu_lower(item_name) if isinstance(item_name, str) else item_name, None)
@read_api @read_api
def get_item_ids(self, field, item_names): def get_item_ids(self, field, item_names):
' Return the item id for item_name (case-insensitive) ' ' Return the item id for item_name (case-insensitive) '
rmap = {icu_lower(v) if isinstance(v, unicode_type) else v:k for k, v in iteritems(self.fields[field].table.id_map)} rmap = {icu_lower(v) if isinstance(v, str) else v:k for k, v in iteritems(self.fields[field].table.id_map)}
return {name:rmap.get(icu_lower(name) if isinstance(name, unicode_type) else name, None) for name in item_names} return {name:rmap.get(icu_lower(name) if isinstance(name, str) else name, None) for name in item_names}
@read_api @read_api
def author_data(self, author_ids=None): def author_data(self, author_ids=None):

View File

@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en'
import copy import copy
from functools import partial from functools import partial
from polyglot.builtins import iteritems, unicode_type, native_string_type from polyglot.builtins import iteritems, native_string_type
from calibre.ebooks.metadata import author_to_author_sort from calibre.ebooks.metadata import author_to_author_sort
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
@ -101,8 +101,8 @@ def clean_user_categories(dbcache):
if len(comps) == 0: if len(comps) == 0:
i = 1 i = 1
while True: while True:
if unicode_type(i) not in user_cats: if str(i) not in user_cats:
new_cats[unicode_type(i)] = user_cats[k] new_cats[str(i)] = user_cats[k]
break break
i += 1 i += 1
else: else:

View File

@ -21,7 +21,6 @@ from calibre.ptempfile import TemporaryDirectory
from calibre.srv.changes import books_added, formats_added from calibre.srv.changes import books_added, formats_added
from calibre.utils.localization import canonicalize_lang from calibre.utils.localization import canonicalize_lang
from calibre.utils.short_uuid import uuid4 from calibre.utils.short_uuid import uuid4
from polyglot.builtins import unicode_type
readonly = False readonly = False
version = 0 # change this if you change signature of implementation() version = 0 # change this if you change signature of implementation()
@ -296,9 +295,9 @@ def do_add(
prints(' ', path) prints(' ', path)
if added_ids: if added_ids:
prints(_('Added book ids: %s') % (', '.join(map(unicode_type, added_ids)))) prints(_('Added book ids: %s') % (', '.join(map(str, added_ids))))
if merged_ids: if merged_ids:
prints(_('Merged book ids: %s') % (', '.join(map(unicode_type, merged_ids)))) prints(_('Merged book ids: %s') % (', '.join(map(str, merged_ids))))
def option_parser(get_parser, args): def option_parser(get_parser, args):

View File

@ -11,7 +11,7 @@ from textwrap import TextWrapper
from calibre.db.cli.utils import str_width from calibre.db.cli.utils import str_width
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.utils.date import isoformat from calibre.utils.date import isoformat
from polyglot.builtins import as_bytes, iteritems, unicode_type from polyglot.builtins import as_bytes, iteritems
readonly = True readonly = True
version = 0 # change this if you change signature of implementation() version = 0 # change this if you change signature of implementation()
@ -123,10 +123,10 @@ def prepare_output_table(fields, book_ids, data, metadata):
ans.append(row) ans.append(row)
for field in fields: for field in fields:
if field == 'id': if field == 'id':
row.append(unicode_type(book_id)) row.append(str(book_id))
continue continue
val = data.get(field.replace('*', '#'), {}).get(book_id) val = data.get(field.replace('*', '#'), {}).get(book_id)
row.append(unicode_type(val).replace('\n', ' ')) row.append(str(val).replace('\n', ' '))
return ans return ans
@ -308,7 +308,7 @@ List the books available in the calibre database.
def main(opts, args, dbctx): def main(opts, args, dbctx):
afields = set(FIELDS) | {'id'} afields = set(FIELDS) | {'id'}
if opts.fields.strip(): if opts.fields.strip():
fields = [unicode_type(f.strip().lower()) for f in opts.fields.split(',')] fields = [str(f.strip().lower()) for f in opts.fields.split(',')]
else: else:
fields = [] fields = []

View File

@ -8,7 +8,7 @@ import sys
from textwrap import TextWrapper from textwrap import TextWrapper
from calibre import prints from calibre import prints
from polyglot.builtins import as_bytes, unicode_type from polyglot.builtins import as_bytes
readonly = True readonly = True
version = 0 # change this if you change signature of implementation() version = 0 # change this if you change signature of implementation()
@ -78,7 +78,7 @@ def do_list(fields, data, opts):
widths = list(map(lambda x: 0, fields)) widths = list(map(lambda x: 0, fields))
for i in data: for i in data:
for j, field in enumerate(fields): for j, field in enumerate(fields):
widths[j] = max(widths[j], max(len(field), len(unicode_type(i[field])))) widths[j] = max(widths[j], max(len(field), len(str(i[field]))))
screen_width = geometry()[0] screen_width = geometry()[0]
if not screen_width: if not screen_width:
@ -109,7 +109,7 @@ def do_list(fields, data, opts):
for record in data: for record in data:
text = [ text = [
wrappers[i].wrap(unicode_type(record[field])) wrappers[i].wrap(str(record[field]))
for i, field in enumerate(fields) for i, field in enumerate(fields)
] ]
lines = max(map(len, text)) lines = max(map(len, text))
@ -167,11 +167,11 @@ def main(opts, args, dbctx):
is_rating = category_metadata(category)['datatype'] == 'rating' is_rating = category_metadata(category)['datatype'] == 'rating'
for tag in category_data[category]: for tag in category_data[category]:
if is_rating: if is_rating:
tag.name = unicode_type(len(tag.name)) tag.name = str(len(tag.name))
data.append({ data.append({
'category': category, 'category': category,
'tag_name': tag.name, 'tag_name': tag.name,
'count': unicode_type(tag.count), 'count': str(tag.count),
'rating': fmtr(tag.avg_rating), 'rating': fmtr(tag.avg_rating),
}) })
else: else:
@ -179,7 +179,7 @@ def main(opts, args, dbctx):
data.append({ data.append({
'category': category, 'category': category,
'tag_name': _('CATEGORY ITEMS'), 'tag_name': _('CATEGORY ITEMS'),
'count': unicode_type(len(category_data[category])), 'count': str(len(category_data[category])),
'rating': '' 'rating': ''
}) })

View File

@ -10,7 +10,7 @@ from calibre.ebooks.metadata.book.base import field_from_string
from calibre.ebooks.metadata.book.serialize import read_cover from calibre.ebooks.metadata.book.serialize import read_cover
from calibre.ebooks.metadata.opf import get_metadata from calibre.ebooks.metadata.opf import get_metadata
from calibre.srv.changes import metadata from calibre.srv.changes import metadata
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
readonly = False readonly = False
version = 0 # change this if you change signature of implementation() version = 0 # change this if you change signature of implementation()
@ -181,5 +181,5 @@ def main(opts, args, dbctx):
if not final_mi: if not final_mi:
raise SystemExit(_('No book with id: %s in the database') % book_id) raise SystemExit(_('No book with id: %s in the database') % book_id)
prints(unicode_type(final_mi)) prints(str(final_mi))
return 0 return 0

View File

@ -8,7 +8,6 @@ import sys
from calibre import prints from calibre import prints
from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.ebooks.metadata.opf2 import OPFCreator
from polyglot.builtins import unicode_type
readonly = True readonly = True
version = 0 # change this if you change signature of implementation() version = 0 # change this if you change signature of implementation()
@ -53,6 +52,6 @@ def main(opts, args, dbctx):
mi = OPFCreator(os.getcwd(), mi) mi = OPFCreator(os.getcwd(), mi)
mi.render(stdout) mi.render(stdout)
else: else:
prints(unicode_type(mi)) prints(str(mi))
return 0 return 0

View File

@ -18,7 +18,7 @@ from calibre.utils.config_base import tweaks
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.date import UNDEFINED_DATE, clean_date_for_sort, parse_date from calibre.utils.date import UNDEFINED_DATE, clean_date_for_sort, parse_date
from calibre.utils.localization import calibre_langcode_to_name from calibre.utils.localization import calibre_langcode_to_name
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
def bool_sort_key(bools_are_tristate): def bool_sort_key(bools_are_tristate):
@ -85,7 +85,7 @@ class Field:
self._sort_key = lambda x: sort_key(author_to_author_sort(x)) self._sort_key = lambda x: sort_key(author_to_author_sort(x))
self.sort_sort_key = False self.sort_sort_key = False
self.default_value = {} if name == 'identifiers' else () if self.is_multiple else None self.default_value = {} if name == 'identifiers' else () if self.is_multiple else None
self.category_formatter = unicode_type self.category_formatter = str
if dt == 'rating': if dt == 'rating':
if self.metadata['display'].get('allow_half_stars', False): if self.metadata['display'].get('allow_half_stars', False):
self.category_formatter = lambda x: rating_to_stars(x, True) self.category_formatter = lambda x: rating_to_stars(x, True)

View File

@ -14,7 +14,7 @@ from copy import deepcopy
from calibre.ebooks.metadata.book.base import Metadata, SIMPLE_GET, TOP_LEVEL_IDENTIFIERS, NULL_VALUES, ALL_METADATA_FIELDS from calibre.ebooks.metadata.book.base import Metadata, SIMPLE_GET, TOP_LEVEL_IDENTIFIERS, NULL_VALUES, ALL_METADATA_FIELDS
from calibre.ebooks.metadata.book.formatter import SafeFormat from calibre.ebooks.metadata.book.formatter import SafeFormat
from calibre.utils.date import utcnow from calibre.utils.date import utcnow
from polyglot.builtins import unicode_type, native_string_type from polyglot.builtins import native_string_type
# Lazy format metadata retrieval {{{ # Lazy format metadata retrieval {{{
''' '''
@ -46,7 +46,7 @@ class MutableBase:
@resolved @resolved
def __unicode__(self): def __unicode__(self):
return unicode_type(self._values) return str(self._values)
@resolved @resolved
def __len__(self): def __len__(self):

View File

@ -10,7 +10,7 @@ import os
from calibre import prints from calibre import prints
from calibre.utils.date import isoformat, DEFAULT_DATE from calibre.utils.date import isoformat, DEFAULT_DATE
from polyglot.builtins import itervalues, unicode_type from polyglot.builtins import itervalues
class SchemaUpgrade: class SchemaUpgrade:
@ -598,10 +598,10 @@ class SchemaUpgrade:
existing = frozenset(map(int, custom_recipes)) existing = frozenset(map(int, custom_recipes))
if id_ in existing: if id_ in existing:
id_ = max(existing) + 1000 id_ = max(existing) + 1000
id_ = unicode_type(id_) id_ = str(id_)
fname = custom_recipe_filename(id_, title) fname = custom_recipe_filename(id_, title)
custom_recipes[id_] = (title, fname) custom_recipes[id_] = (title, fname)
if isinstance(script, unicode_type): if isinstance(script, str):
script = script.encode('utf-8') script = script.encode('utf-8')
with open(os.path.join(bdir, fname), 'wb') as f: with open(os.path.join(bdir, fname), 'wb') as f:
f.write(script) f.write(script)

View File

@ -18,7 +18,7 @@ from calibre.utils.date import parse_date, UNDEFINED_DATE, now, dt_as_local
from calibre.utils.icu import primary_contains, sort_key from calibre.utils.icu import primary_contains, sort_key
from calibre.utils.localization import lang_map, canonicalize_lang from calibre.utils.localization import lang_map, canonicalize_lang
from calibre.utils.search_query_parser import SearchQueryParser, ParseException from calibre.utils.search_query_parser import SearchQueryParser, ParseException
from polyglot.builtins import iteritems, unicode_type, string_or_bytes from polyglot.builtins import iteritems, string_or_bytes
CONTAINS_MATCH = 0 CONTAINS_MATCH = 0
EQUALS_MATCH = 1 EQUALS_MATCH = 1
@ -149,7 +149,7 @@ class DateSearch: # {{{
if query == 'false': if query == 'false':
for v, book_ids in field_iter(): for v, book_ids in field_iter():
if isinstance(v, (bytes, unicode_type)): if isinstance(v, (bytes, str)):
if isinstance(v, bytes): if isinstance(v, bytes):
v = v.decode(preferred_encoding, 'replace') v = v.decode(preferred_encoding, 'replace')
v = parse_date(v) v = parse_date(v)
@ -159,7 +159,7 @@ class DateSearch: # {{{
if query == 'true': if query == 'true':
for v, book_ids in field_iter(): for v, book_ids in field_iter():
if isinstance(v, (bytes, unicode_type)): if isinstance(v, (bytes, str)):
if isinstance(v, bytes): if isinstance(v, bytes):
v = v.decode(preferred_encoding, 'replace') v = v.decode(preferred_encoding, 'replace')
v = parse_date(v) v = parse_date(v)
@ -413,7 +413,7 @@ class SavedSearchQueries: # {{{
return self._db() return self._db()
def force_unicode(self, x): def force_unicode(self, x):
if not isinstance(x, unicode_type): if not isinstance(x, str):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
return x return x

View File

@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en'
import unittest, os, shutil, tempfile, atexit, gc, time import unittest, os, shutil, tempfile, atexit, gc, time
from functools import partial from functools import partial
from io import BytesIO from io import BytesIO
from polyglot.builtins import unicode_type
rmtree = partial(shutil.rmtree, ignore_errors=True) rmtree = partial(shutil.rmtree, ignore_errors=True)
@ -82,7 +81,7 @@ class BaseTest(unittest.TestCase):
atexit.register(rmtree, self.clone_dir) atexit.register(rmtree, self.clone_dir)
self.clone_count = 0 self.clone_count = 0
self.clone_count += 1 self.clone_count += 1
dest = os.path.join(self.clone_dir, unicode_type(self.clone_count)) dest = os.path.join(self.clone_dir, str(self.clone_count))
shutil.copytree(library_path, dest) shutil.copytree(library_path, dest)
return dest return dest

View File

@ -12,7 +12,7 @@ from operator import itemgetter
from calibre.library.field_metadata import fm_as_dict from calibre.library.field_metadata import fm_as_dict
from calibre.db.tests.base import BaseTest from calibre.db.tests.base import BaseTest
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
from polyglot import reprlib from polyglot import reprlib
# Utils {{{ # Utils {{{
@ -116,7 +116,7 @@ class LegacyTest(BaseTest):
for label, loc in iteritems(db.FIELD_MAP): for label, loc in iteritems(db.FIELD_MAP):
if isinstance(label, numbers.Integral): if isinstance(label, numbers.Integral):
label = '#'+db.custom_column_num_map[label]['label'] label = '#'+db.custom_column_num_map[label]['label']
label = unicode_type(label) label = str(label)
ans[label] = tuple(db.get_property(i, index_is_id=True, loc=loc) ans[label] = tuple(db.get_property(i, index_is_id=True, loc=loc)
for i in db.all_ids()) for i in db.all_ids())
if label in ('id', 'title', '#tags'): if label in ('id', 'title', '#tags'):
@ -282,7 +282,7 @@ class LegacyTest(BaseTest):
old = db.get_data_as_dict(prefix='test-prefix') old = db.get_data_as_dict(prefix='test-prefix')
new = ndb.get_data_as_dict(prefix='test-prefix') new = ndb.get_data_as_dict(prefix='test-prefix')
for o, n in zip(old, new): for o, n in zip(old, new):
o = {unicode_type(k) if isinstance(k, bytes) else k:set(v) if isinstance(v, list) else v for k, v in iteritems(o)} o = {str(k) if isinstance(k, bytes) else k:set(v) if isinstance(v, list) else v for k, v in iteritems(o)}
n = {k:set(v) if isinstance(v, list) else v for k, v in iteritems(n)} n = {k:set(v) if isinstance(v, list) else v for k, v in iteritems(n)}
self.assertEqual(o, n) self.assertEqual(o, n)

View File

@ -15,7 +15,7 @@ from calibre.ebooks.metadata.book.base import Metadata
from calibre.utils.date import UNDEFINED_DATE from calibre.utils.date import UNDEFINED_DATE
from calibre.db.tests.base import BaseTest, IMG from calibre.db.tests.base import BaseTest, IMG
from calibre.db.backend import FTSQueryError from calibre.db.backend import FTSQueryError
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
class WritingTest(BaseTest): class WritingTest(BaseTest):
@ -664,11 +664,11 @@ class WritingTest(BaseTest):
def test_set_author_data(self): # {{{ def test_set_author_data(self): # {{{
cache = self.init_cache() cache = self.init_cache()
adata = cache.author_data() adata = cache.author_data()
ldata = {aid:unicode_type(aid) for aid in adata} ldata = {aid:str(aid) for aid in adata}
self.assertEqual({1,2,3}, cache.set_link_for_authors(ldata)) self.assertEqual({1,2,3}, cache.set_link_for_authors(ldata))
for c in (cache, self.init_cache()): for c in (cache, self.init_cache()):
self.assertEqual(ldata, {aid:d['link'] for aid, d in iteritems(c.author_data())}) self.assertEqual(ldata, {aid:d['link'] for aid, d in iteritems(c.author_data())})
self.assertEqual({3}, cache.set_link_for_authors({aid:'xxx' if aid == max(adata) else unicode_type(aid) for aid in adata}), self.assertEqual({3}, cache.set_link_for_authors({aid:'xxx' if aid == max(adata) else str(aid) for aid in adata}),
'Setting the author link to the same value as before, incorrectly marked some books as dirty') 'Setting the author link to the same value as before, incorrectly marked some books as dirty')
sdata = {aid:'%s, changed' % aid for aid in adata} sdata = {aid:'%s, changed' % aid for aid in adata}
self.assertEqual({1,2,3}, cache.set_sort_for_authors(sdata)) self.assertEqual({1,2,3}, cache.set_sort_for_authors(sdata))

View File

@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import os, errno, sys, re import os, errno, sys, re
from locale import localeconv from locale import localeconv
from collections import OrderedDict, namedtuple from collections import OrderedDict, namedtuple
from polyglot.builtins import iteritems, itervalues, unicode_type, string_or_bytes from polyglot.builtins import iteritems, itervalues, string_or_bytes
from threading import Lock from threading import Lock
from calibre import as_unicode, prints from calibre import as_unicode, prints
@ -18,7 +18,7 @@ from calibre.utils.localization import canonicalize_lang
def force_to_bool(val): def force_to_bool(val):
if isinstance(val, (bytes, unicode_type)): if isinstance(val, (bytes, str)):
if isinstance(val, bytes): if isinstance(val, bytes):
val = val.decode(preferred_encoding, 'replace') val = val.decode(preferred_encoding, 'replace')
try: try:
@ -227,7 +227,7 @@ class ThumbnailCache:
def _write_order(self): def _write_order(self):
if hasattr(self, 'items'): if hasattr(self, 'items'):
try: try:
data = '\n'.join(group_id + ' ' + unicode_type(book_id) for (group_id, book_id) in self.items) data = '\n'.join(group_id + ' ' + str(book_id) for (group_id, book_id) in self.items)
with lopen(os.path.join(self.location, 'order'), 'wb') as f: with lopen(os.path.join(self.location, 'order'), 'wb') as f:
f.write(data.encode('utf-8')) f.write(data.encode('utf-8'))
except EnvironmentError as err: except EnvironmentError as err:

View File

@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en'
import weakref, operator, numbers import weakref, operator, numbers
from functools import partial from functools import partial
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
from calibre.ebooks.metadata import title_sort from calibre.ebooks.metadata import title_sort
from calibre.utils.config_base import tweaks, prefs from calibre.utils.config_base import tweaks, prefs
@ -376,7 +376,7 @@ class View:
self.marked_ids = dict.fromkeys(id_dict, u'true') self.marked_ids = dict.fromkeys(id_dict, u'true')
else: else:
# Ensure that all the items in the dict are text # Ensure that all the items in the dict are text
self.marked_ids = {k: unicode_type(v) for k, v in iteritems(id_dict)} self.marked_ids = {k: str(v) for k, v in iteritems(id_dict)}
# This invalidates all searches in the cache even though the cache may # This invalidates all searches in the cache even though the cache may
# be shared by multiple views. This is not ideal, but... # be shared by multiple views. This is not ideal, but...
cmids = set(self.marked_ids) cmids = set(self.marked_ids)

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import re import re
from functools import partial from functools import partial
from datetime import datetime from datetime import datetime
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
from calibre.constants import preferred_encoding from calibre.constants import preferred_encoding
from calibre.ebooks.metadata import author_to_author_sort, title_sort from calibre.ebooks.metadata import author_to_author_sort, title_sort
@ -30,7 +30,7 @@ def sqlite_datetime(x):
def single_text(x): def single_text(x):
if x is None: if x is None:
return x return x
if not isinstance(x, unicode_type): if not isinstance(x, str):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
x = x.strip() x = x.strip()
return x if x else None return x if x else None
@ -58,7 +58,7 @@ def multiple_text(sep, ui_sep, x):
return () return ()
if isinstance(x, bytes): if isinstance(x, bytes):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
if isinstance(x, unicode_type): if isinstance(x, str):
x = x.split(sep) x = x.split(sep)
else: else:
x = (y.decode(preferred_encoding, 'replace') if isinstance(y, bytes) x = (y.decode(preferred_encoding, 'replace') if isinstance(y, bytes)
@ -70,7 +70,7 @@ def multiple_text(sep, ui_sep, x):
def adapt_datetime(x): def adapt_datetime(x):
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
x = parse_date(x, assume_utc=False, as_utc=False) x = parse_date(x, assume_utc=False, as_utc=False)
if x and is_date_undefined(x): if x and is_date_undefined(x):
x = UNDEFINED_DATE x = UNDEFINED_DATE
@ -78,7 +78,7 @@ def adapt_datetime(x):
def adapt_date(x): def adapt_date(x):
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
x = parse_only_date(x) x = parse_only_date(x)
if x is None or is_date_undefined(x): if x is None or is_date_undefined(x):
x = UNDEFINED_DATE x = UNDEFINED_DATE
@ -88,7 +88,7 @@ def adapt_date(x):
def adapt_number(typ, x): def adapt_number(typ, x):
if x is None: if x is None:
return None return None
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
if isinstance(x, bytes): if isinstance(x, bytes):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
if not x or x.lower() == 'none': if not x or x.lower() == 'none':
@ -97,7 +97,7 @@ def adapt_number(typ, x):
def adapt_bool(x): def adapt_bool(x):
if isinstance(x, (unicode_type, bytes)): if isinstance(x, (str, bytes)):
if isinstance(x, bytes): if isinstance(x, bytes):
x = x.decode(preferred_encoding, 'replace') x = x.decode(preferred_encoding, 'replace')
x = x.lower() x = x.lower()

View File

@ -12,7 +12,7 @@ from calibre.utils.config import OptionParser
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre import prints from calibre import prints
from calibre.startup import get_debug_executable from calibre.startup import get_debug_executable
from polyglot.builtins import exec_path, unicode_type from polyglot.builtins import exec_path
def run_calibre_debug(*args, **kw): def run_calibre_debug(*args, **kw):
@ -204,7 +204,7 @@ def print_basic_debug_info(out=None):
out('Linux:', platform.linux_distribution()) out('Linux:', platform.linux_distribution())
except: except:
pass pass
out('Interface language:', unicode_type(set_translators.lang)) out('Interface language:', str(set_translators.lang))
from calibre.customize.ui import has_external_plugins, initialized_plugins from calibre.customize.ui import has_external_plugins, initialized_plugins
if has_external_plugins(): if has_external_plugins():
from calibre.customize import PluginInstallationType from calibre.customize import PluginInstallationType

View File

@ -8,7 +8,6 @@ Device drivers.
import sys, time, pprint import sys, time, pprint
from functools import partial from functools import partial
from polyglot.builtins import unicode_type
DAY_MAP = dict(Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6) DAY_MAP = dict(Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6)
MONTH_MAP = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12) MONTH_MAP = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)
@ -19,8 +18,8 @@ INVERSE_MONTH_MAP = dict(zip(MONTH_MAP.values(), MONTH_MAP.keys()))
def strptime(src): def strptime(src):
src = src.strip() src = src.strip()
src = src.split() src = src.split()
src[0] = unicode_type(DAY_MAP[src[0][:-1]])+',' src[0] = str(DAY_MAP[src[0][:-1]])+','
src[2] = unicode_type(MONTH_MAP[src[2]]) src[2] = str(MONTH_MAP[src[2]])
return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z') return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z')

View File

@ -17,7 +17,6 @@ from calibre.devices.errors import ArgumentError, DeviceError, DeviceLocked
from calibre.customize.ui import device_plugins from calibre.customize.ui import device_plugins
from calibre.devices.scanner import DeviceScanner from calibre.devices.scanner import DeviceScanner
from calibre.utils.config import device_prefs from calibre.utils.config import device_prefs
from polyglot.builtins import unicode_type
from polyglot.io import PolyglotStringIO from polyglot.io import PolyglotStringIO
MINIMUM_COL_WIDTH = 12 # : Minimum width of columns in ls output MINIMUM_COL_WIDTH = 12 # : Minimum width of columns in ls output
@ -125,7 +124,7 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
maxlen = 0 maxlen = 0
if ll: # Calculate column width for size column if ll: # Calculate column width for size column
for file in files: for file in files:
size = len(unicode_type(file.size)) size = len(str(file.size))
if human_readable_size: if human_readable_size:
file = FileFormatter(file) file = FileFormatter(file)
size = len(file.human_readable_size) size = len(file.human_readable_size)
@ -137,10 +136,10 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0):
lsoutput.append(name) lsoutput.append(name)
lscoloutput.append(name) lscoloutput.append(name)
if ll: if ll:
size = unicode_type(file.size) size = str(file.size)
if human_readable_size: if human_readable_size:
size = file.human_readable_size size = file.human_readable_size
prints(file.mode_string, ("%"+unicode_type(maxlen)+"s")%size, file.modification_time, name, file=output) prints(file.mode_string, ("%"+str(maxlen)+"s")%size, file.modification_time, name, file=output)
if not ll and len(lsoutput) > 0: if not ll and len(lsoutput) > 0:
trytable = [] trytable = []
for colwidth in range(MINIMUM_COL_WIDTH, cols): for colwidth in range(MINIMUM_COL_WIDTH, cols):
@ -244,7 +243,7 @@ def main():
print("Filesystem\tSize \tUsed \tAvail \tUse%") print("Filesystem\tSize \tUsed \tAvail \tUse%")
for i in range(3): for i in range(3):
print("%-10s\t%s\t%s\t%s\t%s"%(where[i], human_readable(total[i]), human_readable(total[i]-free[i]), human_readable(free[i]), print("%-10s\t%s\t%s\t%s\t%s"%(where[i], human_readable(total[i]), human_readable(total[i]-free[i]), human_readable(free[i]),
unicode_type(0 if total[i]==0 else int(100*(total[i]-free[i])/(total[i]*1.)))+"%")) str(0 if total[i]==0 else int(100*(total[i]-free[i])/(total[i]*1.)))+"%"))
elif command == 'eject': elif command == 'eject':
dev.eject() dev.eject()
elif command == "books": elif command == "books":

File diff suppressed because one or more lines are too long

View File

@ -8,7 +8,6 @@ Defines the errors that the device drivers generate.
G{classtree ProtocolError} G{classtree ProtocolError}
""" """
from polyglot.builtins import unicode_type
class ProtocolError(Exception): class ProtocolError(Exception):
@ -95,7 +94,7 @@ class DeviceBusy(ProtocolError):
def __init__(self, uerr=""): def __init__(self, uerr=""):
ProtocolError.__init__( ProtocolError.__init__(
self, "Device is in use by another application:" self, "Device is in use by another application:"
"\nUnderlying error:" + unicode_type(uerr) "\nUnderlying error:" + str(uerr)
) )
@ -138,9 +137,9 @@ class ControlError(ProtocolError):
def __str__(self): def __str__(self):
if self.query and self.response: if self.query and self.response:
return "Got unexpected response:\n" + \ return "Got unexpected response:\n" + \
"query:\n"+unicode_type(self.query.query)+"\n"+\ "query:\n"+str(self.query.query)+"\n"+\
"expected:\n"+unicode_type(self.query.response)+"\n" +\ "expected:\n"+str(self.query.response)+"\n" +\
"actual:\n"+unicode_type(self.response) "actual:\n"+str(self.response)
if self.desc: if self.desc:
return self.desc return self.desc
return "Unknown control error occurred" return "Unknown control error occurred"

View File

@ -15,7 +15,6 @@ import re
from calibre.constants import filesystem_encoding from calibre.constants import filesystem_encoding
from calibre.devices.usbms.driver import USBMS from calibre.devices.usbms.driver import USBMS
from calibre.ebooks.metadata import string_to_authors from calibre.ebooks.metadata import string_to_authors
from polyglot.builtins import unicode_type
class JETBOOK(USBMS): class JETBOOK(USBMS):
@ -65,7 +64,7 @@ class JETBOOK(USBMS):
def metadata_from_path(cls, path): def metadata_from_path(cls, path):
def check_unicode(txt): def check_unicode(txt):
if not isinstance(txt, unicode_type): if not isinstance(txt, str):
txt = txt.decode(filesystem_encoding, 'replace') txt = txt.decode(filesystem_encoding, 'replace')
txt = txt.replace('_', ' ') txt = txt.replace('_', ' ')
return txt return txt

View File

@ -18,7 +18,7 @@ from calibre.ebooks.mobi.reader.headers import MetadataHeader
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
from calibre import prints, fsync from calibre import prints, fsync
from calibre.constants import DEBUG from calibre.constants import DEBUG
from polyglot.builtins import as_unicode, as_bytes, unicode_type from polyglot.builtins import as_unicode, as_bytes
class APNXBuilder: class APNXBuilder:
@ -33,7 +33,7 @@ class APNXBuilder:
using either the fast or accurate algorithm. using either the fast or accurate algorithm.
''' '''
import uuid import uuid
apnx_meta = {'guid': unicode_type(uuid.uuid4()).replace('-', '')[:8], 'asin': apnx_meta = {'guid': str(uuid.uuid4()).replace('-', '')[:8], 'asin':
'', 'cdetype': 'EBOK', 'format': 'MOBI_7', 'acr': ''} '', 'cdetype': 'EBOK', 'format': 'MOBI_7', 'acr': ''}
with lopen(mobi_file_path, 'rb') as mf: with lopen(mobi_file_path, 'rb') as mf:
@ -53,11 +53,11 @@ class APNXBuilder:
if mh.exth is None or not mh.exth.cdetype: if mh.exth is None or not mh.exth.cdetype:
apnx_meta['cdetype'] = 'EBOK' apnx_meta['cdetype'] = 'EBOK'
else: else:
apnx_meta['cdetype'] = unicode_type(mh.exth.cdetype) apnx_meta['cdetype'] = str(mh.exth.cdetype)
if mh.exth is None or not mh.exth.uuid: if mh.exth is None or not mh.exth.uuid:
apnx_meta['asin'] = '' apnx_meta['asin'] = ''
else: else:
apnx_meta['asin'] = unicode_type(mh.exth.uuid) apnx_meta['asin'] = str(mh.exth.uuid)
# Get the pages depending on the chosen parser # Get the pages depending on the chosen parser
pages = [] pages = []

View File

@ -15,7 +15,7 @@ from calibre.constants import DEBUG, filesystem_encoding
from calibre.devices.kindle.bookmark import Bookmark from calibre.devices.kindle.bookmark import Bookmark
from calibre.devices.usbms.driver import USBMS from calibre.devices.usbms.driver import USBMS
from calibre import strftime, fsync, prints from calibre import strftime, fsync, prints
from polyglot.builtins import unicode_type, as_bytes, as_unicode from polyglot.builtins import as_bytes, as_unicode
''' '''
Notes on collections: Notes on collections:
@ -610,7 +610,7 @@ class KINDLE2(KINDLE):
cust_col_name = opts.extra_customization[self.OPT_APNX_METHOD_COL] cust_col_name = opts.extra_customization[self.OPT_APNX_METHOD_COL]
if cust_col_name: if cust_col_name:
try: try:
temp = unicode_type(metadata.get(cust_col_name)).lower() temp = str(metadata.get(cust_col_name)).lower()
if temp in self.EXTRA_CUSTOMIZATION_CHOICES[self.OPT_APNX_METHOD]: if temp in self.EXTRA_CUSTOMIZATION_CHOICES[self.OPT_APNX_METHOD]:
method = temp method = temp
else: else:

View File

@ -33,7 +33,7 @@ from calibre import prints, fsync
from calibre.ptempfile import PersistentTemporaryFile, better_mktemp from calibre.ptempfile import PersistentTemporaryFile, better_mktemp
from calibre.constants import DEBUG from calibre.constants import DEBUG
from calibre.utils.config_base import prefs from calibre.utils.config_base import prefs
from polyglot.builtins import iteritems, itervalues, unicode_type, string_or_bytes from polyglot.builtins import iteritems, itervalues, string_or_bytes
EPUB_EXT = '.epub' EPUB_EXT = '.epub'
KEPUB_EXT = '.kepub' KEPUB_EXT = '.kepub'
@ -47,7 +47,7 @@ def qhash(inputstr):
instr = b"" instr = b""
if isinstance(inputstr, bytes): if isinstance(inputstr, bytes):
instr = inputstr instr = inputstr
elif isinstance(inputstr, unicode_type): elif isinstance(inputstr, str):
instr = inputstr.encode("utf8") instr = inputstr.encode("utf8")
else: else:
return -1 return -1
@ -377,7 +377,7 @@ class KOBO(USBMS):
try: try:
cursor.execute(query) cursor.execute(query)
except Exception as e: except Exception as e:
err = unicode_type(e) err = str(e)
if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded')): if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded')):
raise raise
query= ('select Title, Attribution, DateCreated, ContentID, MimeType, ContentType, ' query= ('select Title, Attribution, DateCreated, ContentID, MimeType, ContentType, '
@ -483,13 +483,13 @@ class KOBO(USBMS):
cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0, ___ExpirationStatus=3 ' cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0, ___ExpirationStatus=3 '
'where BookID is Null and ContentID =?',t) 'where BookID is Null and ContentID =?',t)
except Exception as e: except Exception as e:
if 'no such column' not in unicode_type(e): if 'no such column' not in str(e):
raise raise
try: try:
cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0 ' cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\', ___PercentRead=0 '
'where BookID is Null and ContentID =?',t) 'where BookID is Null and ContentID =?',t)
except Exception as e: except Exception as e:
if 'no such column' not in unicode_type(e): if 'no such column' not in str(e):
raise raise
cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\' ' cursor.execute('update content set ReadStatus=0, FirstTimeReading = \'true\' '
'where BookID is Null and ContentID =?',t) 'where BookID is Null and ContentID =?',t)
@ -833,7 +833,7 @@ class KOBO(USBMS):
cursor.execute(query) cursor.execute(query)
except Exception as e: except Exception as e:
debug_print(' Database Exception: Unable to reset Shortlist list') debug_print(' Database Exception: Unable to reset Shortlist list')
if 'no such column' not in unicode_type(e): if 'no such column' not in str(e):
raise raise
finally: finally:
cursor.close() cursor.close()
@ -847,7 +847,7 @@ class KOBO(USBMS):
cursor.execute('update content set FavouritesIndex=1 where BookID is Null and ContentID = ?', t) cursor.execute('update content set FavouritesIndex=1 where BookID is Null and ContentID = ?', t)
except Exception as e: except Exception as e:
debug_print(' Database Exception: Unable set book as Shortlist') debug_print(' Database Exception: Unable set book as Shortlist')
if 'no such column' not in unicode_type(e): if 'no such column' not in str(e):
raise raise
finally: finally:
cursor.close() cursor.close()
@ -1808,7 +1808,7 @@ class KOBOTOUCH(KOBO):
debug_print('KoboTouch:update_booklist - book file does not exist. ContentID="%s"'%ContentID) debug_print('KoboTouch:update_booklist - book file does not exist. ContentID="%s"'%ContentID)
except Exception as e: except Exception as e:
debug_print("KoboTouch:update_booklist - exception creating book: '%s'"%unicode_type(e)) debug_print("KoboTouch:update_booklist - exception creating book: '%s'"%str(e))
debug_print(" prefix: ", prefix, "lpath: ", lpath, "title: ", title, "authors: ", authors, debug_print(" prefix: ", prefix, "lpath: ", lpath, "title: ", title, "authors: ", authors,
"MimeType: ", MimeType, "DateCreated: ", DateCreated, "ContentType: ", ContentType, "ImageID: ", ImageID) "MimeType: ", MimeType, "DateCreated: ", DateCreated, "ContentType: ", ContentType, "ImageID: ", ImageID)
raise raise
@ -1870,7 +1870,7 @@ class KOBOTOUCH(KOBO):
bookshelves.append(row['ShelfName']) bookshelves.append(row['ShelfName'])
cursor.close() cursor.close()
# debug_print("KoboTouch:get_bookshelvesforbook - count bookshelves=" + unicode_type(count_bookshelves)) # debug_print("KoboTouch:get_bookshelvesforbook - count bookshelves=" + str(count_bookshelves))
return bookshelves return bookshelves
self.debug_index = 0 self.debug_index = 0
@ -1963,7 +1963,7 @@ class KOBOTOUCH(KOBO):
try: try:
cursor.execute(query) cursor.execute(query)
except Exception as e: except Exception as e:
err = unicode_type(e) err = str(e)
if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded', 'Series', 'ExternalId')): if not (any_in(err, '___ExpirationStatus', 'FavouritesIndex', 'Accessibility', 'IsDownloaded', 'Series', 'ExternalId')):
raise raise
query= ('SELECT Title, Attribution, DateCreated, ContentID, MimeType, ContentType, ' query= ('SELECT Title, Attribution, DateCreated, ContentID, MimeType, ContentType, '
@ -2174,7 +2174,7 @@ class KOBOTOUCH(KOBO):
cursor.close() cursor.close()
except Exception as e: except Exception as e:
debug_print('KoboTouch:upload_books - Exception: %s'%unicode_type(e)) debug_print('KoboTouch:upload_books - Exception: %s'%str(e))
return result return result
@ -2318,7 +2318,7 @@ class KOBOTOUCH(KOBO):
debug_print('KoboTouch:delete_via_sql: finished SQL') debug_print('KoboTouch:delete_via_sql: finished SQL')
debug_print('KoboTouch:delete_via_sql: After SQL, no exception') debug_print('KoboTouch:delete_via_sql: After SQL, no exception')
except Exception as e: except Exception as e:
debug_print('KoboTouch:delete_via_sql - Database Exception: %s'%unicode_type(e)) debug_print('KoboTouch:delete_via_sql - Database Exception: %s'%str(e))
debug_print('KoboTouch:delete_via_sql: imageId="%s"'%imageId) debug_print('KoboTouch:delete_via_sql: imageId="%s"'%imageId)
if imageId is None: if imageId is None:
@ -2451,7 +2451,7 @@ class KOBOTOUCH(KOBO):
if self.manage_collections: if self.manage_collections:
if collections: if collections:
# debug_print("KoboTouch:update_device_database_collections - length collections=" + unicode_type(len(collections))) # debug_print("KoboTouch:update_device_database_collections - length collections=" + str(len(collections)))
# Need to reset the collections outside the particular loops # Need to reset the collections outside the particular loops
# otherwise the last item will not be removed # otherwise the last item will not be removed
@ -2621,7 +2621,7 @@ class KOBOTOUCH(KOBO):
self.keep_cover_aspect, self.letterbox_fs_covers, self.png_covers, self.keep_cover_aspect, self.letterbox_fs_covers, self.png_covers,
letterbox_color=self.letterbox_fs_covers_color) letterbox_color=self.letterbox_fs_covers_color)
except Exception as e: except Exception as e:
debug_print('KoboTouch: FAILED to upload cover=%s Exception=%s'%(filepath, unicode_type(e))) debug_print('KoboTouch: FAILED to upload cover=%s Exception=%s'%(filepath, str(e)))
def imageid_from_contentid(self, ContentID): def imageid_from_contentid(self, ContentID):
ImageID = ContentID.replace('/', '_') ImageID = ContentID.replace('/', '_')
@ -2831,7 +2831,7 @@ class KOBOTOUCH(KOBO):
f.write(data) f.write(data)
fsync(f) fsync(f)
except Exception as e: except Exception as e:
err = unicode_type(e) err = str(e)
debug_print("KoboTouch:_upload_cover - Exception string: %s"%err) debug_print("KoboTouch:_upload_cover - Exception string: %s"%err)
raise raise
@ -2978,7 +2978,7 @@ class KOBOTOUCH(KOBO):
# count_bookshelves = i + 1 # count_bookshelves = i + 1
cursor.close() cursor.close()
# debug_print("KoboTouch:get_bookshelflist - count bookshelves=" + unicode_type(count_bookshelves)) # debug_print("KoboTouch:get_bookshelflist - count bookshelves=" + str(count_bookshelves))
return bookshelves return bookshelves
@ -3062,7 +3062,7 @@ class KOBOTOUCH(KOBO):
cursor.execute(addquery, add_values) cursor.execute(addquery, add_values)
elif result['_IsDeleted'] == 'true': elif result['_IsDeleted'] == 'true':
debug_print("KoboTouch:check_for_bookshelf - Shelf '%s' is deleted - undeleting. result['_IsDeleted']='%s'" % ( debug_print("KoboTouch:check_for_bookshelf - Shelf '%s' is deleted - undeleting. result['_IsDeleted']='%s'" % (
bookshelf_name, unicode_type(result['_IsDeleted']))) bookshelf_name, str(result['_IsDeleted'])))
cursor.execute(updatequery, test_values) cursor.execute(updatequery, test_values)
cursor.close() cursor.close()

View File

@ -16,7 +16,6 @@ from calibre.devices.usbms.driver import debug_print
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
from calibre.gui2.widgets2 import ColorButton from calibre.gui2.widgets2 import ColorButton
from calibre.gui2.dialogs.template_dialog import TemplateDialog from calibre.gui2.dialogs.template_dialog import TemplateDialog
from polyglot.builtins import unicode_type
def wrap_msg(msg): def wrap_msg(msg):
@ -128,7 +127,7 @@ class KOBOTOUCHConfig(TabbedDeviceConfig):
p['support_newer_firmware'] = self.support_newer_firmware p['support_newer_firmware'] = self.support_newer_firmware
p['debugging_title'] = self.debugging_title p['debugging_title'] = self.debugging_title
p['driver_version'] = '.'.join([unicode_type(i) for i in self.device.version]) p['driver_version'] = '.'.join([str(i) for i in self.device.version])
return p return p
@ -480,7 +479,7 @@ class AdvancedGroupBox(DeviceOptionsGroupBox):
'to perform full read-write functionality - Here be Dragons!! ' 'to perform full read-write functionality - Here be Dragons!! '
'Enable only if you are comfortable with restoring your kobo ' 'Enable only if you are comfortable with restoring your kobo '
'to factory defaults and testing software. ' 'to factory defaults and testing software. '
'This driver supports firmware V2.x.x and DBVersion up to ') + unicode_type( 'This driver supports firmware V2.x.x and DBVersion up to ') + str(
device.supported_dbversion), device.get_pref('support_newer_firmware') device.supported_dbversion), device.get_pref('support_newer_firmware')
) )
@ -638,7 +637,7 @@ class TemplateConfig(QWidget): # {{{
@property @property
def template(self): def template(self):
return unicode_type(self.t.text()).strip() return str(self.t.text()).strip()
@template.setter @template.setter
def template(self, template): def template(self, template):
@ -660,7 +659,7 @@ class TemplateConfig(QWidget): # {{{
except Exception as err: except Exception as err:
error_dialog(self, _('Invalid template'), error_dialog(self, _('Invalid template'),
'<p>'+_('The template "%s" is invalid:')%tmpl + '<p>'+_('The template "%s" is invalid:')%tmpl +
'<br>'+unicode_type(err), show=True) '<br>'+str(err), show=True)
return False return False
# }}} # }}}

View File

@ -16,7 +16,7 @@ from calibre.devices.mtp.base import debug
from calibre.devices.mtp.defaults import DeviceDefaults from calibre.devices.mtp.defaults import DeviceDefaults
from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory
from calibre.utils.filenames import shorten_components_to from calibre.utils.filenames import shorten_components_to
from polyglot.builtins import iteritems, itervalues, unicode_type, as_bytes from polyglot.builtins import iteritems, itervalues, as_bytes
BASE = importlib.import_module('calibre.devices.mtp.%s.driver'%( BASE = importlib.import_module('calibre.devices.mtp.%s.driver'%(
'windows' if iswindows else 'unix')).MTP_DEVICE 'windows' if iswindows else 'unix')).MTP_DEVICE
@ -76,7 +76,7 @@ class MTP_DEVICE(BASE):
def is_folder_ignored(self, storage_or_storage_id, path, def is_folder_ignored(self, storage_or_storage_id, path,
ignored_folders=None): ignored_folders=None):
storage_id = unicode_type(getattr(storage_or_storage_id, 'object_id', storage_id = str(getattr(storage_or_storage_id, 'object_id',
storage_or_storage_id)) storage_or_storage_id))
lpath = tuple(icu_lower(name) for name in path) lpath = tuple(icu_lower(name) for name in path)
if ignored_folders is None: if ignored_folders is None:
@ -168,14 +168,14 @@ class MTP_DEVICE(BASE):
traceback.print_exc() traceback.print_exc()
dinfo = {} dinfo = {}
if dinfo.get('device_store_uuid', None) is None: if dinfo.get('device_store_uuid', None) is None:
dinfo['device_store_uuid'] = unicode_type(uuid.uuid4()) dinfo['device_store_uuid'] = str(uuid.uuid4())
if dinfo.get('device_name', None) is None: if dinfo.get('device_name', None) is None:
dinfo['device_name'] = self.current_friendly_name dinfo['device_name'] = self.current_friendly_name
if name is not None: if name is not None:
dinfo['device_name'] = name dinfo['device_name'] = name
dinfo['location_code'] = location_code dinfo['location_code'] = location_code
dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None) dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None)
dinfo['calibre_version'] = '.'.join([unicode_type(i) for i in numeric_version]) dinfo['calibre_version'] = '.'.join([str(i) for i in numeric_version])
dinfo['date_last_connected'] = isoformat(now()) dinfo['date_last_connected'] = isoformat(now())
dinfo['mtp_prefix'] = storage.storage_prefix dinfo['mtp_prefix'] = storage.storage_prefix
raw = as_bytes(json.dumps(dinfo, default=to_json)) raw = as_bytes(json.dumps(dinfo, default=to_json))

View File

@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en'
import weakref, sys, json import weakref, sys, json
from collections import deque from collections import deque
from operator import attrgetter from operator import attrgetter
from polyglot.builtins import itervalues, unicode_type from polyglot.builtins import itervalues
from datetime import datetime from datetime import datetime
from calibre import human_readable, prints, force_unicode from calibre import human_readable, prints, force_unicode
@ -73,7 +73,7 @@ class FileOrFolder:
def __repr__(self): def __repr__(self):
name = 'Folder' if self.is_folder else 'File' name = 'Folder' if self.is_folder else 'File'
try: try:
path = unicode_type(self.full_path) path = str(self.full_path)
except: except:
path = '' path = ''
datum = 'size=%s'%(self.size) datum = 'size=%s'%(self.size)

View File

@ -15,7 +15,6 @@ from calibre.constants import islinux, ismacos
from calibre.ptempfile import SpooledTemporaryFile from calibre.ptempfile import SpooledTemporaryFile
from calibre.devices.errors import OpenFailed, DeviceError, BlacklistedDevice, OpenActionNeeded from calibre.devices.errors import OpenFailed, DeviceError, BlacklistedDevice, OpenActionNeeded
from calibre.devices.mtp.base import MTPDeviceBase, synchronous, debug from calibre.devices.mtp.base import MTPDeviceBase, synchronous, debug
from polyglot.builtins import unicode_type
MTPDevice = namedtuple('MTPDevice', 'busnum devnum vendor_id product_id ' MTPDevice = namedtuple('MTPDevice', 'busnum devnum vendor_id product_id '
'bcd serial manufacturer product') 'bcd serial manufacturer product')
@ -222,7 +221,7 @@ class MTP_DEVICE(MTPDeviceBase):
try: try:
storage = sorted(self.dev.storage_info, key=operator.itemgetter('id')) storage = sorted(self.dev.storage_info, key=operator.itemgetter('id'))
except self.libmtp.MTPError as e: except self.libmtp.MTPError as e:
if "The device has no storage information." in unicode_type(e): if "The device has no storage information." in str(e):
# This happens on newer Android devices while waiting for # This happens on newer Android devices while waiting for
# the user to allow access. Apparently what happens is # the user to allow access. Apparently what happens is
# that when the user clicks allow, the device disconnects # that when the user clicks allow, the device disconnects
@ -317,7 +316,7 @@ class MTP_DEVICE(MTPDeviceBase):
storage.append({'id':sid, 'size':capacity, storage.append({'id':sid, 'size':capacity,
'is_folder':True, 'name':name, 'can_delete':False, 'is_folder':True, 'name':name, 'can_delete':False,
'is_system':True}) 'is_system':True})
self._currently_getting_sid = unicode_type(sid) self._currently_getting_sid = str(sid)
items, errs = self.dev.get_filesystem(sid, items, errs = self.dev.get_filesystem(sid,
partial(self._filesystem_callback, {})) partial(self._filesystem_callback, {}))
all_items.extend(items), all_errs.extend(errs) all_items.extend(items), all_errs.extend(errs)

View File

@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en'
import time, threading, traceback import time, threading, traceback
from functools import wraps, partial from functools import wraps, partial
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
from itertools import chain from itertools import chain
from calibre import as_unicode, prints, force_unicode from calibre import as_unicode, prints, force_unicode
@ -268,7 +268,7 @@ class MTP_DEVICE(MTPDeviceBase):
break break
storage = {'id':storage_id, 'size':capacity, 'name':name, storage = {'id':storage_id, 'size':capacity, 'name':name,
'is_folder':True, 'can_delete':False, 'is_system':True} 'is_folder':True, 'can_delete':False, 'is_system':True}
self._currently_getting_sid = unicode_type(storage_id) self._currently_getting_sid = str(storage_id)
id_map = self.dev.get_filesystem(storage_id, partial( id_map = self.dev.get_filesystem(storage_id, partial(
self._filesystem_callback, {})) self._filesystem_callback, {}))
for x in itervalues(id_map): for x in itervalues(id_map):

View File

@ -17,7 +17,6 @@ from calibre.ebooks.chardet import xml_to_unicode
from calibre.ebooks.metadata import authors_to_string, title_sort, \ from calibre.ebooks.metadata import authors_to_string, title_sort, \
authors_to_sort_string authors_to_sort_string
from polyglot.binary import from_base64_bytes from polyglot.binary import from_base64_bytes
from polyglot.builtins import unicode_type
''' '''
cacheExt.xml cacheExt.xml
@ -66,8 +65,8 @@ INVERSE_MONTH_MAP = dict(zip(MONTH_MAP.values(), MONTH_MAP.keys()))
def strptime(src): def strptime(src):
src = src.strip() src = src.strip()
src = src.split() src = src.split()
src[0] = unicode_type(DAY_MAP[src[0][:-1]])+',' src[0] = str(DAY_MAP[src[0][:-1]])+','
src[2] = unicode_type(MONTH_MAP[src[2]]) src[2] = str(MONTH_MAP[src[2]])
return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z') return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z')
@ -84,7 +83,7 @@ def strftime(epoch, zone=time.localtime):
def uuid(): def uuid():
from uuid import uuid4 from uuid import uuid4
return unicode_type(uuid4()).replace('-', '', 1).upper() return str(uuid4()).replace('-', '', 1).upper()
# }}} # }}}
@ -197,8 +196,8 @@ class XMLCache:
playlist.set('title', title) playlist.set('title', title)
if title in seen: if title in seen:
for i in range(2, 1000): for i in range(2, 1000):
if title+unicode_type(i) not in seen: if title+str(i) not in seen:
title = title+unicode_type(i) title = title+str(i)
playlist.set('title', title) playlist.set('title', title)
seen.add(title) seen.add(title)
break break
@ -271,7 +270,7 @@ class XMLCache:
nsmap=root.nsmap, attrib={ nsmap=root.nsmap, attrib={
'uuid' : uuid(), 'uuid' : uuid(),
'title': title, 'title': title,
'id' : unicode_type(self.max_id(root)+1), 'id' : str(self.max_id(root)+1),
'sourceid': '1' 'sourceid': '1'
}) })
root.append(ans) root.append(ans)
@ -310,13 +309,13 @@ class XMLCache:
def ensure_media_xml_base_ids(root): def ensure_media_xml_base_ids(root):
for num, tag in enumerate(('library', 'watchSpecial')): for num, tag in enumerate(('library', 'watchSpecial')):
for x in root.xpath('//*[local-name()="%s"]'%tag): for x in root.xpath('//*[local-name()="%s"]'%tag):
x.set('id', unicode_type(num)) x.set('id', str(num))
def rebase_ids(root, base, sourceid, pl_sourceid): def rebase_ids(root, base, sourceid, pl_sourceid):
'Rebase all ids and also make them consecutive' 'Rebase all ids and also make them consecutive'
for item in root.xpath('//*[@sourceid]'): for item in root.xpath('//*[@sourceid]'):
sid = pl_sourceid if item.tag.endswith('playlist') else sourceid sid = pl_sourceid if item.tag.endswith('playlist') else sourceid
item.set('sourceid', unicode_type(sid)) item.set('sourceid', str(sid))
# Only rebase ids of nodes that are immediate children of the # Only rebase ids of nodes that are immediate children of the
# record root (that way playlist/itemnodes are unaffected # record root (that way playlist/itemnodes are unaffected
items = root.xpath('child::*[@id]') items = root.xpath('child::*[@id]')
@ -326,8 +325,8 @@ class XMLCache:
old = int(item.get('id')) old = int(item.get('id'))
new = base + i new = base + i
if old != new: if old != new:
item.set('id', unicode_type(new)) item.set('id', str(new))
idmap[unicode_type(old)] = unicode_type(new) idmap[str(old)] = str(new)
return idmap return idmap
self.prune_empty_playlists() self.prune_empty_playlists()
@ -356,7 +355,7 @@ class XMLCache:
last_bl = max(self.roots.keys()) last_bl = max(self.roots.keys())
max_id = self.max_id(self.roots[last_bl]) max_id = self.max_id(self.roots[last_bl])
self.roots[0].set('nextID', unicode_type(max_id+1)) self.roots[0].set('nextID', str(max_id+1))
debug_print('Finished running fix_ids()') debug_print('Finished running fix_ids()')
# }}} # }}}
@ -513,7 +512,7 @@ class XMLCache:
# Ensure each book has an ID. # Ensure each book has an ID.
for rec in records: for rec in records:
if rec.get('id', None) is None: if rec.get('id', None) is None:
rec.set('id', unicode_type(self.max_id(root)+1)) rec.set('id', str(self.max_id(root)+1))
ids = [x.get('id', None) for x in records] ids = [x.get('id', None) for x in records]
# Given that we set the ids, there shouldn't be any None's. But # Given that we set the ids, there shouldn't be any None's. But
# better to be safe... # better to be safe...
@ -570,7 +569,7 @@ class XMLCache:
id_ = self.max_id(root)+1 id_ = self.max_id(root)+1
attrib = { attrib = {
'page':'0', 'part':'0','pageOffset':'0','scale':'0', 'page':'0', 'part':'0','pageOffset':'0','scale':'0',
'id':unicode_type(id_), 'sourceid':'1', 'path':lpath} 'id':str(id_), 'sourceid':'1', 'path':lpath}
ans = root.makeelement('{%s}text'%namespace, attrib=attrib, nsmap=root.nsmap) ans = root.makeelement('{%s}text'%namespace, attrib=attrib, nsmap=root.nsmap)
root.append(ans) root.append(ans)
return ans return ans
@ -589,7 +588,7 @@ class XMLCache:
if thumbnail and thumbnail[-1]: if thumbnail and thumbnail[-1]:
ans.text = '\n' + '\t\t' ans.text = '\n' + '\t\t'
t = root.makeelement('{%s}thumbnail'%namespace, t = root.makeelement('{%s}thumbnail'%namespace,
attrib={'width':unicode_type(thumbnail[0]), 'height':unicode_type(thumbnail[1])}, attrib={'width':str(thumbnail[0]), 'height':str(thumbnail[1])},
nsmap=root.nsmap) nsmap=root.nsmap)
t.text = 'main_thumbnail.jpg' t.text = 'main_thumbnail.jpg'
ans.append(t) ans.append(t)
@ -658,7 +657,7 @@ class XMLCache:
date = strftime(timestamp, zone=tz) date = strftime(timestamp, zone=tz)
record.set('date', clean(date)) record.set('date', clean(date))
try: try:
record.set('size', clean(unicode_type(os.stat(path).st_size))) record.set('size', clean(str(os.stat(path).st_size)))
except: except:
record.set('size', '0') record.set('size', '0')
title = book.title if book.title else _('Unknown') title = book.title if book.title else _('Unknown')
@ -688,7 +687,7 @@ class XMLCache:
record.set('sourceid', '1') record.set('sourceid', '1')
if 'id' not in record.attrib: if 'id' not in record.attrib:
num = self.max_id(record.getroottree().getroot()) num = self.max_id(record.getroottree().getroot())
record.set('id', unicode_type(num+1)) record.set('id', str(num+1))
return (gtz_count, ltz_count, use_tz_var) return (gtz_count, ltz_count, use_tz_var)
# }}} # }}}

View File

@ -23,7 +23,7 @@ from calibre.devices.usbms.books import CollectionsBookList
from calibre.devices.usbms.books import BookList from calibre.devices.usbms.books import BookList
from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string
from calibre.constants import islinux from calibre.constants import islinux
from polyglot.builtins import unicode_type, long_type from polyglot.builtins import long_type
DBPATH = 'Sony_Reader/database/books.db' DBPATH = 'Sony_Reader/database/books.db'
THUMBPATH = 'Sony_Reader/database/cache/books/%s/thumbnail/main_thumbnail.jpg' THUMBPATH = 'Sony_Reader/database/cache/books/%s/thumbnail/main_thumbnail.jpg'
@ -170,7 +170,7 @@ class PRST1(USBMS):
with closing(sqlite.connect(dbpath)) as connection: with closing(sqlite.connect(dbpath)) as connection:
# Replace undecodable characters in the db instead of erroring out # Replace undecodable characters in the db instead of erroring out
connection.text_factory = lambda x: x if isinstance(x, unicode_type) else x.decode('utf-8', 'replace') connection.text_factory = lambda x: x if isinstance(x, str) else x.decode('utf-8', 'replace')
cursor = connection.cursor() cursor = connection.cursor()
# Query collections # Query collections

View File

@ -47,7 +47,7 @@ from calibre.utils.mdns import (
) )
from calibre.utils.socket_inheritance import set_socket_inherit from calibre.utils.socket_inheritance import set_socket_inherit
from polyglot import queue from polyglot import queue
from polyglot.builtins import as_bytes, iteritems, itervalues, unicode_type from polyglot.builtins import as_bytes, iteritems, itervalues
def synchronous(tlockname): def synchronous(tlockname):
@ -125,13 +125,13 @@ class ConnectionListener(Thread):
content_server_port = '' content_server_port = ''
try: try:
from calibre.srv.opts import server_config from calibre.srv.opts import server_config
content_server_port = unicode_type(server_config().port) content_server_port = str(server_config().port)
except Exception: except Exception:
pass pass
message = (self.driver.ZEROCONF_CLIENT_STRING + ' (on ' + message = (self.driver.ZEROCONF_CLIENT_STRING + ' (on ' +
unicode_type(socket.gethostname().partition('.')[0]) + str(socket.gethostname().partition('.')[0]) +
');' + content_server_port + ');' + content_server_port +
',' + unicode_type(self.driver.port)).encode('utf-8') ',' + str(self.driver.port)).encode('utf-8')
self.driver._debug('received broadcast', packet, message) self.driver._debug('received broadcast', packet, message)
self.driver.broadcast_socket.sendto(message, remote) self.driver.broadcast_socket.sendto(message, remote)
except: except:
@ -414,7 +414,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
if isinstance(a, dict): if isinstance(a, dict):
printable = {} printable = {}
for k,v in iteritems(a): for k,v in iteritems(a):
if isinstance(v, (bytes, unicode_type)) and len(v) > 50: if isinstance(v, (bytes, str)) and len(v) > 50:
printable[k] = 'too long' printable[k] = 'too long'
else: else:
printable[k] = v printable[k] = v
@ -436,14 +436,14 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
if not isinstance(dinfo, dict): if not isinstance(dinfo, dict):
dinfo = {} dinfo = {}
if dinfo.get('device_store_uuid', None) is None: if dinfo.get('device_store_uuid', None) is None:
dinfo['device_store_uuid'] = unicode_type(uuid.uuid4()) dinfo['device_store_uuid'] = str(uuid.uuid4())
if dinfo.get('device_name') is None: if dinfo.get('device_name') is None:
dinfo['device_name'] = self.get_gui_name() dinfo['device_name'] = self.get_gui_name()
if name is not None: if name is not None:
dinfo['device_name'] = name dinfo['device_name'] = name
dinfo['location_code'] = location_code dinfo['location_code'] = location_code
dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None) dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None)
dinfo['calibre_version'] = '.'.join([unicode_type(i) for i in numeric_version]) dinfo['calibre_version'] = '.'.join([str(i) for i in numeric_version])
dinfo['date_last_connected'] = isoformat(now()) dinfo['date_last_connected'] = isoformat(now())
dinfo['prefix'] = self.PREFIX dinfo['prefix'] = self.PREFIX
return dinfo return dinfo
@ -495,9 +495,9 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
from calibre.library.save_to_disk import config, get_components from calibre.library.save_to_disk import config, get_components
opts = config().parse() opts = config().parse()
if not isinstance(template, unicode_type): if not isinstance(template, str):
template = template.decode('utf-8') template = template.decode('utf-8')
app_id = unicode_type(getattr(mdata, 'application_id', '')) app_id = str(getattr(mdata, 'application_id', ''))
id_ = mdata.get('id', fname) id_ = mdata.get('id', fname)
extra_components = get_components(template, mdata, id_, extra_components = get_components(template, mdata, id_,
timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1, timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1,
@ -747,7 +747,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
from calibre.utils.date import now, parse_date from calibre.utils.date import now, parse_date
try: try:
key = self._make_metadata_cache_key(uuid, ext_or_lpath) key = self._make_metadata_cache_key(uuid, ext_or_lpath)
if isinstance(lastmod, unicode_type): if isinstance(lastmod, str):
if lastmod == 'None': if lastmod == 'None':
return None return None
lastmod = parse_date(lastmod) lastmod = parse_date(lastmod)
@ -1909,7 +1909,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
'between 50 and 99. Forced to be %d.')%self.DEFAULT_THUMBNAIL_COMPRESSION_QUALITY 'between 50 and 99. Forced to be %d.')%self.DEFAULT_THUMBNAIL_COMPRESSION_QUALITY
self._debug(message) self._debug(message)
self.set_option('thumbnail_compression_quality', self.set_option('thumbnail_compression_quality',
unicode_type(self.DEFAULT_THUMBNAIL_COMPRESSION_QUALITY)) str(self.DEFAULT_THUMBNAIL_COMPRESSION_QUALITY))
try: try:
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

View File

@ -6,7 +6,6 @@ __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from calibre.utils.config_base import Config, ConfigProxy from calibre.utils.config_base import Config, ConfigProxy
from polyglot.builtins import unicode_type
class DeviceConfig: class DeviceConfig:
@ -109,15 +108,15 @@ class DeviceConfig:
if hasattr(config_widget.opt_extra_customization[i], 'isChecked'): if hasattr(config_widget.opt_extra_customization[i], 'isChecked'):
ec.append(config_widget.opt_extra_customization[i].isChecked()) ec.append(config_widget.opt_extra_customization[i].isChecked())
elif hasattr(config_widget.opt_extra_customization[i], 'currentText'): elif hasattr(config_widget.opt_extra_customization[i], 'currentText'):
ec.append(unicode_type(config_widget.opt_extra_customization[i].currentText()).strip()) ec.append(str(config_widget.opt_extra_customization[i].currentText()).strip())
else: else:
ec.append(unicode_type(config_widget.opt_extra_customization[i].text()).strip()) ec.append(str(config_widget.opt_extra_customization[i].text()).strip())
else: else:
ec = unicode_type(config_widget.opt_extra_customization.text()).strip() ec = str(config_widget.opt_extra_customization.text()).strip()
if not ec: if not ec:
ec = None ec = None
proxy['extra_customization'] = ec proxy['extra_customization'] = ec
st = unicode_type(config_widget.opt_save_template.text()) st = str(config_widget.opt_save_template.text())
proxy['save_template'] = st proxy['save_template'] = st
@classmethod @classmethod

View File

@ -21,7 +21,7 @@ from calibre.devices.usbms.cli import CLI
from calibre.devices.usbms.device import Device from calibre.devices.usbms.device import Device
from calibre.devices.usbms.books import BookList, Book from calibre.devices.usbms.books import BookList, Book
from calibre.ebooks.metadata.book.json_codec import JsonCodec from calibre.ebooks.metadata.book.json_codec import JsonCodec
from polyglot.builtins import itervalues, unicode_type, string_or_bytes from polyglot.builtins import itervalues, string_or_bytes
def debug_print(*args, **kw): def debug_print(*args, **kw):
@ -107,14 +107,14 @@ class USBMS(CLI, Device):
if not isinstance(dinfo, dict): if not isinstance(dinfo, dict):
dinfo = {} dinfo = {}
if dinfo.get('device_store_uuid', None) is None: if dinfo.get('device_store_uuid', None) is None:
dinfo['device_store_uuid'] = unicode_type(uuid.uuid4()) dinfo['device_store_uuid'] = str(uuid.uuid4())
if dinfo.get('device_name', None) is None: if dinfo.get('device_name', None) is None:
dinfo['device_name'] = self.get_gui_name() dinfo['device_name'] = self.get_gui_name()
if name is not None: if name is not None:
dinfo['device_name'] = name dinfo['device_name'] = name
dinfo['location_code'] = location_code dinfo['location_code'] = location_code
dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None) dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None)
dinfo['calibre_version'] = '.'.join([unicode_type(i) for i in numeric_version]) dinfo['calibre_version'] = '.'.join([str(i) for i in numeric_version])
dinfo['date_last_connected'] = isoformat(now()) dinfo['date_last_connected'] = isoformat(now())
dinfo['prefix'] = prefix.replace('\\', '/') dinfo['prefix'] = prefix.replace('\\', '/')
return dinfo return dinfo

View File

@ -10,7 +10,6 @@ import os, time, re
from functools import partial from functools import partial
from calibre.devices.errors import DeviceError, WrongDestinationError, FreeSpaceError from calibre.devices.errors import DeviceError, WrongDestinationError, FreeSpaceError
from polyglot.builtins import unicode_type
def sanity_check(on_card, files, card_prefixes, free_space): def sanity_check(on_card, files, card_prefixes, free_space):
@ -97,9 +96,9 @@ def create_upload_path(mdata, fname, template, sanitize,
ext = path_type.splitext(fname)[1] ext = path_type.splitext(fname)[1]
opts = config().parse() opts = config().parse()
if not isinstance(template, unicode_type): if not isinstance(template, str):
template = template.decode('utf-8') template = template.decode('utf-8')
app_id = unicode_type(getattr(mdata, 'application_id', '')) app_id = str(getattr(mdata, 'application_id', ''))
id_ = mdata.get('id', fname) id_ = mdata.get('id', fname)
extra_components = get_components(template, mdata, id_, extra_components = get_components(template, mdata, id_,
timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1, timefmt=opts.send_timefmt, length=maxlen-len(app_id)-1,

View File

@ -9,13 +9,12 @@ from bs4 import ( # noqa
SoupStrainer, Tag, __version__ SoupStrainer, Tag, __version__
) )
from polyglot.builtins import unicode_type
def parse_html(markup): def parse_html(markup):
from calibre.ebooks.chardet import strip_encoding_declarations, xml_to_unicode, substitute_entites from calibre.ebooks.chardet import strip_encoding_declarations, xml_to_unicode, substitute_entites
from calibre.utils.cleantext import clean_xml_chars from calibre.utils.cleantext import clean_xml_chars
if isinstance(markup, unicode_type): if isinstance(markup, str):
markup = strip_encoding_declarations(markup) markup = strip_encoding_declarations(markup)
markup = substitute_entites(markup) markup = substitute_entites(markup)
else: else:

View File

@ -11,7 +11,6 @@ from various formats.
import os, re, numbers, sys import os, re, numbers, sys
from calibre import prints from calibre import prints
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from polyglot.builtins import unicode_type
class ConversionError(Exception): class ConversionError(Exception):
@ -82,7 +81,7 @@ def extract_calibre_cover(raw, base, log):
if matches is None: if matches is None:
body = soup.find('body') body = soup.find('body')
if body is not None: if body is not None:
text = u''.join(map(unicode_type, body.findAll(text=True))) text = u''.join(map(str, body.findAll(text=True)))
if text.strip(): if text.strip():
# Body has text, abort # Body has text, abort
return return
@ -152,7 +151,7 @@ def check_ebook_format(stream, current_guess):
def normalize(x): def normalize(x):
if isinstance(x, unicode_type): if isinstance(x, str):
import unicodedata import unicodedata
x = unicodedata.normalize('NFC', x) x = unicodedata.normalize('NFC', x)
return x return x

View File

@ -7,7 +7,6 @@ __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import re, codecs, sys import re, codecs, sys
from polyglot.builtins import unicode_type
_encoding_pats = ( _encoding_pats = (
# XML declaration # XML declaration
@ -140,7 +139,7 @@ def force_encoding(raw, verbose, assume_utf8=False):
def detect_xml_encoding(raw, verbose=False, assume_utf8=False): def detect_xml_encoding(raw, verbose=False, assume_utf8=False):
if not raw or isinstance(raw, unicode_type): if not raw or isinstance(raw, str):
return raw, None return raw, None
for x in ('utf8', 'utf-16-le', 'utf-16-be'): for x in ('utf8', 'utf-16-le', 'utf-16-be'):
bom = getattr(codecs, 'BOM_'+x.upper().replace('-16', '16').replace( bom = getattr(codecs, 'BOM_'+x.upper().replace('-16', '16').replace(
@ -184,7 +183,7 @@ def xml_to_unicode(raw, verbose=False, strip_encoding_pats=False,
return '', None return '', None
raw, encoding = detect_xml_encoding(raw, verbose=verbose, raw, encoding = detect_xml_encoding(raw, verbose=verbose,
assume_utf8=assume_utf8) assume_utf8=assume_utf8)
if not isinstance(raw, unicode_type): if not isinstance(raw, str):
raw = raw.decode(encoding, 'replace') raw = raw.decode(encoding, 'replace')
if strip_encoding_pats: if strip_encoding_pats:

View File

@ -13,7 +13,7 @@ from calibre.ebooks.BeautifulSoup import BeautifulSoup, NavigableString
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.metadata.toc import TOC
from chm.chm import CHMFile, chmlib from chm.chm import CHMFile, chmlib
from polyglot.builtins import as_unicode, unicode_type from polyglot.builtins import as_unicode
def match_string(s1, s2_already_lowered): def match_string(s1, s2_already_lowered):
@ -43,7 +43,7 @@ class CHMReader(CHMFile):
def __init__(self, input, log, input_encoding=None): def __init__(self, input, log, input_encoding=None):
CHMFile.__init__(self) CHMFile.__init__(self)
if isinstance(input, unicode_type): if isinstance(input, str):
enc = 'mbcs' if iswindows else filesystem_encoding enc = 'mbcs' if iswindows else filesystem_encoding
try: try:
input = input.encode(enc) input = input.encode(enc)
@ -192,7 +192,7 @@ class CHMReader(CHMFile):
with lopen(lpath, 'r+b') as f: with lopen(lpath, 'r+b') as f:
data = f.read() data = f.read()
data = self._reformat(data, lpath) data = self._reformat(data, lpath)
if isinstance(data, unicode_type): if isinstance(data, str):
data = data.encode('utf-8') data = data.encode('utf-8')
f.seek(0) f.seek(0)
f.truncate() f.truncate()

View File

@ -16,7 +16,6 @@ from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import numeric_sort_key
from calibre.utils.ipc.server import Server from calibre.utils.ipc.server import Server
from calibre.utils.ipc.job import ParallelJob from calibre.utils.ipc.job import ParallelJob
from polyglot.builtins import unicode_type
from polyglot.queue import Empty from polyglot.queue import Empty
# If the specified screen has either dimension larger than this value, no image # If the specified screen has either dimension larger than this value, no image
@ -29,7 +28,7 @@ def extract_comic(path_to_comic_file):
Un-archive the comic file. Un-archive the comic file.
''' '''
tdir = PersistentTemporaryDirectory(suffix='_comic_extract') tdir = PersistentTemporaryDirectory(suffix='_comic_extract')
if not isinstance(tdir, unicode_type): if not isinstance(tdir, str):
# Needed in case the zip file has wrongly encoded unicode file/dir # Needed in case the zip file has wrongly encoded unicode file/dir
# names # names
tdir = tdir.decode(filesystem_encoding) tdir = tdir.decode(filesystem_encoding)

View File

@ -13,7 +13,6 @@ from calibre.utils.lock import ExclusiveFile
from calibre import sanitize_file_name from calibre import sanitize_file_name
from calibre.customize.conversion import OptionRecommendation from calibre.customize.conversion import OptionRecommendation
from calibre.customize.ui import available_output_formats from calibre.customize.ui import available_output_formats
from polyglot.builtins import unicode_type
config_dir = os.path.join(config_dir, 'conversion') config_dir = os.path.join(config_dir, 'conversion')
@ -90,7 +89,7 @@ class GuiRecommendations(dict):
def serialize(self): def serialize(self):
ans = json.dumps(self, indent=2, ensure_ascii=False) ans = json.dumps(self, indent=2, ensure_ascii=False)
if isinstance(ans, unicode_type): if isinstance(ans, str):
ans = ans.encode('utf-8') ans = ans.encode('utf-8')
return b'json:' + ans return b'json:' + ans

View File

@ -10,7 +10,7 @@ import os
from calibre.customize.conversion import InputFormatPlugin from calibre.customize.conversion import InputFormatPlugin
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.constants import filesystem_encoding from calibre.constants import filesystem_encoding
from polyglot.builtins import unicode_type, as_bytes from polyglot.builtins import as_bytes
class CHMInput(InputFormatPlugin): class CHMInput(InputFormatPlugin):
@ -37,7 +37,7 @@ class CHMInput(InputFormatPlugin):
log.debug('Processing CHM...') log.debug('Processing CHM...')
with TemporaryDirectory('_chm2oeb') as tdir: with TemporaryDirectory('_chm2oeb') as tdir:
if not isinstance(tdir, unicode_type): if not isinstance(tdir, str):
tdir = tdir.decode(filesystem_encoding) tdir = tdir.decode(filesystem_encoding)
html_input = plugin_for_input_format('html') html_input = plugin_for_input_format('html')
for opt in html_input.options: for opt in html_input.options:
@ -128,7 +128,7 @@ class CHMInput(InputFormatPlugin):
base = os.path.dirname(os.path.abspath(htmlpath)) base = os.path.dirname(os.path.abspath(htmlpath))
def unquote(x): def unquote(x):
if isinstance(x, unicode_type): if isinstance(x, str):
x = x.encode('utf-8') x = x.encode('utf-8')
return _unquote(x).decode('utf-8') return _unquote(x).decode('utf-8')

View File

@ -12,7 +12,7 @@ from calibre.customize.conversion import (OutputFormatPlugin,
OptionRecommendation) OptionRecommendation)
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre import CurrentDir from calibre import CurrentDir
from polyglot.builtins import unicode_type, as_bytes from polyglot.builtins import as_bytes
block_level_tags = ( block_level_tags = (
'address', 'address',
@ -225,15 +225,15 @@ class EPUBOutput(OutputFormatPlugin):
identifiers = oeb.metadata['identifier'] identifiers = oeb.metadata['identifier']
uuid = None uuid = None
for x in identifiers: for x in identifiers:
if x.get(OPF('scheme'), None).lower() == 'uuid' or unicode_type(x).startswith('urn:uuid:'): if x.get(OPF('scheme'), None).lower() == 'uuid' or str(x).startswith('urn:uuid:'):
uuid = unicode_type(x).split(':')[-1] uuid = str(x).split(':')[-1]
break break
encrypted_fonts = getattr(input_plugin, 'encrypted_fonts', []) encrypted_fonts = getattr(input_plugin, 'encrypted_fonts', [])
if uuid is None: if uuid is None:
self.log.warn('No UUID identifier found') self.log.warn('No UUID identifier found')
from uuid import uuid4 from uuid import uuid4
uuid = unicode_type(uuid4()) uuid = str(uuid4())
oeb.metadata.add('identifier', uuid, scheme='uuid', id=uuid) oeb.metadata.add('identifier', uuid, scheme='uuid', id=uuid)
if encrypted_fonts and not uuid.startswith('urn:uuid:'): if encrypted_fonts and not uuid.startswith('urn:uuid:'):
@ -241,7 +241,7 @@ class EPUBOutput(OutputFormatPlugin):
# for some absurd reason, or it will throw a hissy fit and refuse # for some absurd reason, or it will throw a hissy fit and refuse
# to use the obfuscated fonts. # to use the obfuscated fonts.
for x in identifiers: for x in identifiers:
if unicode_type(x) == uuid: if str(x) == uuid:
x.content = 'urn:uuid:'+uuid x.content = 'urn:uuid:'+uuid
with TemporaryDirectory('_epub_output') as tdir: with TemporaryDirectory('_epub_output') as tdir:
@ -336,7 +336,7 @@ class EPUBOutput(OutputFormatPlugin):
f.write(bytes(bytearray(data[i] ^ key[i%16] for i in range(1024)))) f.write(bytes(bytearray(data[i] ^ key[i%16] for i in range(1024))))
else: else:
self.log.warn('Font', path, 'is invalid, ignoring') self.log.warn('Font', path, 'is invalid, ignoring')
if not isinstance(uri, unicode_type): if not isinstance(uri, str):
uri = uri.decode('utf-8') uri = uri.decode('utf-8')
fonts.append(''' fonts.append('''
<enc:EncryptedData> <enc:EncryptedData>

View File

@ -17,7 +17,7 @@ from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from calibre.utils.imghdr import what from calibre.utils.imghdr import what
from calibre.utils.localization import get_lang from calibre.utils.localization import get_lang
from polyglot.builtins import as_unicode, unicode_type from polyglot.builtins import as_unicode
def sanitize_file_name(x): def sanitize_file_name(x):
@ -144,7 +144,7 @@ class HTMLInput(InputFormatPlugin):
if not metadata.title: if not metadata.title:
oeb.logger.warn('Title not specified') oeb.logger.warn('Title not specified')
metadata.add('title', self.oeb.translate(__('Unknown'))) metadata.add('title', self.oeb.translate(__('Unknown')))
bookid = unicode_type(uuid.uuid4()) bookid = str(uuid.uuid4())
metadata.add('identifier', bookid, id='uuid_id', scheme='uuid') metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
for ident in metadata.identifier: for ident in metadata.identifier:
if 'id' in ident.attrib: if 'id' in ident.attrib:
@ -233,7 +233,7 @@ class HTMLInput(InputFormatPlugin):
def link_to_local_path(self, link_, base=None): def link_to_local_path(self, link_, base=None):
from calibre.ebooks.html.input import Link from calibre.ebooks.html.input import Link
if not isinstance(link_, unicode_type): if not isinstance(link_, str):
try: try:
link_ = link_.decode('utf-8', 'error') link_ = link_.decode('utf-8', 'error')
except: except:
@ -298,7 +298,7 @@ class HTMLInput(InputFormatPlugin):
# bhref refers to an already existing file. The read() method of # bhref refers to an already existing file. The read() method of
# DirContainer will call unquote on it before trying to read the # DirContainer will call unquote on it before trying to read the
# file, therefore we quote it here. # file, therefore we quote it here.
if isinstance(bhref, unicode_type): if isinstance(bhref, str):
bhref = bhref.encode('utf-8') bhref = bhref.encode('utf-8')
item.html_input_href = as_unicode(quote(bhref)) item.html_input_href = as_unicode(quote(bhref))
if is_stylesheet: if is_stylesheet:

View File

@ -10,7 +10,6 @@ from os.path import dirname, abspath, relpath as _relpath, exists, basename
from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
from calibre import CurrentDir from calibre import CurrentDir
from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ptempfile import PersistentTemporaryDirectory
from polyglot.builtins import unicode_type
def relpath(*args): def relpath(*args):
@ -140,7 +139,7 @@ class HTMLOutput(OutputFormatPlugin):
toc=html_toc, meta=meta, nextLink=nextLink, toc=html_toc, meta=meta, nextLink=nextLink,
tocUrl=tocUrl, cssLink=cssLink, tocUrl=tocUrl, cssLink=cssLink,
firstContentPageLink=nextLink) firstContentPageLink=nextLink)
if isinstance(t, unicode_type): if isinstance(t, str):
t = t.encode('utf-8') t = t.encode('utf-8')
f.write(t) f.write(t)

View File

@ -11,7 +11,6 @@ import os
from calibre.customize.conversion import OutputFormatPlugin, \ from calibre.customize.conversion import OutputFormatPlugin, \
OptionRecommendation OptionRecommendation
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from polyglot.builtins import unicode_type
class HTMLZOutput(OutputFormatPlugin): class HTMLZOutput(OutputFormatPlugin):
@ -80,9 +79,9 @@ class HTMLZOutput(OutputFormatPlugin):
fname = u'index' fname = u'index'
if opts.htmlz_title_filename: if opts.htmlz_title_filename:
from calibre.utils.filenames import shorten_components_to from calibre.utils.filenames import shorten_components_to
fname = shorten_components_to(100, (ascii_filename(unicode_type(oeb_book.metadata.title[0])),))[0] fname = shorten_components_to(100, (ascii_filename(str(oeb_book.metadata.title[0])),))[0]
with open(os.path.join(tdir, fname+u'.html'), 'wb') as tf: with open(os.path.join(tdir, fname+u'.html'), 'wb') as tf:
if isinstance(html, unicode_type): if isinstance(html, str):
html = html.encode('utf-8') html = html.encode('utf-8')
tf.write(html) tf.write(html)

View File

@ -10,7 +10,6 @@ import sys, os
from calibre.customize.conversion import OutputFormatPlugin from calibre.customize.conversion import OutputFormatPlugin
from calibre.customize.conversion import OptionRecommendation from calibre.customize.conversion import OptionRecommendation
from polyglot.builtins import unicode_type
class LRFOptions: class LRFOptions:
@ -18,7 +17,7 @@ class LRFOptions:
def __init__(self, output, opts, oeb): def __init__(self, output, opts, oeb):
def f2s(f): def f2s(f):
try: try:
return unicode_type(f[0]) return str(f[0])
except: except:
return '' return ''
m = oeb.metadata m = oeb.metadata
@ -32,13 +31,13 @@ class LRFOptions:
self.title_sort = self.author_sort = '' self.title_sort = self.author_sort = ''
for x in m.creator: for x in m.creator:
if x.role == 'aut': if x.role == 'aut':
self.author = unicode_type(x) self.author = str(x)
fa = unicode_type(getattr(x, 'file_as', '')) fa = str(getattr(x, 'file_as', ''))
if fa: if fa:
self.author_sort = fa self.author_sort = fa
for x in m.title: for x in m.title:
if unicode_type(x.file_as): if str(x.file_as):
self.title_sort = unicode_type(x.file_as) self.title_sort = str(x.file_as)
self.freetext = f2s(m.description) self.freetext = f2s(m.description)
self.category = f2s(m.subject) self.category = f2s(m.subject)
self.cover = None self.cover = None

View File

@ -7,7 +7,6 @@ __docformat__ = 'restructuredtext en'
import os import os
from calibre.customize.conversion import InputFormatPlugin from calibre.customize.conversion import InputFormatPlugin
from polyglot.builtins import unicode_type
class MOBIInput(InputFormatPlugin): class MOBIInput(InputFormatPlugin):
@ -51,7 +50,7 @@ class MOBIInput(InputFormatPlugin):
raw = parse_cache.pop('calibre_raw_mobi_markup', False) raw = parse_cache.pop('calibre_raw_mobi_markup', False)
if raw: if raw:
if isinstance(raw, unicode_type): if isinstance(raw, str):
raw = raw.encode('utf-8') raw = raw.encode('utf-8')
with lopen('debug-raw.html', 'wb') as f: with lopen('debug-raw.html', 'wb') as f:
f.write(raw) f.write(raw)

View File

@ -8,7 +8,6 @@ __docformat__ = 'restructuredtext en'
from calibre.customize.conversion import (OutputFormatPlugin, from calibre.customize.conversion import (OutputFormatPlugin,
OptionRecommendation) OptionRecommendation)
from polyglot.builtins import unicode_type
def remove_html_cover(oeb, log): def remove_html_cover(oeb, log):
@ -122,7 +121,7 @@ class MOBIOutput(OutputFormatPlugin):
if not found: if not found:
from calibre.ebooks import generate_masthead from calibre.ebooks import generate_masthead
self.oeb.log.debug('No masthead found in manifest, generating default mastheadImage...') self.oeb.log.debug('No masthead found in manifest, generating default mastheadImage...')
raw = generate_masthead(unicode_type(self.oeb.metadata['title'][0])) raw = generate_masthead(str(self.oeb.metadata['title'][0]))
id, href = self.oeb.manifest.generate('masthead', 'masthead') id, href = self.oeb.manifest.generate('masthead', 'masthead')
self.oeb.manifest.add(id, href, 'image/gif', data=raw) self.oeb.manifest.add(id, href, 'image/gif', data=raw)
self.oeb.guide.add('masthead', 'Masthead Image', href) self.oeb.guide.add('masthead', 'Masthead Image', href)
@ -166,7 +165,7 @@ class MOBIOutput(OutputFormatPlugin):
sec.nodes.remove(a) sec.nodes.remove(a)
root = TOC(klass='periodical', href=self.oeb.spine[0].href, root = TOC(klass='periodical', href=self.oeb.spine[0].href,
title=unicode_type(self.oeb.metadata.title[0])) title=str(self.oeb.metadata.title[0]))
for s in sections: for s in sections:
if articles[id(s)]: if articles[id(s)]:

View File

@ -14,7 +14,7 @@ import glob, os
from calibre.customize.conversion import (OutputFormatPlugin, from calibre.customize.conversion import (OutputFormatPlugin,
OptionRecommendation) OptionRecommendation)
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
UNITS = ('millimeter', 'centimeter', 'point', 'inch' , 'pica' , 'didot', UNITS = ('millimeter', 'centimeter', 'point', 'inch' , 'pica' , 'didot',
'cicero', 'devicepixel') 'cicero', 'devicepixel')
@ -193,8 +193,8 @@ class PDFOutput(OutputFormatPlugin):
def get_cover_data(self): def get_cover_data(self):
oeb = self.oeb oeb = self.oeb
if (oeb.metadata.cover and unicode_type(oeb.metadata.cover[0]) in oeb.manifest.ids): if (oeb.metadata.cover and str(oeb.metadata.cover[0]) in oeb.manifest.ids):
cover_id = unicode_type(oeb.metadata.cover[0]) cover_id = str(oeb.metadata.cover[0])
item = oeb.manifest.ids[cover_id] item = oeb.manifest.ids[cover_id]
if isinstance(item.data, bytes): if isinstance(item.data, bytes):
self.cover_data = item.data self.cover_data = item.data

View File

@ -10,7 +10,6 @@ import os, io
from calibre.customize.conversion import (OutputFormatPlugin, from calibre.customize.conversion import (OutputFormatPlugin,
OptionRecommendation) OptionRecommendation)
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from polyglot.builtins import unicode_type
class PMLOutput(OutputFormatPlugin): class PMLOutput(OutputFormatPlugin):
@ -42,7 +41,7 @@ class PMLOutput(OutputFormatPlugin):
with TemporaryDirectory('_pmlz_output') as tdir: with TemporaryDirectory('_pmlz_output') as tdir:
pmlmlizer = PMLMLizer(log) pmlmlizer = PMLMLizer(log)
pml = unicode_type(pmlmlizer.extract_content(oeb_book, opts)) pml = str(pmlmlizer.extract_content(oeb_book, opts))
with lopen(os.path.join(tdir, 'index.pml'), 'wb') as out: with lopen(os.path.join(tdir, 'index.pml'), 'wb') as out:
out.write(pml.encode(opts.pml_output_encoding, 'replace')) out.write(pml.encode(opts.pml_output_encoding, 'replace'))

View File

@ -11,7 +11,6 @@ import os
from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
from calibre.constants import numeric_version from calibre.constants import numeric_version
from calibre import walk from calibre import walk
from polyglot.builtins import unicode_type
class RecipeDisabled(Exception): class RecipeDisabled(Exception):
@ -164,6 +163,6 @@ class RecipeInput(InputFormatPlugin):
def save_download(self, zf): def save_download(self, zf):
raw = self.recipe_source raw = self.recipe_source
if isinstance(raw, unicode_type): if isinstance(raw, str):
raw = raw.encode('utf-8') raw = raw.encode('utf-8')
zf.writestr('download.recipe', raw) zf.writestr('download.recipe', raw)

View File

@ -10,7 +10,6 @@ import os
from calibre.customize.conversion import InputFormatPlugin from calibre.customize.conversion import InputFormatPlugin
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from polyglot.builtins import unicode_type
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>' 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>'
@ -75,7 +74,7 @@ class SNBInput(InputFormatPlugin):
if d['cover'] != '': if d['cover'] != '':
oeb.guide.add('cover', 'Cover', d['cover']) oeb.guide.add('cover', 'Cover', d['cover'])
bookid = unicode_type(uuid.uuid4()) bookid = str(uuid.uuid4())
oeb.metadata.add('identifier', bookid, id='uuid_id', scheme='uuid') oeb.metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
for ident in oeb.metadata.identifier: for ident in oeb.metadata.identifier:
if 'id' in ident.attrib: if 'id' in ident.attrib:

View File

@ -10,7 +10,6 @@ import os
from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.constants import __appname__, __version__ from calibre.constants import __appname__, __version__
from polyglot.builtins import unicode_type
class SNBOutput(OutputFormatPlugin): class SNBOutput(OutputFormatPlugin):
@ -75,20 +74,20 @@ class SNBOutput(OutputFormatPlugin):
# Process Meta data # Process Meta data
meta = oeb_book.metadata meta = oeb_book.metadata
if meta.title: if meta.title:
title = unicode_type(meta.title[0]) title = str(meta.title[0])
else: else:
title = '' title = ''
authors = [unicode_type(x) for x in meta.creator if x.role == 'aut'] authors = [str(x) for x in meta.creator if x.role == 'aut']
if meta.publisher: if meta.publisher:
publishers = unicode_type(meta.publisher[0]) publishers = str(meta.publisher[0])
else: else:
publishers = '' publishers = ''
if meta.language: if meta.language:
lang = unicode_type(meta.language[0]).upper() lang = str(meta.language[0]).upper()
else: else:
lang = '' lang = ''
if meta.description: if meta.description:
abstract = unicode_type(meta.description[0]) abstract = str(meta.description[0])
else: else:
abstract = '' abstract = ''

View File

@ -20,7 +20,7 @@ from calibre.utils.zipfile import ZipFile
from calibre import (extract, walk, isbytestring, filesystem_encoding, from calibre import (extract, walk, isbytestring, filesystem_encoding,
get_types_map) get_types_map)
from calibre.constants import __version__ from calibre.constants import __version__
from polyglot.builtins import unicode_type, string_or_bytes from polyglot.builtins import string_or_bytes
DEBUG_README=b''' DEBUG_README=b'''
This debug folder contains snapshots of the e-book as it passes through the This debug folder contains snapshots of the e-book as it passes through the
@ -794,7 +794,7 @@ OptionRecommendation(name='search_replace',
def unarchive(self, path, tdir): def unarchive(self, path, tdir):
extract(path, tdir) extract(path, tdir)
files = list(walk(tdir)) files = list(walk(tdir))
files = [f if isinstance(f, unicode_type) else f.decode(filesystem_encoding) files = [f if isinstance(f, str) else f.decode(filesystem_encoding)
for f in files] for f in files]
from calibre.customize.ui import available_input_formats from calibre.customize.ui import available_input_formats
fmts = set(available_input_formats()) fmts = set(available_input_formats())
@ -847,7 +847,7 @@ OptionRecommendation(name='search_replace',
rec = self.get_option_by_name(name) rec = self.get_option_by_name(name)
help = getattr(rec, 'help', None) help = getattr(rec, 'help', None)
if help is not None: if help is not None:
return help.replace('%default', unicode_type(rec.recommended_value)) return help.replace('%default', str(rec.recommended_value))
def get_all_help(self): def get_all_help(self):
ans = {} ans = {}
@ -915,7 +915,7 @@ OptionRecommendation(name='search_replace',
try: try:
val = parse_date(val, assume_utc=x=='timestamp') val = parse_date(val, assume_utc=x=='timestamp')
except: except:
self.log.exception(_('Failed to parse date/time') + ' ' + unicode_type(val)) self.log.exception(_('Failed to parse date/time') + ' ' + str(val))
continue continue
setattr(mi, x, val) setattr(mi, x, val)

View File

@ -10,7 +10,6 @@ import functools, re, json
from math import ceil from math import ceil
from calibre import entity_to_unicode, as_unicode from calibre import entity_to_unicode, as_unicode
from polyglot.builtins import unicode_type
XMLDECL_RE = re.compile(r'^\s*<[?]xml.*?[?]>') XMLDECL_RE = re.compile(r'^\s*<[?]xml.*?[?]>')
SVG_NS = 'http://www.w3.org/2000/svg' SVG_NS = 'http://www.w3.org/2000/svg'
@ -75,8 +74,8 @@ def smarten_punctuation(html, log=None):
from calibre.ebooks.conversion.utils import HeuristicProcessor from calibre.ebooks.conversion.utils import HeuristicProcessor
preprocessor = HeuristicProcessor(log=log) preprocessor = HeuristicProcessor(log=log)
from uuid import uuid4 from uuid import uuid4
start = 'calibre-smartypants-'+unicode_type(uuid4()) start = 'calibre-smartypants-'+str(uuid4())
stop = 'calibre-smartypants-'+unicode_type(uuid4()) stop = 'calibre-smartypants-'+str(uuid4())
html = html.replace('<!--', start) html = html.replace('<!--', start)
html = html.replace('-->', stop) html = html.replace('-->', stop)
html = preprocessor.fix_nbsp_indents(html) html = preprocessor.fix_nbsp_indents(html)
@ -152,20 +151,20 @@ class DocAnalysis:
maxLineLength=1900 # Discard larger than this to stay in range maxLineLength=1900 # Discard larger than this to stay in range
buckets=20 # Each line is divided into a bucket based on length buckets=20 # Each line is divided into a bucket based on length
# print("there are "+unicode_type(len(lines))+" lines") # print("there are "+str(len(lines))+" lines")
# max = 0 # max = 0
# for line in self.lines: # for line in self.lines:
# l = len(line) # l = len(line)
# if l > max: # if l > max:
# max = l # max = l
# print("max line found is "+unicode_type(max)) # print("max line found is "+str(max))
# Build the line length histogram # Build the line length histogram
hRaw = [0 for i in range(0,buckets)] hRaw = [0 for i in range(0,buckets)]
for line in self.lines: for line in self.lines:
l = len(line) l = len(line)
if l > minLineLength and l < maxLineLength: if l > minLineLength and l < maxLineLength:
l = int(l // 100) l = int(l // 100)
# print("adding "+unicode_type(l)) # print("adding "+str(l))
hRaw[l]+=1 hRaw[l]+=1
# Normalize the histogram into percents # Normalize the histogram into percents
@ -174,8 +173,8 @@ class DocAnalysis:
h = [float(count)/totalLines for count in hRaw] h = [float(count)/totalLines for count in hRaw]
else: else:
h = [] h = []
# print("\nhRaw histogram lengths are: "+unicode_type(hRaw)) # print("\nhRaw histogram lengths are: "+str(hRaw))
# print(" percents are: "+unicode_type(h)+"\n") # print(" percents are: "+str(h)+"\n")
# Find the biggest bucket # Find the biggest bucket
maxValue = 0 maxValue = 0
@ -187,7 +186,7 @@ class DocAnalysis:
# print("Line lengths are too variable. Not unwrapping.") # print("Line lengths are too variable. Not unwrapping.")
return False return False
else: else:
# print(unicode_type(maxValue)+" of the lines were in one bucket") # print(str(maxValue)+" of the lines were in one bucket")
return True return True
@ -223,8 +222,8 @@ class Dehyphenator:
wraptags = match.group('wraptags') wraptags = match.group('wraptags')
except: except:
wraptags = '' wraptags = ''
hyphenated = unicode_type(firsthalf) + "-" + unicode_type(secondhalf) hyphenated = str(firsthalf) + "-" + str(secondhalf)
dehyphenated = unicode_type(firsthalf) + unicode_type(secondhalf) dehyphenated = str(firsthalf) + str(secondhalf)
if self.suffixes.match(secondhalf) is None: if self.suffixes.match(secondhalf) is None:
lookupword = self.removesuffixes.sub('', dehyphenated) lookupword = self.removesuffixes.sub('', dehyphenated)
else: else:
@ -330,7 +329,7 @@ class CSSPreProcessor:
# are commented lines before the first @import or @charset rule. Since # are commented lines before the first @import or @charset rule. Since
# the conversion will remove all stylesheets anyway, we don't lose # the conversion will remove all stylesheets anyway, we don't lose
# anything # anything
data = re.sub(unicode_type(r'/\*.*?\*/'), '', data, flags=re.DOTALL) data = re.sub(r'/\*.*?\*/', '', data, flags=re.DOTALL)
ans, namespaced = [], False ans, namespaced = [], False
for line in data.splitlines(): for line in data.splitlines():
@ -538,7 +537,7 @@ class HTMLPreProcessor:
docanalysis = DocAnalysis('pdf', html) docanalysis = DocAnalysis('pdf', html)
length = docanalysis.line_length(getattr(self.extra_opts, 'unwrap_factor')) length = docanalysis.line_length(getattr(self.extra_opts, 'unwrap_factor'))
if length: if length:
# print("The pdf line length returned is " + unicode_type(length)) # print("The pdf line length returned is " + str(length))
# unwrap em/en dashes # unwrap em/en dashes
end_rules.append((re.compile( end_rules.append((re.compile(
r'(?<=.{%i}[–—])\s*<p>\s*(?=[\[a-z\d])' % length), lambda match: '')) r'(?<=.{%i}[–—])\s*<p>\s*(?=[\[a-z\d])' % length), lambda match: ''))

View File

@ -11,7 +11,6 @@ from math import ceil
from calibre.ebooks.conversion.preprocess import DocAnalysis, Dehyphenator from calibre.ebooks.conversion.preprocess import DocAnalysis, Dehyphenator
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
from calibre.utils.wordcount import get_wordcount_obj from calibre.utils.wordcount import get_wordcount_obj
from polyglot.builtins import unicode_type
class HeuristicProcessor: class HeuristicProcessor:
@ -54,8 +53,8 @@ class HeuristicProcessor:
title = match.group('title') title = match.group('title')
if not title: if not title:
self.html_preprocess_sections = self.html_preprocess_sections + 1 self.html_preprocess_sections = self.html_preprocess_sections + 1
self.log.debug("marked " + unicode_type(self.html_preprocess_sections) + self.log.debug("marked " + str(self.html_preprocess_sections) +
" chapters. - " + unicode_type(chap)) " chapters. - " + str(chap))
return '<h2>'+chap+'</h2>\n' return '<h2>'+chap+'</h2>\n'
else: else:
delete_whitespace = re.compile('^\\s*(?P<c>.*?)\\s*$') delete_whitespace = re.compile('^\\s*(?P<c>.*?)\\s*$')
@ -63,16 +62,16 @@ class HeuristicProcessor:
txt_chap = delete_quotes.sub('', delete_whitespace.sub('\\g<c>', html2text(chap))) txt_chap = delete_quotes.sub('', delete_whitespace.sub('\\g<c>', html2text(chap)))
txt_title = delete_quotes.sub('', delete_whitespace.sub('\\g<c>', html2text(title))) txt_title = delete_quotes.sub('', delete_whitespace.sub('\\g<c>', html2text(title)))
self.html_preprocess_sections = self.html_preprocess_sections + 1 self.html_preprocess_sections = self.html_preprocess_sections + 1
self.log.debug("marked " + unicode_type(self.html_preprocess_sections) + self.log.debug("marked " + str(self.html_preprocess_sections) +
" chapters & titles. - " + unicode_type(chap) + ", " + unicode_type(title)) " chapters & titles. - " + str(chap) + ", " + str(title))
return '<h2 title="'+txt_chap+', '+txt_title+'">'+chap+'</h2>\n<h3 class="sigilNotInTOC">'+title+'</h3>\n' return '<h2 title="'+txt_chap+', '+txt_title+'">'+chap+'</h2>\n<h3 class="sigilNotInTOC">'+title+'</h3>\n'
def chapter_break(self, match): def chapter_break(self, match):
chap = match.group('section') chap = match.group('section')
styles = match.group('styles') styles = match.group('styles')
self.html_preprocess_sections = self.html_preprocess_sections + 1 self.html_preprocess_sections = self.html_preprocess_sections + 1
self.log.debug("marked " + unicode_type(self.html_preprocess_sections) + self.log.debug("marked " + str(self.html_preprocess_sections) +
" section markers based on punctuation. - " + unicode_type(chap)) " section markers based on punctuation. - " + str(chap))
return '<'+styles+' style="page-break-before:always">'+chap return '<'+styles+' style="page-break-before:always">'+chap
def analyze_title_matches(self, match): def analyze_title_matches(self, match):
@ -115,8 +114,8 @@ class HeuristicProcessor:
line_end = line_end_ere.findall(raw) line_end = line_end_ere.findall(raw)
tot_htm_ends = len(htm_end) tot_htm_ends = len(htm_end)
tot_ln_fds = len(line_end) tot_ln_fds = len(line_end)
# self.log.debug("There are " + unicode_type(tot_ln_fds) + " total Line feeds, and " + # self.log.debug("There are " + str(tot_ln_fds) + " total Line feeds, and " +
# unicode_type(tot_htm_ends) + " marked up endings") # str(tot_htm_ends) + " marked up endings")
if percent > 1: if percent > 1:
percent = 1 percent = 1
@ -124,7 +123,7 @@ class HeuristicProcessor:
percent = 0 percent = 0
min_lns = tot_ln_fds * percent min_lns = tot_ln_fds * percent
# self.log.debug("There must be fewer than " + unicode_type(min_lns) + " unmarked lines to add markup") # self.log.debug("There must be fewer than " + str(min_lns) + " unmarked lines to add markup")
return min_lns > tot_htm_ends return min_lns > tot_htm_ends
def dump(self, raw, where): def dump(self, raw, where):
@ -161,17 +160,17 @@ class HeuristicProcessor:
] ]
ITALICIZE_STYLE_PATS = [ ITALICIZE_STYLE_PATS = [
unicode_type(r'(?msu)(?<=[\s>"\'])_\*/(?P<words>[^\*_]+)/\*_'), r'(?msu)(?<=[\s>"\'])_\*/(?P<words>[^\*_]+)/\*_',
unicode_type(r'(?msu)(?<=[\s>"\'])~~(?P<words>[^~]+)~~'), r'(?msu)(?<=[\s>"\'])~~(?P<words>[^~]+)~~',
unicode_type(r'(?msu)(?<=[\s>"\'])_/(?P<words>[^/_]+)/_'), r'(?msu)(?<=[\s>"\'])_/(?P<words>[^/_]+)/_',
unicode_type(r'(?msu)(?<=[\s>"\'])_\*(?P<words>[^\*_]+)\*_'), r'(?msu)(?<=[\s>"\'])_\*(?P<words>[^\*_]+)\*_',
unicode_type(r'(?msu)(?<=[\s>"\'])\*/(?P<words>[^/\*]+)/\*'), r'(?msu)(?<=[\s>"\'])\*/(?P<words>[^/\*]+)/\*',
unicode_type(r'(?msu)(?<=[\s>"\'])/:(?P<words>[^:/]+):/'), r'(?msu)(?<=[\s>"\'])/:(?P<words>[^:/]+):/',
unicode_type(r'(?msu)(?<=[\s>"\'])\|:(?P<words>[^:\|]+):\|'), r'(?msu)(?<=[\s>"\'])\|:(?P<words>[^:\|]+):\|',
unicode_type(r'(?msu)(?<=[\s>"\'])\*(?P<words>[^\*]+)\*'), r'(?msu)(?<=[\s>"\'])\*(?P<words>[^\*]+)\*',
unicode_type(r'(?msu)(?<=[\s>"\'])~(?P<words>[^~]+)~'), r'(?msu)(?<=[\s>"\'])~(?P<words>[^~]+)~',
unicode_type(r'(?msu)(?<=[\s>"\'])/(?P<words>[^/\*><]+)/'), r'(?msu)(?<=[\s>"\'])/(?P<words>[^/\*><]+)/',
unicode_type(r'(?msu)(?<=[\s>"\'])_(?P<words>[^_]+)_'), r'(?msu)(?<=[\s>"\'])_(?P<words>[^_]+)_',
] ]
for word in ITALICIZE_WORDS: for word in ITALICIZE_WORDS:
@ -181,10 +180,10 @@ class HeuristicProcessor:
search_text = re.sub(r'<[^>]*>', '', search_text) search_text = re.sub(r'<[^>]*>', '', search_text)
for pat in ITALICIZE_STYLE_PATS: for pat in ITALICIZE_STYLE_PATS:
for match in re.finditer(pat, search_text): for match in re.finditer(pat, search_text):
ital_string = unicode_type(match.group('words')) ital_string = str(match.group('words'))
# self.log.debug("italicising "+unicode_type(match.group(0))+" with <i>"+ital_string+"</i>") # self.log.debug("italicising "+str(match.group(0))+" with <i>"+ital_string+"</i>")
try: try:
html = re.sub(re.escape(unicode_type(match.group(0))), '<i>%s</i>' % ital_string, html) html = re.sub(re.escape(str(match.group(0))), '<i>%s</i>' % ital_string, html)
except OverflowError: except OverflowError:
# match.group(0) was too large to be compiled into a regex # match.group(0) was too large to be compiled into a regex
continue continue
@ -209,10 +208,10 @@ class HeuristicProcessor:
if wordcount > 200000: if wordcount > 200000:
typical_chapters = 15000. typical_chapters = 15000.
self.min_chapters = int(ceil(wordcount / typical_chapters)) self.min_chapters = int(ceil(wordcount / typical_chapters))
self.log.debug("minimum chapters required are: "+unicode_type(self.min_chapters)) self.log.debug("minimum chapters required are: "+str(self.min_chapters))
heading = re.compile('<h[1-3][^>]*>', re.IGNORECASE) heading = re.compile('<h[1-3][^>]*>', re.IGNORECASE)
self.html_preprocess_sections = len(heading.findall(html)) self.html_preprocess_sections = len(heading.findall(html))
self.log.debug("found " + unicode_type(self.html_preprocess_sections) + " pre-existing headings") self.log.debug("found " + str(self.html_preprocess_sections) + " pre-existing headings")
# Build the Regular Expressions in pieces # Build the Regular Expressions in pieces
init_lookahead = "(?=<(p|div))" init_lookahead = "(?=<(p|div))"
@ -302,7 +301,7 @@ class HeuristicProcessor:
if n_lookahead_req: if n_lookahead_req:
n_lookahead = re.sub("(ou|in|cha)", "lookahead_", full_chapter_line) n_lookahead = re.sub("(ou|in|cha)", "lookahead_", full_chapter_line)
if not analyze: if not analyze:
self.log.debug("Marked " + unicode_type(self.html_preprocess_sections) + " headings, " + log_message) self.log.debug("Marked " + str(self.html_preprocess_sections) + " headings, " + log_message)
chapter_marker = arg_ignorecase+init_lookahead+full_chapter_line+blank_lines+lp_n_lookahead_open+n_lookahead+lp_n_lookahead_close+ \ chapter_marker = arg_ignorecase+init_lookahead+full_chapter_line+blank_lines+lp_n_lookahead_open+n_lookahead+lp_n_lookahead_close+ \
lp_opt_title_open+title_line_open+title_header_open+lp_title+title_header_close+title_line_close+lp_opt_title_close lp_opt_title_open+title_line_open+title_header_open+lp_title+title_header_close+title_line_close+lp_opt_title_close
@ -316,10 +315,10 @@ class HeuristicProcessor:
title_req = True title_req = True
strict_title = False strict_title = False
self.log.debug( self.log.debug(
unicode_type(type_name)+" had "+unicode_type(hits)+ str(type_name)+" had "+str(hits)+
" hits - "+unicode_type(self.chapters_no_title)+" chapters with no title, "+ " hits - "+str(self.chapters_no_title)+" chapters with no title, "+
unicode_type(self.chapters_with_title)+" chapters with titles, "+ str(self.chapters_with_title)+" chapters with titles, "+
unicode_type(float(self.chapters_with_title) / float(hits))+" percent. ") str(float(self.chapters_with_title) / float(hits))+" percent. ")
if type_name == 'common': if type_name == 'common':
analysis_result.append([chapter_type, n_lookahead_req, strict_title, ignorecase, title_req, log_message, type_name]) analysis_result.append([chapter_type, n_lookahead_req, strict_title, ignorecase, title_req, log_message, type_name])
elif self.min_chapters <= hits < max_chapters or self.min_chapters < 3 > hits: elif self.min_chapters <= hits < max_chapters or self.min_chapters < 3 > hits:
@ -336,8 +335,8 @@ class HeuristicProcessor:
words_per_chptr = wordcount words_per_chptr = wordcount
if words_per_chptr > 0 and self.html_preprocess_sections > 0: if words_per_chptr > 0 and self.html_preprocess_sections > 0:
words_per_chptr = wordcount // self.html_preprocess_sections words_per_chptr = wordcount // self.html_preprocess_sections
self.log.debug("Total wordcount is: "+ unicode_type(wordcount)+", Average words per section is: "+ self.log.debug("Total wordcount is: "+ str(wordcount)+", Average words per section is: "+
unicode_type(words_per_chptr)+", Marked up "+unicode_type(self.html_preprocess_sections)+" chapters") str(words_per_chptr)+", Marked up "+str(self.html_preprocess_sections)+" chapters")
return html return html
def punctuation_unwrap(self, length, content, format): def punctuation_unwrap(self, length, content, format):
@ -367,8 +366,8 @@ class HeuristicProcessor:
# define the pieces of the regex # define the pieces of the regex
# (?<!\&\w{4});) is a semicolon not part of an entity # (?<!\&\w{4});) is a semicolon not part of an entity
lookahead = "(?<=.{"+unicode_type(length)+r"}([a-zა-ჰäëïöüàèìòùáćéíĺóŕńśúýâêîôûçąężıãõñæøþðßěľščťžňďřů,:)\\IAß]|(?<!\&\w{4});))" lookahead = "(?<=.{"+str(length)+r"}([a-zა-ჰäëïöüàèìòùáćéíĺóŕńśúýâêîôûçąężıãõñæøþðßěľščťžňďřů,:)\\IAß]|(?<!\&\w{4});))"
em_en_lookahead = "(?<=.{"+unicode_type(length)+"}[\u2013\u2014])" em_en_lookahead = "(?<=.{"+str(length)+"}[\u2013\u2014])"
soft_hyphen = "\xad" soft_hyphen = "\xad"
line_ending = "\\s*(?P<style_close></(span|[iub])>)?\\s*(</(p|div)>)?" line_ending = "\\s*(?P<style_close></(span|[iub])>)?\\s*(</(p|div)>)?"
blanklines = "\\s*(?P<up2threeblanks><(p|span|div)[^>]*>\\s*(<(p|span|div)[^>]*>\\s*</(span|p|div)>\\s*)</(span|p|div)>\\s*){0,3}\\s*" blanklines = "\\s*(?P<up2threeblanks><(p|span|div)[^>]*>\\s*(<(p|span|div)[^>]*>\\s*</(span|p|div)>\\s*)</(span|p|div)>\\s*){0,3}\\s*"
@ -428,18 +427,18 @@ class HeuristicProcessor:
return html return html
def fix_nbsp_indents(self, html): def fix_nbsp_indents(self, html):
txtindent = re.compile(unicode_type(r'<(?P<tagtype>p|div)(?P<formatting>[^>]*)>\s*(?P<span>(<span[^>]*>\s*)+)?\s*(\u00a0){2,}'), re.IGNORECASE) txtindent = re.compile(r'<(?P<tagtype>p|div)(?P<formatting>[^>]*)>\s*(?P<span>(<span[^>]*>\s*)+)?\s*(\u00a0){2,}', re.IGNORECASE)
html = txtindent.sub(self.insert_indent, html) html = txtindent.sub(self.insert_indent, html)
if self.found_indents > 1: if self.found_indents > 1:
self.log.debug("replaced "+unicode_type(self.found_indents)+ " nbsp indents with inline styles") self.log.debug("replaced "+str(self.found_indents)+ " nbsp indents with inline styles")
return html return html
def cleanup_markup(self, html): def cleanup_markup(self, html):
# remove remaining non-breaking spaces # remove remaining non-breaking spaces
html = re.sub(unicode_type(r'\u00a0'), ' ', html) html = re.sub(r'\u00a0', ' ', html)
# Get rid of various common microsoft specific tags which can cause issues later # Get rid of various common microsoft specific tags which can cause issues later
# Get rid of empty <o:p> tags to simplify other processing # Get rid of empty <o:p> tags to simplify other processing
html = re.sub(unicode_type(r'\s*<o:p>\s*</o:p>'), ' ', html) html = re.sub(r'\s*<o:p>\s*</o:p>', ' ', html)
# Delete microsoft 'smart' tags # Delete microsoft 'smart' tags
html = re.sub('(?i)</?st1:\\w+>', '', html) html = re.sub('(?i)</?st1:\\w+>', '', html)
# Re-open self closing paragraph tags # Re-open self closing paragraph tags
@ -479,8 +478,8 @@ class HeuristicProcessor:
blanklines = self.blankreg.findall(html) blanklines = self.blankreg.findall(html)
lines = self.linereg.findall(html) lines = self.linereg.findall(html)
if len(lines) > 1: if len(lines) > 1:
self.log.debug("There are " + unicode_type(len(blanklines)) + " blank lines. " + self.log.debug("There are " + str(len(blanklines)) + " blank lines. " +
unicode_type(float(len(blanklines)) / float(len(lines))) + " percent blank") str(float(len(blanklines)) / float(len(lines))) + " percent blank")
if float(len(blanklines)) / float(len(lines)) > 0.40: if float(len(blanklines)) / float(len(lines)) > 0.40:
return True return True
@ -502,11 +501,11 @@ class HeuristicProcessor:
lines = float(len(self.single_blank.findall(to_merge))) - 1. lines = float(len(self.single_blank.findall(to_merge))) - 1.
em = base_em + (em_per_line * lines) em = base_em + (em_per_line * lines)
if to_merge.find('whitespace'): if to_merge.find('whitespace'):
newline = self.any_multi_blank.sub('\n<p class="whitespace'+unicode_type(int(em * 10))+ newline = self.any_multi_blank.sub('\n<p class="whitespace'+str(int(em * 10))+
'" style="text-align:center; margin-top:'+unicode_type(em)+'em"> </p>', match.group(0)) '" style="text-align:center; margin-top:'+str(em)+'em"> </p>', match.group(0))
else: else:
newline = self.any_multi_blank.sub('\n<p class="softbreak'+unicode_type(int(em * 10))+ newline = self.any_multi_blank.sub('\n<p class="softbreak'+str(int(em * 10))+
'" style="text-align:center; margin-top:'+unicode_type(em)+'em"> </p>', match.group(0)) '" style="text-align:center; margin-top:'+str(em)+'em"> </p>', match.group(0))
return newline return newline
html = self.any_multi_blank.sub(merge_matches, html) html = self.any_multi_blank.sub(merge_matches, html)
@ -530,9 +529,9 @@ class HeuristicProcessor:
top_margin = '' top_margin = ''
bottom_margin = '' bottom_margin = ''
if initblanks is not None: if initblanks is not None:
top_margin = 'margin-top:'+unicode_type(len(self.single_blank.findall(initblanks)))+'em;' top_margin = 'margin-top:'+str(len(self.single_blank.findall(initblanks)))+'em;'
if endblanks is not None: if endblanks is not None:
bottom_margin = 'margin-bottom:'+unicode_type(len(self.single_blank.findall(endblanks)))+'em;' bottom_margin = 'margin-bottom:'+str(len(self.single_blank.findall(endblanks)))+'em;'
if initblanks is None and endblanks is None: if initblanks is None and endblanks is None:
return content return content
@ -609,7 +608,7 @@ class HeuristicProcessor:
else: else:
replacement_break = re.sub('(?i)(width=\\d+\\%?|width:\\s*\\d+(\\%|px|pt|em)?;?)', '', replacement_break) replacement_break = re.sub('(?i)(width=\\d+\\%?|width:\\s*\\d+(\\%|px|pt|em)?;?)', '', replacement_break)
divpercent = (100 - width) // 2 divpercent = (100 - width) // 2
hr_open = re.sub('45', unicode_type(divpercent), hr_open) hr_open = re.sub('45', str(divpercent), hr_open)
scene_break = hr_open+replacement_break+'</div>' scene_break = hr_open+replacement_break+'</div>'
else: else:
scene_break = hr_open+'<hr style="height: 3px; background:#505050" /></div>' scene_break = hr_open+'<hr style="height: 3px; background:#505050" /></div>'
@ -669,12 +668,12 @@ class HeuristicProcessor:
else: else:
styles = match.group('styles').split(';') styles = match.group('styles').split(';')
is_paragraph = self.check_paragraph(content) is_paragraph = self.check_paragraph(content)
# print "styles for this line are: "+unicode_type(styles) # print "styles for this line are: "+str(styles)
split_styles = [] split_styles = []
for style in styles: for style in styles:
# print "style is: "+unicode_type(style) # print "style is: "+str(style)
newstyle = style.split(':') newstyle = style.split(':')
# print "newstyle is: "+unicode_type(newstyle) # print "newstyle is: "+str(newstyle)
split_styles.append(newstyle) split_styles.append(newstyle)
styles = split_styles styles = split_styles
for style, setting in styles: for style, setting in styles:
@ -685,7 +684,7 @@ class HeuristicProcessor:
if 9 < setting < 14: if 9 < setting < 14:
text_indent = indented_text text_indent = indented_text
else: else:
text_indent = style+':'+unicode_type(setting)+'pt;' text_indent = style+':'+str(setting)+'pt;'
if style == 'padding': if style == 'padding':
setting = re.sub('pt', '', setting).split(' ') setting = re.sub('pt', '', setting).split(' ')
if int(setting[1]) < 16 and int(setting[3]) < 16: if int(setting[1]) < 16 and int(setting[3]) < 16:
@ -706,23 +705,23 @@ class HeuristicProcessor:
blockquote_open_loop = blockquote_open blockquote_open_loop = blockquote_open
if debugabby: if debugabby:
self.log.debug('\n\n******\n') self.log.debug('\n\n******\n')
self.log.debug('padding top is: '+unicode_type(setting[0])) self.log.debug('padding top is: '+str(setting[0]))
self.log.debug('padding right is:' +unicode_type(setting[1])) self.log.debug('padding right is:' +str(setting[1]))
self.log.debug('padding bottom is: ' + unicode_type(setting[2])) self.log.debug('padding bottom is: ' + str(setting[2]))
self.log.debug('padding left is: ' +unicode_type(setting[3])) self.log.debug('padding left is: ' +str(setting[3]))
# print "text-align is: "+unicode_type(text_align) # print "text-align is: "+str(text_align)
# print "\n***\nline is:\n "+unicode_type(match.group(0))+'\n' # print "\n***\nline is:\n "+str(match.group(0))+'\n'
if debugabby: if debugabby:
# print "this line is a paragraph = "+unicode_type(is_paragraph)+", previous line was "+unicode_type(self.previous_was_paragraph) # print "this line is a paragraph = "+str(is_paragraph)+", previous line was "+str(self.previous_was_paragraph)
self.log.debug("styles for this line were:", styles) self.log.debug("styles for this line were:", styles)
self.log.debug('newline is:') self.log.debug('newline is:')
self.log.debug(blockquote_open_loop+blockquote_close_loop+ self.log.debug(blockquote_open_loop+blockquote_close_loop+
paragraph_before+'<p style="'+text_indent+text_align+ paragraph_before+'<p style="'+text_indent+text_align+
'">'+content+'</p>'+paragraph_after+'\n\n\n\n\n') '">'+content+'</p>'+paragraph_after+'\n\n\n\n\n')
# print "is_paragraph is "+unicode_type(is_paragraph)+", previous_was_paragraph is "+unicode_type(self.previous_was_paragraph) # print "is_paragraph is "+str(is_paragraph)+", previous_was_paragraph is "+str(self.previous_was_paragraph)
self.previous_was_paragraph = is_paragraph self.previous_was_paragraph = is_paragraph
# print "previous_was_paragraph is now set to "+unicode_type(self.previous_was_paragraph)+"\n\n\n" # print "previous_was_paragraph is now set to "+str(self.previous_was_paragraph)+"\n\n\n"
return blockquote_open_loop+blockquote_close_loop+paragraph_before+'<p style="'+text_indent+text_align+'">'+content+'</p>'+paragraph_after return blockquote_open_loop+blockquote_close_loop+paragraph_before+'<p style="'+text_indent+text_align+'">'+content+'</p>'+paragraph_after
html = abbyy_line.sub(convert_styles, html) html = abbyy_line.sub(convert_styles, html)
@ -805,12 +804,12 @@ class HeuristicProcessor:
# more of the lines break in the same region of the document then unwrapping is required # more of the lines break in the same region of the document then unwrapping is required
docanalysis = DocAnalysis(format, html) docanalysis = DocAnalysis(format, html)
hardbreaks = docanalysis.line_histogram(.50) hardbreaks = docanalysis.line_histogram(.50)
self.log.debug("Hard line breaks check returned "+unicode_type(hardbreaks)) self.log.debug("Hard line breaks check returned "+str(hardbreaks))
# Calculate Length # Calculate Length
unwrap_factor = getattr(self.extra_opts, 'html_unwrap_factor', 0.4) unwrap_factor = getattr(self.extra_opts, 'html_unwrap_factor', 0.4)
length = docanalysis.line_length(unwrap_factor) length = docanalysis.line_length(unwrap_factor)
self.log.debug("Median line length is " + unicode_type(length) + ", calculated with " + format + " format") self.log.debug("Median line length is " + str(length) + ", calculated with " + format + " format")
# ##### Unwrap lines ###### # ##### Unwrap lines ######
if getattr(self.extra_opts, 'unwrap_lines', False): if getattr(self.extra_opts, 'unwrap_lines', False):
@ -832,7 +831,7 @@ class HeuristicProcessor:
# If still no sections after unwrapping mark split points on lines with no punctuation # If still no sections after unwrapping mark split points on lines with no punctuation
if self.html_preprocess_sections < self.min_chapters and getattr(self.extra_opts, 'markup_chapter_headings', False): if self.html_preprocess_sections < self.min_chapters and getattr(self.extra_opts, 'markup_chapter_headings', False):
self.log.debug("Looking for more split points based on punctuation," self.log.debug("Looking for more split points based on punctuation,"
" currently have " + unicode_type(self.html_preprocess_sections)) " currently have " + str(self.html_preprocess_sections))
chapdetect3 = re.compile( chapdetect3 = re.compile(
r'<(?P<styles>(p|div)[^>]*)>\s*(?P<section>(<span[^>]*>)?\s*(?!([\W]+\s*)+)' r'<(?P<styles>(p|div)[^>]*)>\s*(?P<section>(<span[^>]*>)?\s*(?!([\W]+\s*)+)'
r'(<[ibu][^>]*>){0,2}\s*(<span[^>]*>)?\s*(<[ibu][^>]*>){0,2}\s*(<span[^>]*>)?\s*' r'(<[ibu][^>]*>){0,2}\s*(<span[^>]*>)?\s*(<[ibu][^>]*>){0,2}\s*(<span[^>]*>)?\s*'

View File

@ -12,7 +12,7 @@ from css_parser.css import Property, CSSRule
from calibre import force_unicode from calibre import force_unicode
from calibre.ebooks import parse_css_length from calibre.ebooks import parse_css_length
from calibre.ebooks.oeb.normalize_css import normalizers, safe_parser from calibre.ebooks.oeb.normalize_css import normalizers, safe_parser
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
def compile_pat(pat): def compile_pat(pat):
@ -161,7 +161,7 @@ def transform_number(val, op, raw):
v = op(v, val) v = op(v, val)
if int(v) == v: if int(v) == v:
v = int(v) v = int(v)
return unicode_type(v) + u return str(v) + u
class Rule: class Rule:
@ -379,7 +379,7 @@ def test(return_tests=False): # {{{
r = Rule(**rule) r = Rule(**rule)
decl = StyleDeclaration(safe_parser().parseStyle(style)) decl = StyleDeclaration(safe_parser().parseStyle(style))
r.process_declaration(decl) r.process_declaration(decl)
return unicode_type(decl) return str(decl)
class TestTransforms(unittest.TestCase): class TestTransforms(unittest.TestCase):
longMessage = True longMessage = True

View File

@ -6,7 +6,7 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from collections import OrderedDict from collections import OrderedDict
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
class Note: class Note:
@ -52,8 +52,8 @@ class Footnotes:
if note is not None and note.type == 'normal': if note is not None and note.type == 'normal':
self.counter += 1 self.counter += 1
anchor = 'note_%d' % self.counter anchor = 'note_%d' % self.counter
self.notes[anchor] = (unicode_type(self.counter), note) self.notes[anchor] = (str(self.counter), note)
return anchor, unicode_type(self.counter) return anchor, str(self.counter)
return None, None return None, None
def __iter__(self): def __iter__(self):

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.block_styles import ParagraphStyle
from calibre.ebooks.docx.char_styles import RunStyle, inherit from calibre.ebooks.docx.char_styles import RunStyle, inherit
from calibre.ebooks.metadata import roman from calibre.ebooks.metadata import roman
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
STYLE_MAP = { STYLE_MAP = {
'aiueo': 'hiragana', 'aiueo': 'hiragana',
@ -291,7 +291,7 @@ class Numbering:
seen_instances.add(num_id) seen_instances.add(num_id)
p.tag = 'li' p.tag = 'li'
p.set('value', '%s' % counter[ilvl]) p.set('value', '%s' % counter[ilvl])
p.set('list-lvl', unicode_type(ilvl)) p.set('list-lvl', str(ilvl))
p.set('list-id', num_id) p.set('list-id', num_id)
if lvl.num_template is not None: if lvl.num_template is not None:
val = lvl.format_template(counter, ilvl, lvl.num_template) val = lvl.format_template(counter, ilvl, lvl.num_template)

View File

@ -9,7 +9,7 @@ from lxml.html.builder import TABLE, TR, TD
from calibre.ebooks.docx.block_styles import inherit, read_shd as rs, read_border, binary_property, border_props, ParagraphStyle, border_to_css from calibre.ebooks.docx.block_styles import inherit, read_shd as rs, read_border, binary_property, border_props, ParagraphStyle, border_to_css
from calibre.ebooks.docx.char_styles import RunStyle from calibre.ebooks.docx.char_styles import RunStyle
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
# Read from XML {{{ # Read from XML {{{
read_shd = rs read_shd = rs
@ -646,9 +646,9 @@ class Table:
td = TD() td = TD()
style_map[td] = s = self.style_map[tc] style_map[td] = s = self.style_map[tc]
if s.col_span is not inherit: if s.col_span is not inherit:
td.set('colspan', unicode_type(s.col_span)) td.set('colspan', str(s.col_span))
if s.row_span is not inherit: if s.row_span is not inherit:
td.set('rowspan', unicode_type(s.row_span)) td.set('rowspan', str(s.row_span))
td.tail = '\n\t\t\t' td.tail = '\n\t\t\t'
tr.append(td) tr.append(td)
for x in self.namespace.XPath('./w:p|./w:tbl')(tc): for x in self.namespace.XPath('./w:p|./w:tbl')(tc):

View File

@ -28,7 +28,7 @@ from calibre.ebooks.docx.fields import Fields
from calibre.ebooks.docx.settings import Settings from calibre.ebooks.docx.settings import Settings
from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.ebooks.metadata.opf2 import OPFCreator
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1 from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
NBSP = '\xa0' NBSP = '\xa0'
@ -480,7 +480,7 @@ class Convert:
current_hyperlink = x current_hyperlink = x
elif x.tag.endswith('}instrText') and x.text and x.text.strip().startswith('TOC '): elif x.tag.endswith('}instrText') and x.text and x.text.strip().startswith('TOC '):
old_anchor = current_anchor old_anchor = current_anchor
anchor = unicode_type(uuid.uuid4()) anchor = str(uuid.uuid4())
self.anchor_map[anchor] = current_anchor = generate_anchor('toc', frozenset(itervalues(self.anchor_map))) self.anchor_map[anchor] = current_anchor = generate_anchor('toc', frozenset(itervalues(self.anchor_map)))
self.toc_anchor = current_anchor self.toc_anchor = current_anchor
if old_anchor is not None: if old_anchor is not None:
@ -507,7 +507,7 @@ class Convert:
if m is not None: if m is not None:
n = min(6, max(1, int(m.group(1)))) n = min(6, max(1, int(m.group(1))))
dest.tag = 'h%d' % n dest.tag = 'h%d' % n
dest.set('data-heading-level', unicode_type(n)) dest.set('data-heading-level', str(n))
if style.bidi is True: if style.bidi is True:
dest.set('dir', 'rtl') dest.set('dir', 'rtl')

View File

@ -18,7 +18,7 @@ from calibre.ebooks.pdf.render.common import PAPER_SIZES
from calibre.utils.date import utcnow from calibre.utils.date import utcnow
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1 from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
from calibre.utils.zipfile import ZipFile from calibre.utils.zipfile import ZipFile
from polyglot.builtins import iteritems, unicode_type, native_string_type from polyglot.builtins import iteritems, native_string_type
def xml2str(root, pretty_print=False, with_tail=False): def xml2str(root, pretty_print=False, with_tail=False):
@ -65,9 +65,9 @@ def create_skeleton(opts, namespaces=None):
def margin(which): def margin(which):
val = page_margin(opts, which) val = page_margin(opts, which)
return w(which), unicode_type(int(val * 20)) return w(which), str(int(val * 20))
body.append(E.sectPr( body.append(E.sectPr(
E.pgSz(**{w('w'):unicode_type(width), w('h'):unicode_type(height)}), E.pgSz(**{w('w'):str(width), w('h'):str(height)}),
E.pgMar(**dict(map(margin, 'left top right bottom'.split()))), E.pgMar(**dict(map(margin, 'left top right bottom'.split()))),
E.cols(**{w('space'):'720'}), E.cols(**{w('space'):'720'}),
E.docGrid(**{w('linePitch'):"360"}), E.docGrid(**{w('linePitch'):"360"}),

View File

@ -18,7 +18,7 @@ from calibre.ebooks.docx.writer.lists import ListsManager
from calibre.ebooks.oeb.stylizer import Stylizer as Sz, Style as St from calibre.ebooks.oeb.stylizer import Stylizer as Sz, Style as St
from calibre.ebooks.oeb.base import XPath, barename from calibre.ebooks.oeb.base import XPath, barename
from calibre.utils.localization import lang_as_iso639_1 from calibre.utils.localization import lang_as_iso639_1
from polyglot.builtins import unicode_type, string_or_bytes from polyglot.builtins import string_or_bytes
def lang_for_tag(tag): def lang_for_tag(tag):
@ -108,7 +108,7 @@ class TextRun:
for text, preserve_whitespace, bookmark in self.texts: for text, preserve_whitespace, bookmark in self.texts:
if bookmark is not None: if bookmark is not None:
bid = links_manager.bookmark_id bid = links_manager.bookmark_id
makeelement(r, 'w:bookmarkStart', w_id=unicode_type(bid), w_name=bookmark) makeelement(r, 'w:bookmarkStart', w_id=str(bid), w_name=bookmark)
if text is None: if text is None:
makeelement(r, 'w:br', w_clear=preserve_whitespace) makeelement(r, 'w:br', w_clear=preserve_whitespace)
elif hasattr(text, 'xpath'): elif hasattr(text, 'xpath'):
@ -123,7 +123,7 @@ class TextRun:
else: else:
add_text('', preserve_whitespace) add_text('', preserve_whitespace)
if bookmark is not None: if bookmark is not None:
makeelement(r, 'w:bookmarkEnd', w_id=unicode_type(bid)) makeelement(r, 'w:bookmarkEnd', w_id=str(bid))
def __repr__(self): def __repr__(self):
return repr(self.texts) return repr(self.texts)
@ -139,7 +139,7 @@ class TextRun:
def style_weight(self): def style_weight(self):
ans = 0 ans = 0
for text, preserve_whitespace, bookmark in self.texts: for text, preserve_whitespace, bookmark in self.texts:
if isinstance(text, unicode_type): if isinstance(text, str):
ans += len(text) ans += len(text)
return ans return ans
@ -219,7 +219,7 @@ class Block:
p = makeelement(body, 'w:p') p = makeelement(body, 'w:p')
end_bookmarks = [] end_bookmarks = []
for bmark in self.bookmarks: for bmark in self.bookmarks:
end_bookmarks.append(unicode_type(self.links_manager.bookmark_id)) end_bookmarks.append(str(self.links_manager.bookmark_id))
makeelement(p, 'w:bookmarkStart', w_id=end_bookmarks[-1], w_name=bmark) makeelement(p, 'w:bookmarkStart', w_id=end_bookmarks[-1], w_name=bmark)
if self.block_lang: if self.block_lang:
rpr = makeelement(p, 'w:rPr') rpr = makeelement(p, 'w:rPr')
@ -232,8 +232,8 @@ class Block:
self.float_spec.serialize(self, ppr) self.float_spec.serialize(self, ppr)
if self.numbering_id is not None: if self.numbering_id is not None:
numpr = makeelement(ppr, 'w:numPr') numpr = makeelement(ppr, 'w:numPr')
makeelement(numpr, 'w:ilvl', w_val=unicode_type(self.numbering_id[1])) makeelement(numpr, 'w:ilvl', w_val=str(self.numbering_id[1]))
makeelement(numpr, 'w:numId', w_val=unicode_type(self.numbering_id[0])) makeelement(numpr, 'w:numId', w_val=str(self.numbering_id[0]))
if self.linked_style is not None: if self.linked_style is not None:
makeelement(ppr, 'w:pStyle', w_val=self.linked_style.id) makeelement(ppr, 'w:pStyle', w_val=self.linked_style.id)
elif self.style.id: elif self.style.id:
@ -453,8 +453,8 @@ class Convert:
if self.add_toc: if self.add_toc:
self.links_manager.process_toc_links(self.oeb) self.links_manager.process_toc_links(self.oeb)
if self.add_cover and self.oeb.metadata.cover and unicode_type(self.oeb.metadata.cover[0]) in self.oeb.manifest.ids: if self.add_cover and self.oeb.metadata.cover and str(self.oeb.metadata.cover[0]) in self.oeb.manifest.ids:
cover_id = unicode_type(self.oeb.metadata.cover[0]) cover_id = str(self.oeb.metadata.cover[0])
item = self.oeb.manifest.ids[cover_id] item = self.oeb.manifest.ids[cover_id]
self.cover_img = self.images_manager.read_image(item.href) self.cover_img = self.images_manager.read_image(item.href)

View File

@ -9,7 +9,7 @@ import os
import posixpath import posixpath
from collections import namedtuple from collections import namedtuple
from functools import partial from functools import partial
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
from lxml import etree from lxml import etree
@ -34,7 +34,7 @@ def get_image_margins(style):
ans = {} ans = {}
for edge in 'Left Right Top Bottom'.split(): for edge in 'Left Right Top Bottom'.split():
val = as_num(getattr(style, 'padding' + edge)) + as_num(getattr(style, 'margin' + edge)) val = as_num(getattr(style, 'padding' + edge)) + as_num(getattr(style, 'margin' + edge))
ans['dist' + edge[0]] = unicode_type(pt_to_emu(val)) ans['dist' + edge[0]] = str(pt_to_emu(val))
return ans return ans
@ -131,7 +131,7 @@ class ImagesManager:
makeelement(parent, 'wp:simplePos', x='0', y='0') makeelement(parent, 'wp:simplePos', x='0', y='0')
makeelement(makeelement(parent, 'wp:positionH', relativeFrom='margin'), 'wp:align').text = floating makeelement(makeelement(parent, 'wp:positionH', relativeFrom='margin'), 'wp:align').text = floating
makeelement(makeelement(parent, 'wp:positionV', relativeFrom='line'), 'wp:align').text = 'top' makeelement(makeelement(parent, 'wp:positionV', relativeFrom='line'), 'wp:align').text = 'top'
makeelement(parent, 'wp:extent', cx=unicode_type(width), cy=unicode_type(height)) makeelement(parent, 'wp:extent', cx=str(width), cy=str(height))
if fake_margins: if fake_margins:
# DOCX does not support setting margins for inline images, so we # DOCX does not support setting margins for inline images, so we
# fake it by using effect extents to simulate margins # fake it by using effect extents to simulate margins
@ -149,7 +149,7 @@ class ImagesManager:
def create_docx_image_markup(self, parent, name, alt, img_rid, width, height): def create_docx_image_markup(self, parent, name, alt, img_rid, width, height):
makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces
makeelement(parent, 'wp:docPr', id=unicode_type(self.count), name=name, descr=alt) makeelement(parent, 'wp:docPr', id=str(self.count), name=name, descr=alt)
makeelement(makeelement(parent, 'wp:cNvGraphicFramePr'), 'a:graphicFrameLocks', noChangeAspect="1") makeelement(makeelement(parent, 'wp:cNvGraphicFramePr'), 'a:graphicFrameLocks', noChangeAspect="1")
g = makeelement(parent, 'a:graphic') g = makeelement(parent, 'a:graphic')
gd = makeelement(g, 'a:graphicData', uri=namespaces['pic']) gd = makeelement(g, 'a:graphicData', uri=namespaces['pic'])
@ -162,7 +162,7 @@ class ImagesManager:
makeelement(makeelement(bf, 'a:stretch'), 'a:fillRect') makeelement(makeelement(bf, 'a:stretch'), 'a:fillRect')
spPr = makeelement(pic, 'pic:spPr') spPr = makeelement(pic, 'pic:spPr')
xfrm = makeelement(spPr, 'a:xfrm') xfrm = makeelement(spPr, 'a:xfrm')
makeelement(xfrm, 'a:off', x='0', y='0'), makeelement(xfrm, 'a:ext', cx=unicode_type(width), cy=unicode_type(height)) makeelement(xfrm, 'a:off', x='0', y='0'), makeelement(xfrm, 'a:ext', cx=str(width), cy=str(height))
makeelement(makeelement(spPr, 'a:prstGeom', prst='rect'), 'a:avLst') makeelement(makeelement(spPr, 'a:prstGeom', prst='rect'), 'a:avLst')
def create_filename(self, href, fmt): def create_filename(self, href, fmt):
@ -173,7 +173,7 @@ class ImagesManager:
base = fname base = fname
while fname.lower() in self.seen_filenames: while fname.lower() in self.seen_filenames:
num += 1 num += 1
fname = base + unicode_type(num) fname = base + str(num)
self.seen_filenames.add(fname.lower()) self.seen_filenames.add(fname.lower())
fname += os.extsep + fmt.lower() fname += os.extsep + fmt.lower()
return fname return fname
@ -208,7 +208,7 @@ class ImagesManager:
makeelement(makeelement(parent, 'wp:positionH', relativeFrom='page'), 'wp:align').text = 'center' makeelement(makeelement(parent, 'wp:positionH', relativeFrom='page'), 'wp:align').text = 'center'
makeelement(makeelement(parent, 'wp:positionV', relativeFrom='page'), 'wp:align').text = 'center' makeelement(makeelement(parent, 'wp:positionV', relativeFrom='page'), 'wp:align').text = 'center'
width, height = map(pt_to_emu, (width, height)) width, height = map(pt_to_emu, (width, height))
makeelement(parent, 'wp:extent', cx=unicode_type(width), cy=unicode_type(height)) makeelement(parent, 'wp:extent', cx=str(width), cy=str(height))
makeelement(parent, 'wp:effectExtent', l='0', r='0', t='0', b='0') makeelement(parent, 'wp:effectExtent', l='0', r='0', t='0', b='0')
makeelement(parent, 'wp:wrapTopAndBottom') makeelement(parent, 'wp:wrapTopAndBottom')
self.create_docx_image_markup(parent, 'cover.jpg', _('Cover'), img.rid, width, height) self.create_docx_image_markup(parent, 'cover.jpg', _('Cover'), img.rid, width, height)

View File

@ -10,7 +10,6 @@ from uuid import uuid4
from calibre.ebooks.oeb.base import urlquote from calibre.ebooks.oeb.base import urlquote
from calibre.utils.filenames import ascii_text from calibre.utils.filenames import ascii_text
from polyglot.builtins import unicode_type
from polyglot.urllib import urlparse from polyglot.urllib import urlparse
@ -37,7 +36,7 @@ class TOCItem:
p = makeelement(body, 'w:p', append=False) p = makeelement(body, 'w:p', append=False)
ppr = makeelement(p, 'w:pPr') ppr = makeelement(p, 'w:pPr')
makeelement(ppr, 'w:pStyle', w_val="Normal") makeelement(ppr, 'w:pStyle', w_val="Normal")
makeelement(ppr, 'w:ind', w_left='0', w_firstLineChars='0', w_firstLine='0', w_leftChars=unicode_type(200 * self.level)) makeelement(ppr, 'w:ind', w_left='0', w_firstLineChars='0', w_firstLine='0', w_leftChars=str(200 * self.level))
if self.is_first: if self.is_first:
makeelement(ppr, 'w:pageBreakBefore', w_val='off') makeelement(ppr, 'w:pageBreakBefore', w_val='off')
r = makeelement(p, 'w:r') r = makeelement(p, 'w:r')
@ -71,7 +70,7 @@ class LinksManager:
self.namespace = namespace self.namespace = namespace
self.log = log self.log = log
self.document_relationships = document_relationships self.document_relationships = document_relationships
self.top_anchor = unicode_type(uuid4().hex) self.top_anchor = str(uuid4().hex)
self.anchor_map = {} self.anchor_map = {}
self.used_bookmark_names = set() self.used_bookmark_names = set()
self.bmark_id = 0 self.bmark_id = 0

View File

@ -8,7 +8,7 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
from collections import defaultdict from collections import defaultdict
from operator import attrgetter from operator import attrgetter
from polyglot.builtins import iteritems, itervalues, unicode_type from polyglot.builtins import iteritems, itervalues
LIST_STYLES = frozenset( LIST_STYLES = frozenset(
'disc circle square decimal decimal-leading-zero lower-roman upper-roman' 'disc circle square decimal decimal-leading-zero lower-roman upper-roman'
@ -83,7 +83,7 @@ class NumberingDefinition:
def serialize(self, parent): def serialize(self, parent):
makeelement = self.namespace.makeelement makeelement = self.namespace.makeelement
an = makeelement(parent, 'w:abstractNum', w_abstractNumId=unicode_type(self.num_id)) an = makeelement(parent, 'w:abstractNum', w_abstractNumId=str(self.num_id))
makeelement(an, 'w:multiLevelType', w_val='hybridMultilevel') makeelement(an, 'w:multiLevelType', w_val='hybridMultilevel')
makeelement(an, 'w:name', w_val='List %d' % (self.num_id + 1)) makeelement(an, 'w:name', w_val='List %d' % (self.num_id + 1))
for level in self.levels: for level in self.levels:
@ -114,12 +114,12 @@ class Level:
return hash((self.start, self.num_fmt, self.lvl_text)) return hash((self.start, self.num_fmt, self.lvl_text))
def serialize(self, parent, makeelement): def serialize(self, parent, makeelement):
lvl = makeelement(parent, 'w:lvl', w_ilvl=unicode_type(self.ilvl)) lvl = makeelement(parent, 'w:lvl', w_ilvl=str(self.ilvl))
makeelement(lvl, 'w:start', w_val=unicode_type(self.start)) makeelement(lvl, 'w:start', w_val=str(self.start))
makeelement(lvl, 'w:numFmt', w_val=self.num_fmt) makeelement(lvl, 'w:numFmt', w_val=self.num_fmt)
makeelement(lvl, 'w:lvlText', w_val=self.lvl_text) makeelement(lvl, 'w:lvlText', w_val=self.lvl_text)
makeelement(lvl, 'w:lvlJc', w_val='left') makeelement(lvl, 'w:lvlJc', w_val='left')
makeelement(makeelement(lvl, 'w:pPr'), 'w:ind', w_hanging='360', w_left=unicode_type(1152 + self.ilvl * 360)) makeelement(makeelement(lvl, 'w:pPr'), 'w:ind', w_hanging='360', w_left=str(1152 + self.ilvl * 360))
if self.num_fmt == 'bullet': if self.num_fmt == 'bullet':
ff = {'\uf0b7':'Symbol', '\uf0a7':'Wingdings'}.get(self.lvl_text, 'Courier New') 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") makeelement(makeelement(lvl, 'w:rPr'), 'w:rFonts', w_ascii=ff, w_hAnsi=ff, w_hint="default")
@ -165,5 +165,5 @@ class ListsManager:
defn.serialize(parent) defn.serialize(parent)
makeelement = self.namespace.makeelement makeelement = self.namespace.makeelement
for defn in self.definitions: for defn in self.definitions:
n = makeelement(parent, 'w:num', w_numId=unicode_type(defn.num_id + 1)) n = makeelement(parent, 'w:num', w_numId=str(defn.num_id + 1))
makeelement(n, 'w:abstractNumId', w_val=unicode_type(defn.num_id)) makeelement(n, 'w:abstractNumId', w_val=str(defn.num_id))

View File

@ -14,7 +14,7 @@ from lxml import etree
from calibre.ebooks import parse_css_length from calibre.ebooks import parse_css_length
from calibre.ebooks.docx.writer.utils import convert_color, int_or_zero from calibre.ebooks.docx.writer.utils import convert_color, int_or_zero
from calibre.utils.localization import lang_as_iso639_1 from calibre.utils.localization import lang_as_iso639_1
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
from tinycss.css21 import CSS21Parser from tinycss.css21 import CSS21Parser
css_parser = CSS21Parser() css_parser = CSS21Parser()
@ -76,7 +76,7 @@ class CombinedStyle:
pPr = makeelement(block, 'w:pPr') pPr = makeelement(block, 'w:pPr')
self.bs.serialize_properties(pPr, normal_style.bs) self.bs.serialize_properties(pPr, normal_style.bs)
if self.outline_level is not None: if self.outline_level is not None:
makeelement(pPr, 'w:outlineLvl', w_val=unicode_type(self.outline_level + 1)) makeelement(pPr, 'w:outlineLvl', w_val=str(self.outline_level + 1))
rPr = makeelement(block, 'w:rPr') rPr = makeelement(block, 'w:rPr')
self.rs.serialize_properties(rPr, normal_style.rs) self.rs.serialize_properties(rPr, normal_style.rs)
@ -109,16 +109,16 @@ class FloatSpec:
def serialize(self, block, parent): def serialize(self, block, parent):
if self.is_dropcaps: if self.is_dropcaps:
attrs = dict(w_dropCap='drop', w_lines=unicode_type(self.dropcaps_lines), w_wrap='around', w_vAnchor='text', w_hAnchor='text') attrs = dict(w_dropCap='drop', w_lines=str(self.dropcaps_lines), w_wrap='around', w_vAnchor='text', w_hAnchor='text')
else: else:
attrs = dict( attrs = dict(
w_wrap='around', w_vAnchor='text', w_hAnchor='text', w_xAlign=self.x_align, w_y='1', w_wrap='around', w_vAnchor='text', w_hAnchor='text', w_xAlign=self.x_align, w_y='1',
w_hSpace=unicode_type(self.h_space), w_vSpace=unicode_type(self.v_space), w_hRule=self.h_rule w_hSpace=str(self.h_space), w_vSpace=str(self.v_space), w_hRule=self.h_rule
) )
if self.w is not None: if self.w is not None:
attrs['w_w'] = unicode_type(self.w) attrs['w_w'] = str(self.w)
if self.h is not None: if self.h is not None:
attrs['w_h'] = unicode_type(self.h) attrs['w_h'] = str(self.h)
self.makeelement(parent, 'w:framePr', **attrs) self.makeelement(parent, 'w:framePr', **attrs)
# Margins are already applied by the frame style, so override them to # Margins are already applied by the frame style, so override them to
# be zero on individual blocks # be zero on individual blocks
@ -138,7 +138,7 @@ class FloatSpec:
width = getattr(self, 'border_%s_width' % edge) width = getattr(self, 'border_%s_width' % edge)
bstyle = getattr(self, 'border_%s_style' % edge) bstyle = getattr(self, 'border_%s_style' % edge)
self.makeelement( 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)) bdr, 'w:'+edge, w_space=str(padding), w_val=bstyle, w_sz=str(width), w_color=getattr(self, 'border_%s_color' % edge))
class DOCXStyle: class DOCXStyle:
@ -234,7 +234,7 @@ class TextStyle(DOCXStyle):
self.spacing = None self.spacing = None
va = css.first_vertical_align va = css.first_vertical_align
if isinstance(va, numbers.Number): if isinstance(va, numbers.Number):
self.vertical_align = unicode_type(int(va * 2)) self.vertical_align = str(int(va * 2))
else: else:
val = { val = {
'top':'superscript', 'text-top':'superscript', 'sup':'superscript', 'super':'superscript', 'top':'superscript', 'text-top':'superscript', 'sup':'superscript', 'super':'superscript',
@ -290,9 +290,9 @@ class TextStyle(DOCXStyle):
w = self.w w = self.w
is_normal_style = self is normal_style is_normal_style = self is normal_style
if is_normal_style or self.padding != normal_style.padding: if is_normal_style or self.padding != normal_style.padding:
bdr.set(w('space'), unicode_type(self.padding)) bdr.set(w('space'), str(self.padding))
if is_normal_style or self.border_width != normal_style.border_width: if is_normal_style or self.border_width != normal_style.border_width:
bdr.set(w('sz'), unicode_type(self.border_width)) bdr.set(w('sz'), str(self.border_width))
if is_normal_style or self.border_style != normal_style.border_style: if is_normal_style or self.border_style != normal_style.border_style:
bdr.set(w('val'), self.border_style) bdr.set(w('val'), self.border_style)
if is_normal_style or self.border_color != normal_style.border_color: if is_normal_style or self.border_color != normal_style.border_color:
@ -342,7 +342,7 @@ class TextStyle(DOCXStyle):
if check_attr('shadow'): if check_attr('shadow'):
rPr.append(makeelement(rPr, 'shadow', val=bmap(self.shadow))) rPr.append(makeelement(rPr, 'shadow', val=bmap(self.shadow)))
if check_attr('spacing'): if check_attr('spacing'):
rPr.append(makeelement(rPr, 'spacing', val=unicode_type(self.spacing or 0))) rPr.append(makeelement(rPr, 'spacing', val=str(self.spacing or 0)))
if is_normal_style: if is_normal_style:
rPr.append(makeelement(rPr, 'vertAlign', val=self.vertical_align if self.vertical_align in {'superscript', 'subscript'} else 'baseline')) 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: elif self.vertical_align != normal_style.vertical_align:
@ -380,7 +380,7 @@ class DescendantTextStyle:
for name, attr in (('sz', 'font_size'), ('b', 'bold'), ('i', 'italic')): for name, attr in (('sz', 'font_size'), ('b', 'bold'), ('i', 'italic')):
pval, cval = vals(attr) pval, cval = vals(attr)
if pval != cval: if pval != cval:
val = 'on' if attr in {'bold', 'italic'} else unicode_type(cval) # bold, italic are toggle properties val = 'on' if attr in {'bold', 'italic'} else str(cval) # bold, italic are toggle properties
for suffix in ('', 'Cs'): for suffix in ('', 'Cs'):
add(name + suffix, val=val) add(name + suffix, val=val)
@ -401,7 +401,7 @@ class DescendantTextStyle:
if check('shadow'): if check('shadow'):
add('shadow', val='on') # toggle property add('shadow', val='on') # toggle property
if check('spacing'): if check('spacing'):
add('spacing', val=unicode_type(child_style.spacing or 0)) add('spacing', val=str(child_style.spacing or 0))
if check('vertical_align'): if check('vertical_align'):
val = child_style.vertical_align val = child_style.vertical_align
if val in {'superscript', 'subscript', 'baseline'}: if val in {'superscript', 'subscript', 'baseline'}:
@ -411,9 +411,9 @@ class DescendantTextStyle:
bdr = {} bdr = {}
if check('padding'): if check('padding'):
bdr['space'] = unicode_type(child_style.padding) bdr['space'] = str(child_style.padding)
if check('border_width'): if check('border_width'):
bdr['sz'] = unicode_type(child_style.border_width) bdr['sz'] = str(child_style.border_width)
if check('border_style'): if check('border_style'):
bdr['val'] = child_style.border_style bdr['val'] = child_style.border_style
if check('border_color'): if check('border_color'):
@ -537,14 +537,14 @@ class BlockStyle(DOCXStyle):
e = bdr.makeelement(w(edge)) e = bdr.makeelement(w(edge))
padding = getattr(self, 'padding_' + edge) padding = getattr(self, 'padding_' + edge)
if (self is normal_style and padding > 0) or (padding != getattr(normal_style, 'padding_' + edge)): if (self is normal_style and padding > 0) or (padding != getattr(normal_style, 'padding_' + edge)):
e.set(w('space'), unicode_type(padding)) e.set(w('space'), str(padding))
width = getattr(self, 'border_%s_width' % edge) width = getattr(self, 'border_%s_width' % edge)
bstyle = getattr(self, 'border_%s_style' % edge) bstyle = getattr(self, 'border_%s_style' % edge)
if (self is normal_style and width > 0 and bstyle != 'none' if (self is normal_style and width > 0 and bstyle != 'none'
) or width != getattr(normal_style, 'border_%s_width' % edge ) or width != getattr(normal_style, 'border_%s_width' % edge
) or bstyle != getattr(normal_style, 'border_%s_style' % edge): ) or bstyle != getattr(normal_style, 'border_%s_style' % edge):
e.set(w('val'), bstyle) e.set(w('val'), bstyle)
e.set(w('sz'), unicode_type(width)) e.set(w('sz'), str(width))
e.set(w('color'), getattr(self, 'border_%s_color' % edge)) e.set(w('color'), getattr(self, 'border_%s_color' % edge))
if e.attrib: if e.attrib:
bdr.append(e) bdr.append(e)
@ -568,15 +568,15 @@ class BlockStyle(DOCXStyle):
if css_unit in ('em', 'ex'): if css_unit in ('em', 'ex'):
lines = max(0, int(css_val * (50 if css_unit == 'ex' else 100))) 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): if (self is normal_style and lines > 0) or getter(self) != getter(normal_style):
spacing.set(w(attr + 'Lines'), unicode_type(lines)) spacing.set(w(attr + 'Lines'), str(lines))
else: else:
getter = attrgetter('margin_' + edge) getter = attrgetter('margin_' + edge)
val = getter(self) val = getter(self)
if (self is normal_style and val > 0) or val != getter(normal_style): if (self is normal_style and val > 0) or val != getter(normal_style):
spacing.set(w(attr), unicode_type(val)) spacing.set(w(attr), str(val))
if self is normal_style or self.line_height != normal_style.line_height: if self is normal_style or self.line_height != normal_style.line_height:
spacing.set(w('line'), unicode_type(self.line_height)) spacing.set(w('line'), str(self.line_height))
spacing.set(w('lineRule'), 'atLeast') spacing.set(w('lineRule'), 'atLeast')
if spacing.attrib: if spacing.attrib:
@ -589,31 +589,31 @@ class BlockStyle(DOCXStyle):
if css_unit in ('em', 'ex'): if css_unit in ('em', 'ex'):
chars = max(0, int(css_val * (50 if css_unit == 'ex' else 100))) 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): if (self is normal_style and chars > 0) or getter(self) != getter(normal_style):
ind.set(w(edge + 'Chars'), unicode_type(chars)) ind.set(w(edge + 'Chars'), str(chars))
else: else:
getter = attrgetter('margin_' + edge) getter = attrgetter('margin_' + edge)
val = getter(self) val = getter(self)
if (self is normal_style and val > 0) or val != getter(normal_style): if (self is normal_style and val > 0) or val != getter(normal_style):
ind.set(w(edge), unicode_type(val)) ind.set(w(edge), str(val))
ind.set(w(edge + 'Chars'), '0') # This is needed to override any declaration in the parent style 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) css_val, css_unit = parse_css_length(self.css_text_indent)
if css_unit in ('em', 'ex'): if css_unit in ('em', 'ex'):
chars = int(css_val * (50 if css_unit == 'ex' else 100)) chars = int(css_val * (50 if css_unit == 'ex' else 100))
if css_val >= 0: if css_val >= 0:
if (self is normal_style and chars > 0) or self.css_text_indent != normal_style.css_text_indent: if (self is normal_style and chars > 0) or self.css_text_indent != normal_style.css_text_indent:
ind.set(w('firstLineChars'), unicode_type(chars)) ind.set(w('firstLineChars'), str(chars))
else: else:
if (self is normal_style and chars < 0) or self.css_text_indent != normal_style.css_text_indent: if (self is normal_style and chars < 0) or self.css_text_indent != normal_style.css_text_indent:
ind.set(w('hangingChars'), unicode_type(abs(chars))) ind.set(w('hangingChars'), str(abs(chars)))
else: else:
val = self.text_indent val = self.text_indent
if val >= 0: if val >= 0:
if (self is normal_style and val > 0) or self.text_indent != normal_style.text_indent: if (self is normal_style and val > 0) or self.text_indent != normal_style.text_indent:
ind.set(w('firstLine'), unicode_type(val)) ind.set(w('firstLine'), str(val))
ind.set(w('firstLineChars'), '0') # This is needed to override any declaration in the parent style ind.set(w('firstLineChars'), '0') # This is needed to override any declaration in the parent style
else: else:
if (self is normal_style and val < 0) or self.text_indent != normal_style.text_indent: if (self is normal_style and val < 0) or self.text_indent != normal_style.text_indent:
ind.set(w('hanging'), unicode_type(abs(val))) ind.set(w('hanging'), str(abs(val)))
ind.set(w('hangingChars'), '0') ind.set(w('hangingChars'), '0')
if ind.attrib: if ind.attrib:
pPr.append(ind) pPr.append(ind)
@ -687,7 +687,7 @@ class StylesManager:
pure_block_styles.add(bs) pure_block_styles.add(bs)
self.pure_block_styles = sorted(pure_block_styles, key=block_counts.__getitem__) self.pure_block_styles = sorted(pure_block_styles, key=block_counts.__getitem__)
bnum = len(unicode_type(max(1, len(pure_block_styles) - 1))) bnum = len(str(max(1, len(pure_block_styles) - 1)))
for i, bs in enumerate(self.pure_block_styles): for i, bs in enumerate(self.pure_block_styles):
bs.id = bs.name = '%0{}d Block'.format(bnum) % i bs.id = bs.name = '%0{}d Block'.format(bnum) % i
bs.seq = i bs.seq = i
@ -707,7 +707,7 @@ class StylesManager:
heading_style = styles[-1] heading_style = styles[-1]
heading_style.outline_level = i heading_style.outline_level = i
snum = len(unicode_type(max(1, len(counts) - 1))) snum = len(str(max(1, len(counts) - 1)))
heading_styles = [] heading_styles = []
for i, (style, count) in enumerate(counts.most_common()): for i, (style, count) in enumerate(counts.most_common()):
if i == 0: if i == 0:
@ -735,7 +735,7 @@ class StylesManager:
if run.descendant_style is None: if run.descendant_style is None:
run.descendant_style = descendant_style_map[ds] = ds run.descendant_style = descendant_style_map[ds] = ds
ds_counts[run.descendant_style] += run.style_weight ds_counts[run.descendant_style] += run.style_weight
rnum = len(unicode_type(max(1, len(ds_counts) - 1))) rnum = len(str(max(1, len(ds_counts) - 1)))
for i, (text_style, count) in enumerate(ds_counts.most_common()): for i, (text_style, count) in enumerate(ds_counts.most_common()):
text_style.id = 'Text%d' % i text_style.id = 'Text%d' % i
text_style.name = '%0{}d Text'.format(rnum) % i text_style.name = '%0{}d Text'.format(rnum) % i

View File

@ -9,7 +9,7 @@ from collections import namedtuple
from calibre.ebooks.docx.writer.utils import convert_color from calibre.ebooks.docx.writer.utils import convert_color
from calibre.ebooks.docx.writer.styles import read_css_block_borders as rcbb, border_edges from calibre.ebooks.docx.writer.styles import read_css_block_borders as rcbb, border_edges
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
class Dummy: class Dummy:
@ -115,7 +115,7 @@ class Cell:
def serialize(self, parent, makeelement): def serialize(self, parent, makeelement):
tc = makeelement(parent, 'w:tc') tc = makeelement(parent, 'w:tc')
tcPr = makeelement(tc, 'w:tcPr') tcPr = makeelement(tc, 'w:tcPr')
makeelement(tcPr, 'w:tcW', w_type=self.width[0], w_w=unicode_type(self.width[1])) makeelement(tcPr, 'w:tcW', w_type=self.width[0], w_w=str(self.width[1]))
# For some reason, Word 2007 refuses to honor <w:shd> at the table or row # For some reason, Word 2007 refuses to honor <w:shd> at the table or row
# level, despite what the specs say, so we inherit and apply at the # level, despite what the specs say, so we inherit and apply at the
# cell level # cell level
@ -126,7 +126,7 @@ class Cell:
b = makeelement(tcPr, 'w:tcBorders', append=False) b = makeelement(tcPr, 'w:tcBorders', append=False)
for edge, border in iteritems(self.borders): for edge, border in iteritems(self.borders):
if border is not None and border.width > 0 and border.style != 'none': if border is not None and border.width > 0 and border.style != 'none':
makeelement(b, 'w:' + edge, w_val=border.style, w_sz=unicode_type(border.width), w_color=border.color) makeelement(b, 'w:' + edge, w_val=border.style, w_sz=str(border.width), w_color=border.color)
if len(b) > 0: if len(b) > 0:
tcPr.append(b) tcPr.append(b)
@ -136,7 +136,7 @@ class Cell:
if edge in {'top', 'bottom'} or (edge == 'left' and self is self.row.first_cell) or (edge == 'right' and self is self.row.last_cell): if edge in {'top', 'bottom'} or (edge == 'left' and self is self.row.first_cell) or (edge == 'right' and self is self.row.last_cell):
padding += getattr(self.row, 'padding_' + edge) padding += getattr(self.row, 'padding_' + edge)
if padding > 0: if padding > 0:
makeelement(m, 'w:' + edge, w_type='dxa', w_w=unicode_type(int(padding * 20))) makeelement(m, 'w:' + edge, w_type='dxa', w_w=str(int(padding * 20)))
if len(m) > 0: if len(m) > 0:
tcPr.append(m) tcPr.append(m)
@ -356,14 +356,14 @@ class Table:
return return
tbl = makeelement(parent, 'w:tbl') tbl = makeelement(parent, 'w:tbl')
tblPr = makeelement(tbl, 'w:tblPr') tblPr = makeelement(tbl, 'w:tblPr')
makeelement(tblPr, 'w:tblW', w_type=self.width[0], w_w=unicode_type(self.width[1])) makeelement(tblPr, 'w:tblW', w_type=self.width[0], w_w=str(self.width[1]))
if self.float in {'left', 'right'}: if self.float in {'left', 'right'}:
kw = {'w_vertAnchor':'text', 'w_horzAnchor':'text', 'w_tblpXSpec':self.float} kw = {'w_vertAnchor':'text', 'w_horzAnchor':'text', 'w_tblpXSpec':self.float}
for edge in border_edges: for edge in border_edges:
val = getattr(self, 'margin_' + edge) or 0 val = getattr(self, 'margin_' + edge) or 0
if {self.float, edge} == {'left', 'right'}: if {self.float, edge} == {'left', 'right'}:
val = max(val, 2) val = max(val, 2)
kw['w_' + edge + 'FromText'] = unicode_type(max(0, int(val *20))) kw['w_' + edge + 'FromText'] = str(max(0, int(val *20)))
makeelement(tblPr, 'w:tblpPr', **kw) makeelement(tblPr, 'w:tblpPr', **kw)
if self.jc is not None: if self.jc is not None:
makeelement(tblPr, 'w:jc', w_val=self.jc) makeelement(tblPr, 'w:jc', w_val=self.jc)

View File

@ -8,7 +8,7 @@ __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
import unittest, numbers import unittest, numbers
from calibre.ebooks.epub.cfi.parse import parser, cfi_sort_key, decode_cfi from calibre.ebooks.epub.cfi.parse import parser, cfi_sort_key, decode_cfi
from polyglot.builtins import iteritems, unicode_type from polyglot.builtins import iteritems
class Tests(unittest.TestCase): class Tests(unittest.TestCase):
@ -59,7 +59,7 @@ class Tests(unittest.TestCase):
if after is not None: if after is not None:
ta['after'] = after ta['after'] = after
if params: if params:
ta['params'] = {unicode_type(k):(v,) if isinstance(v, unicode_type) else v for k, v in iteritems(params)} ta['params'] = {str(k):(v,) if isinstance(v, str) else v for k, v in iteritems(params)}
if ta: if ta:
step['text_assertion'] = ta step['text_assertion'] = ta
return ans return ans

View File

@ -12,7 +12,6 @@ from itertools import count
from calibre.ebooks.oeb.base import XHTML_NS from calibre.ebooks.oeb.base import XHTML_NS
from calibre.ebooks.oeb.base import OEBBook from calibre.ebooks.oeb.base import OEBBook
from lxml.etree import XPath from lxml.etree import XPath
from polyglot.builtins import unicode_type
NSMAP = {'h': XHTML_NS, 'html': XHTML_NS, 'xhtml': XHTML_NS} NSMAP = {'h': XHTML_NS, 'html': XHTML_NS, 'xhtml': XHTML_NS}
PAGE_RE = re.compile(r'page', re.IGNORECASE) PAGE_RE = re.compile(r'page', re.IGNORECASE)
@ -32,7 +31,7 @@ def filter_name(name):
def build_name_for(expr): def build_name_for(expr):
if not expr: if not expr:
counter = count(1) counter = count(1)
return lambda elem: unicode_type(next(counter)) return lambda elem: str(next(counter))
selector = XPath(expr, namespaces=NSMAP) selector = XPath(expr, namespaces=NSMAP)
def name_for(elem): def name_for(elem):

View File

@ -12,7 +12,6 @@ import time
from calibre.constants import __appname__, __version__ from calibre.constants import __appname__, __version__
from calibre import strftime, prepare_string_for_xml as xml from calibre import strftime, prepare_string_for_xml as xml
from calibre.utils.date import parse_date from calibre.utils.date import parse_date
from polyglot.builtins import unicode_type
SONY_METADATA = '''\ SONY_METADATA = '''\
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
@ -83,21 +82,21 @@ SONY_ATOM_ENTRY = '''\
def sony_metadata(oeb): def sony_metadata(oeb):
m = oeb.metadata m = oeb.metadata
title = short_title = unicode_type(m.title[0]) title = short_title = str(m.title[0])
publisher = __appname__ + ' ' + __version__ publisher = __appname__ + ' ' + __version__
try: try:
pt = unicode_type(oeb.metadata.publication_type[0]) pt = str(oeb.metadata.publication_type[0])
short_title = ':'.join(pt.split(':')[2:]) short_title = ':'.join(pt.split(':')[2:])
except: except:
pass pass
try: try:
date = parse_date(unicode_type(m.date[0]), date = parse_date(str(m.date[0]),
as_utc=False).strftime('%Y-%m-%d') as_utc=False).strftime('%Y-%m-%d')
except: except:
date = strftime('%Y-%m-%d') date = strftime('%Y-%m-%d')
try: try:
language = unicode_type(m.language[0]).replace('_', '-') language = str(m.language[0]).replace('_', '-')
except: except:
language = 'en' language = 'en'
short_title = xml(short_title, True) short_title = xml(short_title, True)
@ -115,9 +114,9 @@ def sony_metadata(oeb):
return True return True
try: try:
base_id = unicode_type(list(filter(cal_id, m.identifier))[0]) base_id = str(list(filter(cal_id, m.identifier))[0])
except: except:
base_id = unicode_type(uuid4()) base_id = str(uuid4())
toc = oeb.toc toc = oeb.toc
@ -130,7 +129,7 @@ def sony_metadata(oeb):
for x in toc: for x in toc:
section.nodes.append(x) section.nodes.append(x)
toc = TOC(klass='periodical', href=oeb.spine[2].href, toc = TOC(klass='periodical', href=oeb.spine[2].href,
title=unicode_type(oeb.metadata.title[0])) title=str(oeb.metadata.title[0]))
toc.nodes.append(section) toc.nodes.append(section)
entries = [] entries = []
@ -145,7 +144,7 @@ def sony_metadata(oeb):
d = 1 d = 1
bsectitle = sectitle bsectitle = sectitle
while sectitle in seen_titles: while sectitle in seen_titles:
sectitle = bsectitle + ' ' + unicode_type(d) sectitle = bsectitle + ' ' + str(d)
d += 1 d += 1
seen_titles.add(sectitle) seen_titles.add(sectitle)
sectitle = xml(sectitle, True) sectitle = xml(sectitle, True)
@ -164,7 +163,7 @@ def sony_metadata(oeb):
btitle = atitle btitle = atitle
d = 1 d = 1
while atitle in seen_titles: while atitle in seen_titles:
atitle = btitle + ' ' + unicode_type(d) atitle = btitle + ' ' + str(d)
d += 1 d += 1
auth = article.author if article.author else '' auth = article.author if article.author else ''
@ -181,7 +180,7 @@ def sony_metadata(oeb):
short_title=short_title, short_title=short_title,
section_title=sectitle, section_title=sectitle,
href=article.href, href=article.href,
word_count=unicode_type(1), word_count=str(1),
id=xml(base_id)+'/'+secid+'/'+aid id=xml(base_id)+'/'+secid+'/'+aid
)) ))

View File

@ -20,7 +20,7 @@ from calibre.utils.localization import lang_as_iso639_1
from calibre.utils.xml_parse import safe_xml_fromstring from calibre.utils.xml_parse import safe_xml_fromstring
from calibre.utils.img import save_cover_data_to from calibre.utils.img import save_cover_data_to
from calibre.ebooks.oeb.base import urlnormalize from calibre.ebooks.oeb.base import urlnormalize
from polyglot.builtins import unicode_type, string_or_bytes from polyglot.builtins import string_or_bytes
from polyglot.binary import as_base64_unicode from polyglot.binary import as_base64_unicode
from polyglot.urllib import urlparse from polyglot.urllib import urlparse
@ -154,7 +154,7 @@ class FB2MLizer:
metadata['author'] = '<author><first-name></first-name><last-name></last-name></author>' metadata['author'] = '<author><first-name></first-name><last-name></last-name></author>'
metadata['keywords'] = '' metadata['keywords'] = ''
tags = list(map(unicode_type, self.oeb_book.metadata.subject)) tags = list(map(str, self.oeb_book.metadata.subject))
if tags: if tags:
tags = ', '.join(prepare_string_for_xml(x) for x in tags) tags = ', '.join(prepare_string_for_xml(x) for x in tags)
metadata['keywords'] = '<keywords>%s</keywords>'%tags metadata['keywords'] = '<keywords>%s</keywords>'%tags
@ -169,12 +169,12 @@ class FB2MLizer:
year = publisher = isbn = '' year = publisher = isbn = ''
identifiers = self.oeb_book.metadata['identifier'] identifiers = self.oeb_book.metadata['identifier']
for x in identifiers: for x in identifiers:
if x.get(OPF('scheme'), None).lower() == 'uuid' or unicode_type(x).startswith('urn:uuid:'): if x.get(OPF('scheme'), None).lower() == 'uuid' or str(x).startswith('urn:uuid:'):
metadata['id'] = unicode_type(x).split(':')[-1] metadata['id'] = str(x).split(':')[-1]
break break
if metadata['id'] is None: if metadata['id'] is None:
self.log.warn('No UUID identifier found') self.log.warn('No UUID identifier found')
metadata['id'] = unicode_type(uuid.uuid4()) metadata['id'] = str(uuid.uuid4())
try: try:
date = self.oeb_book.metadata['date'][0] date = self.oeb_book.metadata['date'][0]
@ -236,7 +236,7 @@ class FB2MLizer:
</description>''') % metadata </description>''') % metadata
# Remove empty lines. # Remove empty lines.
return '\n'.join(filter(unicode_type.strip, header.splitlines())) return '\n'.join(filter(str.strip, header.splitlines()))
def fb2_footer(self): def fb2_footer(self):
return '</FictionBook>' return '</FictionBook>'
@ -247,8 +247,8 @@ class FB2MLizer:
cover_href = None cover_href = None
# Get the raster cover if it's available. # Get the raster cover if it's available.
if self.oeb_book.metadata.cover and unicode_type(self.oeb_book.metadata.cover[0]) in self.oeb_book.manifest.ids: if self.oeb_book.metadata.cover and str(self.oeb_book.metadata.cover[0]) in self.oeb_book.manifest.ids:
id = unicode_type(self.oeb_book.metadata.cover[0]) id = str(self.oeb_book.metadata.cover[0])
cover_item = self.oeb_book.manifest.ids[id] cover_item = self.oeb_book.manifest.ids[id]
if cover_item.media_type in OEB_RASTER_IMAGES: if cover_item.media_type in OEB_RASTER_IMAGES:
cover_href = cover_item.href cover_href = cover_item.href

View File

@ -17,7 +17,6 @@ from calibre.ebooks.oeb.base import urlunquote
from calibre.ebooks.chardet import detect_xml_encoding from calibre.ebooks.chardet import detect_xml_encoding
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre import unicode_path, as_unicode, replace_entities from calibre import unicode_path, as_unicode, replace_entities
from polyglot.builtins import unicode_type
from polyglot.urllib import urlparse, urlunparse from polyglot.urllib import urlparse, urlunparse
@ -46,7 +45,7 @@ class Link:
:param base: The base folder that relative URLs are with respect to. :param base: The base folder that relative URLs are with respect to.
Must be a unicode string. Must be a unicode string.
''' '''
assert isinstance(url, unicode_type) and isinstance(base, unicode_type) assert isinstance(url, str) and isinstance(base, str)
self.url = url self.url = url
self.parsed_url = urlparse(self.url) self.parsed_url = urlparse(self.url)
self.is_local = self.parsed_url.scheme in ('', 'file') self.is_local = self.parsed_url.scheme in ('', 'file')
@ -155,7 +154,7 @@ class HTMLFile:
return 'HTMLFile:%d:%s:%r'%(self.level, 'b' if self.is_binary else 'a', self.path) return 'HTMLFile:%d:%s:%r'%(self.level, 'b' if self.is_binary else 'a', self.path)
def __repr__(self): def __repr__(self):
return unicode_type(self) return str(self)
def find_links(self, src): def find_links(self, src):
for match in self.LINK_PAT.finditer(src): for match in self.LINK_PAT.finditer(src):

View File

@ -10,7 +10,6 @@ import textwrap, os, glob
from calibre.customize import FileTypePlugin from calibre.customize import FileTypePlugin
from calibre.constants import numeric_version from calibre.constants import numeric_version
from polyglot.builtins import unicode_type
class HTML2ZIP(FileTypePlugin): class HTML2ZIP(FileTypePlugin):
@ -114,7 +113,7 @@ every time you add an HTML file to the library.\
config_dialog.exec_() config_dialog.exec_()
if config_dialog.result() == QDialog.DialogCode.Accepted: if config_dialog.result() == QDialog.DialogCode.Accepted:
sc = unicode_type(sc.text()).strip() sc = str(sc.text()).strip()
if bf.isChecked(): if bf.isChecked():
sc += '|bf' sc += '|bf'
customize_plugin(self, sc) customize_plugin(self, sc)

View File

@ -20,7 +20,7 @@ from calibre.ebooks.oeb.base import (
XHTML, XHTML_NS, SVG_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize) XHTML, XHTML_NS, SVG_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize)
from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.stylizer import Stylizer
from calibre.utils.logging import default_log from calibre.utils.logging import default_log
from polyglot.builtins import unicode_type, string_or_bytes, as_unicode from polyglot.builtins import string_or_bytes, as_unicode
from polyglot.urllib import urldefrag from polyglot.urllib import urldefrag
SELF_CLOSING_TAGS = {'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta'} SELF_CLOSING_TAGS = {'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta'}
@ -46,7 +46,7 @@ class OEB2HTML:
self.log.info('Converting OEB book to HTML...') self.log.info('Converting OEB book to HTML...')
self.opts = opts self.opts = opts
try: try:
self.book_title = unicode_type(oeb_book.metadata.title[0]) self.book_title = str(oeb_book.metadata.title[0])
except Exception: except Exception:
self.book_title = _('Unknown') self.book_title = _('Unknown')
self.links = {} self.links = {}

View File

@ -17,7 +17,7 @@ import calibre.ebooks.lit.mssha1 as mssha1
from calibre.ebooks.oeb.base import urlnormalize, xpath from calibre.ebooks.oeb.base import urlnormalize, xpath
from calibre.ebooks.oeb.reader import OEBReader from calibre.ebooks.oeb.reader import OEBReader
from calibre.ebooks import DRMError from calibre.ebooks import DRMError
from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes, itervalues from polyglot.builtins import codepoint_to_chr, string_or_bytes, itervalues
from polyglot.urllib import unquote as urlunquote, urldefrag from polyglot.urllib import unquote as urlunquote, urldefrag
from calibre_extensions import lzx, msdes from calibre_extensions import lzx, msdes
@ -121,7 +121,7 @@ def consume_sized_utf8_string(bytes, zpad=False):
def encode(string): def encode(string):
return unicode_type(string).encode('ascii', 'xmlcharrefreplace') return str(string).encode('ascii', 'xmlcharrefreplace')
class UnBinary: class UnBinary:
@ -325,7 +325,7 @@ class UnBinary:
c = '&quot;' c = '&quot;'
elif c == '<': elif c == '<':
c = '&lt;' c = '&lt;'
if isinstance(c, unicode_type): if isinstance(c, str):
c = c.encode('ascii', 'xmlcharrefreplace') c = c.encode('ascii', 'xmlcharrefreplace')
buf.write(c) buf.write(c)
count -= 1 count -= 1

View File

@ -29,7 +29,7 @@ from calibre.ebooks.lit.lzx import Compressor
import calibre import calibre
from calibre_extensions import msdes from calibre_extensions import msdes
import calibre.ebooks.lit.mssha1 as mssha1 import calibre.ebooks.lit.mssha1 as mssha1
from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes, native_string_type from polyglot.builtins import codepoint_to_chr, string_or_bytes, native_string_type
from polyglot.urllib import urldefrag, unquote from polyglot.urllib import urldefrag, unquote
__all__ = ['LitWriter'] __all__ = ['LitWriter']
@ -283,7 +283,7 @@ class ReBinary:
data.write(codepoint_to_chr(len(self.anchors)).encode('utf-8')) data.write(codepoint_to_chr(len(self.anchors)).encode('utf-8'))
for anchor, offset in self.anchors: for anchor, offset in self.anchors:
data.write(codepoint_to_chr(len(anchor)).encode('utf-8')) data.write(codepoint_to_chr(len(anchor)).encode('utf-8'))
if isinstance(anchor, unicode_type): if isinstance(anchor, str):
anchor = anchor.encode('utf-8') anchor = anchor.encode('utf-8')
data.write(anchor) data.write(anchor)
data.write(pack('<I', offset)) data.write(pack('<I', offset))
@ -314,7 +314,7 @@ class LitWriter:
oeb.metadata.add('calibre-version', calibre.__version__) oeb.metadata.add('calibre-version', calibre.__version__)
cover = None cover = None
if oeb.metadata.cover: if oeb.metadata.cover:
id = unicode_type(oeb.metadata.cover[0]) id = str(oeb.metadata.cover[0])
cover = oeb.manifest.ids[id] cover = oeb.manifest.ids[id]
for type, title in ALL_MS_COVER_TYPES: for type, title in ALL_MS_COVER_TYPES:
if type not in oeb.guide: if type not in oeb.guide:
@ -486,7 +486,7 @@ class LitWriter:
data = rebin.content data = rebin.content
name = name + '/content' name = name + '/content'
secnum = 1 secnum = 1
elif isinstance(data, unicode_type): elif isinstance(data, str):
data = data.encode('utf-8') data = data.encode('utf-8')
elif hasattr(data, 'cssText'): elif hasattr(data, 'cssText'):
data = item.bytes_representation data = item.bytes_representation
@ -521,9 +521,9 @@ class LitWriter:
item.offset = offset \ item.offset = offset \
if state in ('linear', 'nonlinear') else 0 if state in ('linear', 'nonlinear') else 0
data.write(pack('<I', item.offset)) data.write(pack('<I', item.offset))
entry = [codepoint_to_chr(len(id)), unicode_type(id), entry = [codepoint_to_chr(len(id)), str(id),
codepoint_to_chr(len(href)), unicode_type(href), codepoint_to_chr(len(href)), str(href),
codepoint_to_chr(len(media_type)), unicode_type(media_type)] codepoint_to_chr(len(media_type)), str(media_type)]
for value in entry: for value in entry:
data.write(value.encode('utf-8')) data.write(value.encode('utf-8'))
data.write(b'\0') data.write(b'\0')

View File

@ -32,7 +32,7 @@ from calibre.ebooks.lrf.pylrs.pylrs import (
RuledLine, Span, Sub, Sup, TextBlock RuledLine, Span, Sub, Sup, TextBlock
) )
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from polyglot.builtins import itervalues, string_or_bytes, unicode_type from polyglot.builtins import itervalues, string_or_bytes
from polyglot.urllib import unquote, urlparse from polyglot.urllib import unquote, urlparse
""" """
@ -278,7 +278,7 @@ class HTMLConverter:
update_css(npcss, self.override_pcss) update_css(npcss, self.override_pcss)
paths = [os.path.abspath(path) for path in paths] paths = [os.path.abspath(path) for path in paths]
paths = [path.decode(sys.getfilesystemencoding()) if not isinstance(path, unicode_type) else path for path in paths] paths = [path.decode(sys.getfilesystemencoding()) if not isinstance(path, str) else path for path in paths]
while len(paths) > 0 and self.link_level <= self.link_levels: while len(paths) > 0 and self.link_level <= self.link_levels:
for path in paths: for path in paths:
@ -358,7 +358,7 @@ class HTMLConverter:
os.makedirs(tdir) os.makedirs(tdir)
try: try:
with open(os.path.join(tdir, 'html2lrf-verbose.html'), 'wb') as f: with open(os.path.join(tdir, 'html2lrf-verbose.html'), 'wb') as f:
f.write(unicode_type(soup).encode('utf-8')) f.write(str(soup).encode('utf-8'))
self.log.info(_('Written preprocessed HTML to ')+f.name) self.log.info(_('Written preprocessed HTML to ')+f.name)
except: except:
pass pass
@ -391,7 +391,7 @@ class HTMLConverter:
self.log.info(_('\tConverting to BBeB...')) self.log.info(_('\tConverting to BBeB...'))
self.current_style = {} self.current_style = {}
self.page_break_found = False self.page_break_found = False
if not isinstance(path, unicode_type): if not isinstance(path, str):
path = path.decode(sys.getfilesystemencoding()) path = path.decode(sys.getfilesystemencoding())
self.target_prefix = path self.target_prefix = path
self.previous_text = '\n' self.previous_text = '\n'
@ -401,7 +401,7 @@ class HTMLConverter:
def parse_css(self, style): def parse_css(self, style):
""" """
Parse the contents of a <style> tag or .css file. Parse the contents of a <style> tag or .css file.
@param style: C{unicode_type(style)} should be the CSS to parse. @param style: C{str(style)} should be the CSS to parse.
@return: A dictionary with one entry per selector where the key is the @return: A dictionary with one entry per selector where the key is the
selector name and the value is a dictionary of properties selector name and the value is a dictionary of properties
""" """
@ -589,7 +589,7 @@ class HTMLConverter:
if isinstance(c, HTMLConverter.IGNORED_TAGS): if isinstance(c, HTMLConverter.IGNORED_TAGS):
continue continue
if isinstance(c, NavigableString): if isinstance(c, NavigableString):
text += unicode_type(c) text += str(c)
elif isinstance(c, Tag): elif isinstance(c, Tag):
if c.name.lower() == 'img' and c.has_attr('alt'): if c.name.lower() == 'img' and c.has_attr('alt'):
alt_text += c['alt'] alt_text += c['alt']
@ -644,7 +644,7 @@ class HTMLConverter:
para, text, path, fragment = link['para'], link['text'], link['path'], link['fragment'] para, text, path, fragment = link['para'], link['text'], link['path'], link['fragment']
ascii_text = text ascii_text = text
if not isinstance(path, unicode_type): if not isinstance(path, str):
path = path.decode(sys.getfilesystemencoding()) path = path.decode(sys.getfilesystemencoding())
if path in self.processed_files: if path in self.processed_files:
if path+fragment in self.targets.keys(): if path+fragment in self.targets.keys():
@ -1087,7 +1087,7 @@ class HTMLConverter:
s1, s2 = get('margin'), get('padding') s1, s2 = get('margin'), get('padding')
bl = unicode_type(self.current_block.blockStyle.attrs['blockwidth'])+'px' bl = str(self.current_block.blockStyle.attrs['blockwidth'])+'px'
def set(default, one, two): def set(default, one, two):
fval = None fval = None
@ -1216,7 +1216,7 @@ class HTMLConverter:
ans = 120 ans = 120
if ans is not None: if ans is not None:
ans += int(self.font_delta * 20) ans += int(self.font_delta * 20)
ans = unicode_type(ans) ans = str(ans)
return ans return ans
family, weight, style, variant = 'serif', 'normal', 'normal', None family, weight, style, variant = 'serif', 'normal', 'normal', None
@ -1322,10 +1322,10 @@ class HTMLConverter:
def text_properties(self, tag_css): def text_properties(self, tag_css):
indent = self.book.defaultTextStyle.attrs['parindent'] indent = self.book.defaultTextStyle.attrs['parindent']
if 'text-indent' in tag_css: if 'text-indent' in tag_css:
bl = unicode_type(self.current_block.blockStyle.attrs['blockwidth'])+'px' bl = str(self.current_block.blockStyle.attrs['blockwidth'])+'px'
if 'em' in tag_css['text-indent']: if 'em' in tag_css['text-indent']:
bl = '10pt' bl = '10pt'
indent = self.unit_convert(unicode_type(tag_css['text-indent']), pts=True, base_length=bl) indent = self.unit_convert(str(tag_css['text-indent']), pts=True, base_length=bl)
if not indent: if not indent:
indent = 0 indent = 0
if indent > 0 and indent < 10 * self.minimum_indent: if indent > 0 and indent < 10 * self.minimum_indent:
@ -1519,11 +1519,11 @@ class HTMLConverter:
elif not urlparse(tag['src'])[0]: elif not urlparse(tag['src'])[0]:
self.log.warn('Could not find image: '+tag['src']) self.log.warn('Could not find image: '+tag['src'])
else: else:
self.log.debug("Failed to process: %s"%unicode_type(tag)) self.log.debug("Failed to process: %s"%str(tag))
elif tagname in ['style', 'link']: elif tagname in ['style', 'link']:
ncss, npcss = {}, {} ncss, npcss = {}, {}
if tagname == 'style': if tagname == 'style':
text = ''.join([unicode_type(i) for i in tag.findAll(text=True)]) text = ''.join([str(i) for i in tag.findAll(text=True)])
css, pcss = self.parse_css(text) css, pcss = self.parse_css(text)
ncss.update(css) ncss.update(css)
npcss.update(pcss) npcss.update(pcss)
@ -1555,7 +1555,7 @@ class HTMLConverter:
if tag.contents: if tag.contents:
c = tag.contents[0] c = tag.contents[0]
if isinstance(c, NavigableString): if isinstance(c, NavigableString):
c = unicode_type(c).replace('\r\n', '\n').replace('\r', '\n') c = str(c).replace('\r\n', '\n').replace('\r', '\n')
if c.startswith('\n'): if c.startswith('\n'):
c = c[1:] c = c[1:]
tag.contents[0] = NavigableString(c) tag.contents[0] = NavigableString(c)
@ -1613,7 +1613,7 @@ class HTMLConverter:
in_ol = parent.name.lower() == 'ol' in_ol = parent.name.lower() == 'ol'
break break
parent = parent.parent parent = parent.parent
prepend = unicode_type(self.list_counter)+'. ' if in_ol else '\u2022' + ' ' prepend = str(self.list_counter)+'. ' if in_ol else '\u2022' + ' '
self.current_para.append(Span(prepend)) self.current_para.append(Span(prepend))
self.process_children(tag, tag_css, tag_pseudo_css) self.process_children(tag, tag_css, tag_pseudo_css)
if in_ol: if in_ol:
@ -1656,7 +1656,7 @@ class HTMLConverter:
if (self.anchor_ids and tag.has_attr('id')) or (self.book_designer and tag.get('class') in ('title', ['title'])): if (self.anchor_ids and tag.has_attr('id')) or (self.book_designer and tag.get('class') in ('title', ['title'])):
if not tag.has_attr('id'): if not tag.has_attr('id'):
tag['id'] = __appname__+'_id_'+unicode_type(self.id_counter) tag['id'] = __appname__+'_id_'+str(self.id_counter)
self.id_counter += 1 self.id_counter += 1
tkey = self.target_prefix+tag['id'] tkey = self.target_prefix+tag['id']
@ -1729,7 +1729,7 @@ class HTMLConverter:
except Exception as err: except Exception as err:
self.log.warning(_('An error occurred while processing a table: %s. Ignoring table markup.')%repr(err)) self.log.warning(_('An error occurred while processing a table: %s. Ignoring table markup.')%repr(err))
self.log.exception('') self.log.exception('')
self.log.debug(_('Bad table:\n%s')%unicode_type(tag)[:300]) self.log.debug(_('Bad table:\n%s')%str(tag)[:300])
self.in_table = False self.in_table = False
self.process_children(tag, tag_css, tag_pseudo_css) self.process_children(tag, tag_css, tag_pseudo_css)
finally: finally:
@ -1825,9 +1825,9 @@ def process_file(path, options, logger):
for prop in ('author', 'author_sort', 'title', 'title_sort', 'publisher', 'freetext'): for prop in ('author', 'author_sort', 'title', 'title_sort', 'publisher', 'freetext'):
val = getattr(options, prop, None) val = getattr(options, prop, None)
if val and not isinstance(val, unicode_type): if val and not isinstance(val, str):
soup = BeautifulSoup(val) soup = BeautifulSoup(val)
setattr(options, prop, unicode_type(soup)) setattr(options, prop, str(soup))
title = (options.title, options.title_sort) title = (options.title, options.title_sort)
author = (options.author, options.author_sort) author = (options.author, options.author_sort)
@ -1871,7 +1871,7 @@ def process_file(path, options, logger):
options.force_page_break = fpb options.force_page_break = fpb
options.link_exclude = le options.link_exclude = le
options.page_break = pb options.page_break = pb
if not isinstance(options.chapter_regex, unicode_type): if not isinstance(options.chapter_regex, str):
options.chapter_regex = options.chapter_regex.decode(preferred_encoding) options.chapter_regex = options.chapter_regex.decode(preferred_encoding)
options.chapter_regex = re.compile(options.chapter_regex, re.IGNORECASE) options.chapter_regex = re.compile(options.chapter_regex, re.IGNORECASE)
fpba = options.force_page_break_attr.split(',') fpba = options.force_page_break_attr.split(',')

View File

@ -12,7 +12,7 @@ from copy import deepcopy, copy
from lxml import etree from lxml import etree
from calibre import guess_type from calibre import guess_type
from polyglot.builtins import as_bytes, unicode_type from polyglot.builtins import as_bytes
class Canvas(etree.XSLTExtension): class Canvas(etree.XSLTExtension):
@ -70,9 +70,9 @@ class Canvas(etree.XSLTExtension):
height = self.styles.to_num(block.get("ysize", None)) height = self.styles.to_num(block.get("ysize", None))
img = div.makeelement('img') img = div.makeelement('img')
if width is not None: if width is not None:
img.set('width', unicode_type(int(width))) img.set('width', str(int(width)))
if height is not None: if height is not None:
img.set('height', unicode_type(int(height))) img.set('height', str(int(height)))
ref = block.get('refstream', None) ref = block.get('refstream', None)
if ref is not None: if ref is not None:
imstr = self.doc.xpath('//ImageStream[@objid="%s"]'%ref) imstr = self.doc.xpath('//ImageStream[@objid="%s"]'%ref)
@ -267,9 +267,9 @@ class TextBlock(etree.XSLTExtension):
ysize = self.styles.to_num(child.get('ysize', None), 166/720) ysize = self.styles.to_num(child.get('ysize', None), 166/720)
img = self.root.makeelement('img') img = self.root.makeelement('img')
if xsize is not None: if xsize is not None:
img.set('width', unicode_type(int(xsize))) img.set('width', str(int(xsize)))
if ysize is not None: if ysize is not None:
img.set('height', unicode_type(int(ysize))) img.set('height', str(int(ysize)))
ro = child.get('refobj', None) ro = child.get('refobj', None)
if ro in self.plot_map: if ro in self.plot_map:
img.set('src', self.plot_map[ro]) img.set('src', self.plot_map[ro])

View File

@ -13,7 +13,7 @@ from calibre.utils.filenames import ascii_filename
from calibre.ebooks.lrf.meta import LRFMetaFile from calibre.ebooks.lrf.meta import LRFMetaFile
from calibre.ebooks.lrf.objects import get_object, PageTree, StyleObject, \ from calibre.ebooks.lrf.objects import get_object, PageTree, StyleObject, \
Font, Text, TOCObject, BookAttr, ruby_tags Font, Text, TOCObject, BookAttr, ruby_tags
from polyglot.builtins import unicode_type, itervalues from polyglot.builtins import itervalues
class LRFDocument(LRFMetaFile): class LRFDocument(LRFMetaFile):
@ -118,7 +118,7 @@ class LRFDocument(LRFMetaFile):
pages += '<PageTree objid="%d">\n'%(page_tree.id,) pages += '<PageTree objid="%d">\n'%(page_tree.id,)
close = '</PageTree>\n' close = '</PageTree>\n'
for page in page_tree: for page in page_tree:
pages += unicode_type(page) pages += str(page)
pages += close pages += close
traversed_objects = [int(i) for i in re.findall(r'objid="(\w+)"', pages)] + [pt_id] traversed_objects = [int(i) for i in re.findall(r'objid="(\w+)"', pages)] + [pt_id]
@ -131,9 +131,9 @@ class LRFDocument(LRFMetaFile):
if isinstance(obj, (Font, Text, TOCObject)): if isinstance(obj, (Font, Text, TOCObject)):
continue continue
if isinstance(obj, StyleObject): if isinstance(obj, StyleObject):
styles += unicode_type(obj) styles += str(obj)
else: else:
objects += unicode_type(obj) objects += str(obj)
styles += '</Style>\n' styles += '</Style>\n'
objects += '</Objects>\n' objects += '</Objects>\n'
if write_files: if write_files:

View File

@ -22,7 +22,7 @@ from calibre.ebooks.lrf.pylrs.pylrs import (
TextStyle TextStyle
) )
from calibre.utils.config import OptionParser from calibre.utils.config import OptionParser
from polyglot.builtins import string_or_bytes, unicode_type from polyglot.builtins import string_or_bytes
class LrsParser: class LrsParser:
@ -55,7 +55,7 @@ class LrsParser:
for key, val in tag.attrs: for key, val in tag.attrs:
if key in exclude: if key in exclude:
continue continue
result[unicode_type(key)] = val result[str(key)] = val
return result return result
def text_tag_to_element(self, tag): def text_tag_to_element(self, tag):

View File

@ -22,7 +22,7 @@ from functools import wraps
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.utils.cleantext import clean_xml_chars from calibre.utils.cleantext import clean_xml_chars
from calibre.ebooks.metadata import MetaInformation, string_to_authors from calibre.ebooks.metadata import MetaInformation, string_to_authors
from polyglot.builtins import unicode_type, string_or_bytes from polyglot.builtins import string_or_bytes
BYTE = "<B" #: Unsigned char little endian encoded in 1 byte BYTE = "<B" #: Unsigned char little endian encoded in 1 byte
WORD = "<H" #: Unsigned short little endian encoded in 2 bytes WORD = "<H" #: Unsigned short little endian encoded in 2 bytes
@ -52,8 +52,8 @@ class field:
def __repr__(self): def __repr__(self):
typ = {DWORD: 'unsigned int', 'QWORD': 'unsigned long long', BYTE: 'unsigned char', WORD: 'unsigned short'}.get(self._fmt, '') typ = {DWORD: 'unsigned int', 'QWORD': 'unsigned long long', BYTE: 'unsigned char', WORD: 'unsigned short'}.get(self._fmt, '')
return "An " + typ + " stored in " + \ return "An " + typ + " stored in " + \
unicode_type(struct.calcsize(self._fmt)) + \ str(struct.calcsize(self._fmt)) + \
" bytes starting at byte " + unicode_type(self._start) " bytes starting at byte " + str(self._start)
class versioned_field(field): class versioned_field(field):
@ -94,22 +94,22 @@ class fixed_stringfield:
self._start = start self._start = start
def __get__(self, obj, typ=None): def __get__(self, obj, typ=None):
length = unicode_type(self._length) length = str(self._length)
return obj.unpack(start=self._start, fmt="<"+length+"s")[0] return obj.unpack(start=self._start, fmt="<"+length+"s")[0]
def __set__(self, obj, val): def __set__(self, obj, val):
if not isinstance(val, string_or_bytes): if not isinstance(val, string_or_bytes):
val = unicode_type(val) val = str(val)
if isinstance(val, unicode_type): if isinstance(val, str):
val = val.encode('utf-8') val = val.encode('utf-8')
if len(val) != self._length: if len(val) != self._length:
raise LRFException("Trying to set fixed_stringfield with a " + raise LRFException("Trying to set fixed_stringfield with a " +
"string of incorrect length") "string of incorrect length")
obj.pack(val, start=self._start, fmt="<"+unicode_type(len(val))+"s") obj.pack(val, start=self._start, fmt="<"+str(len(val))+"s")
def __repr__(self): def __repr__(self):
return "A string of length " + unicode_type(self._length) + \ return "A string of length " + str(self._length) + \
" starting at byte " + unicode_type(self._start) " starting at byte " + str(self._start)
class xml_attr_field: class xml_attr_field:
@ -196,7 +196,7 @@ class xml_field:
if not val: if not val:
val = '' val = ''
if not isinstance(val, unicode_type): if not isinstance(val, str):
val = val.decode('utf-8') val = val.decode('utf-8')
elems = document.getElementsByTagName(self.tag_name) elems = document.getElementsByTagName(self.tag_name)
@ -726,8 +726,8 @@ def main(args=sys.argv):
fields = LRFMetaFile.__dict__.items() fields = LRFMetaFile.__dict__.items()
fields.sort() fields.sort()
for f in fields: for f in fields:
if "XML" in unicode_type(f): if "XML" in str(f):
print(unicode_type(f[1]) + ":", lrf.__getattribute__(f[0]).encode('utf-8')) print(str(f[1]) + ":", lrf.__getattribute__(f[0]).encode('utf-8'))
if options.get_thumbnail: if options.get_thumbnail:
print("Thumbnail:", td) print("Thumbnail:", td)
if options.get_cover: if options.get_cover:

View File

@ -7,7 +7,6 @@ import struct, array, zlib, io, collections, re
from calibre.ebooks.lrf import LRFParseError, PRS500_PROFILE from calibre.ebooks.lrf import LRFParseError, PRS500_PROFILE
from calibre import entity_to_unicode, prepare_string_for_xml from calibre import entity_to_unicode, prepare_string_for_xml
from calibre.ebooks.lrf.tags import Tag from calibre.ebooks.lrf.tags import Tag
from polyglot.builtins import unicode_type
ruby_tags = { ruby_tags = {
0xF575: ['rubyAlignAndAdjust', 'W'], 0xF575: ['rubyAlignAndAdjust', 'W'],
@ -83,7 +82,7 @@ class LRFObject:
if h[1] != '' and h[0] != '': if h[1] != '' and h[0] != '':
setattr(self, h[0], val) setattr(self, h[0], val)
else: else:
raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, unicode_type(tag))) raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, str(tag)))
def __iter__(self): def __iter__(self):
for i in range(0): for i in range(0):
@ -121,13 +120,13 @@ class LRFContentObject(LRFObject):
def handle_tag(self, tag): def handle_tag(self, tag):
if tag.id in self.tag_map: if tag.id in self.tag_map:
action = self.tag_map[tag.id] action = self.tag_map[tag.id]
if isinstance(action, unicode_type): if isinstance(action, str):
func, args = action, () func, args = action, ()
else: else:
func, args = action[0], (action[1],) func, args = action[0], (action[1],)
getattr(self, func)(tag, *args) getattr(self, func)(tag, *args)
else: else:
raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, unicode_type(tag))) raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, str(tag)))
def __iter__(self): def __iter__(self):
for i in self._contents: for i in self._contents:
@ -270,7 +269,7 @@ class EmptyPageElement:
yield i yield i
def __str__(self): def __str__(self):
return unicode_type(self) return str(self)
class PageDiv(EmptyPageElement): class PageDiv(EmptyPageElement):
@ -425,7 +424,7 @@ class Page(LRFStream):
def __str__(self): def __str__(self):
s = '\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: for i in self:
s += unicode_type(i) s += str(i)
s += '\n</Page>\n' s += '\n</Page>\n'
return s return s
@ -470,7 +469,7 @@ class BlockAttr(StyleObject, LRFObject):
return ans return ans
if hasattr(obj, 'sidemargin'): if hasattr(obj, 'sidemargin'):
margin = unicode_type(obj.sidemargin) + 'px' margin = str(obj.sidemargin) + 'px'
ans += item('margin-left: %(m)s; margin-right: %(m)s;'%dict(m=margin)) ans += item('margin-left: %(m)s; margin-right: %(m)s;'%dict(m=margin))
if hasattr(obj, 'topskip'): if hasattr(obj, 'topskip'):
ans += item('margin-top: %dpx;'%obj.topskip) ans += item('margin-top: %dpx;'%obj.topskip)
@ -612,7 +611,7 @@ class Block(LRFStream, TextCSS):
s += '%s="%s" '%(attr, self.attrs[attr]) s += '%s="%s" '%(attr, self.attrs[attr])
if self.name != 'ImageBlock': if self.name != 'ImageBlock':
s = s.rstrip()+'>\n' s = s.rstrip()+'>\n'
s += unicode_type(self.content) s += str(self.content)
s += '</%s>\n'%(self.name,) s += '</%s>\n'%(self.name,)
return s return s
return s.rstrip() + ' />\n' return s.rstrip() + ' />\n'
@ -710,7 +709,7 @@ class Text(LRFStream):
lineposition_map = {1:'before', 2:'after'} lineposition_map = {1:'before', 2:'after'}
def add_text(self, text): def add_text(self, text):
s = unicode_type(text, "utf-16-le") s = str(text, "utf-16-le")
if s: if s:
s = s.translate(self.text_map) s = s.translate(self.text_map)
self.content.append(self.entity_pattern.sub(entity_to_unicode, s)) self.content.append(self.entity_pattern.sub(entity_to_unicode, s))
@ -850,7 +849,7 @@ class Text(LRFStream):
self.add_text(stream.read(tag.word)) self.add_text(stream.read(tag.word))
elif tag.id in self.__class__.text_tags: # A Text tag elif tag.id in self.__class__.text_tags: # A Text tag
action = self.__class__.text_tags[tag.id] action = self.__class__.text_tags[tag.id]
if isinstance(action, unicode_type): if isinstance(action, str):
getattr(self, action)(tag, stream) getattr(self, action)(tag, stream)
else: else:
getattr(self, action[0])(tag, action[1]) getattr(self, action[0])(tag, action[1])
@ -874,14 +873,14 @@ class Text(LRFStream):
s = '' s = ''
open_containers = collections.deque() open_containers = collections.deque()
for c in self.content: for c in self.content:
if isinstance(c, unicode_type): if isinstance(c, str):
s += prepare_string_for_xml(c).replace('\0', '') s += prepare_string_for_xml(c).replace('\0', '')
elif c is None: elif c is None:
if open_containers: if open_containers:
p = open_containers.pop() p = open_containers.pop()
s += '</%s>'%(p.name,) s += '</%s>'%(p.name,)
else: else:
s += unicode_type(c) s += str(c)
if not c.self_closing: if not c.self_closing:
open_containers.append(c) open_containers.append(c)
@ -897,7 +896,7 @@ class Text(LRFStream):
open_containers = collections.deque() open_containers = collections.deque()
in_p = False in_p = False
for c in self.content: for c in self.content:
if isinstance(c, unicode_type): if isinstance(c, str):
s += c s += c
elif c is None: elif c is None:
p = open_containers.pop() p = open_containers.pop()
@ -992,7 +991,7 @@ class Canvas(LRFStream):
s += '%s="%s" '%(attr, self.attrs[attr]) s += '%s="%s" '%(attr, self.attrs[attr])
s = s.rstrip() + '>\n' s = s.rstrip() + '>\n'
for po in self: for po in self:
s += unicode_type(po) + '\n' s += str(po) + '\n'
s += '</%s>\n'%(self.__class__.__name__,) s += '</%s>\n'%(self.__class__.__name__,)
return s return s
@ -1025,7 +1024,7 @@ class ImageStream(LRFStream):
def end_stream(self, *args): def end_stream(self, *args):
LRFStream.end_stream(self, *args) LRFStream.end_stream(self, *args)
self.file = unicode_type(self.id) + '.' + self.encoding.lower() self.file = str(self.id) + '.' + self.encoding.lower()
if self._document is not None: if self._document is not None:
self._document.image_map[self.id] = self self._document.image_map[self.id] = self
@ -1189,7 +1188,7 @@ class BookAttr(StyleObject, LRFObject):
s += '<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) (self.binding_map[doc.binding], doc.dpi, doc.width, doc.height, doc.color_depth)
for font in self._document.font_map.values(): for font in self._document.font_map.values():
s += unicode_type(font) s += str(font)
s += '</BookStyle>\n' s += '</BookStyle>\n'
return s return s
@ -1230,7 +1229,7 @@ class TOCObject(LRFStream):
def __str__(self): def __str__(self):
s = '<TOC>\n' s = '<TOC>\n'
for i in self: for i in self:
s += unicode_type(i) s += str(i)
return s + '</TOC>\n' return s + '</TOC>\n'

View File

@ -2,7 +2,7 @@
""" elements.py -- replacements and helpers for ElementTree """ """ elements.py -- replacements and helpers for ElementTree """
from polyglot.builtins import unicode_type, string_or_bytes from polyglot.builtins import string_or_bytes
class ElementWriter: class ElementWriter:
@ -25,9 +25,9 @@ class ElementWriter:
return text return text
def _writeAttribute(self, f, name, value): def _writeAttribute(self, f, name, value):
f.write(' %s="' % unicode_type(name)) f.write(' %s="' % str(name))
if not isinstance(value, string_or_bytes): if not isinstance(value, string_or_bytes):
value = unicode_type(value) value = str(value)
value = self._encodeCdata(value) value = self._encodeCdata(value)
value = value.replace('"', '&quot;') value = value.replace('"', '&quot;')
f.write(value) f.write(value)
@ -38,7 +38,7 @@ class ElementWriter:
f.write(text) f.write(text)
def _write(self, f, e): def _write(self, f, e):
f.write('<' + unicode_type(e.tag)) f.write('<' + str(e.tag))
attributes = e.items() attributes = e.items()
attributes.sort() attributes.sort()

View File

@ -12,7 +12,7 @@ import codecs
import os import os
from .pylrfopt import tagListOptimizer from .pylrfopt import tagListOptimizer
from polyglot.builtins import iteritems, string_or_bytes, unicode_type from polyglot.builtins import iteritems, string_or_bytes
PYLRF_VERSION = "1.0" PYLRF_VERSION = "1.0"
@ -85,7 +85,7 @@ def writeWord(f, word):
if int(word) > 65535: if int(word) > 65535:
raise LrfError('Cannot encode a number greater than 65535 in a word.') raise LrfError('Cannot encode a number greater than 65535 in a word.')
if int(word) < 0: if int(word) < 0:
raise LrfError('Cannot encode a number < 0 in a word: '+unicode_type(word)) raise LrfError('Cannot encode a number < 0 in a word: '+str(word))
f.write(struct.pack("<H", int(word))) f.write(struct.pack("<H", int(word)))
@ -511,7 +511,7 @@ class LrfObject:
raise LrfError("object name %s not recognized" % name) raise LrfError("object name %s not recognized" % name)
def __str__(self): def __str__(self):
return 'LRFObject: ' + self.name + ", " + unicode_type(self.objId) return 'LRFObject: ' + self.name + ", " + str(self.objId)
def appendLrfTag(self, tag): def appendLrfTag(self, tag):
self.tags.append(tag) self.tags.append(tag)

View File

@ -54,7 +54,7 @@ DEFAULT_GENREADING = "fs" # default is yes to both lrf and lrs
from calibre import __appname__, __version__ from calibre import __appname__, __version__
from calibre import entity_to_unicode from calibre import entity_to_unicode
from polyglot.builtins import string_or_bytes, unicode_type, iteritems, native_string_type from polyglot.builtins import string_or_bytes, iteritems, native_string_type
class LrsError(Exception): class LrsError(Exception):
@ -229,7 +229,7 @@ class LrsAttributes:
raise LrsError("%s does not support setting %s" % raise LrsError("%s does not support setting %s" %
(self.__class__.__name__, name)) (self.__class__.__name__, name))
if isinstance(value, int): if isinstance(value, int):
value = unicode_type(value) value = str(value)
self.attrs[name] = value self.attrs[name] = value
@ -333,13 +333,13 @@ class LrsObject:
def lrsObjectElement(self, name, objlabel="objlabel", labelName=None, def lrsObjectElement(self, name, objlabel="objlabel", labelName=None,
labelDecorate=True, **settings): labelDecorate=True, **settings):
element = Element(name) element = Element(name)
element.attrib["objid"] = unicode_type(self.objId) element.attrib["objid"] = str(self.objId)
if labelName is None: if labelName is None:
labelName = name labelName = name
if labelDecorate: if labelDecorate:
label = "%s.%d" % (labelName, self.objId) label = "%s.%d" % (labelName, self.objId)
else: else:
label = unicode_type(self.objId) label = str(self.objId)
element.attrib[objlabel] = label element.attrib[objlabel] = label
element.attrib.update(settings) element.attrib.update(settings)
return element return element
@ -565,7 +565,7 @@ class Book(Delegator):
factor = base_font_size / old_base_font_size factor = base_font_size / old_base_font_size
def rescale(old): def rescale(old):
return unicode_type(int(int(old) * factor)) return str(int(int(old) * factor))
text_blocks = list(main.get_all(lambda x: isinstance(x, TextBlock))) text_blocks = list(main.get_all(lambda x: isinstance(x, TextBlock)))
for tb in text_blocks: for tb in text_blocks:
@ -696,7 +696,7 @@ class TableOfContents:
def addTocEntry(self, tocLabel, textBlock): def addTocEntry(self, tocLabel, textBlock):
if not isinstance(textBlock, (Canvas, TextBlock, ImageBlock, RuledLine)): if not isinstance(textBlock, (Canvas, TextBlock, ImageBlock, RuledLine)):
raise LrsError("TOC destination must be a Canvas, TextBlock, ImageBlock or RuledLine"+ raise LrsError("TOC destination must be a Canvas, TextBlock, ImageBlock or RuledLine"+
" not a " + unicode_type(type(textBlock))) " not a " + str(type(textBlock)))
if textBlock.parent is None: if textBlock.parent is None:
raise LrsError("TOC text block must be already appended to a page") raise LrsError("TOC text block must be already appended to a page")
@ -746,8 +746,8 @@ class TocLabel:
def toElement(self, se): def toElement(self, se):
return ElementWithText("TocLabel", self.label, return ElementWithText("TocLabel", self.label,
refobj=unicode_type(self.textBlock.objId), refobj=str(self.textBlock.objId),
refpage=unicode_type(self.textBlock.parent.objId)) refpage=str(self.textBlock.parent.objId))
class BookInfo: class BookInfo:
@ -808,7 +808,7 @@ class DocInfo:
self.thumbnail = None self.thumbnail = None
self.language = "en" self.language = "en"
self.creator = None self.creator = None
self.creationdate = unicode_type(isoformat(date.today())) self.creationdate = str(isoformat(date.today()))
self.producer = "%s v%s"%(__appname__, __version__) self.producer = "%s v%s"%(__appname__, __version__)
self.numberofpages = "0" self.numberofpages = "0"
@ -832,7 +832,7 @@ class DocInfo:
docInfo.append(ElementWithText("Creator", self.creator)) docInfo.append(ElementWithText("Creator", self.creator))
docInfo.append(ElementWithText("CreationDate", self.creationdate)) docInfo.append(ElementWithText("CreationDate", self.creationdate))
docInfo.append(ElementWithText("Producer", self.producer)) docInfo.append(ElementWithText("Producer", self.producer))
docInfo.append(ElementWithText("SumPage", unicode_type(self.numberofpages))) docInfo.append(ElementWithText("SumPage", str(self.numberofpages)))
return docInfo return docInfo
@ -1094,7 +1094,7 @@ class LrsStyle(LrsObject, LrsAttributes, LrsContainer):
self.elementName = elementName self.elementName = elementName
self.objectsAppended = False self.objectsAppended = False
# self.label = "%s.%d" % (elementName, self.objId) # self.label = "%s.%d" % (elementName, self.objId)
# self.label = unicode_type(self.objId) # self.label = str(self.objId)
# self.parent = None # self.parent = None
def update(self, settings): def update(self, settings):
@ -1104,11 +1104,11 @@ class LrsStyle(LrsObject, LrsAttributes, LrsContainer):
self.attrs[name] = value self.attrs[name] = value
def getLabel(self): def getLabel(self):
return unicode_type(self.objId) return str(self.objId)
def toElement(self, se): def toElement(self, se):
element = Element(self.elementName, stylelabel=self.getLabel(), element = Element(self.elementName, stylelabel=self.getLabel(),
objid=unicode_type(self.objId)) objid=str(self.objId))
element.attrib.update(self.attrs) element.attrib.update(self.attrs)
return element return element
@ -1236,14 +1236,14 @@ class PageStyle(LrsStyle):
del settings[evenbase] del settings[evenbase]
if evenObj.parent is None: if evenObj.parent is None:
parent.append(evenObj) parent.append(evenObj)
settings[evenbase + "id"] = unicode_type(evenObj.objId) settings[evenbase + "id"] = str(evenObj.objId)
if oddbase in settings: if oddbase in settings:
oddObj = settings[oddbase] oddObj = settings[oddbase]
del settings[oddbase] del settings[oddbase]
if oddObj.parent is None: if oddObj.parent is None:
parent.append(oddObj) parent.append(oddObj)
settings[oddbase + "id"] = unicode_type(oddObj.objId) settings[oddbase + "id"] = str(oddObj.objId)
def appendReferencedObjects(self, parent): def appendReferencedObjects(self, parent):
if self.objectsAppended: if self.objectsAppended:
@ -1486,7 +1486,7 @@ class Paragraph(LrsContainer):
def __init__(self, text=None): def __init__(self, text=None):
LrsContainer.__init__(self, [Text, CR, DropCaps, CharButton, LrsContainer.__init__(self, [Text, CR, DropCaps, CharButton,
LrsSimpleChar1, bytes, unicode_type]) LrsSimpleChar1, bytes, str])
if text is not None: if text is not None:
if isinstance(text, string_or_bytes): if isinstance(text, string_or_bytes):
text = Text(text) text = Text(text)
@ -1521,7 +1521,7 @@ class Paragraph(LrsContainer):
class LrsTextTag(LrsContainer): class LrsTextTag(LrsContainer):
def __init__(self, text, validContents): def __init__(self, text, validContents):
LrsContainer.__init__(self, [Text, bytes, unicode_type] + validContents) LrsContainer.__init__(self, [Text, bytes, str] + validContents)
if text is not None: if text is not None:
self.append(text) self.append(text)
@ -1580,7 +1580,7 @@ class DropCaps(LrsTextTag):
return self.text is None or not self.text.strip() return self.text is None or not self.text.strip()
def toElement(self, se): def toElement(self, se):
elem = Element('DrawChar', line=unicode_type(self.line)) elem = Element('DrawChar', line=str(self.line))
appendTextElements(elem, self.contents, se) appendTextElements(elem, self.contents, se)
return elem return elem
@ -1656,7 +1656,7 @@ class JumpTo(LrsContainer):
self.textBlock = textBlock self.textBlock = textBlock
def toElement(self, se): def toElement(self, se):
return Element("JumpTo", refpage=unicode_type(self.textBlock.parent.objId), refobj=unicode_type(self.textBlock.objId)) return Element("JumpTo", refpage=str(self.textBlock.parent.objId), refobj=str(self.textBlock.objId))
class Plot(LrsSimpleChar1, LrsContainer): class Plot(LrsSimpleChar1, LrsContainer):
@ -1688,8 +1688,8 @@ class Plot(LrsSimpleChar1, LrsContainer):
parent.append(self.obj) parent.append(self.obj)
def toElement(self, se): def toElement(self, se):
elem = Element('Plot', xsize=unicode_type(self.xsize), ysize=unicode_type(self.ysize), elem = Element('Plot', xsize=str(self.xsize), ysize=str(self.ysize),
refobj=unicode_type(self.obj.objId)) refobj=str(self.obj.objId))
if self.adjustment: if self.adjustment:
elem.set('adjustment', self.adjustment) elem.set('adjustment', self.adjustment)
return elem return elem
@ -1771,7 +1771,7 @@ class Space(LrsSimpleChar1, LrsContainer):
if self.xsize == 0: if self.xsize == 0:
return return
return Element("Space", xsize=unicode_type(self.xsize)) return Element("Space", xsize=str(self.xsize))
def toLrfContainer(self, lrfWriter, container): def toLrfContainer(self, lrfWriter, container):
if self.xsize != 0: if self.xsize != 0:
@ -1785,7 +1785,7 @@ class Box(LrsSimpleChar1, LrsContainer):
""" """
def __init__(self, linetype="solid"): def __init__(self, linetype="solid"):
LrsContainer.__init__(self, [Text, bytes, unicode_type]) LrsContainer.__init__(self, [Text, bytes, str])
if linetype not in LINE_TYPE_ENCODING: if linetype not in LINE_TYPE_ENCODING:
raise LrsError(linetype + " is not a valid line type") raise LrsError(linetype + " is not a valid line type")
self.linetype = linetype self.linetype = linetype
@ -1805,7 +1805,7 @@ class Box(LrsSimpleChar1, LrsContainer):
class Span(LrsSimpleChar1, LrsContainer): class Span(LrsSimpleChar1, LrsContainer):
def __init__(self, text=None, **attrs): def __init__(self, text=None, **attrs):
LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, unicode_type]) LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, str])
if text is not None: if text is not None:
if isinstance(text, string_or_bytes): if isinstance(text, string_or_bytes):
text = Text(text) text = Text(text)
@ -1858,7 +1858,7 @@ class Span(LrsSimpleChar1, LrsContainer):
def toElement(self, se): def toElement(self, se):
element = Element('Span') element = Element('Span')
for (key, value) in self.attrs.items(): for (key, value) in self.attrs.items():
element.set(key, unicode_type(value)) element.set(key, str(value))
appendTextElements(element, self.contents, se) appendTextElements(element, self.contents, se)
return element return element
@ -1871,9 +1871,9 @@ class EmpLine(LrsTextTag, LrsSimpleChar1):
def __init__(self, text=None, emplineposition='before', emplinetype='solid'): def __init__(self, text=None, emplineposition='before', emplinetype='solid'):
LrsTextTag.__init__(self, text, [LrsSimpleChar1]) LrsTextTag.__init__(self, text, [LrsSimpleChar1])
if emplineposition not in self.__class__.emplinepositions: if emplineposition not in self.__class__.emplinepositions:
raise LrsError('emplineposition for an EmpLine must be one of: '+unicode_type(self.__class__.emplinepositions)) raise LrsError('emplineposition for an EmpLine must be one of: '+str(self.__class__.emplinepositions))
if emplinetype not in self.__class__.emplinetypes: if emplinetype not in self.__class__.emplinetypes:
raise LrsError('emplinetype for an EmpLine must be one of: '+unicode_type(self.__class__.emplinetypes)) raise LrsError('emplinetype for an EmpLine must be one of: '+str(self.__class__.emplinetypes))
self.emplinetype = emplinetype self.emplinetype = emplinetype
self.emplineposition = emplineposition self.emplineposition = emplineposition
@ -1933,9 +1933,9 @@ class BlockSpace(LrsContainer):
element = Element("BlockSpace") element = Element("BlockSpace")
if self.xspace != 0: if self.xspace != 0:
element.attrib["xspace"] = unicode_type(self.xspace) element.attrib["xspace"] = str(self.xspace)
if self.yspace != 0: if self.yspace != 0:
element.attrib["yspace"] = unicode_type(self.yspace) element.attrib["yspace"] = str(self.yspace)
return element return element
@ -1949,7 +1949,7 @@ class CharButton(LrsSimpleChar1, LrsContainer):
""" """
def __init__(self, button, text=None): def __init__(self, button, text=None):
LrsContainer.__init__(self, [bytes, unicode_type, Text, LrsSimpleChar1]) LrsContainer.__init__(self, [bytes, str, Text, LrsSimpleChar1])
self.button = None self.button = None
if button is not None: if button is not None:
self.setButton(button) self.setButton(button)
@ -1979,7 +1979,7 @@ class CharButton(LrsSimpleChar1, LrsContainer):
container.appendLrfTag(LrfTag("CharButtonEnd")) container.appendLrfTag(LrfTag("CharButtonEnd"))
def toElement(self, se): def toElement(self, se):
cb = Element("CharButton", refobj=unicode_type(self.button.objId)) cb = Element("CharButton", refobj=str(self.button.objId))
appendTextElements(cb, self.contents, se) appendTextElements(cb, self.contents, se)
return cb return cb
@ -2081,8 +2081,8 @@ class JumpButton(LrsObject, LrsContainer):
b = self.lrsObjectElement("Button") b = self.lrsObjectElement("Button")
pb = SubElement(b, "PushButton") pb = SubElement(b, "PushButton")
SubElement(pb, "JumpTo", SubElement(pb, "JumpTo",
refpage=unicode_type(self.textBlock.parent.objId), refpage=str(self.textBlock.parent.objId),
refobj=unicode_type(self.textBlock.objId)) refobj=str(self.textBlock.objId))
return b return b
@ -2230,8 +2230,8 @@ class PutObj(LrsContainer):
self.content.objId))) self.content.objId)))
def toElement(self, se): def toElement(self, se):
el = Element("PutObj", x1=unicode_type(self.x1), y1=unicode_type(self.y1), el = Element("PutObj", x1=str(self.x1), y1=str(self.y1),
refobj=unicode_type(self.content.objId)) refobj=str(self.content.objId))
return el return el
@ -2313,9 +2313,9 @@ class Image(LrsObject, LrsContainer, LrsAttributes):
def toElement(self, se): def toElement(self, se):
element = self.lrsObjectElement("Image", **self.attrs) element = self.lrsObjectElement("Image", **self.attrs)
element.set("refstream", unicode_type(self.refstream.objId)) element.set("refstream", str(self.refstream.objId))
for name in ["x0", "y0", "x1", "y1", "xsize", "ysize"]: for name in ["x0", "y0", "x1", "y1", "xsize", "ysize"]:
element.set(name, unicode_type(getattr(self, name))) element.set(name, str(getattr(self, name)))
return element return element
def toLrf(self, lrfWriter): def toLrf(self, lrfWriter):
@ -2396,9 +2396,9 @@ class ImageBlock(LrsObject, LrsContainer, LrsAttributes):
def toElement(self, se): def toElement(self, se):
element = self.lrsObjectElement("ImageBlock", **self.attrs) element = self.lrsObjectElement("ImageBlock", **self.attrs)
element.set("refstream", unicode_type(self.refstream.objId)) element.set("refstream", str(self.refstream.objId))
for name in ["x0", "y0", "x1", "y1", "xsize", "ysize"]: for name in ["x0", "y0", "x1", "y1", "xsize", "ysize"]:
element.set(name, unicode_type(getattr(self, name))) element.set(name, str(getattr(self, name)))
element.text = self.alttext element.text = self.alttext
return element return element

View File

@ -7,7 +7,6 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import struct import struct
from calibre.ebooks.lrf import LRFParseError from calibre.ebooks.lrf import LRFParseError
from polyglot.builtins import unicode_type
class Tag: class Tag:
@ -198,7 +197,7 @@ class Tag:
self.id = 0xF500 + tag_id[0] self.id = 0xF500 + tag_id[0]
size, self.name = self.__class__.tags[tag_id[0]] size, self.name = self.__class__.tags[tag_id[0]]
if isinstance(size, unicode_type): if isinstance(size, str):
parser = getattr(self, size + '_parser') parser = getattr(self, size + '_parser')
self.contents = parser(stream) self.contents = parser(stream)
else: else:
@ -241,7 +240,7 @@ class Tag:
@classmethod @classmethod
def string_parser(self, stream): def string_parser(self, stream):
size = struct.unpack("<H", stream.read(2))[0] size = struct.unpack("<H", stream.read(2))[0]
return unicode_type(stream.read(size), "utf_16") return str(stream.read(size), "utf_16")
def type_one_parser(self, stream): def type_one_parser(self, stream):
cnt = struct.unpack("<H", stream.read(2))[0] cnt = struct.unpack("<H", stream.read(2))[0]

View File

@ -14,7 +14,7 @@ from contextlib import suppress
from calibre import relpath, guess_type, prints, force_unicode from calibre import relpath, guess_type, prints, force_unicode
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from polyglot.builtins import codepoint_to_chr, unicode_type, iteritems, as_unicode from polyglot.builtins import codepoint_to_chr, iteritems, as_unicode
from polyglot.urllib import quote, unquote, urlparse from polyglot.urllib import quote, unquote, urlparse
@ -207,7 +207,7 @@ coding = list(zip(
def roman(num): def roman(num):
if num <= 0 or num >= 4000 or int(num) != num: if num <= 0 or num >= 4000 or int(num) != num:
return unicode_type(num) return str(num)
result = [] result = []
for d, r in coding: for d, r in coding:
while num >= d: while num >= d:
@ -222,7 +222,7 @@ def fmt_sidx(i, fmt='%.2f', use_roman=False):
try: try:
i = float(i) i = float(i)
except Exception: except Exception:
return unicode_type(i) return str(i)
if int(i) == float(i): if int(i) == float(i):
return roman(int(i)) if use_roman else '%d'%int(i) return roman(int(i)) if use_roman else '%d'%int(i)
return fmt%i return fmt%i
@ -266,7 +266,7 @@ class Resource:
self._href = href_or_path self._href = href_or_path
else: else:
pc = url[2] pc = url[2]
if isinstance(pc, unicode_type): if isinstance(pc, str):
pc = pc.encode('utf-8') pc = pc.encode('utf-8')
pc = unquote(pc).decode('utf-8') pc = unquote(pc).decode('utf-8')
self.path = os.path.abspath(os.path.join(basedir, pc.replace('/', os.sep))) self.path = os.path.abspath(os.path.join(basedir, pc.replace('/', os.sep)))
@ -287,7 +287,7 @@ class Resource:
basedir = os.getcwd() basedir = os.getcwd()
if self.path is None: if self.path is None:
return self._href return self._href
f = self.fragment.encode('utf-8') if isinstance(self.fragment, unicode_type) else self.fragment f = self.fragment.encode('utf-8') if isinstance(self.fragment, str) else self.fragment
frag = '#'+as_unicode(quote(f)) if self.fragment else '' frag = '#'+as_unicode(quote(f)) if self.fragment else ''
if self.path == basedir: if self.path == basedir:
return ''+frag return ''+frag
@ -295,7 +295,7 @@ class Resource:
rpath = relpath(self.path, basedir) rpath = relpath(self.path, basedir)
except OSError: # On windows path and basedir could be on different drives except OSError: # On windows path and basedir could be on different drives
rpath = self.path rpath = self.path
if isinstance(rpath, unicode_type): if isinstance(rpath, str):
rpath = rpath.encode('utf-8') rpath = rpath.encode('utf-8')
return as_unicode(quote(rpath.replace(os.sep, '/')))+frag return as_unicode(quote(rpath.replace(os.sep, '/')))+frag
@ -332,7 +332,7 @@ class ResourceCollection:
return '[%s]'%', '.join(resources) return '[%s]'%', '.join(resources)
def __repr__(self): def __repr__(self):
return unicode_type(self) return str(self)
def append(self, resource): def append(self, resource):
if not isinstance(resource, Resource): if not isinstance(resource, Resource):

View File

@ -11,7 +11,6 @@ from contextlib import closing
from calibre.customize import FileTypePlugin from calibre.customize import FileTypePlugin
from calibre.utils.localization import canonicalize_lang from calibre.utils.localization import canonicalize_lang
from polyglot.builtins import unicode_type
def is_comic(list_of_names): def is_comic(list_of_names):
@ -170,7 +169,7 @@ def get_comic_book_info(d, mi, series_index='volume'):
from datetime import date from datetime import date
try: try:
dt = date(puby, 6 if pubm is None else pubm, 15) dt = date(puby, 6 if pubm is None else pubm, 15)
dt = parse_only_date(unicode_type(dt)) dt = parse_only_date(str(dt))
mi.pubdate = dt mi.pubdate = dt
except Exception: except Exception:
pass pass

Some files were not shown because too many files have changed in this diff Show More