Get rid of basestring

This commit is contained in:
Kovid Goyal 2019-03-13 14:57:26 +05:30
parent 073e97121e
commit 55151a3cdd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
86 changed files with 251 additions and 226 deletions

View File

@ -15,6 +15,7 @@ from functools import partial
from calibre import as_unicode from calibre import as_unicode
from calibre.customize import (Plugin, numeric_version, platform, from calibre.customize import (Plugin, numeric_version, platform,
InvalidPlugin, PluginNotFound) InvalidPlugin, PluginNotFound)
from polyglot.builtins import 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
@ -34,7 +35,7 @@ def get_resources(zfp, name_or_list_of_names):
be just the bytes of the resource or None if it wasn't found. be just the bytes of the resource or None if it wasn't found.
''' '''
names = name_or_list_of_names names = name_or_list_of_names
if isinstance(names, basestring): if isinstance(names, string_or_bytes):
names = [names] names = [names]
ans = {} ans = {}
with zipfile.ZipFile(zfp) as zf: with zipfile.ZipFile(zfp) as zf:
@ -65,11 +66,11 @@ def get_icons(zfp, name_or_list_of_names):
from PyQt5.Qt import QIcon, QPixmap from PyQt5.Qt import QIcon, QPixmap
names = name_or_list_of_names names = name_or_list_of_names
ans = get_resources(zfp, names) ans = get_resources(zfp, names)
if isinstance(names, basestring): if isinstance(names, string_or_bytes):
names = [names] names = [names]
if ans is None: if ans is None:
ans = {} ans = {}
if isinstance(ans, basestring): if isinstance(ans, string_or_bytes):
ans = dict([(names[0], ans)]) ans = dict([(names[0], ans)])
ians = {} ians = {}

View File

@ -12,7 +12,7 @@ import os, shutil, uuid, json, glob, time, hashlib, errno, sys
from functools import partial from functools import partial
import apsw import apsw
from polyglot.builtins import unicode_type, reraise from polyglot.builtins import unicode_type, reraise, string_or_bytes
from calibre import isbytestring, force_unicode, prints, as_unicode from calibre import isbytestring, force_unicode, prints, as_unicode
from calibre.constants import (iswindows, filesystem_encoding, from calibre.constants import (iswindows, filesystem_encoding,
@ -1348,7 +1348,7 @@ class DB(object):
def copy_cover_to(self, path, dest, windows_atomic_move=None, use_hardlink=False, report_file_size=None): def copy_cover_to(self, path, dest, windows_atomic_move=None, use_hardlink=False, report_file_size=None):
path = os.path.abspath(os.path.join(self.library_path, path, 'cover.jpg')) path = os.path.abspath(os.path.join(self.library_path, path, 'cover.jpg'))
if windows_atomic_move is not None: if windows_atomic_move is not None:
if not isinstance(dest, basestring): if not isinstance(dest, string_or_bytes):
raise Exception("Error, you must pass the dest as a path when" raise Exception("Error, you must pass the dest as a path when"
" using windows_atomic_move") " using windows_atomic_move")
if os.access(path, os.R_OK) and dest and not samefile(dest, path): if os.access(path, os.R_OK) and dest and not samefile(dest, path):
@ -1438,7 +1438,7 @@ class DB(object):
if path is None: if path is None:
return False return False
if windows_atomic_move is not None: if windows_atomic_move is not None:
if not isinstance(dest, basestring): if not isinstance(dest, string_or_bytes):
raise Exception("Error, you must pass the dest as a path when" raise Exception("Error, you must pass the dest as a path when"
" using windows_atomic_move") " using windows_atomic_move")
if dest: if dest:

View File

@ -11,7 +11,7 @@ import os, traceback, random, shutil, operator
from io import BytesIO from io import BytesIO
from collections import defaultdict, Set, MutableSet from collections import defaultdict, Set, MutableSet
from functools import wraps, partial from functools import wraps, partial
from polyglot.builtins import unicode_type, zip from polyglot.builtins import unicode_type, zip, string_or_bytes
from time import time from time import time
from calibre import isbytestring, as_unicode from calibre import isbytestring, as_unicode
@ -1076,7 +1076,7 @@ class Cache(object):
bimap, simap = {}, {} bimap, simap = {}, {}
sfield = self.fields[name + '_index'] sfield = self.fields[name + '_index']
for k, v in book_id_to_val_map.iteritems(): for k, v in book_id_to_val_map.iteritems():
if isinstance(v, basestring): if isinstance(v, string_or_bytes):
v, sid = get_series_values(v) v, sid = get_series_values(v)
else: else:
v = sid = None v = sid = None
@ -1298,7 +1298,7 @@ class Cache(object):
# force_changes has no effect on cover manipulation # force_changes has no effect on cover manipulation
try: try:
cdata = mi.cover_data[1] cdata = mi.cover_data[1]
if cdata is None and isinstance(mi.cover, basestring) and mi.cover and os.access(mi.cover, os.R_OK): if cdata is None and isinstance(mi.cover, string_or_bytes) and mi.cover and os.access(mi.cover, os.R_OK):
with lopen(mi.cover, 'rb') as f: with lopen(mi.cover, 'rb') as f:
cdata = f.read() or None cdata = f.read() or None
if cdata is not None: if cdata is not None:

View File

@ -19,7 +19,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 unicode_type from polyglot.builtins import unicode_type, string_or_bytes
CONTAINS_MATCH = 0 CONTAINS_MATCH = 0
EQUALS_MATCH = 1 EQUALS_MATCH = 1
@ -706,7 +706,7 @@ class Parser(SearchQueryParser): # {{{
if location in text_fields: if location in text_fields:
for val, book_ids in self.field_iter(location, current_candidates): for val, book_ids in self.field_iter(location, current_candidates):
if val is not None: if val is not None:
if isinstance(val, basestring): if isinstance(val, string_or_bytes):
val = (val,) val = (val,)
if _match(q, val, matchkind, use_primary_find_in_search=upf, case_sensitive=case_sensitive): if _match(q, val, matchkind, use_primary_find_in_search=upf, case_sensitive=case_sensitive):
matches |= book_ids matches |= book_ids

View File

@ -9,7 +9,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import os, errno, cPickle, sys, re import os, errno, cPickle, sys, re
from locale import localeconv from locale import localeconv
from collections import OrderedDict, namedtuple from collections import OrderedDict, namedtuple
from polyglot.builtins import map, unicode_type from polyglot.builtins import map, unicode_type, string_or_bytes
from threading import Lock from threading import Lock
from calibre import as_unicode, prints from calibre import as_unicode, prints
@ -45,7 +45,7 @@ def fuzzy_title_patterns():
if _fuzzy_title_patterns is None: if _fuzzy_title_patterns is None:
from calibre.ebooks.metadata import get_title_sort_pat from calibre.ebooks.metadata import get_title_sort_pat
_fuzzy_title_patterns = tuple((re.compile(pat, re.IGNORECASE) if _fuzzy_title_patterns = tuple((re.compile(pat, re.IGNORECASE) if
isinstance(pat, basestring) else pat, repl) for pat, repl in isinstance(pat, string_or_bytes) else pat, repl) for pat, repl in
[ [
(r'[\[\](){}<>\'";,:#]', ''), (r'[\[\](){}<>\'";,:#]', ''),
(get_title_sort_pat(), ''), (get_title_sort_pat(), ''),

View File

@ -10,6 +10,7 @@ import cStringIO
from calibre import fsync from calibre import fsync
from calibre.devices.usbms.driver import USBMS from calibre.devices.usbms.driver import USBMS
from polyglot.builtins import string_or_bytes
HTC_BCDS = [0x100, 0x0222, 0x0224, 0x0226, 0x227, 0x228, 0x229, 0x0231, 0x9999] HTC_BCDS = [0x100, 0x0222, 0x0224, 0x0226, 0x227, 0x228, 0x229, 0x0231, 0x9999]
@ -286,7 +287,7 @@ class ANDROID(USBMS):
opts = [self.EBOOK_DIR_MAIN, ''] opts = [self.EBOOK_DIR_MAIN, '']
def strtolist(x): def strtolist(x):
if isinstance(x, basestring): if isinstance(x, string_or_bytes):
x = [y.strip() for y in x.split(',')] x = [y.strip() for y in x.split(',')]
return x or [] return x or []

View File

@ -14,7 +14,7 @@ from calibre.devices.usbms.books import CollectionsBookList
from calibre.utils.config_base import prefs from calibre.utils.config_base import prefs
from calibre.devices.usbms.driver import debug_print from calibre.devices.usbms.driver import debug_print
from calibre.ebooks.metadata import author_to_author_sort from calibre.ebooks.metadata import author_to_author_sort
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class Book(Book_): class Book(Book_):
@ -297,7 +297,7 @@ class KTCollectionsBookList(CollectionsBookList):
return 1 return 1
if y is None: if y is None:
return -1 return -1
if isinstance(x, basestring) and isinstance(y, basestring): if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes):
x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y)) x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y))
c = cmp(x, y) c = cmp(x, y)
if c != 0: if c != 0:

View File

@ -32,7 +32,7 @@ from calibre import prints, fsync
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
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 unicode_type from polyglot.builtins import unicode_type, string_or_bytes
EPUB_EXT = '.epub' EPUB_EXT = '.epub'
KEPUB_EXT = '.kepub' KEPUB_EXT = '.kepub'
@ -3543,14 +3543,14 @@ class KOBOTOUCH(KOBO):
# a string, so looking for that. # a string, so looking for that.
start_subclass_extra_options = OPT_MODIFY_CSS start_subclass_extra_options = OPT_MODIFY_CSS
debugging_title = '' debugging_title = ''
if isinstance(settings.extra_customization[OPT_MODIFY_CSS], basestring): if isinstance(settings.extra_customization[OPT_MODIFY_CSS], string_or_bytes):
debug_print("KoboTouch::migrate_old_settings - Don't have update_series option") debug_print("KoboTouch::migrate_old_settings - Don't have update_series option")
settings.update_series = config.get_option('update_series').default settings.update_series = config.get_option('update_series').default
settings.modify_css = config.get_option('modify_css').default settings.modify_css = config.get_option('modify_css').default
settings.support_newer_firmware = settings.extra_customization[OPT_UPDATE_SERIES_DETAILS] settings.support_newer_firmware = settings.extra_customization[OPT_UPDATE_SERIES_DETAILS]
debugging_title = settings.extra_customization[OPT_MODIFY_CSS] debugging_title = settings.extra_customization[OPT_MODIFY_CSS]
start_subclass_extra_options = OPT_MODIFY_CSS + 1 start_subclass_extra_options = OPT_MODIFY_CSS + 1
elif isinstance(settings.extra_customization[OPT_SUPPORT_NEWER_FIRMWARE], basestring): elif isinstance(settings.extra_customization[OPT_SUPPORT_NEWER_FIRMWARE], string_or_bytes):
debug_print("KoboTouch::migrate_old_settings - Don't have modify_css option") debug_print("KoboTouch::migrate_old_settings - Don't have modify_css option")
settings.update_series = settings.extra_customization[OPT_UPDATE_SERIES_DETAILS] settings.update_series = settings.extra_customization[OPT_UPDATE_SERIES_DETAILS]
settings.modify_css = config.get_option('modify_css').default settings.modify_css = config.get_option('modify_css').default
@ -3565,7 +3565,7 @@ class KOBOTOUCH(KOBO):
debugging_title = settings.extra_customization[OPT_DEBUGGING_TITLE] debugging_title = settings.extra_customization[OPT_DEBUGGING_TITLE]
start_subclass_extra_options = OPT_DEBUGGING_TITLE + 1 start_subclass_extra_options = OPT_DEBUGGING_TITLE + 1
settings.debugging_title = debugging_title if isinstance(debugging_title, basestring) else '' settings.debugging_title = debugging_title if isinstance(debugging_title, string_or_bytes) else ''
settings.update_device_metadata = settings.update_series settings.update_device_metadata = settings.update_series
settings.extra_customization = settings.extra_customization[start_subclass_extra_options:] settings.extra_customization = settings.extra_customization[start_subclass_extra_options:]

View File

@ -14,6 +14,7 @@ from calibre.constants import preferred_encoding
from calibre import isbytestring, force_unicode from calibre import isbytestring, force_unicode
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from polyglot.builtins import string_or_bytes
class Book(Metadata): class Book(Metadata):
@ -291,7 +292,7 @@ class CollectionsBookList(BookList):
return 1 return 1
if y is None: if y is None:
return -1 return -1
if isinstance(x, basestring) and isinstance(y, basestring): if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes):
x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y)) x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y))
try: try:
c = cmp(x, y) c = cmp(x, y)

View File

@ -23,6 +23,7 @@ from calibre.devices.errors import DeviceError
from calibre.devices.usbms.deviceconfig import DeviceConfig from calibre.devices.usbms.deviceconfig import DeviceConfig
from calibre.constants import iswindows, islinux, isosx, isfreebsd, plugins from calibre.constants import iswindows, islinux, isosx, isfreebsd, plugins
from calibre.utils.filenames import ascii_filename as sanitize from calibre.utils.filenames import ascii_filename as sanitize
from polyglot.builtins import string_or_bytes
if isosx: if isosx:
usbobserver, usbobserver_err = plugins['usbobserver'] usbobserver, usbobserver_err = plugins['usbobserver']
@ -938,7 +939,7 @@ class Device(DeviceConfig, DevicePlugin):
sanity_check(on_card, files, self.card_prefix(), self.free_space()) sanity_check(on_card, files, self.card_prefix(), self.free_space())
def get_dest_dir(prefix, candidates): def get_dest_dir(prefix, candidates):
if isinstance(candidates, basestring): if isinstance(candidates, string_or_bytes):
candidates = [candidates] candidates = [candidates]
if not candidates: if not candidates:
candidates = [''] candidates = ['']

