1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-25 16:01:29 +02:00

Removing couple of "buildins" polyglot types

This commit is contained in:
2020-04-20 20:22:50 +02:00
parent eac0b98d6f
commit c867f0321b
36 changed files with 85 additions and 109 deletions
+1 -3
View File
@@ -1,7 +1,5 @@
import collections import collections
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
@@ -18,7 +16,7 @@ def is_iterable(obj):
Strings, however, should be considered as atomic values to look up, not Strings, however, should be considered as atomic values to look up, not
iterables. iterables.
""" """
return hasattr(obj, '__iter__') and not isinstance(obj, string_or_bytes) return hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes))
class OrderedSet(collections.MutableSet): class OrderedSet(collections.MutableSet):
+4 -5
View File
@@ -11,8 +11,7 @@ from ebook_converter import as_unicode
from ebook_converter.constants import ispy3 from ebook_converter.constants import ispy3
from ebook_converter.customize import (Plugin, numeric_version, platform, from ebook_converter.customize import (Plugin, numeric_version, platform,
InvalidPlugin, PluginNotFound) InvalidPlugin, PluginNotFound)
from ebook_converter.polyglot.builtins import (itervalues, string_or_bytes, from ebook_converter.polyglot.builtins import itervalues, reload
reload)
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -33,7 +32,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, string_or_bytes): if isinstance(names, (str, bytes)):
names = [names] names = [names]
ans = {} ans = {}
with zipfile.ZipFile(zfp) as zf: with zipfile.ZipFile(zfp) as zf:
@@ -64,11 +63,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, string_or_bytes): if isinstance(names, (str, bytes)):
names = [names] names = [names]
if ans is None: if ans is None:
ans = {} ans = {}
if isinstance(ans, string_or_bytes): if isinstance(ans, (str, bytes)):
ans = dict([(names[0], ans)]) ans = dict([(names[0], ans)])
ians = {} ians = {}
+2 -3
View File
@@ -13,7 +13,6 @@ from ebook_converter.utils.zipfile import ZipFile
from ebook_converter import (extract, walk, isbytestring, filesystem_encoding, from ebook_converter import (extract, walk, isbytestring, filesystem_encoding,
get_types_map) get_types_map)
from ebook_converter.constants import __version__ from ebook_converter.constants import __version__
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3' __license__ = 'GPL 3'
@@ -1023,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, string_or_bytes): if isinstance(ret, (str, 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):
@@ -1216,7 +1215,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, string_or_bytes): if isinstance(transform_css_rules, (str, 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,
+2 -2
View File
@@ -2,7 +2,7 @@ import re, random, unicodedata, numbers
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 ebook_converter.polyglot.builtins import iteritems, itervalues, string_or_bytes from ebook_converter.polyglot.builtins import iteritems, itervalues
from itertools import chain from itertools import chain
from PyQt5.Qt import ( from PyQt5.Qt import (
@@ -270,7 +270,7 @@ def format_fields(mi, prefs):
@contextmanager @contextmanager
def preserve_fields(obj, fields): def preserve_fields(obj, fields):
if isinstance(fields, string_or_bytes): if isinstance(fields, (str, 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}
@@ -11,7 +11,6 @@ from ebook_converter.ebooks.docx.writer.lists import ListsManager
from ebook_converter.ebooks.oeb.stylizer import Stylizer as Sz, Style as St from ebook_converter.ebooks.oeb.stylizer import Stylizer as Sz, Style as St
from ebook_converter.ebooks.oeb.base import XPath, barename from ebook_converter.ebooks.oeb.base import XPath, barename
from ebook_converter.utils.localization import lang_as_iso639_1 from ebook_converter.utils.localization import lang_as_iso639_1
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -527,7 +526,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), string_or_bytes): if isinstance(getattr(child, 'tag', None), (str, 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)
+2 -3
View File
@@ -15,7 +15,6 @@ from ebook_converter.utils.localization import lang_as_iso639_1
from ebook_converter.utils.xml_parse import safe_xml_fromstring from ebook_converter.utils.xml_parse import safe_xml_fromstring
from ebook_converter.utils.img import save_cover_data_to from ebook_converter.utils.img import save_cover_data_to
from ebook_converter.ebooks.oeb.base import urlnormalize from ebook_converter.ebooks.oeb.base import urlnormalize
from ebook_converter.polyglot.builtins import string_or_bytes
from ebook_converter.polyglot.binary import as_base64_unicode from ebook_converter.polyglot.binary import as_base64_unicode
@@ -408,9 +407,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, string_or_bytes) or namespace(elem_tree.tag) != XHTML_NS: if not isinstance(elem_tree.tag, (str, bytes)) or namespace(elem_tree.tag) != XHTML_NS:
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [] return []
+8 -8
View File
@@ -13,7 +13,7 @@ from ebook_converter.ebooks.oeb.base import (
XHTML, XHTML_NS, SVG_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize) XHTML, XHTML_NS, SVG_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize)
from ebook_converter.ebooks.oeb.stylizer import Stylizer from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.utils.logging import default_log from ebook_converter.utils.logging import default_log
from ebook_converter.polyglot.builtins import string_or_bytes, as_bytes from ebook_converter.polyglot.builtins import as_bytes
__license__ = 'GPL 3' __license__ = 'GPL 3'
@@ -94,7 +94,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, string_or_bytes): if not isinstance(el.tag, (str, bytes)):
continue continue
except: except:
continue continue
@@ -155,10 +155,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, string_or_bytes) \ if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in (XHTML_NS, SVG_NS): or namespace(elem.tag) not in (XHTML_NS, SVG_NS):
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) in (XHTML_NS, SVG_NS) \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
@@ -244,10 +244,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, string_or_bytes) \ if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in (XHTML_NS, SVG_NS): or namespace(elem.tag) not in (XHTML_NS, SVG_NS):
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) in (XHTML_NS, SVG_NS) \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
@@ -347,10 +347,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, string_or_bytes) \ if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) not in (XHTML_NS, SVG_NS): or namespace(elem.tag) not in (XHTML_NS, SVG_NS):
p = elem.getparent() p = elem.getparent()
if p is not None and isinstance(p.tag, string_or_bytes) and namespace(p.tag) in (XHTML_NS, SVG_NS) \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) in (XHTML_NS, SVG_NS) \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
@@ -37,7 +37,7 @@ from ebook_converter.ebooks.lrf.pylrs.pylrs import (
RuledLine, Span, Sub, Sup, TextBlock RuledLine, Span, Sub, Sup, TextBlock
) )
from ebook_converter.ptempfile import PersistentTemporaryFile from ebook_converter.ptempfile import PersistentTemporaryFile
from ebook_converter.polyglot.builtins import getcwd, itervalues, string_or_bytes from ebook_converter.polyglot.builtins import getcwd, itervalues
from ebook_converter.polyglot.urllib import unquote from ebook_converter.polyglot.urllib import unquote
from PIL import Image as PILImage from PIL import Image as PILImage
@@ -1118,7 +1118,7 @@ class HTMLConverter(object):
ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth'])) / 2) ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth'])) / 2)
for prop in ('topskip', 'footskip', 'sidemargin'): for prop in ('topskip', 'footskip', 'sidemargin'):
if isinstance(ans[prop], string_or_bytes): if isinstance(ans[prop], (str, bytes)):
ans[prop] = int(ans[prop]) ans[prop] = int(ans[prop])
if ans[prop] < 0: if ans[prop] < 0:
ans[prop] = 0 ans[prop] = 0
+2 -2
View File
@@ -4,7 +4,7 @@ from ebook_converter.ebooks.lrf.fonts import get_font
from ebook_converter.ebooks.lrf.pylrs.pylrs import TextBlock, Text, CR, Span, \ from ebook_converter.ebooks.lrf.pylrs.pylrs import TextBlock, Text, CR, Span, \
CharButton, Plot, Paragraph, \ CharButton, Plot, Paragraph, \
LrsTextTag LrsTextTag
from ebook_converter.polyglot.builtins import string_or_bytes, native_string_type from ebook_converter.polyglot.builtins import native_string_type
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -40,7 +40,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, string_or_bytes): elif isinstance(x, (str, 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:
+1 -2
View File
@@ -1,7 +1,6 @@
""" """
elements.py -- replacements and helpers for ElementTree elements.py -- replacements and helpers for ElementTree
""" """
from ebook_converter.polyglot.builtins import string_or_bytes
class ElementWriter(object): class ElementWriter(object):
@@ -25,7 +24,7 @@ class ElementWriter(object):
def _writeAttribute(self, f, name, value): def _writeAttribute(self, f, name, value):
f.write(' %s="' % str(name)) f.write(' %s="' % str(name))
if not isinstance(value, string_or_bytes): if not isinstance(value, (str, bytes)):
value = str(value) value = str(value)
value = self._encodeCdata(value) value = self._encodeCdata(value)
value = value.replace('"', '&quot;') value = value.replace('"', '&quot;')
+2 -2
View File
@@ -9,7 +9,7 @@ import codecs
import os import os
from .pylrfopt import tagListOptimizer from .pylrfopt import tagListOptimizer
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes from ebook_converter.polyglot.builtins import iteritems
PYLRF_VERSION = "1.0" PYLRF_VERSION = "1.0"
@@ -397,7 +397,7 @@ class LrfTag(object):
for f in self.format: for f in self.format:
if isinstance(f, dict): if isinstance(f, dict):
p = f[p] p = f[p]
elif isinstance(f, string_or_bytes): elif isinstance(f, (str, bytes)):
if isinstance(p, tuple): if isinstance(p, tuple):
writeString(lrf, struct.pack(f, *p)) writeString(lrf, struct.pack(f, *p))
else: else:
+8 -8
View File
@@ -51,7 +51,7 @@ DEFAULT_GENREADING = "fs" # default is yes to both lrf and lrs
from ebook_converter import __appname__, __version__ from ebook_converter import __appname__, __version__
from ebook_converter import entity_to_unicode from ebook_converter import entity_to_unicode
from ebook_converter.polyglot.builtins import string_or_bytes, iteritems, native_string_type from ebook_converter.polyglot.builtins import iteritems, native_string_type
class LrsError(Exception): class LrsError(Exception):
@@ -90,7 +90,7 @@ def ElementWithReading(tag, text, reading=False):
if text is None: if text is None:
readingText = "" readingText = ""
elif isinstance(text, string_or_bytes): elif isinstance(text, (str, bytes)):
readingText = text readingText = text
else: else:
# assumed to be a sequence of (name, sortas) # assumed to be a sequence of (name, sortas)
@@ -148,7 +148,7 @@ class Delegator(object):
""" """
for setting in d.getSettings(): for setting in d.getSettings():
if isinstance(setting, string_or_bytes): if isinstance(setting, (str, bytes)):
setting = (d, setting) setting = (d, setting)
delegates = \ delegates = \
self.delegatedSettingsDict.setdefault(setting[1], []) self.delegatedSettingsDict.setdefault(setting[1], [])
@@ -286,7 +286,7 @@ class LrsContainer(object):
(content.__class__.__name__, (content.__class__.__name__,
self.__class__.__name__)) self.__class__.__name__))
if convertText and isinstance(content, string_or_bytes): if convertText and isinstance(content, (str, bytes)):
content = Text(content) content = Text(content)
content.setParent(self) content.setParent(self)
@@ -580,14 +580,14 @@ 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, string_or_bytes): if isinstance(lrsFile, (str, 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, string_or_bytes): if isinstance(lrfFile, (str, bytes)):
lrfFile = open(lrfFile, "wb") lrfFile = open(lrfFile, "wb")
lrfWriter = LrfWriter(self.sourceencoding) lrfWriter = LrfWriter(self.sourceencoding)
@@ -1488,7 +1488,7 @@ class Paragraph(LrsContainer):
LrsContainer.__init__(self, [Text, CR, DropCaps, CharButton, LrsContainer.__init__(self, [Text, CR, DropCaps, CharButton,
LrsSimpleChar1, bytes, str]) LrsSimpleChar1, bytes, str])
if text is not None: if text is not None:
if isinstance(text, string_or_bytes): if isinstance(text, (str, bytes)):
text = Text(text) text = Text(text)
self.append(text) self.append(text)
@@ -1807,7 +1807,7 @@ class Span(LrsSimpleChar1, LrsContainer):
def __init__(self, text=None, **attrs): def __init__(self, text=None, **attrs):
LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, str]) LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, str])
if text is not None: if text is not None:
if isinstance(text, string_or_bytes): if isinstance(text, (str, bytes)):
text = Text(text) text = Text(text)
self.append(text) self.append(text)
+2 -2
View File
@@ -6,7 +6,7 @@ import re
from ebook_converter import force_unicode from ebook_converter import force_unicode
from ebook_converter.ebooks.metadata import MetaInformation from ebook_converter.ebooks.metadata import MetaInformation
from ebook_converter.polyglot.builtins import string_or_bytes, int_to_byte from ebook_converter.polyglot.builtins import int_to_byte
title_pat = re.compile(br'\{\\info.*?\{\\title(.*?)(?<!\\)\}', re.DOTALL) title_pat = re.compile(br'\{\\info.*?\{\\title(.*?)(?<!\\)\}', re.DOTALL)
author_pat = re.compile(br'\{\\info.*?\{\\author(.*?)(?<!\\)\}', re.DOTALL) author_pat = re.compile(br'\{\\info.*?\{\\author(.*?)(?<!\\)\}', re.DOTALL)
@@ -156,7 +156,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, string_or_bytes): if not isinstance(au, (str, bytes)):
au = ', '.join(au) au = ', '.join(au)
author = encode(au) author = encode(au)
md.append(r'{\author %s}'%(author,)) md.append(r'{\author %s}'%(author,))
+2 -2
View File
@@ -12,7 +12,7 @@ from ebook_converter.ebooks.metadata.book.base import Metadata
from ebook_converter.ebooks.metadata.opf2 import dump_dict from ebook_converter.ebooks.metadata.opf2 import dump_dict
from ebook_converter.utils.date import parse_date, isoformat, now from ebook_converter.utils.date import parse_date, isoformat, now
from ebook_converter.utils.localization import canonicalize_lang, lang_as_iso639_1 from ebook_converter.utils.localization import canonicalize_lang, lang_as_iso639_1
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -477,7 +477,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),
}): }):
val = mi.get(prop) or () val = mi.get(prop) or ()
if isinstance(val, string_or_bytes): if isinstance(val, (str, 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'):
+4 -5
View File
@@ -11,7 +11,6 @@ from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.ebooks.oeb.transforms.flatcss import KeyMapper from ebook_converter.ebooks.oeb.transforms.flatcss import KeyMapper
from ebook_converter.ebooks.mobi.utils import convert_color_for_font_tag from ebook_converter.ebooks.mobi.utils import convert_color_for_font_tag
from ebook_converter.utils.imghdr import identify from ebook_converter.utils.imghdr import identify
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -143,7 +142,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, string_or_bytes): if isinstance(ptsize, (str, bytes)):
return ptsize return ptsize
embase = self.profile.fbase embase = self.profile.fbase
if round(ptsize) < embase: if round(ptsize) < embase:
@@ -186,7 +185,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, string_or_bytes): if isinstance(indent, (str, bytes)):
indent = 0 indent = 0
if indent < 0 and abs(indent) < left: if indent < 0 and abs(indent) < left:
left += indent left += indent
@@ -307,7 +306,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, string_or_bytes): if isinstance(item, (str, bytes)):
if len(inline) == 0: if len(inline) == 0:
inline.text = (inline.text or '') + item inline.text = (inline.text or '') + item
else: else:
@@ -318,7 +317,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, string_or_bytes) \ if not isinstance(elem.tag, (str, bytes)) \
or namespace(elem.tag) != XHTML_NS: or namespace(elem.tag) != XHTML_NS:
return return
style = stylizer.style(elem) style = stylizer.style(elem)
@@ -9,7 +9,6 @@ from ebook_converter.ebooks.mobi.utils import is_guide_ref_start
from ebook_converter.ebooks.oeb.base import ( from ebook_converter.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 ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -306,7 +305,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, string_or_bytes) \ if not isinstance(elem.tag, (str, 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)
+2 -2
View File
@@ -15,7 +15,7 @@ from odf.namespaces import TEXTNS as odTEXTNS
from ebook_converter import CurrentDir, walk from ebook_converter import CurrentDir, walk
from ebook_converter.ebooks.oeb.base import _css_logger from ebook_converter.ebooks.oeb.base import _css_logger
from ebook_converter.utils.xml_parse import safe_xml_fromstring from ebook_converter.utils.xml_parse import safe_xml_fromstring
from ebook_converter.polyglot.builtins import string_or_bytes, getcwd, as_bytes from ebook_converter.polyglot.builtins import getcwd, as_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -250,7 +250,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, string_or_bytes) \ if isinstance(odffile, (str, 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:
+3 -3
View File
@@ -17,7 +17,7 @@ from ebook_converter import (isbytestring, as_unicode, get_types_map)
from ebook_converter.ebooks.oeb.parse_utils import barename, XHTML_NS, namespace, XHTML, parse_html, NotHTML from ebook_converter.ebooks.oeb.parse_utils import barename, XHTML_NS, namespace, XHTML, parse_html, NotHTML
from ebook_converter.utils.cleantext import clean_xml_chars from ebook_converter.utils.cleantext import clean_xml_chars
from ebook_converter.utils.short_uuid import uuid4 from ebook_converter.utils.short_uuid import uuid4
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes, itervalues from ebook_converter.polyglot.builtins import iteritems, itervalues
from ebook_converter.polyglot.urllib import unquote as urlunquote from ebook_converter.polyglot.urllib import unquote as urlunquote
@@ -1035,7 +1035,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, string_or_bytes): if not isinstance(data, (str, 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)
@@ -1296,7 +1296,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, string_or_bytes): if isinstance(linear, (str, 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
+3 -3
View File
@@ -5,7 +5,7 @@ from css_parser.css import PropertyValue
from css_parser import profile as cssprofiles, CSSParser from css_parser import profile as cssprofiles, CSSParser
from ebook_converter.tinycss.fonts3 import parse_font, serialize_font_family from ebook_converter.tinycss.fonts3 import parse_font, serialize_font_family
from ebook_converter.ebooks.oeb.base import css_text from ebook_converter.ebooks.oeb.base import css_text
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -125,10 +125,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'], string_or_bytes): if isinstance(ans['font-family'], (str, 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'], string_or_bytes): if not isinstance(ans['font-family'], (str, bytes)):
ans['font-family'] = serialize_font_family(ans['font-family']) ans['font-family'] = serialize_font_family(ans['font-family'])
return ans return ans
+3 -3
View File
@@ -6,7 +6,7 @@ from ebook_converter import xml_replace_entities, force_unicode
from ebook_converter.utils.xml_parse import safe_xml_fromstring from ebook_converter.utils.xml_parse import safe_xml_fromstring
from ebook_converter.constants import filesystem_encoding from ebook_converter.constants import filesystem_encoding
from ebook_converter.ebooks.chardet import xml_to_unicode, strip_encoding_declarations from ebook_converter.ebooks.chardet import xml_to_unicode, strip_encoding_declarations
from ebook_converter.polyglot.builtins import iteritems, itervalues, string_or_bytes from ebook_converter.polyglot.builtins import iteritems, itervalues
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -96,7 +96,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, string_or_bytes) and not len(x): # Leaf node if isinstance(x.tag, (str, 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'
@@ -294,7 +294,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, string_or_bytes) and \ if isinstance(elem.tag, (str, 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:
+3 -4
View File
@@ -1,5 +1,4 @@
import copy, os, re import copy, os, re
from ebook_converter.polyglot.builtins import string_or_bytes
import urllib.parse import urllib.parse
from ebook_converter.ebooks.oeb.base import barename, XPNSMAP, XPath, OPF, XHTML, OEB_DOCS from ebook_converter.ebooks.oeb.base import barename, XPNSMAP, XPath, OPF, XHTML, OEB_DOCS
@@ -393,9 +392,9 @@ def merge_html(container, names, master, insert_page_breaks=False):
first_child = '' first_child = ''
for first_child in children: for first_child in children:
if not isinstance(first_child, string_or_bytes): if not isinstance(first_child, (str, bytes)):
break break
if isinstance(first_child, string_or_bytes): if isinstance(first_child, (str, 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
@@ -431,7 +430,7 @@ def merge_html(container, names, master, insert_page_breaks=False):
a.set('href', '#' + amap[q]) a.set('href', '#' + amap[q])
for child in children: for child in children:
if isinstance(child, string_or_bytes): if isinstance(child, (str, 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))
@@ -16,7 +16,7 @@ from ebook_converter.ebooks.oeb.base import (XHTML, XHTML_NS, CSS_MIME, OEB_STYL
from ebook_converter.ebooks.oeb.stylizer import Stylizer from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.utils.filenames import ascii_filename, ascii_text from ebook_converter.utils.filenames import ascii_filename, ascii_text
from ebook_converter.utils.icu import numeric_sort_key from ebook_converter.utils.icu import numeric_sort_key
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -351,7 +351,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, recurse=True): def flatten_node(self, node, stylizer, names, styles, pseudo_styles, psize, item_id, recurse=True):
if not isinstance(node.tag, string_or_bytes) \ if not isinstance(node.tag, (str, bytes)) \
or namespace(node.tag) != XHTML_NS: or namespace(node.tag) != XHTML_NS:
return return
tag = barename(node.tag) tag = barename(node.tag)
@@ -6,7 +6,6 @@ from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS
from ebook_converter.ebooks.oeb.base import CSS_MIME from ebook_converter.ebooks.oeb.base import CSS_MIME
from ebook_converter.ebooks.oeb.base import namespace from ebook_converter.ebooks.oeb.base import namespace
from ebook_converter.ebooks.oeb.stylizer import Stylizer from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -95,7 +94,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, string_or_bytes) or \ if not isinstance(elem.tag, (str, bytes)) or \
namespace(elem.tag) != XHTML_NS: namespace(elem.tag) != XHTML_NS:
return return
children = list(elem) children = list(elem)
+1 -2
View File
@@ -5,7 +5,6 @@ Read the header data from a pdb file.
import re import re
import struct import struct
import time import time
from ebook_converter.polyglot.builtins import long_type
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -83,6 +82,6 @@ class PdbHeaderBuilder(object):
offset = 78 + (8 * nrecords) + 2 offset = 78 + (8 * nrecords) + 2
for id, record in enumerate(section_lengths): for id, record in enumerate(section_lengths):
out_stream.write(struct.pack('>LBBBB', long_type(offset), 0, 0, 0, 0)) out_stream.write(struct.pack('>LBBBB', int(offset), 0, 0, 0, 0))
offset += record offset += record
out_stream.write(b'\x00\x00') out_stream.write(b'\x00\x00')
+2 -3
View File
@@ -8,7 +8,6 @@ from functools import partial
from ebook_converter.ebooks.htmlz.oeb2html import OEB2HTML from ebook_converter.ebooks.htmlz.oeb2html import OEB2HTML
from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, rewrite_links from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace, rewrite_links
from ebook_converter.ebooks.oeb.stylizer import Stylizer from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3' __license__ = 'GPL 3'
@@ -110,10 +109,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, string_or_bytes) \ if not isinstance(elem.tag, (str, 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, string_or_bytes) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
+2 -2
View File
@@ -8,7 +8,7 @@ from ebook_converter.ebooks.metadata.opf2 import OPFCreator
from ebook_converter.ebooks.conversion.preprocess import DocAnalysis from ebook_converter.ebooks.conversion.preprocess import DocAnalysis
from ebook_converter.utils.cleantext import clean_ascii_chars from ebook_converter.utils.cleantext import clean_ascii_chars
from ebook_converter.polyglot.builtins import iteritems, long_type from ebook_converter.polyglot.builtins import iteritems
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -62,7 +62,7 @@ def split_txt(txt, epub_split_size_kb=0):
txt = txt.encode('utf-8') txt = txt.encode('utf-8')
length_byte = len(txt) length_byte = len(txt)
# Calculating the average chunk value for easy splitting as EPUB (+2 as a safe margin) # Calculating the average chunk value for easy splitting as EPUB (+2 as a safe margin)
chunk_size = long_type(length_byte / (int(length_byte / (epub_split_size_kb * 1024)) + 2)) chunk_size = int(length_byte / (int(length_byte / (epub_split_size_kb * 1024)) + 2))
# if there are chunks with a superior size then go and break # if there are chunks with a superior size then go and break
parts = txt.split(b'\n\n') parts = txt.split(b'\n\n')
lengths = tuple(map(len, parts)) lengths = tuple(map(len, parts))
+2 -3
View File
@@ -10,7 +10,6 @@ from ebook_converter.ebooks.oeb.base import XHTML, XHTML_NS, barename, namespace
from ebook_converter.ebooks.oeb.stylizer import Stylizer from ebook_converter.ebooks.oeb.stylizer import Stylizer
from ebook_converter.ebooks import unit_convert from ebook_converter.ebooks import unit_convert
from ebook_converter.ebooks.textile.unsmarten import unsmarten from ebook_converter.ebooks.textile.unsmarten import unsmarten
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3' __license__ = 'GPL 3'
@@ -225,10 +224,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, string_or_bytes) \ if not isinstance(elem.tag, (str, 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, string_or_bytes) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
+2 -3
View File
@@ -4,7 +4,6 @@ Transform OEB content into plain text
import re import re
from lxml import etree from lxml import etree
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL 3' __license__ = 'GPL 3'
@@ -189,10 +188,10 @@ class TXTMLizer(object):
''' '''
from ebook_converter.ebooks.oeb.base import XHTML_NS, barename, namespace from ebook_converter.ebooks.oeb.base import XHTML_NS, barename, namespace
if not isinstance(elem.tag, string_or_bytes) \ if not isinstance(elem.tag, (str, 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, string_or_bytes) and namespace(p.tag) == XHTML_NS \ if p is not None and isinstance(p.tag, (str, bytes)) and namespace(p.tag) == XHTML_NS \
and elem.tail: and elem.tail:
return [elem.tail] return [elem.tail]
return [''] return ['']
+4 -5
View File
@@ -6,7 +6,6 @@ from ebook_converter.customize import CatalogPlugin
from ebook_converter.library.catalogs import FIELDS, TEMPLATE_ALLOWED_FIELDS from ebook_converter.library.catalogs import FIELDS, TEMPLATE_ALLOWED_FIELDS
from ebook_converter.customize.conversion import DummyReporter from ebook_converter.customize.conversion import DummyReporter
from ebook_converter.ebooks.metadata import format_isbn from ebook_converter.ebooks.metadata import format_isbn
from ebook_converter.polyglot.builtins import string_or_bytes
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -208,11 +207,11 @@ class BIBTEX(CatalogPlugin):
bibtex_entry.append('year = "%s"' % item.year) bibtex_entry.append('year = "%s"' % item.year)
bibtex_entry.append('month = "%s"' % bibtexdict.utf8ToBibtex(strftime("%b", item))) bibtex_entry.append('month = "%s"' % bibtexdict.utf8ToBibtex(strftime("%b", item)))
elif field.startswith('#') and isinstance(item, string_or_bytes): elif field.startswith('#') and isinstance(item, (str, bytes)):
bibtex_entry.append('custom_%s = "%s"' % (field[1:], bibtex_entry.append('custom_%s = "%s"' % (field[1:],
bibtexdict.utf8ToBibtex(item))) bibtexdict.utf8ToBibtex(item)))
elif isinstance(item, string_or_bytes): elif isinstance(item, (str, 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('%s = "%s"' % (field, bibtexdict.utf8ToBibtex(item))) bibtex_entry.append('%s = "%s"' % (field, bibtexdict.utf8ToBibtex(item)))
@@ -346,7 +345,7 @@ class BIBTEX(CatalogPlugin):
bibtexc.ascii_bibtex = True bibtexc.ascii_bibtex = True
# Check citation choice and go to default in case of bad CLI # Check citation choice and go to default in case of bad CLI
if isinstance(opts.impcit, string_or_bytes) : if isinstance(opts.impcit, (str, bytes)) :
if opts.impcit == 'False' : if opts.impcit == 'False' :
citation_bibtex= False citation_bibtex= False
elif opts.impcit == 'True' : elif opts.impcit == 'True' :
@@ -358,7 +357,7 @@ class BIBTEX(CatalogPlugin):
citation_bibtex= opts.impcit citation_bibtex= opts.impcit
# Check add file entry and go to default in case of bad CLI # Check add file entry and go to default in case of bad CLI
if isinstance(opts.addfiles, string_or_bytes) : if isinstance(opts.addfiles, (str, bytes)) :
if opts.addfiles == 'False' : if opts.addfiles == 'False' :
addfiles_bibtex = False addfiles_bibtex = False
elif opts.addfiles == 'True' : elif opts.addfiles == 'True' :
+1 -7
View File
@@ -21,10 +21,7 @@ def as_bytes(x, encoding='utf-8'):
return bytes(x) return bytes(x)
if isinstance(x, memoryview): if isinstance(x, memoryview):
return x.tobytes() return x.tobytes()
ans = str(x) return str(x).encode(encoding)
if isinstance(ans, str):
ans = ans.encode(encoding)
return ans
def as_unicode(x, encoding='utf-8', errors='strict'): def as_unicode(x, encoding='utf-8', errors='strict'):
@@ -45,9 +42,6 @@ def reraise(tp, value, tb=None):
tb = None tb = None
string_or_bytes = str, bytes
string_or_unicode = str
long_type = int
raw_input = input raw_input = input
getcwd = os.getcwd getcwd = os.getcwd
getenv = os.getenv getenv = os.getenv
+2 -2
View File
@@ -14,7 +14,7 @@ from ebook_converter.utils.config_base import (
tweaks, from_json, to_json tweaks, from_json, to_json
) )
from ebook_converter.utils.lock import ExclusiveFile from ebook_converter.utils.lock import ExclusiveFile
from ebook_converter.polyglot.builtins import string_or_bytes, native_string_type from ebook_converter.polyglot.builtins import native_string_type
__license__ = 'GPL v3' __license__ = 'GPL v3'
@@ -191,7 +191,7 @@ class OptionParser(optparse.OptionParser):
upper.__dict__[dest] = lower.__dict__[dest] upper.__dict__[dest] = lower.__dict__[dest]
def add_option_group(self, *args, **kwargs): def add_option_group(self, *args, **kwargs):
if isinstance(args[0], string_or_bytes): if isinstance(args[0], (str, bytes)):
args = list(args) args = list(args)
args[0] = native_string_type(args[0]) args[0] = native_string_type(args[0])
return optparse.OptionParser.add_option_group(self, *args, **kwargs) return optparse.OptionParser.add_option_group(self, *args, **kwargs)
+1 -2
View File
@@ -17,7 +17,6 @@ from ebook_converter.ptempfile import TemporaryDirectory
from ebook_converter.utils.config_base import tweaks from ebook_converter.utils.config_base import tweaks
from ebook_converter.utils.filenames import atomic_rename from ebook_converter.utils.filenames import atomic_rename
from ebook_converter.utils.imghdr import what from ebook_converter.utils.imghdr import what
from ebook_converter.polyglot.builtins import string_or_bytes
# Utilities {{{ # Utilities {{{
# imageops, imageops_err = plugins['imageops'] # imageops, imageops_err = plugins['imageops']
@@ -505,7 +504,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, string_or_bytes): if palette and isinstance(palette, (str, 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])
+1 -2
View File
@@ -6,7 +6,6 @@ import os
from ebook_converter.utils.speedups import ReadOnlyFileBuffer from ebook_converter.utils.speedups import ReadOnlyFileBuffer
from ebook_converter.constants import ispy3 from ebook_converter.constants import ispy3
from ebook_converter.polyglot.builtins import string_or_bytes
HSIZE = 120 HSIZE = 120
@@ -15,7 +14,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, string_or_bytes): if isinstance(file, (str, bytes)):
with lopen(file, 'rb') as f: with lopen(file, 'rb') as f:
h = f.read(HSIZE) h = f.read(HSIZE)
else: else:
+2 -2
View File
@@ -5,7 +5,7 @@ from ebook_converter.constants import isosx, isfrozen, filesystem_encoding, ispy
from ebook_converter.utils.config import prefs from ebook_converter.utils.config import prefs
from ebook_converter.ptempfile import PersistentTemporaryFile, base_dir from ebook_converter.ptempfile import PersistentTemporaryFile, base_dir
from ebook_converter.utils.serialize import msgpack_dumps from ebook_converter.utils.serialize import msgpack_dumps
from ebook_converter.polyglot.builtins import iteritems, string_or_bytes, environ_item, native_string_type, getcwd from ebook_converter.polyglot.builtins import iteritems, environ_item, native_string_type, getcwd
from ebook_converter.polyglot.binary import as_hex_unicode from ebook_converter.polyglot.binary import as_hex_unicode
try: try:
import win32process import win32process
@@ -191,7 +191,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, string_or_bytes) else exe cmd = [exe] if isinstance(exe, (str, bytes)) else exe
args = { args = {
'env' : env, 'env' : env,
'cwd' : _cwd, 'cwd' : _cwd,
+2 -2
View File
@@ -8,7 +8,7 @@ from ebook_converter.utils.ipc import eintr_retry_call
from ebook_converter.utils.ipc.launch import Worker from ebook_converter.utils.ipc.launch import Worker
from ebook_converter.utils.serialize import msgpack_loads, msgpack_dumps from ebook_converter.utils.serialize import msgpack_loads, msgpack_dumps
from ebook_converter.utils.monotonic import monotonic from ebook_converter.utils.monotonic import monotonic
from ebook_converter.polyglot.builtins import string_or_bytes, environ_item from ebook_converter.polyglot.builtins import environ_item
from ebook_converter.polyglot.binary import as_hex_unicode, from_hex_bytes from ebook_converter.polyglot.binary import as_hex_unicode, from_hex_bytes
@@ -162,7 +162,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, string_or_bytes) else exe cmd = [exe] if isinstance(exe, (str, bytes)) else exe
p = subprocess.Popen(cmd + ['--pipe-worker', command], **args) p = subprocess.Popen(cmd + ['--pipe-worker', command], **args)
return p return p
+2 -2
View File
@@ -10,7 +10,7 @@ from tempfile import SpooledTemporaryFile
from ebook_converter import sanitize_file_name from ebook_converter import sanitize_file_name
from ebook_converter.constants import filesystem_encoding from ebook_converter.constants import filesystem_encoding
from ebook_converter.ebooks.chardet import detect from ebook_converter.ebooks.chardet import detect
from ebook_converter.polyglot.builtins import string_or_bytes, getcwd, as_bytes from ebook_converter.polyglot.builtins import getcwd, as_bytes
try: try:
import zlib # We may need its compression method import zlib # We may need its compression method
@@ -749,7 +749,7 @@ class ZipFile:
self.comment = b'' self.comment = b''
# Check if we were passed a file-like object # Check if we were passed a file-like object
if isinstance(file, string_or_bytes): if isinstance(file, (str, 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'}