View File

@ -20,7 +20,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 unicode_type from polyglot.builtins import unicode_type, string_or_bytes
BASE_TIME = None BASE_TIME = None
@ -243,7 +243,7 @@ class USBMS(CLI, Device):
import traceback import traceback
traceback.print_exc() traceback.print_exc()
return changed return changed
if isinstance(ebook_dirs, basestring): if isinstance(ebook_dirs, string_or_bytes):
ebook_dirs = [ebook_dirs] ebook_dirs = [ebook_dirs]
for ebook_dir in ebook_dirs: for ebook_dir in ebook_dirs:
ebook_dir = self.path_to_unicode(ebook_dir) ebook_dir = self.path_to_unicode(ebook_dir)

View File

@ -89,7 +89,7 @@ import types
import re import re
import calibre.ebooks.sgmllib as sgmllib import calibre.ebooks.sgmllib as sgmllib
from htmlentitydefs import name2codepoint from htmlentitydefs import name2codepoint
from polyglot.builtins import codepoint_to_chr, unicode_type from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes
#This hack makes Beautiful Soup able to parse XML with namespaces #This hack makes Beautiful Soup able to parse XML with namespaces
sgmllib.tagfind = re.compile('[a-zA-Z][-_.:a-zA-Z0-9]*') sgmllib.tagfind = re.compile('[a-zA-Z][-_.:a-zA-Z0-9]*')
@ -177,7 +177,7 @@ class PageElement:
return lastChild return lastChild
def insert(self, position, newChild): def insert(self, position, newChild):
if (isinstance(newChild, basestring) if (isinstance(newChild, string_or_bytes)
or isinstance(newChild, unicode_type)) \ or isinstance(newChild, unicode_type)) \
and not isinstance(newChild, NavigableString): and not isinstance(newChild, NavigableString):
newChild = NavigableString(newChild) newChild = NavigableString(newChild)
@ -937,7 +937,7 @@ def isString(s):
"""Convenience method that works with all 2.x versions of Python """Convenience method that works with all 2.x versions of Python
to determine whether or not something is stringlike.""" to determine whether or not something is stringlike."""
try: try:
return isinstance(s, unicode_type) or isinstance(s, basestring) return isinstance(s, unicode_type) or isinstance(s, string_or_bytes)
except NameError: except NameError:
return isinstance(s, str) return isinstance(s, str)

View File

@ -18,7 +18,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 from polyglot.builtins import unicode_type, string_or_bytes
DEBUG_README=u''' DEBUG_README=u'''
This debug directory contains snapshots of the e-book as it passes through the This debug directory contains snapshots of the e-book as it passes through the
@ -1022,7 +1022,7 @@ OptionRecommendation(name='search_replace',
def dump_input(self, ret, output_dir): def dump_input(self, ret, output_dir):
out_dir = os.path.join(self.opts.debug_pipeline, 'input') out_dir = os.path.join(self.opts.debug_pipeline, 'input')
if isinstance(ret, basestring): if isinstance(ret, string_or_bytes):
shutil.copytree(output_dir, out_dir) shutil.copytree(output_dir, out_dir)
else: else:
if not os.path.exists(out_dir): if not os.path.exists(out_dir):
@ -1214,7 +1214,7 @@ OptionRecommendation(name='search_replace',
transform_css_rules = () transform_css_rules = ()
if self.opts.transform_css_rules: if self.opts.transform_css_rules:
transform_css_rules = self.opts.transform_css_rules transform_css_rules = self.opts.transform_css_rules
if isinstance(transform_css_rules, basestring): if isinstance(transform_css_rules, string_or_bytes):
transform_css_rules = json.loads(transform_css_rules) transform_css_rules = json.loads(transform_css_rules)
flattener = CSSFlattener(fbase=fbase, fkey=fkey, flattener = CSSFlattener(fbase=fbase, fkey=fkey,
lineh=line_height, lineh=line_height,

View File

@ -10,7 +10,7 @@ import re, random, unicodedata
from collections import namedtuple from collections import namedtuple
from contextlib import contextmanager from contextlib import contextmanager
from math import ceil, sqrt, cos, sin, atan2 from math import ceil, sqrt, cos, sin, atan2
from polyglot.builtins import map, zip from polyglot.builtins import map, zip, string_or_bytes
from itertools import chain from itertools import chain
from PyQt5.Qt import ( from PyQt5.Qt import (
@ -275,7 +275,7 @@ def format_fields(mi, prefs):
@contextmanager @contextmanager
def preserve_fields(obj, fields): def preserve_fields(obj, fields):
if isinstance(fields, basestring): if isinstance(fields, string_or_bytes):
fields = fields.split() fields = fields.split()
null = object() null = object()
mem = {f:getattr(obj, f, null) for f in fields} mem = {f:getattr(obj, f, null) for f in fields}

View File

@ -19,7 +19,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 from polyglot.builtins import unicode_type, string_or_bytes
def lang_for_tag(tag): def lang_for_tag(tag):
@ -528,7 +528,7 @@ class Convert(object):
self.add_block_tag(tagname, html_tag, tag_style, stylizer, float_spec=float_spec) self.add_block_tag(tagname, html_tag, tag_style, stylizer, float_spec=float_spec)
for child in html_tag.iterchildren(): for child in html_tag.iterchildren():
if isinstance(getattr(child, 'tag', None), basestring): if isinstance(getattr(child, 'tag', None), string_or_bytes):
self.process_tag(child, stylizer, float_spec=float_spec) self.process_tag(child, stylizer, float_spec=float_spec)
else: # Comment/PI/etc. else: # Comment/PI/etc.
tail = getattr(child, 'tail', None) tail = getattr(child, 'tail', None)

View File

@ -19,7 +19,7 @@ from calibre.constants import __appname__, __version__
from calibre.utils.localization import lang_as_iso639_1 from calibre.utils.localization import lang_as_iso639_1
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 from polyglot.builtins import unicode_type, string_or_bytes
class FB2MLizer(object): class FB2MLizer(object):
@ -396,9 +396,9 @@ class FB2MLizer(object):
elem = elem_tree elem = elem_tree
# Ensure what we are converting is not a string and that the fist tag is part of the XHTML namespace. # Ensure what we are converting is not a string and that the fist tag is part of the XHTML namespace.
if not isinstance(elem_tree.tag, basestring) or namespace(elem_tree.tag) != XHTML_NS: if not isinstance(elem_tree.tag, string_or_bytes) or namespace(elem_tree.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [] return []

View File

@ -22,7 +22,7 @@ from calibre.ebooks.oeb.base import (
XHTML, XHTML_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize) XHTML, XHTML_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 from polyglot.builtins import unicode_type, string_or_bytes
SELF_CLOSING_TAGS = {'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta'} SELF_CLOSING_TAGS = {'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta'}
@ -98,7 +98,7 @@ class OEB2HTML(object):
for el in root.iter(): for el in root.iter():
attribs = el.attrib attribs = el.attrib
try: try:
if not isinstance(el.tag, basestring): if not isinstance(el.tag, string_or_bytes):
continue continue
except: except:
continue continue
@ -159,10 +159,10 @@ class OEB2HTMLNoCSSizer(OEB2HTML):
''' '''
# We can only processes tags. If there isn't a tag return any text. # We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
@ -248,10 +248,10 @@ class OEB2HTMLInlineCSSizer(OEB2HTML):
''' '''
# We can only processes tags. If there isn't a tag return any text. # We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
@ -351,10 +351,10 @@ class OEB2HTMLClassCSSizer(OEB2HTML):
''' '''
# We can only processes tags. If there isn't a tag return any text. # We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']

View File

@ -22,7 +22,7 @@ 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 calibre import plugins from calibre import plugins
from polyglot.builtins import codepoint_to_chr, unicode_type from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes
lzx, lxzerror = plugins['lzx'] lzx, lxzerror = plugins['lzx']
msdes, msdeserror = plugins['msdes'] msdes, msdeserror = plugins['msdes']
@ -281,7 +281,7 @@ class UnBinary(object):
attr = current_map[oc] attr = current_map[oc]
elif oc in self.attr_map: elif oc in self.attr_map:
attr = self.attr_map[oc] attr = self.attr_map[oc]
if not attr or not isinstance(attr, basestring): if not attr or not isinstance(attr, string_or_bytes):
raise LitError( raise LitError(
'Unknown attribute %d in tag %s' % (oc, tag_name)) 'Unknown attribute %d in tag %s' % (oc, tag_name))
if attr.startswith('%'): if attr.startswith('%'):

View File

@ -31,7 +31,7 @@ import calibre
from calibre import plugins from calibre import plugins
msdes, msdeserror = plugins['msdes'] msdes, msdeserror = plugins['msdes']
import calibre.ebooks.lit.mssha1 as mssha1 import calibre.ebooks.lit.mssha1 as mssha1
from polyglot.builtins import codepoint_to_chr, unicode_type from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes
__all__ = ['LitWriter'] __all__ = ['LitWriter']
@ -175,7 +175,7 @@ class ReBinary(object):
def tree_to_binary(self, elem, nsrmap=NSRMAP, parents=[], def tree_to_binary(self, elem, nsrmap=NSRMAP, parents=[],
inhead=False, preserve=False): inhead=False, preserve=False):
if not isinstance(elem.tag, basestring): if not isinstance(elem.tag, string_or_bytes):
# Don't emit any comments or raw entities # Don't emit any comments or raw entities
return return
nsrmap = copy.copy(nsrmap) nsrmap = copy.copy(nsrmap)

View File

@ -7,6 +7,7 @@ from calibre.ebooks.lrf.fonts import get_font
from calibre.ebooks.lrf.pylrs.pylrs import TextBlock, Text, CR, Span, \ from calibre.ebooks.lrf.pylrs.pylrs import TextBlock, Text, CR, Span, \
CharButton, Plot, Paragraph, \ CharButton, Plot, Paragraph, \
LrsTextTag LrsTextTag
from polyglot.builtins import string_or_bytes
def ceil(num): def ceil(num):
@ -38,7 +39,7 @@ def tokens(tb):
yield 2, None yield 2, None
elif isinstance(x, Text): elif isinstance(x, Text):
yield x.text, cattrs(attrs, {}) yield x.text, cattrs(attrs, {})
elif isinstance(x, basestring): elif isinstance(x, string_or_bytes):
yield x, cattrs(attrs, {}) yield x, cattrs(attrs, {})
elif isinstance(x, (CharButton, LrsTextTag)): elif isinstance(x, (CharButton, LrsTextTag)):
if x.contents: if x.contents:
@ -382,7 +383,3 @@ class Table(object):
yield tb, xpos[c], sypos, delta, None yield tb, xpos[c], sypos, delta, None
sypos += tb.blockStyle.attrs['blockheight'] sypos += tb.blockStyle.attrs['blockheight']

View File

@ -17,6 +17,7 @@ from calibre.ebooks.lrf.pylrs.pylrs import Book, PageStyle, TextStyle, \
Italic, Sup, Sub, Bold, EmpLine, JumpButton, CharButton, Plot, \ Italic, Sup, Sub, Bold, EmpLine, JumpButton, CharButton, Plot, \
DropCaps, Footer, RuledLine DropCaps, Footer, RuledLine
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from polyglot.builtins import string_or_bytes
class LrsParser(object): class LrsParser(object):
@ -95,7 +96,7 @@ class LrsParser(object):
if isinstance(contents[0], NavigableString): if isinstance(contents[0], NavigableString):
contents[0] = contents[0].string.lstrip() contents[0] = contents[0].string.lstrip()
for item in contents: for item in contents:
if isinstance(item, basestring): if isinstance(item, string_or_bytes):
p.append(item) p.append(item)
elif isinstance(item, NavigableString): elif isinstance(item, NavigableString):
p.append(item.string) p.append(item.string)

View File

@ -7,7 +7,7 @@ from calibre.ebooks.lrf import LRFParseError, PRS500_PROFILE
from calibre.constants import ispy3 from calibre.constants import ispy3
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 from polyglot.builtins import unicode_type, string_or_bytes
ruby_tags = { ruby_tags = {
0xF575: ['rubyAlignAndAdjust', 'W'], 0xF575: ['rubyAlignAndAdjust', 'W'],
@ -121,7 +121,7 @@ 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, basestring): if isinstance(action, string_or_bytes):
func, args = action, tuple([]) func, args = action, tuple([])
else: else:
func, args = action[0], (action[1],) func, args = action[0], (action[1],)
@ -880,7 +880,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, basestring): if isinstance(action, string_or_bytes):
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])
@ -904,7 +904,7 @@ class Text(LRFStream):
s = u'' s = u''
open_containers = collections.deque() open_containers = collections.deque()
for c in self.content: for c in self.content:
if isinstance(c, basestring): if isinstance(c, string_or_bytes):
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:
@ -930,7 +930,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, basestring): if isinstance(c, string_or_bytes):
s += c s += c
elif c is None: elif c is None:
if c.name == 'P': if c.name == 'P':

View File

@ -1,6 +1,6 @@
""" elements.py -- replacements and helpers for ElementTree """ """ elements.py -- replacements and helpers for ElementTree """
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class ElementWriter(object): class ElementWriter(object):
@ -24,7 +24,7 @@ class ElementWriter(object):
def _writeAttribute(self, f, name, value): def _writeAttribute(self, f, name, value):
f.write(u' %s="' % unicode_type(name)) f.write(u' %s="' % unicode_type(name))
if not isinstance(value, basestring): if not isinstance(value, string_or_bytes):
value = unicode_type(value) value = unicode_type(value)
value = self._encodeCdata(value) value = self._encodeCdata(value)
value = value.replace('"', '&quot;') value = value.replace('"', '&quot;')

View File

@ -58,6 +58,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
class LrsError(Exception): class LrsError(Exception):
@ -96,7 +97,7 @@ def ElementWithReading(tag, text, reading=False):
if text is None: if text is None:
readingText = "" readingText = ""
elif isinstance(text, basestring): elif isinstance(text, string_or_bytes):
readingText = text readingText = text
else: else:
# assumed to be a sequence of (name, sortas) # assumed to be a sequence of (name, sortas)
@ -155,7 +156,7 @@ class Delegator(object):
""" """
for setting in d.getSettings(): for setting in d.getSettings():
if isinstance(setting, basestring): if isinstance(setting, string_or_bytes):
setting = (d, setting) setting = (d, setting)
delegates = \ delegates = \
self.delegatedSettingsDict.setdefault(setting[1], []) self.delegatedSettingsDict.setdefault(setting[1], [])
@ -293,7 +294,7 @@ class LrsContainer(object):
(content.__class__.__name__, (content.__class__.__name__,
self.__class__.__name__)) self.__class__.__name__))
if convertText and isinstance(content, basestring): if convertText and isinstance(content, string_or_bytes):
content = Text(content) content = Text(content)
content.setParent(self) content.setParent(self)
@ -587,15 +588,15 @@ class Book(Delegator):
ts.attrs['baselineskip'] = rescale(ts.attrs['baselineskip']) ts.attrs['baselineskip'] = rescale(ts.attrs['baselineskip'])
def renderLrs(self, lrsFile, encoding="UTF-8"): def renderLrs(self, lrsFile, encoding="UTF-8"):
if isinstance(lrsFile, basestring): if isinstance(lrsFile, string_or_bytes):
lrsFile = codecs.open(lrsFile, "wb", encoding=encoding) lrsFile = codecs.open(lrsFile, "wb", encoding=encoding)
self.render(lrsFile, outputEncodingName=encoding) self.render(lrsFile, outputEncodingName=encoding)
lrsFile.close() lrsFile.close()
def renderLrf(self, lrfFile): def renderLrf(self, lrfFile):
self.appendReferencedObjects(self) self.appendReferencedObjects(self)
if isinstance(lrfFile, basestring): if isinstance(lrfFile, string_or_bytes):
lrfFile = file(lrfFile, "wb") lrfFile = open(lrfFile, "wb")
lrfWriter = LrfWriter(self.sourceencoding) lrfWriter = LrfWriter(self.sourceencoding)
lrfWriter.optimizeTags = self.optimizeTags lrfWriter.optimizeTags = self.optimizeTags
@ -1493,9 +1494,9 @@ 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, basestring]) LrsSimpleChar1, bytes, unicode_type])
if text is not None: if text is not None:
if isinstance(text, basestring): if isinstance(text, string_or_bytes):
text = Text(text) text = Text(text)
self.append(text) self.append(text)
@ -1528,7 +1529,7 @@ class Paragraph(LrsContainer):
class LrsTextTag(LrsContainer): class LrsTextTag(LrsContainer):
def __init__(self, text, validContents): def __init__(self, text, validContents):
LrsContainer.__init__(self, [Text, basestring] + validContents) LrsContainer.__init__(self, [Text, bytes, unicode_type] + validContents)
if text is not None: if text is not None:
self.append(text) self.append(text)
@ -1792,7 +1793,7 @@ class Box(LrsSimpleChar1, LrsContainer):
""" """
def __init__(self, linetype="solid"): def __init__(self, linetype="solid"):
LrsContainer.__init__(self, [Text, basestring]) LrsContainer.__init__(self, [Text, bytes, unicode_type])
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
@ -1812,9 +1813,9 @@ 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, basestring]) LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, unicode_type])
if text is not None: if text is not None:
if isinstance(text, basestring): if isinstance(text, string_or_bytes):
text = Text(text) text = Text(text)
self.append(text) self.append(text)
@ -1956,7 +1957,7 @@ class CharButton(LrsSimpleChar1, LrsContainer):
""" """
def __init__(self, button, text=None): def __init__(self, button, text=None):
LrsContainer.__init__(self, [basestring, Text, LrsSimpleChar1]) LrsContainer.__init__(self, [bytes, unicode_type, Text, LrsSimpleChar1])
self.button = None self.button = None
if button is not None: if button is not None:
self.setButton(button) self.setButton(button)

View File

@ -5,7 +5,7 @@ __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 from polyglot.builtins import unicode_type, string_or_bytes
class Tag(object): class Tag(object):
@ -196,7 +196,7 @@ class Tag(object):
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, basestring): if isinstance(size, string_or_bytes):
parser = getattr(self, size + '_parser') parser = getattr(self, size + '_parser')
self.contents = parser(stream) self.contents = parser(stream)
else: else:

View File

@ -33,6 +33,7 @@ from calibre.ebooks.metadata import MetaInformation, string_to_authors, check_is
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
from calibre.utils.date import parse_date from calibre.utils.date import parse_date
from calibre.utils.localization import canonicalize_lang from calibre.utils.localization import canonicalize_lang
from polyglot.builtins import string_or_bytes
whitespace = re.compile(r'\s+') whitespace = re.compile(r'\s+')
@ -125,7 +126,7 @@ class odfmetaparser(xml.sax.saxutils.XMLGenerator):
if name == (OFFICENS,u'meta'): if name == (OFFICENS,u'meta'):
for k,v in self.addfields.items(): for k,v in self.addfields.items():
if len(v) > 0: if len(v) > 0:
if isinstance(k, basestring): if isinstance(k, string_or_bytes):
xml.sax.saxutils.XMLGenerator.startElementNS(self,(METANS,u'user-defined'),None,{(METANS,u'name'):k}) xml.sax.saxutils.XMLGenerator.startElementNS(self,(METANS,u'user-defined'),None,{(METANS,u'name'):k})
xml.sax.saxutils.XMLGenerator.characters(self, v) xml.sax.saxutils.XMLGenerator.characters(self, v)
xml.sax.saxutils.XMLGenerator.endElementNS(self, (METANS,u'user-defined'),None) xml.sax.saxutils.XMLGenerator.endElementNS(self, (METANS,u'user-defined'),None)
@ -270,4 +271,3 @@ def read_cover(stream, zin, mi, opfmeta, extract_cover):
else: else:
cover_data = (fmt, raw) cover_data = (fmt, raw)
mi.cover_data = cover_data mi.cover_data = cover_data

View File

@ -8,7 +8,7 @@ import re, cStringIO, codecs
from calibre import force_unicode from calibre import force_unicode
from calibre.ebooks.metadata import MetaInformation, string_to_authors from calibre.ebooks.metadata import MetaInformation, string_to_authors
from polyglot.builtins import codepoint_to_chr, unicode_type from polyglot.builtins import codepoint_to_chr, unicode_type, string_or_bytes
title_pat = re.compile(r'\{\\info.*?\{\\title(.*?)(?<!\\)\}', re.DOTALL) title_pat = re.compile(r'\{\\info.*?\{\\title(.*?)(?<!\\)\}', re.DOTALL)
author_pat = re.compile(r'\{\\info.*?\{\\author(.*?)(?<!\\)\}', re.DOTALL) author_pat = re.compile(r'\{\\info.*?\{\\author(.*?)(?<!\\)\}', re.DOTALL)
@ -146,7 +146,7 @@ def create_metadata(stream, options):
md.append(r'{\title %s}'%(title,)) md.append(r'{\title %s}'%(title,))
if options.authors: if options.authors:
au = options.authors au = options.authors
if not isinstance(au, basestring): if not isinstance(au, string_or_bytes):
au = u', '.join(au) au = u', '.join(au)
author = encode(au) author = encode(au)
md.append(r'{\author %s}'%(author,)) md.append(r'{\author %s}'%(author,))

View File

@ -19,6 +19,7 @@ from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.opf2 import dump_dict from calibre.ebooks.metadata.opf2 import dump_dict
from calibre.utils.date import parse_date, isoformat, now from calibre.utils.date import parse_date, isoformat, now
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 string_or_bytes
_xml_declaration = re.compile(r'<\?xml[^<>]+encoding\s*=\s*[\'"](.*?)[\'"][^<>]*>', re.IGNORECASE) _xml_declaration = re.compile(r'<\?xml[^<>]+encoding\s*=\s*[\'"](.*?)[\'"][^<>]*>', re.IGNORECASE)
@ -49,6 +50,7 @@ def expand(name):
prefix, name = name.partition(':')[::2] prefix, name = name.partition(':')[::2]
return '{%s}%s' % (NS_MAP[prefix], name) return '{%s}%s' % (NS_MAP[prefix], name)
xpath_cache = {} xpath_cache = {}
@ -478,7 +480,7 @@ def metadata_to_xmp_packet(mi):
'authors':('dc:creator', True), 'tags':('dc:subject', False), 'publisher':('dc:publisher', False), 'authors':('dc:creator', True), 'tags':('dc:subject', False), 'publisher':('dc:publisher', False),
}.iteritems(): }.iteritems():
val = mi.get(prop) or () val = mi.get(prop) or ()
if isinstance(val, basestring): if isinstance(val, string_or_bytes):
val = [val] val = [val]
create_sequence_property(dc, tag, val, ordered) create_sequence_property(dc, tag, val, ordered)
if not mi.is_null('pubdate'): if not mi.is_null('pubdate'):
@ -636,10 +638,10 @@ def merge_xmp_packet(old, new):
return serialize_xmp_packet(root) return serialize_xmp_packet(root)
if __name__ == '__main__': if __name__ == '__main__':
from calibre.utils.podofo import get_xmp_metadata from calibre.utils.podofo import get_xmp_metadata
xmp_packet = get_xmp_metadata(sys.argv[-1]) xmp_packet = get_xmp_metadata(sys.argv[-1])
mi = metadata_from_xmp_packet(xmp_packet) mi = metadata_from_xmp_packet(xmp_packet)
np = metadata_to_xmp_packet(mi) np = metadata_to_xmp_packet(mi)
print (merge_xmp_packet(xmp_packet, np)) print(merge_xmp_packet(xmp_packet, np))

View File

@ -16,7 +16,7 @@ from calibre.ebooks.oeb.stylizer import Stylizer
from calibre.ebooks.oeb.transforms.flatcss import KeyMapper from calibre.ebooks.oeb.transforms.flatcss import KeyMapper
from calibre.ebooks.mobi.utils import convert_color_for_font_tag from calibre.ebooks.mobi.utils import convert_color_for_font_tag
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
MBP_NS = 'http://mobipocket.com/ns/mbp' MBP_NS = 'http://mobipocket.com/ns/mbp'
@ -144,7 +144,7 @@ class MobiMLizer(object):
return self.fnums[self.fmap[ptsize]] return self.fnums[self.fmap[ptsize]]
def mobimlize_measure(self, ptsize): def mobimlize_measure(self, ptsize):
if isinstance(ptsize, basestring): if isinstance(ptsize, string_or_bytes):
return ptsize return ptsize
embase = self.profile.fbase embase = self.profile.fbase
if round(ptsize) < embase: if round(ptsize) < embase:
@ -187,7 +187,7 @@ class MobiMLizer(object):
parent = bstate.nested[-1] if bstate.nested else bstate.body parent = bstate.nested[-1] if bstate.nested else bstate.body
indent = istate.indent indent = istate.indent
left = istate.left left = istate.left
if isinstance(indent, basestring): if isinstance(indent, string_or_bytes):
indent = 0 indent = 0
if indent < 0 and abs(indent) < left: if indent < 0 and abs(indent) < left:
left += indent left += indent
@ -308,7 +308,7 @@ class MobiMLizer(object):
inline = bstate.inline inline = bstate.inline
content = self.preize_text(text, pre_wrap=istate.pre_wrap) if istate.preserve or istate.pre_wrap else [text] content = self.preize_text(text, pre_wrap=istate.pre_wrap) if istate.preserve or istate.pre_wrap else [text]
for item in content: for item in content:
if isinstance(item, basestring): if isinstance(item, string_or_bytes):
if len(inline) == 0: if len(inline) == 0:
inline.text = (inline.text or '') + item inline.text = (inline.text or '') + item
else: else:
@ -319,7 +319,7 @@ class MobiMLizer(object):
def mobimlize_elem(self, elem, stylizer, bstate, istates, def mobimlize_elem(self, elem, stylizer, bstate, istates,
ignore_valign=False): ignore_valign=False):
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
return return
style = stylizer.style(elem) style = stylizer.style(elem)

View File

@ -18,7 +18,7 @@ from calibre.ebooks.mobi.utils import is_guide_ref_start
from calibre.ebooks.oeb.base import ( from calibre.ebooks.oeb.base import (
OEB_DOCS, XHTML, XHTML_NS, XML_NS, namespace, prefixname, urlnormalize OEB_DOCS, XHTML, XHTML_NS, XML_NS, namespace, prefixname, urlnormalize
) )
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class Buf(BytesIO): class Buf(BytesIO):
@ -310,7 +310,7 @@ class Serializer(object):
def serialize_elem(self, elem, item, nsrmap=NSRMAP): def serialize_elem(self, elem, item, nsrmap=NSRMAP):
buf = self.buf buf = self.buf
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) not in nsrmap: or namespace(elem.tag) not in nsrmap:
return return
tag = prefixname(elem.tag, nsrmap) tag = prefixname(elem.tag, nsrmap)

View File

@ -19,7 +19,7 @@ from odf.namespaces import TEXTNS as odTEXTNS
from calibre import CurrentDir, walk from calibre import CurrentDir, walk
from calibre.ebooks.oeb.base import _css_logger from calibre.ebooks.oeb.base import _css_logger
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class Extract(ODF2XHTML): class Extract(ODF2XHTML):
@ -249,7 +249,7 @@ class Extract(ODF2XHTML):
# first load the odf structure # first load the odf structure
self.lines = [] self.lines = []
self._wfunc = self._wlines self._wfunc = self._wlines
if isinstance(odffile, basestring) \ if isinstance(odffile, string_or_bytes) \
or hasattr(odffile, 'read'): # Added by Kovid or hasattr(odffile, 'read'): # Added by Kovid
self.document = odLoad(odffile) self.document = odLoad(odffile)
else: else:

View File

@ -22,7 +22,7 @@ from calibre.ebooks.oeb.parse_utils import (barename, XHTML_NS, RECOVER_PARSER,
namespace, XHTML, parse_html, NotHTML) namespace, XHTML, parse_html, NotHTML)
from calibre.utils.cleantext import clean_xml_chars from calibre.utils.cleantext import clean_xml_chars
from calibre.utils.short_uuid import uuid4 from calibre.utils.short_uuid import uuid4
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
XML_NS = 'http://www.w3.org/XML/1998/namespace' XML_NS = 'http://www.w3.org/XML/1998/namespace'
OEB_DOC_NS = 'http://openebook.org/namespaces/oeb-document/1.0/' OEB_DOC_NS = 'http://openebook.org/namespaces/oeb-document/1.0/'
@ -1055,7 +1055,7 @@ class Manifest(object):
mt = self.media_type.lower() mt = self.media_type.lower()
except Exception: except Exception:
mt = 'application/octet-stream' mt = 'application/octet-stream'
if not isinstance(data, basestring): if not isinstance(data, string_or_bytes):
pass # already parsed pass # already parsed
elif mt in OEB_DOCS: elif mt in OEB_DOCS:
data = self._parse_xhtml(data) data = self._parse_xhtml(data)
@ -1318,7 +1318,7 @@ class Spine(object):
self.page_progression_direction = None self.page_progression_direction = None
def _linear(self, linear): def _linear(self, linear):
if isinstance(linear, basestring): if isinstance(linear, string_or_bytes):
linear = linear.lower() linear = linear.lower()
if linear is None or linear in ('yes', 'true'): if linear is None or linear in ('yes', 'true'):
linear = True linear = True

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from polyglot.builtins import zip from polyglot.builtins import zip, string_or_bytes
from functools import wraps from functools import wraps
from css_parser.css import PropertyValue from css_parser.css import PropertyValue
@ -127,10 +127,10 @@ def normalize_font(cssvalue, font_family_as_list=False):
ans = {k:DEFAULTS[k] for k in composition} ans = {k:DEFAULTS[k] for k in composition}
ans.update(parse_font(val)) ans.update(parse_font(val))
if font_family_as_list: if font_family_as_list:
if isinstance(ans['font-family'], basestring): if isinstance(ans['font-family'], string_or_bytes):
ans['font-family'] = [x.strip() for x in ans['font-family'].split(',')] ans['font-family'] = [x.strip() for x in ans['font-family'].split(',')]
else: else:
if not isinstance(ans['font-family'], basestring): if not isinstance(ans['font-family'], string_or_bytes):
ans['font-family'] = serialize_font_family(ans['font-family']) ans['font-family'] = serialize_font_family(ans['font-family'])
return ans return ans

View File

@ -14,7 +14,7 @@ from lxml import etree, html
from calibre import xml_replace_entities, force_unicode from calibre import xml_replace_entities, force_unicode
from calibre.constants import filesystem_encoding from calibre.constants import filesystem_encoding
from calibre.ebooks.chardet import xml_to_unicode, strip_encoding_declarations from calibre.ebooks.chardet import xml_to_unicode, strip_encoding_declarations
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
RECOVER_PARSER = etree.XMLParser(recover=True, no_network=True) RECOVER_PARSER = etree.XMLParser(recover=True, no_network=True)
XHTML_NS = 'http://www.w3.org/1999/xhtml' XHTML_NS = 'http://www.w3.org/1999/xhtml'
@ -99,7 +99,7 @@ def html5_parse(data, max_nesting_depth=100):
# Check that the asinine HTML 5 algorithm did not result in a tree with # Check that the asinine HTML 5 algorithm did not result in a tree with
# insane nesting depths # insane nesting depths
for x in data.iterdescendants(): for x in data.iterdescendants():
if isinstance(x.tag, basestring) and not len(x): # Leaf node if isinstance(x.tag, string_or_bytes) and not len(x): # Leaf node
depth = node_depth(x) depth = node_depth(x)
if depth > max_nesting_depth: if depth > max_nesting_depth:
raise ValueError('HTML 5 parsing resulted in a tree with nesting' raise ValueError('HTML 5 parsing resulted in a tree with nesting'
@ -310,7 +310,7 @@ def parse_html(data, log=None, decoder=None, preprocessor=None,
nroot = etree.Element(XHTML('html'), nroot = etree.Element(XHTML('html'),
nsmap={None: XHTML_NS}, attrib=attrib) nsmap={None: XHTML_NS}, attrib=attrib)
for elem in data.iterdescendants(): for elem in data.iterdescendants():
if isinstance(elem.tag, basestring) and \ if isinstance(elem.tag, string_or_bytes) and \
namespace(elem.tag) == ns: namespace(elem.tag) == ns:
elem.tag = XHTML(barename(elem.tag)) elem.tag = XHTML(barename(elem.tag))
for elem in data: for elem in data:

View File

@ -15,13 +15,14 @@ from lxml import etree
from calibre import prints from calibre import prints
from calibre.ebooks.oeb.base import XHTML from calibre.ebooks.oeb.base import XHTML
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from polyglot.builtins import string_or_bytes
props = {'font-family':None, 'font-weight':'normal', 'font-style':'normal', 'font-stretch':'normal'} props = {'font-family':None, 'font-weight':'normal', 'font-style':'normal', 'font-stretch':'normal'}
def matching_rule(font, rules): def matching_rule(font, rules):
ff = font['font-family'] ff = font['font-family']
if not isinstance(ff, basestring): if not isinstance(ff, string_or_bytes):
ff = tuple(ff)[0] ff = tuple(ff)[0]
family = icu_lower(ff) family = icu_lower(ff)
wt = font['font-weight'] wt = font['font-weight']
@ -31,7 +32,7 @@ def matching_rule(font, rules):
for rule in rules: for rule in rules:
if rule['font-style'] == style and rule['font-stretch'] == stretch and rule['font-weight'] == wt: if rule['font-style'] == style and rule['font-stretch'] == stretch and rule['font-weight'] == wt:
ff = rule['font-family'] ff = rule['font-family']
if not isinstance(ff, basestring): if not isinstance(ff, string_or_bytes):
ff = tuple(ff)[0] ff = tuple(ff)[0]
if icu_lower(ff) == family: if icu_lower(ff) == family:
return rule return rule
@ -162,7 +163,7 @@ def do_embed(container, font, report):
def embed_font(container, font, all_font_rules, report, warned): def embed_font(container, font, all_font_rules, report, warned):
rule = matching_rule(font, all_font_rules) rule = matching_rule(font, all_font_rules)
ff = font['font-family'] ff = font['font-family']
if not isinstance(ff, basestring): if not isinstance(ff, string_or_bytes):
ff = ff[0] ff = ff[0]
if rule is None: if rule is None:
from calibre.utils.fonts.scanner import font_scanner, NoFonts from calibre.utils.fonts.scanner import font_scanner, NoFonts

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import copy, os, re import copy, os, re
from polyglot.builtins import map from polyglot.builtins import map, string_or_bytes
from urlparse import urlparse from urlparse import urlparse
from calibre.ebooks.oeb.base import barename, XPNSMAP, XPath, OPF, XHTML, OEB_DOCS from calibre.ebooks.oeb.base import barename, XPNSMAP, XPath, OPF, XHTML, OEB_DOCS
@ -395,9 +395,9 @@ def merge_html(container, names, master):
first_child = '' first_child = ''
for first_child in children: for first_child in children:
if not isinstance(first_child, basestring): if not isinstance(first_child, string_or_bytes):
break break
if isinstance(first_child, basestring): if isinstance(first_child, string_or_bytes):
# body contained only text, no tags # body contained only text, no tags
first_child = body.makeelement(XHTML('p')) first_child = body.makeelement(XHTML('p'))
first_child.text, children[0] = children[0], first_child first_child.text, children[0] = children[0], first_child
@ -429,7 +429,7 @@ def merge_html(container, names, master):
a.set('href', '#' + amap[q]) a.set('href', '#' + amap[q])
for child in children: for child in children:
if isinstance(child, basestring): if isinstance(child, string_or_bytes):
add_text(master_body, child) add_text(master_body, child)
else: else:
master_body.append(copy.deepcopy(child)) master_body.append(copy.deepcopy(child))

View File

@ -21,7 +21,7 @@ from calibre.ebooks.oeb.base import (XHTML, XHTML_NS, CSS_MIME, OEB_STYLES,
from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.stylizer import Stylizer
from calibre.utils.filenames import ascii_filename, ascii_text from calibre.utils.filenames import ascii_filename, ascii_text
from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import numeric_sort_key
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
COLLAPSE = re.compile(r'[ \t\r\n\v]+') COLLAPSE = re.compile(r'[ \t\r\n\v]+')
STRIPNUM = re.compile(r'[-0-9]+$') STRIPNUM = re.compile(r'[-0-9]+$')
@ -367,7 +367,7 @@ class CSSFlattener(object):
cssdict[property] = "%0.5fem" % (value / fsize) cssdict[property] = "%0.5fem" % (value / fsize)
def flatten_node(self, node, stylizer, names, styles, pseudo_styles, psize, item_id): def flatten_node(self, node, stylizer, names, styles, pseudo_styles, psize, item_id):
if not isinstance(node.tag, basestring) \ if not isinstance(node.tag, string_or_bytes) \
or namespace(node.tag) != XHTML_NS: or namespace(node.tag) != XHTML_NS:
return return
tag = barename(node.tag) tag = barename(node.tag)

View File

@ -11,6 +11,7 @@ from calibre.ebooks.oeb.base import XHTML, XHTML_NS
from calibre.ebooks.oeb.base import CSS_MIME from calibre.ebooks.oeb.base import CSS_MIME
from calibre.ebooks.oeb.base import namespace from calibre.ebooks.oeb.base import namespace
from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.stylizer import Stylizer
from polyglot.builtins import string_or_bytes
CASE_MANGLER_CSS = """ CASE_MANGLER_CSS = """
.calibre_lowercase { .calibre_lowercase {
@ -95,7 +96,7 @@ class CaseMangler(object):
last = child last = child
def mangle_elem(self, elem, stylizer): def mangle_elem(self, elem, stylizer):
if not isinstance(elem.tag, basestring) or \ if not isinstance(elem.tag, string_or_bytes) or \
namespace(elem.tag) != XHTML_NS: namespace(elem.tag) != XHTML_NS:
return return
children = list(elem) children = list(elem)

View File

@ -14,7 +14,7 @@ from lxml import etree
from calibre.ebooks.pdb.ereader import image_name from calibre.ebooks.pdb.ereader import image_name
from calibre.ebooks.pml import unipmlcode from calibre.ebooks.pml import unipmlcode
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
TAG_MAP = { TAG_MAP = {
'b' : 'B', 'b' : 'B',
@ -224,9 +224,9 @@ class PMLMLizer(object):
def dump_text(self, elem, stylizer, page, tag_stack=[]): def dump_text(self, elem, stylizer, page, tag_stack=[]):
from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace
if not isinstance(elem.tag, basestring) or namespace(elem.tag) != XHTML_NS: if not isinstance(elem.tag, string_or_bytes) or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [] return []

View File

@ -12,6 +12,7 @@ import re
from calibre import prepare_string_for_xml from calibre import prepare_string_for_xml
from calibre.ebooks.rb import unique_name from calibre.ebooks.rb import unique_name
from polyglot.builtins import string_or_bytes
TAGS = [ TAGS = [
'b', 'b',
@ -142,9 +143,9 @@ class RBMLizer(object):
def dump_text(self, elem, stylizer, page, tag_stack=[]): def dump_text(self, elem, stylizer, page, tag_stack=[]):
from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace
if not isinstance(elem.tag, basestring) or namespace(elem.tag) != XHTML_NS: if not isinstance(elem.tag, string_or_bytes) or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [u''] return [u'']

View File

@ -17,7 +17,7 @@ from lxml import etree
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.utils.img import save_cover_data_to from calibre.utils.img import save_cover_data_to
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
TAGS = { TAGS = {
'b': '\\b', 'b': '\\b',
@ -224,10 +224,10 @@ class RTFMLizer(object):
from calibre.ebooks.oeb.base import (XHTML_NS, namespace, barename, from calibre.ebooks.oeb.base import (XHTML_NS, namespace, barename,
urlnormalize) urlnormalize)
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return elem.tail return elem.tail
return u'' return u''

View File

@ -12,7 +12,7 @@ import os
import re import re
from lxml import etree from lxml import etree
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
def ProcessFileName(fileName): def ProcessFileName(fileName):
@ -212,10 +212,10 @@ class SNBMLizer(object):
def dump_text(self, subitems, elem, stylizer, end='', pre=False, li=''): def dump_text(self, subitems, elem, stylizer, end='', pre=False, li=''):
from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']

View File

@ -15,7 +15,7 @@ from functools import partial
from calibre.ebooks.htmlz.oeb2html import OEB2HTML from calibre.ebooks.htmlz.oeb2html import OEB2HTML
from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, rewrite_links from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, rewrite_links
from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.stylizer import Stylizer
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class MarkdownMLizer(OEB2HTML): class MarkdownMLizer(OEB2HTML):
@ -111,10 +111,10 @@ class MarkdownMLizer(OEB2HTML):
''' '''
# We can only processes tags. If there isn't a tag return any text. # We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']

View File

@ -16,6 +16,7 @@ from calibre.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, rewrit
from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.stylizer import Stylizer
from calibre.ebooks import unit_convert from calibre.ebooks import unit_convert
from calibre.ebooks.textile.unsmarten import unsmarten from calibre.ebooks.textile.unsmarten import unsmarten
from polyglot.builtins import string_or_bytes
class TextileMLizer(OEB2HTML): class TextileMLizer(OEB2HTML):
@ -72,9 +73,9 @@ class TextileMLizer(OEB2HTML):
# I'm not checking for duplicated spans '%' as any that follow each other were being incorrectly merged # I'm not checking for duplicated spans '%' as any that follow each other were being incorrectly merged
txt = '%s' % t txt = '%s' % t
if txt != '%': if txt != '%':
text = re.sub(r'([^'+t+'|^\n])'+t+'\]\['+t+'([^'+t+'])', r'\1\2', text) text = re.sub(r'([^'+t+'|^\n])'+t+r'\]\['+t+'([^'+t+'])', r'\1\2', text)
text = re.sub(r'([^'+t+'|^\n])'+t+t+'([^'+t+'])', r'\1\2', text) text = re.sub(r'([^'+t+'|^\n])'+t+t+'([^'+t+'])', r'\1\2', text)
text = re.sub(r'(\s|[*_\'"])\[('+t+'[a-zA-Z0-9 \'",.*_]+'+t+')\](\s|[*_\'"?!,.])', r'\1\2\3', text) text = re.sub(r'(\s|[*_\'"])\[('+t+'[a-zA-Z0-9 \'",.*_]+'+t+r')\](\s|[*_\'"?!,.])', r'\1\2\3', text)
return text return text
# Now tidyup links and ids - remove ones that don't have a correponding opposite # Now tidyup links and ids - remove ones that don't have a correponding opposite
@ -82,13 +83,13 @@ class TextileMLizer(OEB2HTML):
for i in self.our_links: for i in self.our_links:
if i[0] == '#': if i[0] == '#':
if i not in self.our_ids: if i not in self.our_ids:
text = re.sub(r'"(.+)":'+i+'(\s)', r'\1\2', text) text = re.sub(r'"(.+)":'+i+r'(\s)', r'\1\2', text)
for i in self.our_ids: for i in self.our_ids:
if i not in self.our_links: if i not in self.our_links:
text = re.sub(r'%?\('+i+'\)\xa0?%?', r'', text) text = re.sub(r'%?\('+i+'\\)\xa0?%?', r'', text)
# Remove obvious non-needed escaping, add sub/sup-script ones # Remove obvious non-needed escaping, add sub/sup-script ones
text = check_escaping(text, ['\*', '_', '\*']) text = check_escaping(text, [r'\*', '_', r'\*'])
# escape the super/sub-scripts if needed # escape the super/sub-scripts if needed
text = re.sub(r'(\w)([~^]\w+[~^])', r'\1[\2]', text) text = re.sub(r'(\w)([~^]\w+[~^])', r'\1[\2]', text)
# escape the super/sub-scripts if needed # escape the super/sub-scripts if needed
@ -111,16 +112,16 @@ class TextileMLizer(OEB2HTML):
# reduce blank lines # reduce blank lines
text = re.sub(r'\n{3}', r'\n\np. \n\n', text) text = re.sub(r'\n{3}', r'\n\np. \n\n', text)
text = re.sub(u'%\n(p[<>=]{1,2}\.|p\.)', r'%\n\n\1', text) text = re.sub(u'%\n(p[<>=]{1,2}\\.|p\\.)', r'%\n\n\1', text)
# Check span following blank para # Check span following blank para
text = re.sub(r'\n+ +%', r' %', text) text = re.sub(r'\n+ +%', r' %', text)
text = re.sub(u'p[<>=]{1,2}\.\n\n?', r'', text) text = re.sub(u'p[<>=]{1,2}\\.\n\n?', r'', text)
# blank paragraph # blank paragraph
text = re.sub(r'\n(p.*\.)\n', r'\n\1 \n\n', text) text = re.sub(r'\n(p.*\.)\n', r'\n\1 \n\n', text)
# blank paragraph # blank paragraph
text = re.sub(u'\n\xa0', r'\np. ', text) text = re.sub(u'\n\xa0', r'\np. ', text)
# blank paragraph # blank paragraph
text = re.sub(u'\np[<>=]{1,2}?\. \xa0', r'\np. ', text) text = re.sub(u'\np[<>=]{1,2}?\\. \xa0', r'\np. ', text)
text = re.sub(r'(^|\n)(p.*\. ?\n)(p.*\.)', r'\1\3', text) text = re.sub(r'(^|\n)(p.*\. ?\n)(p.*\.)', r'\1\3', text)
text = re.sub(r'\n(p\. \n)(p.*\.|h.*\.)', r'\n\2', text) text = re.sub(r'\n(p\. \n)(p.*\.|h.*\.)', r'\n\2', text)
# sort out spaces in tables # sort out spaces in tables
@ -225,10 +226,10 @@ class TextileMLizer(OEB2HTML):
''' '''
# We can only processes tags. If there isn't a tag return any text. # We can only processes tags. If there isn't a tag return any text.
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']

View File

@ -11,7 +11,7 @@ Transform OEB content into plain text
import re import re
from lxml import etree from lxml import etree
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
BLOCK_TAGS = [ BLOCK_TAGS = [
@ -192,10 +192,10 @@ class TXTMLizer(object):
''' '''
from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace
if not isinstance(elem.tag, basestring) \ if not isinstance(elem.tag, string_or_bytes) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, basestring) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']

View File

@ -35,7 +35,7 @@ from calibre.utils.config import Config, ConfigProxy, JSONConfig, dynamic
from calibre.utils.date import UNDEFINED_DATE from calibre.utils.date import UNDEFINED_DATE
from calibre.utils.file_type_icons import EXT_MAP from calibre.utils.file_type_icons import EXT_MAP
from calibre.utils.localization import get_lang from calibre.utils.localization import get_lang
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
try: try:
NO_URL_FORMATTING = QUrl.None_ NO_URL_FORMATTING = QUrl.None_
@ -1125,7 +1125,7 @@ def open_url(qurl):
# Qt 5 requires QApplication to be constructed before trying to use # Qt 5 requires QApplication to be constructed before trying to use
# QDesktopServices::openUrl() # QDesktopServices::openUrl()
ensure_app() ensure_app()
if isinstance(qurl, basestring): if isinstance(qurl, string_or_bytes):
qurl = QUrl(qurl) qurl = QUrl(qurl)
with sanitize_env_vars(): with sanitize_env_vars():
QDesktopServices.openUrl(qurl) QDesktopServices.openUrl(qurl)

View File

@ -15,7 +15,7 @@ from calibre import prints
from calibre.constants import isosx from calibre.constants import isosx
from calibre.gui2 import Dispatcher from calibre.gui2 import Dispatcher
from calibre.gui2.keyboard import NameConflict from calibre.gui2.keyboard import NameConflict
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
def menu_action_unique_name(plugin, unique_name): def menu_action_unique_name(plugin, unique_name):
@ -177,7 +177,7 @@ class InterfaceAction(QObject):
if attr == 'qaction': if attr == 'qaction':
shortcut_action = ma shortcut_action = ma
if shortcut is not None: if shortcut is not None:
keys = ((shortcut,) if isinstance(shortcut, basestring) else keys = ((shortcut,) if isinstance(shortcut, string_or_bytes) else
tuple(shortcut)) tuple(shortcut))
if shortcut_name is None and spec[0]: if shortcut_name is None and spec[0]:
shortcut_name = unicode_type(spec[0]) shortcut_name = unicode_type(spec[0])
@ -249,7 +249,7 @@ class InterfaceAction(QObject):
ac.setIcon(icon) ac.setIcon(icon)
keys = () keys = ()
if shortcut is not None and shortcut is not False: if shortcut is not None and shortcut is not False:
keys = ((shortcut,) if isinstance(shortcut, basestring) else keys = ((shortcut,) if isinstance(shortcut, string_or_bytes) else
tuple(shortcut)) tuple(shortcut))
unique_name = menu_action_unique_name(self, unique_name) unique_name = menu_action_unique_name(self, unique_name)
if description is not None: if description is not None:

View File

@ -25,6 +25,7 @@ from calibre.gui2.actions import InterfaceAction
from calibre.gui2 import question_dialog from calibre.gui2 import question_dialog
from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata import MetaInformation
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from polyglot.builtins import string_or_bytes
def get_filters(): def get_filters():
@ -436,7 +437,7 @@ class AddAction(InterfaceAction):
self.gui.refresh_cover_browser() self.gui.refresh_cover_browser()
def __add_filesystem_book(self, paths, allow_device=True): def __add_filesystem_book(self, paths, allow_device=True):
if isinstance(paths, basestring): if isinstance(paths, string_or_bytes):
paths = [paths] paths = [paths]
books = [path for path in map(os.path.abspath, paths) if os.access(path, books = [path for path in map(os.path.abspath, paths) if os.access(path,
os.R_OK)] os.R_OK)]
@ -591,7 +592,7 @@ class AddAction(InterfaceAction):
self.gui.device_job_exception(job) self.gui.device_job_exception(job)
return return
paths = job.result paths = job.result
ok_paths = [x for x in paths if isinstance(x, basestring)] ok_paths = [x for x in paths if isinstance(x, string_or_bytes)]
failed_paths = [x for x in paths if isinstance(x, tuple)] failed_paths = [x for x in paths if isinstance(x, tuple)]
if failed_paths: if failed_paths:
if not ok_paths: if not ok_paths:

View File

@ -9,6 +9,7 @@ __docformat__ = 'restructuredtext en'
from PyQt5.Qt import QToolButton from PyQt5.Qt import QToolButton
from calibre.gui2.actions import InterfaceAction from calibre.gui2.actions import InterfaceAction
from polyglot.builtins import string_or_bytes
class SimilarBooksAction(InterfaceAction): class SimilarBooksAction(InterfaceAction):
@ -74,7 +75,7 @@ class SimilarBooksAction(InterfaceAction):
if not val: if not val:
return return
if isinstance(val, basestring): if isinstance(val, string_or_bytes):
val = [val] val = [val]
search = [col + ':"='+t.replace('"', '\\"')+'"' for t in val] search = [col + ':"='+t.replace('"', '\\"')+'"' for t in val]
if search: if search:

View File

@ -11,7 +11,7 @@ from threading import Thread
from collections import OrderedDict from collections import OrderedDict
from Queue import Empty from Queue import Empty
from io import BytesIO from io import BytesIO
from polyglot.builtins import map, unicode_type from polyglot.builtins import map, unicode_type, string_or_bytes
from PyQt5.Qt import QObject, Qt, pyqtSignal from PyQt5.Qt import QObject, Qt, pyqtSignal
@ -33,7 +33,7 @@ from calibre.utils.ipc.pool import Pool, Failure
def validate_source(source, parent=None): # {{{ def validate_source(source, parent=None): # {{{
if isinstance(source, basestring): if isinstance(source, string_or_bytes):
if not os.path.exists(source): if not os.path.exists(source):
error_dialog(parent, _('Cannot add books'), _( error_dialog(parent, _('Cannot add books'), _(
'The path %s does not exist') % source, show=True) 'The path %s does not exist') % source, show=True)
@ -173,7 +173,7 @@ class Adder(QObject):
return tdir return tdir
try: try:
if isinstance(self.source, basestring): if isinstance(self.source, string_or_bytes):
find_files(self.source) find_files(self.source)
self.ignore_opf = True self.ignore_opf = True
else: else:

View File

@ -34,7 +34,7 @@ from calibre.utils.config import tweaks, device_prefs
from calibre.utils.img import scale_image from calibre.utils.img import scale_image
from calibre.library.save_to_disk import find_plugboard from calibre.library.save_to_disk import find_plugboard
from calibre.ptempfile import PersistentTemporaryFile, force_unicode as filename_to_unicode from calibre.ptempfile import PersistentTemporaryFile, force_unicode as filename_to_unicode
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
# }}} # }}}
@ -471,7 +471,7 @@ class DeviceManager(Thread): # {{{
info = self.device.get_device_information(end_session=False) info = self.device.get_device_information(end_session=False)
if len(info) < 5: if len(info) < 5:
info = tuple(list(info) + [{}]) info = tuple(list(info) + [{}])
info = [i.replace('\x00', '').replace('\x01', '') if isinstance(i, basestring) else i info = [i.replace('\x00', '').replace('\x01', '') if isinstance(i, string_or_bytes) else i
for i in info] for i in info]
cp = self.device.card_prefix(end_session=False) cp = self.device.card_prefix(end_session=False)
fs = self.device.free_space() fs = self.device.free_space()

View File

@ -19,6 +19,7 @@ from calibre.gui2.threaded_jobs import ThreadedJob
from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from calibre.web import get_download_filename_from_response from calibre.web import get_download_filename_from_response
from polyglot.builtins import string_or_bytes
class DownloadInfo(MessageBox): class DownloadInfo(MessageBox):
@ -141,7 +142,7 @@ class EbookDownloadMixin(object):
def download_ebook(self, url='', cookie_file=None, filename='', save_loc='', add_to_lib=True, tags=[], create_browser=None): def download_ebook(self, url='', cookie_file=None, filename='', save_loc='', add_to_lib=True, tags=[], create_browser=None):
if tags: if tags:
if isinstance(tags, basestring): if isinstance(tags, string_or_bytes):
tags = tags.split(',') tags = tags.split(',')
start_ebook_download(Dispatcher(self.downloaded_ebook), self.job_manager, self, cookie_file, url, filename, save_loc, add_to_lib, tags, create_browser) start_ebook_download(Dispatcher(self.downloaded_ebook), self.job_manager, self, cookie_file, url, filename, save_loc, add_to_lib, tags, create_browser)
self.status_bar.show_message(_('Downloading') + ' ' + filename.decode('utf-8', 'ignore') if filename else url.decode('utf-8', 'ignore'), 3000) self.status_bar.show_message(_('Downloading') + ' ' + filename.decode('utf-8', 'ignore') if filename else url.decode('utf-8', 'ignore'), 3000)

View File

@ -30,7 +30,7 @@ from calibre.constants import filesystem_encoding, DEBUG, config_dir
from calibre.gui2.library import DEFAULT_SORT from calibre.gui2.library import DEFAULT_SORT
from calibre.utils.localization import calibre_langcode_to_name from calibre.utils.localization import calibre_langcode_to_name
from calibre.library.coloring import color_row_key from calibre.library.coloring import color_row_key
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
Counts = namedtuple('Counts', 'library_total total current') Counts = namedtuple('Counts', 'library_total total current')
@ -1282,7 +1282,7 @@ class OnDeviceSearch(SearchQueryParser): # {{{
vals = accessor(row) vals = accessor(row)
if vals is None: if vals is None:
vals = '' vals = ''
if isinstance(vals, basestring): if isinstance(vals, string_or_bytes):
vals = vals.split(',') if locvalue == 'collections' else [vals] vals = vals.split(',') if locvalue == 'collections' else [vals]
if _match(query, vals, m, use_primary_find_in_search=upf): if _match(query, vals, m, use_primary_find_in_search=upf):
matches.add(index) matches.add(index)

View File

@ -11,7 +11,7 @@ import sys
import time import time
from threading import Thread from threading import Thread
from polyglot.builtins import reraise, unicode_type from polyglot.builtins import reraise, unicode_type, string_or_bytes
from PyQt5.Qt import QEventLoop from PyQt5.Qt import QEventLoop
from calibre import force_unicode from calibre import force_unicode
@ -68,7 +68,7 @@ def get_initial_dir(name, title, default_dir, no_save_dir):
return ensure_dir(process_path(default_dir)) return ensure_dir(process_path(default_dir))
key = dialog_name(name, title) key = dialog_name(name, title)
saved = dynamic.get(key) saved = dynamic.get(key)
if not isinstance(saved, basestring): if not isinstance(saved, string_or_bytes):
saved = None saved = None
if saved and os.path.isdir(saved): if saved and os.path.isdir(saved):
return ensure_dir(process_path(saved)) return ensure_dir(process_path(saved))

View File

@ -10,7 +10,7 @@ from PyQt5.Qt import (
from calibre.ebooks.lrf.fonts import LIBERATION_FONT_MAP from calibre.ebooks.lrf.fonts import LIBERATION_FONT_MAP
from calibre.ebooks.BeautifulSoup import Tag from calibre.ebooks.BeautifulSoup import Tag
from calibre.ebooks.hyphenate import hyphenate_word from calibre.ebooks.hyphenate import hyphenate_word
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
WEIGHT_MAP = lambda wt : int((wt/10.)-1) WEIGHT_MAP = lambda wt : int((wt/10.)-1)
NULL = lambda a, b: a NULL = lambda a, b: a
@ -223,7 +223,7 @@ class TextBlock(object):
open_containers = collections.deque() open_containers = collections.deque()
self.in_para = False self.in_para = False
for i in tb.content: for i in tb.content:
if isinstance(i, basestring): if isinstance(i, string_or_bytes):
self.process_text(i) self.process_text(i)
elif i is None: elif i is None:
if len(open_containers) > 0: if len(open_containers) > 0:

View File

@ -22,6 +22,7 @@ from calibre.gui2.widgets2 import Dialog
from calibre.gui2.progress_indicator import ProgressIndicator from calibre.gui2.progress_indicator import ProgressIndicator
from calibre.utils.config import JSONConfig from calibre.utils.config import JSONConfig
from calibre.utils.icu import numeric_sort_key as sort_key from calibre.utils.icu import numeric_sort_key as sort_key
from polyglot.builtins import string_or_bytes
ENTRY_ROLE = Qt.UserRole ENTRY_ROLE = Qt.UserRole
@ -197,7 +198,7 @@ else:
def entry_to_item(entry, parent): def entry_to_item(entry, parent):
icon_path = entry.get('Icon') or I('blank.png') icon_path = entry.get('Icon') or I('blank.png')
if not isinstance(icon_path, basestring): if not isinstance(icon_path, string_or_bytes):
icon_path = I('blank.png') icon_path = I('blank.png')
ans = QListWidgetItem(QIcon(icon_path), entry.get('Name') or _('Unknown'), parent) ans = QListWidgetItem(QIcon(icon_path), entry.get('Name') or _('Unknown'), parent)
ans.setData(ENTRY_ROLE, entry) ans.setData(ENTRY_ROLE, entry)

View File

@ -14,7 +14,7 @@ from PyQt5.Qt import (QWidget, pyqtSignal, QCheckBox, QAbstractSpinBox,
from calibre.customize.ui import preferences_plugins from calibre.customize.ui import preferences_plugins
from calibre.utils.config import ConfigProxy from calibre.utils.config import ConfigProxy
from calibre.gui2.complete2 import EditWithComplete from calibre.gui2.complete2 import EditWithComplete
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class AbortCommit(Exception): class AbortCommit(Exception):
@ -144,7 +144,7 @@ class Setting(object):
else: else:
self.gui_obj.clear() self.gui_obj.clear()
for x in choices: for x in choices:
if isinstance(x, basestring): if isinstance(x, string_or_bytes):
x = (x, x) x = (x, x)
self.gui_obj.addItem(x[0], (x[1])) self.gui_obj.addItem(x[0], (x[1]))
self.set_gui_val(self.get_config_val(default=False)) self.set_gui_val(self.get_config_val(default=False))

View File

@ -11,7 +11,7 @@ from PyQt5.Qt import QFileDialog, QObject
from calibre.gui2.linux_file_dialogs import dialog_name, image_extensions from calibre.gui2.linux_file_dialogs import dialog_name, image_extensions
from calibre.utils.filenames import expanduser from calibre.utils.filenames import expanduser
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
def select_initial_dir(q): def select_initial_dir(q):
@ -71,7 +71,7 @@ class FileDialog(QObject):
else: else:
initial_dir = dynamic.get(self.dialog_name, initial_dir = dynamic.get(self.dialog_name,
expanduser(default_dir)) expanduser(default_dir))
if not isinstance(initial_dir, basestring): if not isinstance(initial_dir, string_or_bytes):
initial_dir = expanduser(default_dir) initial_dir = expanduser(default_dir)
if not initial_dir or (not os.path.exists(initial_dir) and not ( if not initial_dir or (not os.path.exists(initial_dir) and not (
mode == QFileDialog.AnyFile and (no_save_dir or combine_file_and_saved_dir))): mode == QFileDialog.AnyFile and (no_save_dir or combine_file_and_saved_dir))):

View File

@ -20,7 +20,7 @@ from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.gui2.dialogs.saved_search_editor import SavedSearchEditor from calibre.gui2.dialogs.saved_search_editor import SavedSearchEditor
from calibre.gui2.dialogs.search import SearchDialog from calibre.gui2.dialogs.search import SearchDialog
from calibre.utils.icu import primary_sort_key from calibre.utils.icu import primary_sort_key
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
QT_HIDDEN_CLEAR_ACTION = '_q_qlineeditclearaction' QT_HIDDEN_CLEAR_ACTION = '_q_qlineeditclearaction'
@ -183,7 +183,7 @@ class SearchBox2(QComboBox): # {{{
self.setFocus(Qt.OtherFocusReason) self.setFocus(Qt.OtherFocusReason)
def search_done(self, ok): def search_done(self, ok):
if isinstance(ok, basestring): if isinstance(ok, string_or_bytes):
self.setToolTip(ok) self.setToolTip(ok)
ok = False ok = False
if not unicode_type(self.currentText()).strip(): if not unicode_type(self.currentText()).strip():

View File

@ -70,7 +70,7 @@ from calibre.utils.config import JSONConfig
from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import numeric_sort_key
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
from calibre.utils.tdir_in_cache import tdir_in_cache from calibre.utils.tdir_in_cache import tdir_in_cache
from polyglot.builtins import iteritems from polyglot.builtins import iteritems, string_or_bytes
_diff_dialogs = [] _diff_dialogs = []
last_used_transform_rules = [] last_used_transform_rules = []
@ -1337,7 +1337,7 @@ class Boss(QObject):
@in_thread_job @in_thread_job
def export_requested(self, name_or_names, path): def export_requested(self, name_or_names, path):
if isinstance(name_or_names, basestring): if isinstance(name_or_names, string_or_bytes):
return self.export_file(name_or_names, path) return self.export_file(name_or_names, path)
for name in name_or_names: for name in name_or_names:
dest = os.path.abspath(os.path.join(path, name)) dest = os.path.abspath(os.path.join(path, name))

View File

@ -24,11 +24,11 @@ from calibre.gui2.tweak_book.editor import SPELL_PROPERTY, LINK_PROPERTY, TAG_NA
from calibre.gui2.tweak_book.editor.help import help_url from calibre.gui2.tweak_book.editor.help import help_url
from calibre.gui2.tweak_book.editor.text import TextEdit from calibre.gui2.tweak_book.editor.text import TextEdit
from calibre.utils.icu import utf16_length from calibre.utils.icu import utf16_length
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
def create_icon(text, palette=None, sz=None, divider=2, fill='white'): def create_icon(text, palette=None, sz=None, divider=2, fill='white'):
if isinstance(fill, basestring): if isinstance(fill, string_or_bytes):
fill = QColor(fill) fill = QColor(fill)
sz = sz or int(math.ceil(tprefs['toolbar_icon_size'] * QApplication.instance().devicePixelRatio())) sz = sz or int(math.ceil(tprefs['toolbar_icon_size'] * QApplication.instance().devicePixelRatio()))
if palette is None: if palette is None:

View File

@ -53,7 +53,7 @@ from calibre.gui2.dbus_export.widgets import factory
from calibre.gui2.open_with import register_keyboard_shortcuts from calibre.gui2.open_with import register_keyboard_shortcuts
from calibre.library import current_library_name from calibre.library import current_library_name
from calibre.srv.library_broker import GuiLibraryBroker from calibre.srv.library_broker import GuiLibraryBroker
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class Listener(Thread): # {{{ class Listener(Thread): # {{{
@ -615,7 +615,7 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{
self.tags_view.recount() self.tags_view.recount()
def handle_cli_args(self, args): def handle_cli_args(self, args):
if isinstance(args, basestring): if isinstance(args, string_or_bytes):
args = [args] args = [args]
files = [os.path.abspath(p) for p in args if not os.path.isdir(p) and os.access(p, os.R_OK)] files = [os.path.abspath(p) for p in args if not os.path.isdir(p) and os.access(p, os.R_OK)]
if files: if files:

View File

@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
import math, json import math, json
from base64 import b64encode from base64 import b64encode
from functools import partial from functools import partial
from polyglot.builtins import map, unicode_type from polyglot.builtins import map, unicode_type, string_or_bytes
from PyQt5.Qt import ( from PyQt5.Qt import (
QSize, QSizePolicy, QUrl, Qt, QPainter, QPalette, QBrush, QSize, QSizePolicy, QUrl, Qt, QPainter, QPalette, QBrush,
@ -1169,12 +1169,12 @@ class DocumentView(QWebView): # {{{
old_pos = (self.document.xpos if self.document.in_paged_mode else old_pos = (self.document.xpos if self.document.in_paged_mode else
self.document.ypos) self.document.ypos)
if self.document.in_paged_mode: if self.document.in_paged_mode:
if isinstance(pos, basestring): if isinstance(pos, string_or_bytes):
self.document.jump_to_anchor(pos) self.document.jump_to_anchor(pos)
else: else:
self.document.scroll_fraction = pos self.document.scroll_fraction = pos
else: else:
if isinstance(pos, basestring): if isinstance(pos, string_or_bytes):
self.document.jump_to_anchor(pos) self.document.jump_to_anchor(pos)
else: else:
if pos >= 1: if pos >= 1:

View File

@ -9,6 +9,7 @@ from threading import Thread
from uuid import uuid4 from uuid import uuid4
from PyQt5.Qt import pyqtSignal, QEventLoop, Qt from PyQt5.Qt import pyqtSignal, QEventLoop, Qt
from polyglot.builtins import string_or_bytes
is64bit = sys.maxsize > (1 << 32) is64bit = sys.maxsize > (1 << 32)
base = sys.extensions_location if hasattr(sys, 'new_app_layout') else os.path.dirname(sys.executable) base = sys.extensions_location if hasattr(sys, 'new_app_layout') else os.path.dirname(sys.executable)
@ -76,7 +77,7 @@ def serialize_file_types(file_types):
buf.append(struct.pack(b'=H%ds' % len(x), len(x), x)) buf.append(struct.pack(b'=H%ds' % len(x), len(x), x))
for name, extensions in file_types: for name, extensions in file_types:
add(name or _('Files')) add(name or _('Files'))
if isinstance(extensions, basestring): if isinstance(extensions, string_or_bytes):
extensions = extensions.split() extensions = extensions.split()
add('; '.join('*.' + ext.lower() for ext in extensions)) add('; '.join('*.' + ext.lower() for ext in extensions))
return b''.join(buf) return b''.join(buf)

View File

@ -20,7 +20,7 @@ from calibre.db.search import CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH, _match
from calibre.ebooks.metadata import title_sort, author_to_author_sort from calibre.ebooks.metadata import title_sort, author_to_author_sort
from calibre.ebooks.metadata.opf2 import metadata_to_opf from calibre.ebooks.metadata.opf2 import metadata_to_opf
from calibre import prints, force_unicode from calibre import prints, force_unicode
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class MetadataBackup(Thread): # {{{ class MetadataBackup(Thread): # {{{
@ -793,7 +793,7 @@ class ResultCache(SearchQueryParser): # {{{
continue continue
if q == 'true' and matchkind == CONTAINS_MATCH: if q == 'true' and matchkind == CONTAINS_MATCH:
if isinstance(item[loc], basestring): if isinstance(item[loc], string_or_bytes):
if item[loc].strip() == '': if item[loc].strip() == '':
continue continue
matches.add(item[0]) matches.add(item[0])

View File

@ -15,6 +15,7 @@ from calibre.library.catalogs import FIELDS, TEMPLATE_ALLOWED_FIELDS
from calibre.customize.conversion import DummyReporter from calibre.customize.conversion import DummyReporter
from calibre.constants import preferred_encoding from calibre.constants import preferred_encoding
from calibre.ebooks.metadata import format_isbn from calibre.ebooks.metadata import format_isbn
from polyglot.builtins import string_or_bytes
class BIBTEX(CatalogPlugin): class BIBTEX(CatalogPlugin):
@ -212,13 +213,13 @@ class BIBTEX(CatalogPlugin):
bibtex_entry.append(u'year = "%s"' % item.year) bibtex_entry.append(u'year = "%s"' % item.year)
bibtex_entry.append(u'month = "%s"' % bibtexdict.utf8ToBibtex(strftime("%b", item))) bibtex_entry.append(u'month = "%s"' % bibtexdict.utf8ToBibtex(strftime("%b", item)))
elif field.startswith('#') and isinstance(item, basestring): elif field.startswith('#') and isinstance(item, string_or_bytes):
bibtex_entry.append(u'custom_%s = "%s"' % (field[1:], bibtex_entry.append(u'custom_%s = "%s"' % (field[1:],
bibtexdict.utf8ToBibtex(item))) bibtexdict.utf8ToBibtex(item)))
elif isinstance(item, basestring): elif isinstance(item, string_or_bytes):
# elif field in ['title', 'publisher', 'cover', 'uuid', 'ondevice', # elif field in ['title', 'publisher', 'cover', 'uuid', 'ondevice',
# 'author_sort', 'series', 'title_sort'] : # 'author_sort', 'series', 'title_sort'] :
bibtex_entry.append(u'%s = "%s"' % (field, bibtexdict.utf8ToBibtex(item))) bibtex_entry.append(u'%s = "%s"' % (field, bibtexdict.utf8ToBibtex(item)))
bibtex_entry = u',\n '.join(bibtex_entry) bibtex_entry = u',\n '.join(bibtex_entry)

View File

@ -48,7 +48,7 @@ from calibre.db.lazy import FormatMetadata, FormatsList
from calibre.db.categories import Tag, CATEGORY_SORTS from calibre.db.categories import Tag, CATEGORY_SORTS
from calibre.utils.localization import (canonicalize_lang, from calibre.utils.localization import (canonicalize_lang,
calibre_langcode_to_name) calibre_langcode_to_name)
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
copyfile = os.link if hasattr(os, 'link') else shutil.copyfile copyfile = os.link if hasattr(os, 'link') else shutil.copyfile
SPOOL_SIZE = 30*1024*1024 SPOOL_SIZE = 30*1024*1024
@ -1115,7 +1115,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
def find_identical_books(self, mi): def find_identical_books(self, mi):
fuzzy_title_patterns = [(re.compile(pat, re.IGNORECASE) if fuzzy_title_patterns = [(re.compile(pat, re.IGNORECASE) if
isinstance(pat, basestring) else pat, repl) for pat, repl in isinstance(pat, string_or_bytes) else pat, repl) for pat, repl in
[ [
(r'[\[\](){}<>\'";,:#]', ''), (r'[\[\](){}<>\'";,:#]', ''),
(get_title_sort_pat(), ''), (get_title_sort_pat(), ''),
@ -1398,7 +1398,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
id_ = index if index_is_id else self.id(index) id_ = index if index_is_id else self.id(index)
raise NoSuchFormat('Record %d has no %s file'%(id_, fmt)) raise NoSuchFormat('Record %d has no %s file'%(id_, fmt))
if windows_atomic_move is not None: if windows_atomic_move is not None:
if not isinstance(dest, basestring): if not isinstance(dest, string_or_bytes):
raise Exception("Error, you must pass the dest as a path when" raise Exception("Error, you must pass the dest as a path when"
" using windows_atomic_move") " using windows_atomic_move")
if dest: if dest:
@ -1454,7 +1454,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
id = index if index_is_id else self.id(index) id = index if index_is_id else self.id(index)
path = os.path.join(self.library_path, self.path(id, index_is_id=True), 'cover.jpg') path = os.path.join(self.library_path, self.path(id, index_is_id=True), 'cover.jpg')
if windows_atomic_move is not None: if windows_atomic_move is not None:
if not isinstance(dest, basestring): if not isinstance(dest, string_or_bytes):
raise Exception("Error, you must pass the dest as a path when" raise Exception("Error, you must pass the dest as a path when"
" using windows_atomic_move") " using windows_atomic_move")
if os.access(path, os.R_OK) and dest and not samefile(dest, path): if os.access(path, os.R_OK) and dest and not samefile(dest, path):
@ -2334,7 +2334,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
# force_changes has no effect on cover manipulation # force_changes has no effect on cover manipulation
if mi.cover_data[1] is not None: if mi.cover_data[1] is not None:
doit(self.set_cover, id, mi.cover_data[1], commit=False) doit(self.set_cover, id, mi.cover_data[1], commit=False)
elif isinstance(mi.cover, basestring) and mi.cover: elif isinstance(mi.cover, string_or_bytes) and mi.cover:
if os.access(mi.cover, os.R_OK): if os.access(mi.cover, os.R_OK):
with lopen(mi.cover, 'rb') as f: with lopen(mi.cover, 'rb') as f:
raw = f.read() raw = f.read()
@ -2641,7 +2641,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
def set_pubdate(self, id, dt, notify=True, commit=True): def set_pubdate(self, id, dt, notify=True, commit=True):
if not dt: if not dt:
dt = UNDEFINED_DATE dt = UNDEFINED_DATE
if isinstance(dt, basestring): if isinstance(dt, string_or_bytes):
dt = parse_only_date(dt) dt = parse_only_date(dt)
self.conn.execute('UPDATE books SET pubdate=? WHERE id=?', (dt, id)) self.conn.execute('UPDATE books SET pubdate=? WHERE id=?', (dt, id))
self.data.set(id, self.FIELD_MAP['pubdate'], dt, row_is_id=True) self.data.set(id, self.FIELD_MAP['pubdate'], dt, row_is_id=True)

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
from functools import partial from functools import partial
from polyglot.builtins import unicode_type, zip from polyglot.builtins import unicode_type, zip, string_or_bytes
from itertools import cycle from itertools import cycle
from calibre import force_unicode from calibre import force_unicode
@ -107,7 +107,7 @@ def book_to_json(ctx, rd, db, book_id,
if (fm and fm['is_category'] and not fm['is_csp'] and if (fm and fm['is_category'] and not fm['is_csp'] and
key != 'formats' and fm['datatype'] != 'rating'): key != 'formats' and fm['datatype'] != 'rating'):
categories = mi.get(key) or [] categories = mi.get(key) or []
if isinstance(categories, basestring): if isinstance(categories, string_or_bytes):
categories = [categories] categories = [categories]
category_urls[key] = dbtags = {} category_urls[key] = dbtags = {}
for category in categories: for category in categories:

View File

@ -2,29 +2,31 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import (unicode_literals, division, absolute_import, from __future__ import absolute_import, division, print_function, unicode_literals
print_function)
from functools import partial from functools import partial
from urllib import urlencode
from lxml.html import tostring from lxml.html import tostring
from lxml.html.builder import E as E_ from lxml.html.builder import E as E_
from urllib import urlencode
from calibre import strftime from calibre import strftime
from calibre.constants import __appname__ from calibre.constants import __appname__
from calibre.db.view import sanitize_sort_field_name from calibre.db.view import sanitize_sort_field_name
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.srv.content import get, book_filename from calibre.srv.content import book_filename, get
from calibre.srv.errors import HTTPRedirect, HTTPBadRequest from calibre.srv.errors import HTTPBadRequest, HTTPRedirect
from calibre.srv.routes import endpoint from calibre.srv.routes import endpoint
from calibre.srv.utils import get_library_data, http_date from calibre.srv.utils import get_library_data, http_date
from calibre.utils.cleantext import clean_xml_chars from calibre.utils.cleantext import clean_xml_chars
from calibre.utils.date import timestampfromdt, dt_as_local, is_date_undefined from calibre.utils.date import dt_as_local, is_date_undefined, timestampfromdt
from polyglot.builtins import string_or_bytes
# /mobile {{{ # /mobile {{{
def clean(x): def clean(x):
if isinstance(x, basestring): if isinstance(x, string_or_bytes):
x = clean_xml_chars(x) x = clean_xml_chars(x)
return x return x

View File

@ -234,7 +234,7 @@ class OptionSet(object):
def serialize_opt(self, val): def serialize_opt(self, val):
if val is val is True or val is False or val is None or \ if val is val is True or val is False or val is None or \
isinstance(val, (int, float, long, basestring)): isinstance(val, (int, float, long, bytes, unicode_type)):
return repr(val) return repr(val)
pickle = cPickle.dumps(val, -1) pickle = cPickle.dumps(val, -1)
return 'cPickle.loads(%s)'%repr(pickle) return 'cPickle.loads(%s)'%repr(pickle)

View File

@ -23,6 +23,7 @@ from calibre.ptempfile import TemporaryDirectory
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from calibre.utils.filenames import atomic_rename from calibre.utils.filenames import atomic_rename
from calibre.utils.imghdr import what from calibre.utils.imghdr import what
from polyglot.builtins import string_or_bytes
# Utilities {{{ # Utilities {{{
imageops, imageops_err = plugins['imageops'] imageops, imageops_err = plugins['imageops']
@ -436,7 +437,7 @@ def quantize_image(img, max_colors=256, dither=True, palette=''):
img = image_from_data(img) img = image_from_data(img)
if img.hasAlphaChannel(): if img.hasAlphaChannel():
img = blend_image(img) img = blend_image(img)
if palette and isinstance(palette, basestring): if palette and isinstance(palette, string_or_bytes):
palette = palette.split() palette = palette.split()
return imageops.quantize(img, max_colors, dither, [QColor(x).rgb() for x in palette]) return imageops.quantize(img, max_colors, dither, [QColor(x).rgb() for x in palette])

View File

@ -7,6 +7,7 @@ from __future__ import (unicode_literals, division, absolute_import,
from struct import unpack, error from struct import unpack, error
import os import os
from calibre.utils.speedups import ReadOnlyFileBuffer from calibre.utils.speedups import ReadOnlyFileBuffer
from polyglot.builtins import string_or_bytes
""" Recognize image file formats and sizes based on their first few bytes.""" """ Recognize image file formats and sizes based on their first few bytes."""
@ -16,7 +17,7 @@ HSIZE = 120
def what(file, h=None): def what(file, h=None):
' Recognize image headers ' ' Recognize image headers '
if h is None: if h is None:
if isinstance(file, basestring): if isinstance(file, string_or_bytes):
with lopen(file, 'rb') as f: with lopen(file, 'rb') as f:
h = f.read(HSIZE) h = f.read(HSIZE)
else: else:

View File

@ -12,7 +12,7 @@ from functools import partial
from calibre.constants import iswindows, isosx, isfrozen, filesystem_encoding from calibre.constants import iswindows, isosx, isfrozen, filesystem_encoding
from calibre.utils.config import prefs from calibre.utils.config import prefs
from calibre.ptempfile import PersistentTemporaryFile, base_dir from calibre.ptempfile import PersistentTemporaryFile, base_dir
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
if iswindows: if iswindows:
import win32process import win32process
@ -186,7 +186,7 @@ class Worker(object):
_cwd = cwd _cwd = cwd
if priority is None: if priority is None:
priority = prefs['worker_process_priority'] priority = prefs['worker_process_priority']
cmd = [exe] if isinstance(exe, basestring) else exe cmd = [exe] if isinstance(exe, string_or_bytes) else exe
args = { args = {
'env' : env, 'env' : env,
'cwd' : _cwd, 'cwd' : _cwd,

View File

@ -21,6 +21,7 @@ from calibre.utils.ipc.worker import PARALLEL_FUNCS
from calibre import detect_ncpus as cpu_count from calibre import detect_ncpus as cpu_count
from calibre.constants import iswindows, DEBUG, islinux from calibre.constants import iswindows, DEBUG, islinux
from calibre.ptempfile import base_dir from calibre.ptempfile import base_dir
from polyglot.builtins import string_or_bytes
_counter = 0 _counter = 0
@ -216,7 +217,7 @@ class Server(Thread):
'CALIBRE_WORKER_RESULT' : hexlify(rfile.encode('utf-8')), 'CALIBRE_WORKER_RESULT' : hexlify(rfile.encode('utf-8')),
} }
cw = self.do_launch(env, gui, redirect_output, rfile, job_name=job_name) cw = self.do_launch(env, gui, redirect_output, rfile, job_name=job_name)
if isinstance(cw, basestring): if isinstance(cw, string_or_bytes):
raise CriticalError('Failed to launch worker process:\n'+cw) raise CriticalError('Failed to launch worker process:\n'+cw)
if DEBUG: if DEBUG:
print('Worker Launch took:', time.time() - start) print('Worker Launch took:', time.time() - start)

View File

@ -16,7 +16,7 @@ from contextlib import closing
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.utils.ipc.launch import Worker from calibre.utils.ipc.launch import Worker
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class WorkerError(Exception): class WorkerError(Exception):
@ -164,7 +164,7 @@ def start_pipe_worker(command, env=None, priority='normal', **process_args):
args['close_fds'] = True args['close_fds'] = True
exe = w.executable exe = w.executable
cmd = [exe] if isinstance(exe, basestring) else exe cmd = [exe] if isinstance(exe, string_or_bytes) else exe
p = subprocess.Popen(cmd + ['--pipe-worker', command], **args) p = subprocess.Popen(cmd + ['--pipe-worker', command], **args)
return p return p

View File

@ -13,6 +13,7 @@ from calibre import walk, guess_type, prints, force_unicode
from calibre.constants import filesystem_encoding, cache_dir from calibre.constants import filesystem_encoding, cache_dir
from calibre.utils.icu import numeric_sort_key as sort_key from calibre.utils.icu import numeric_sort_key as sort_key
from calibre.utils.localization import canonicalize_lang, get_lang from calibre.utils.localization import canonicalize_lang, get_lang
from polyglot.builtins import string_or_bytes
def parse_localized_key(key): def parse_localized_key(key):
@ -73,6 +74,7 @@ def parse_desktop_file(path):
if 'Exec' in ans and 'MimeType' in ans and 'Name' in ans: if 'Exec' in ans and 'MimeType' in ans and 'Name' in ans:
return ans return ans
icon_data = None icon_data = None
@ -197,7 +199,7 @@ def find_programs(extensions):
data['Icon'] = icon data['Icon'] = icon
else: else:
data.pop('Icon') data.pop('Icon')
if not isinstance(data.get('Icon'), basestring): if not isinstance(data.get('Icon'), string_or_bytes):
data.pop('Icon', None) data.pop('Icon', None)
for k in ('Name', 'GenericName', 'Comment'): for k in ('Name', 'GenericName', 'Comment'):
val = data.get(k) val = data.get(k)

View File

@ -11,7 +11,7 @@ from collections import defaultdict
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import numeric_sort_key
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
application_locations = ('/Applications', '~/Applications', '~/Desktop') application_locations = ('/Applications', '~/Applications', '~/Desktop')
@ -240,7 +240,7 @@ def get_extensions_from_utis(utis, plist):
for decl in plist.get(key, ()): for decl in plist.get(key, ()):
if isinstance(decl, dict): if isinstance(decl, dict):
uti = decl.get('UTTypeIdentifier') uti = decl.get('UTTypeIdentifier')
if isinstance(uti, basestring): if isinstance(uti, string_or_bytes):
spec = decl.get('UTTypeTagSpecification') spec = decl.get('UTTypeTagSpecification')
if isinstance(spec, dict): if isinstance(spec, dict):
ext = spec.get('public.filename-extension') ext = spec.get('public.filename-extension')
@ -287,10 +287,10 @@ def get_bundle_data(path):
extensions |= get_extensions_from_utis(utis, plist) extensions |= get_extensions_from_utis(utis, plist)
else: else:
for ext in dtype.get('CFBundleTypeExtensions', ()): for ext in dtype.get('CFBundleTypeExtensions', ()):
if isinstance(ext, basestring): if isinstance(ext, string_or_bytes):
extensions.add(ext.lower()) extensions.add(ext.lower())
for mt in dtype.get('CFBundleTypeMIMETypes', ()): for mt in dtype.get('CFBundleTypeMIMETypes', ()):
if isinstance(mt, basestring): if isinstance(mt, string_or_bytes):
for ext in mimetypes.guess_all_extensions(mt, strict=False): for ext in mimetypes.guess_all_extensions(mt, strict=False):
extensions.add(ext.lower()) extensions.add(ext.lower())
return ans return ans

View File

@ -7,6 +7,7 @@ __author__ = "Andrew Dalke <dalke@dalkescientific.com>"
_generator_name = __name__ + "-" + ".".join(map(str, __version__)) _generator_name = __name__ + "-" + ".".join(map(str, __version__))
import datetime import datetime
from polyglot.builtins import string_or_bytes
# Could make this the base class; will need to add 'publish' # Could make this the base class; will need to add 'publish'
@ -32,7 +33,7 @@ class WriteXmlMixin:
def _element(handler, name, obj, d={}): def _element(handler, name, obj, d={}):
if isinstance(obj, basestring) or obj is None: if isinstance(obj, string_or_bytes) or obj is None:
# special-case handling to make the API easier # special-case handling to make the API easier
# to use for the common case. # to use for the common case.
handler.startElement(name, d) handler.startElement(name, d)
@ -373,7 +374,7 @@ class RSS2(WriteXmlMixin):
_opt_element(handler, "lastBuildDate", lastBuildDate) _opt_element(handler, "lastBuildDate", lastBuildDate)
for category in self.categories: for category in self.categories:
if isinstance(category, basestring): if isinstance(category, string_or_bytes):
category = Category(category) category = Category(category)
category.publish(handler) category.publish(handler)
@ -454,7 +455,7 @@ class RSSItem(WriteXmlMixin):
_opt_element(handler, "author", self.author) _opt_element(handler, "author", self.author)
for category in self.categories: for category in self.categories:
if isinstance(category, basestring): if isinstance(category, string_or_bytes):
category = Category(category) category = Category(category)
category.publish(handler) category.publish(handler)

View File

@ -52,7 +52,7 @@ from email.base64mime import encode as encode_base64
from sys import stderr from sys import stderr
from functools import partial from functools import partial
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
__all__ = ["SMTPException", "SMTPServerDisconnected", "SMTPResponseException", __all__ = ["SMTPException", "SMTPServerDisconnected", "SMTPResponseException",
"SMTPSenderRefused", "SMTPRecipientsRefused", "SMTPDataError", "SMTPSenderRefused", "SMTPRecipientsRefused", "SMTPDataError",
@ -766,7 +766,7 @@ class SMTP:
self.rset() self.rset()
raise SMTPSenderRefused(code, resp, from_addr) raise SMTPSenderRefused(code, resp, from_addr)
senderrs = {} senderrs = {}
if isinstance(to_addrs, basestring): if isinstance(to_addrs, string_or_bytes):
to_addrs = [to_addrs] to_addrs = [to_addrs]
for each in to_addrs: for each in to_addrs:
(code, resp) = self.rcpt(each, rcpt_options) (code, resp) = self.rcpt(each, rcpt_options)

View File

@ -13,6 +13,7 @@ from io import BytesIO
from calibre.constants import filesystem_encoding, iswindows from calibre.constants import filesystem_encoding, iswindows
from calibre.ptempfile import PersistentTemporaryFile, TemporaryDirectory from calibre.ptempfile import PersistentTemporaryFile, TemporaryDirectory
from polyglot.builtins import string_or_bytes
def as_unicode(x): def as_unicode(x):
@ -28,7 +29,7 @@ class StreamAsPath(object):
def __enter__(self): def __enter__(self):
self.temppath = None self.temppath = None
if isinstance(self.stream, basestring): if isinstance(self.stream, string_or_bytes):
return as_unicode(self.stream) return as_unicode(self.stream)
name = getattr(self.stream, 'name', None) name = getattr(self.stream, 'name', None)
if name and os.access(name, os.R_OK): if name and os.access(name, os.R_OK):

View File

@ -11,7 +11,7 @@ from tempfile import SpooledTemporaryFile
from calibre import sanitize_file_name2 from calibre import sanitize_file_name2
from calibre.constants import filesystem_encoding from calibre.constants import filesystem_encoding
from calibre.ebooks.chardet import detect from calibre.ebooks.chardet import detect
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
try: try:
import zlib # We may need its compression method import zlib # We may need its compression method
@ -747,7 +747,7 @@ class ZipFile:
self.comment = '' self.comment = ''
# Check if we were passed a file-like object # Check if we were passed a file-like object
if isinstance(file, basestring): if isinstance(file, string_or_bytes):
self._filePassed = 0 self._filePassed = 0
self.filename = file self.filename = file
modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'} modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}

View File

@ -12,7 +12,7 @@ from calibre.utils.logging import default_log
from calibre import entity_to_unicode, strftime, force_unicode from calibre import entity_to_unicode, strftime, force_unicode
from calibre.utils.date import dt_factory, utcnow, local_tz from calibre.utils.date import dt_factory, utcnow, local_tz
from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class Article(object): class Article(object):
@ -21,7 +21,7 @@ class Article(object):
from lxml import html from lxml import html
self.downloaded = False self.downloaded = False
self.id = id self.id = id
if not title or not isinstance(title, basestring): if not title or not isinstance(title, string_or_bytes):
title = _('Unknown') title = _('Unknown')
title = force_unicode(title, 'utf-8') title = force_unicode(title, 'utf-8')
self._title = clean_xml_chars(title).strip() self._title = clean_xml_chars(title).strip()

View File

@ -31,7 +31,7 @@ from calibre.utils.icu import numeric_sort_key
from calibre.utils.img import save_cover_data_to, add_borders_to_image, image_to_data from calibre.utils.img import save_cover_data_to, add_borders_to_image, image_to_data
from calibre.utils.localization import canonicalize_lang from calibre.utils.localization import canonicalize_lang
from calibre.utils.logging import ThreadSafeWrapper from calibre.utils.logging import ThreadSafeWrapper
from polyglot.builtins import unicode_type from polyglot.builtins import unicode_type, string_or_bytes
class LoginFailed(ValueError): class LoginFailed(ValueError):
@ -1612,7 +1612,7 @@ class BasicNewsRecipe(Recipe):
feeds = self.get_feeds() feeds = self.get_feeds()
parsed_feeds = [] parsed_feeds = []
for obj in feeds: for obj in feeds:
if isinstance(obj, basestring): if isinstance(obj, string_or_bytes):
title, url = None, obj title, url = None, obj
else: else:
title, url = obj title, url = obj
@ -1659,7 +1659,7 @@ class BasicNewsRecipe(Recipe):
''' '''
if tag is None: if tag is None:
return '' return ''
if isinstance(tag, basestring): if isinstance(tag, string_or_bytes):
return tag return tag
if callable(getattr(tag, 'xpath', None)) and not hasattr(tag, 'contents'): # a lxml tag if callable(getattr(tag, 'xpath', None)) and not hasattr(tag, 'contents'): # a lxml tag
from lxml.etree import tostring from lxml.etree import tostring

View File

@ -1580,7 +1580,7 @@ dl.notes dd:last-of-type { page-break-after: avoid }
""" """
self.lines = [] self.lines = []
self._wfunc = self._wlines self._wfunc = self._wlines
if isinstance(odffile, basestring) \ if isinstance(odffile, (bytes, type(u'')) \
or hasattr(odffile, 'read'): # Added by Kovid or hasattr(odffile, 'read'): # Added by Kovid
self.document = load(odffile) self.document = load(odffile)
else: else:

View File

@ -62,7 +62,7 @@ class UserFields(object):
self.document = None self.document = None
def loaddoc(self): def loaddoc(self):
if isinstance(self.src_file, basestring): if isinstance(self.src_file, (bytes, type(u'')):
# src_file is a filename, check if it is a zip-file # src_file is a filename, check if it is a zip-file
if not zipfile.is_zipfile(self.src_file): if not zipfile.is_zipfile(self.src_file):
raise TypeError("%s is no odt file." % self.src_file) raise TypeError("%s is no odt file." % self.src_file)
@ -166,4 +166,3 @@ class UserFields(object):
else: else:
f.setAttribute('value', value) f.setAttribute('value', value)
self.savedoc() self.savedoc()

View File

@ -29,6 +29,7 @@ if is_py3:
codepoint_to_chr = chr codepoint_to_chr = chr
unicode_type = str unicode_type = str
string_or_bytes = str, bytes
def iteritems(d): def iteritems(d):
return iter(d.items()) return iter(d.items())
@ -53,6 +54,7 @@ else:
codepoint_to_chr = unichr codepoint_to_chr = unichr
unicode_type = unicode unicode_type = unicode
string_or_bytes = unicode, bytes
def iteritems(d): def iteritems(d):
return d.iteritems() return d.iteritems()