diff --git a/ebook_converter/css_selectors/ordered_set.py b/ebook_converter/css_selectors/ordered_set.py index c68418d..c85b456 100644 --- a/ebook_converter/css_selectors/ordered_set.py +++ b/ebook_converter/css_selectors/ordered_set.py @@ -1,7 +1,5 @@ import collections -from ebook_converter.polyglot.builtins import string_or_bytes - __license__ = 'GPL v3' __copyright__ = '2015, Kovid Goyal ' @@ -18,7 +16,7 @@ def is_iterable(obj): Strings, however, should be considered as atomic values to look up, not 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): diff --git a/ebook_converter/customize/zipplugin.py b/ebook_converter/customize/zipplugin.py index 4113c4a..9f129e7 100644 --- a/ebook_converter/customize/zipplugin.py +++ b/ebook_converter/customize/zipplugin.py @@ -11,8 +11,7 @@ from ebook_converter import as_unicode from ebook_converter.constants import ispy3 from ebook_converter.customize import (Plugin, numeric_version, platform, InvalidPlugin, PluginNotFound) -from ebook_converter.polyglot.builtins import (itervalues, string_or_bytes, - reload) +from ebook_converter.polyglot.builtins import itervalues, reload __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. ''' names = name_or_list_of_names - if isinstance(names, string_or_bytes): + if isinstance(names, (str, bytes)): names = [names] ans = {} 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 names = name_or_list_of_names ans = get_resources(zfp, names) - if isinstance(names, string_or_bytes): + if isinstance(names, (str, bytes)): names = [names] if ans is None: ans = {} - if isinstance(ans, string_or_bytes): + if isinstance(ans, (str, bytes)): ans = dict([(names[0], ans)]) ians = {} diff --git a/ebook_converter/ebooks/conversion/plumber.py b/ebook_converter/ebooks/conversion/plumber.py index c78145c..f97b5cf 100644 --- a/ebook_converter/ebooks/conversion/plumber.py +++ b/ebook_converter/ebooks/conversion/plumber.py @@ -13,7 +13,6 @@ from ebook_converter.utils.zipfile import ZipFile from ebook_converter import (extract, walk, isbytestring, filesystem_encoding, get_types_map) from ebook_converter.constants import __version__ -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL 3' @@ -1023,7 +1022,7 @@ OptionRecommendation(name='search_replace', def dump_input(self, ret, output_dir): 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) else: if not os.path.exists(out_dir): @@ -1216,7 +1215,7 @@ OptionRecommendation(name='search_replace', transform_css_rules = () if 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) flattener = CSSFlattener(fbase=fbase, fkey=fkey, lineh=line_height, diff --git a/ebook_converter/ebooks/covers.py b/ebook_converter/ebooks/covers.py index 8de7cd2..6794bb8 100644 --- a/ebook_converter/ebooks/covers.py +++ b/ebook_converter/ebooks/covers.py @@ -2,7 +2,7 @@ import re, random, unicodedata, numbers from collections import namedtuple from contextlib import contextmanager 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 PyQt5.Qt import ( @@ -270,7 +270,7 @@ def format_fields(mi, prefs): @contextmanager def preserve_fields(obj, fields): - if isinstance(fields, string_or_bytes): + if isinstance(fields, (str, bytes)): fields = fields.split() null = object() mem = {f:getattr(obj, f, null) for f in fields} diff --git a/ebook_converter/ebooks/docx/writer/from_html.py b/ebook_converter/ebooks/docx/writer/from_html.py index b9f03a7..c9894a5 100644 --- a/ebook_converter/ebooks/docx/writer/from_html.py +++ b/ebook_converter/ebooks/docx/writer/from_html.py @@ -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.base import XPath, barename from ebook_converter.utils.localization import lang_as_iso639_1 -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL v3' @@ -527,7 +526,7 @@ class Convert(object): self.add_block_tag(tagname, html_tag, tag_style, stylizer, float_spec=float_spec) 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) else: # Comment/PI/etc. tail = getattr(child, 'tail', None) diff --git a/ebook_converter/ebooks/fb2/fb2ml.py b/ebook_converter/ebooks/fb2/fb2ml.py index 540c603..f8b718c 100644 --- a/ebook_converter/ebooks/fb2/fb2ml.py +++ b/ebook_converter/ebooks/fb2/fb2ml.py @@ -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.img import save_cover_data_to 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 @@ -408,9 +407,9 @@ class FB2MLizer(object): elem = elem_tree # 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() - 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: return [elem.tail] return [] diff --git a/ebook_converter/ebooks/htmlz/oeb2html.py b/ebook_converter/ebooks/htmlz/oeb2html.py index dacc03f..6d95b4b 100644 --- a/ebook_converter/ebooks/htmlz/oeb2html.py +++ b/ebook_converter/ebooks/htmlz/oeb2html.py @@ -13,7 +13,7 @@ from ebook_converter.ebooks.oeb.base import ( XHTML, XHTML_NS, SVG_NS, barename, namespace, OEB_IMAGES, XLINK, rewrite_links, urlnormalize) from ebook_converter.ebooks.oeb.stylizer import Stylizer 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' @@ -94,7 +94,7 @@ class OEB2HTML(object): for el in root.iter(): attribs = el.attrib try: - if not isinstance(el.tag, string_or_bytes): + if not isinstance(el.tag, (str, bytes)): continue except: continue @@ -155,10 +155,10 @@ class OEB2HTMLNoCSSizer(OEB2HTML): ''' # 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): 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: return [elem.tail] return [''] @@ -244,10 +244,10 @@ class OEB2HTMLInlineCSSizer(OEB2HTML): ''' # 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): 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: return [elem.tail] return [''] @@ -347,10 +347,10 @@ class OEB2HTMLClassCSSizer(OEB2HTML): ''' # 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): 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: return [elem.tail] return [''] diff --git a/ebook_converter/ebooks/lrf/html/convert_from.py b/ebook_converter/ebooks/lrf/html/convert_from.py index 5285c75..26f5f02 100644 --- a/ebook_converter/ebooks/lrf/html/convert_from.py +++ b/ebook_converter/ebooks/lrf/html/convert_from.py @@ -37,7 +37,7 @@ from ebook_converter.ebooks.lrf.pylrs.pylrs import ( RuledLine, Span, Sub, Sup, TextBlock ) 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 PIL import Image as PILImage @@ -1118,7 +1118,7 @@ class HTMLConverter(object): ans['sidemargin'] = int((factor*int(self.current_block.blockStyle.attrs['blockwidth'])) / 2) for prop in ('topskip', 'footskip', 'sidemargin'): - if isinstance(ans[prop], string_or_bytes): + if isinstance(ans[prop], (str, bytes)): ans[prop] = int(ans[prop]) if ans[prop] < 0: ans[prop] = 0 diff --git a/ebook_converter/ebooks/lrf/html/table.py b/ebook_converter/ebooks/lrf/html/table.py index a698f50..ca41acc 100644 --- a/ebook_converter/ebooks/lrf/html/table.py +++ b/ebook_converter/ebooks/lrf/html/table.py @@ -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, \ CharButton, Plot, Paragraph, \ 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' @@ -40,7 +40,7 @@ def tokens(tb): yield 2, None elif isinstance(x, Text): yield x.text, cattrs(attrs, {}) - elif isinstance(x, string_or_bytes): + elif isinstance(x, (str, bytes)): yield x, cattrs(attrs, {}) elif isinstance(x, (CharButton, LrsTextTag)): if x.contents: diff --git a/ebook_converter/ebooks/lrf/pylrs/elements.py b/ebook_converter/ebooks/lrf/pylrs/elements.py index 531d9cc..7da08e7 100644 --- a/ebook_converter/ebooks/lrf/pylrs/elements.py +++ b/ebook_converter/ebooks/lrf/pylrs/elements.py @@ -1,7 +1,6 @@ """ elements.py -- replacements and helpers for ElementTree """ -from ebook_converter.polyglot.builtins import string_or_bytes class ElementWriter(object): @@ -25,7 +24,7 @@ class ElementWriter(object): def _writeAttribute(self, f, name, value): f.write(' %s="' % str(name)) - if not isinstance(value, string_or_bytes): + if not isinstance(value, (str, bytes)): value = str(value) value = self._encodeCdata(value) value = value.replace('"', '"') diff --git a/ebook_converter/ebooks/lrf/pylrs/pylrf.py b/ebook_converter/ebooks/lrf/pylrs/pylrf.py index 9ff904b..7b78ae9 100644 --- a/ebook_converter/ebooks/lrf/pylrs/pylrf.py +++ b/ebook_converter/ebooks/lrf/pylrs/pylrf.py @@ -9,7 +9,7 @@ import codecs import os 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" @@ -397,7 +397,7 @@ class LrfTag(object): for f in self.format: if isinstance(f, dict): p = f[p] - elif isinstance(f, string_or_bytes): + elif isinstance(f, (str, bytes)): if isinstance(p, tuple): writeString(lrf, struct.pack(f, *p)) else: diff --git a/ebook_converter/ebooks/lrf/pylrs/pylrs.py b/ebook_converter/ebooks/lrf/pylrs/pylrs.py index ccc71cf..96c07bd 100644 --- a/ebook_converter/ebooks/lrf/pylrs/pylrs.py +++ b/ebook_converter/ebooks/lrf/pylrs/pylrs.py @@ -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 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): @@ -90,7 +90,7 @@ def ElementWithReading(tag, text, reading=False): if text is None: readingText = "" - elif isinstance(text, string_or_bytes): + elif isinstance(text, (str, bytes)): readingText = text else: # assumed to be a sequence of (name, sortas) @@ -148,7 +148,7 @@ class Delegator(object): """ for setting in d.getSettings(): - if isinstance(setting, string_or_bytes): + if isinstance(setting, (str, bytes)): setting = (d, setting) delegates = \ self.delegatedSettingsDict.setdefault(setting[1], []) @@ -286,7 +286,7 @@ class LrsContainer(object): (content.__class__.__name__, self.__class__.__name__)) - if convertText and isinstance(content, string_or_bytes): + if convertText and isinstance(content, (str, bytes)): content = Text(content) content.setParent(self) @@ -580,14 +580,14 @@ class Book(Delegator): ts.attrs['baselineskip'] = rescale(ts.attrs['baselineskip']) 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) self.render(lrsFile, outputEncodingName=encoding) lrsFile.close() def renderLrf(self, lrfFile): self.appendReferencedObjects(self) - if isinstance(lrfFile, string_or_bytes): + if isinstance(lrfFile, (str, bytes)): lrfFile = open(lrfFile, "wb") lrfWriter = LrfWriter(self.sourceencoding) @@ -1488,7 +1488,7 @@ class Paragraph(LrsContainer): LrsContainer.__init__(self, [Text, CR, DropCaps, CharButton, LrsSimpleChar1, bytes, str]) if text is not None: - if isinstance(text, string_or_bytes): + if isinstance(text, (str, bytes)): text = Text(text) self.append(text) @@ -1807,7 +1807,7 @@ class Span(LrsSimpleChar1, LrsContainer): def __init__(self, text=None, **attrs): LrsContainer.__init__(self, [LrsSimpleChar1, Text, bytes, str]) if text is not None: - if isinstance(text, string_or_bytes): + if isinstance(text, (str, bytes)): text = Text(text) self.append(text) diff --git a/ebook_converter/ebooks/metadata/rtf.py b/ebook_converter/ebooks/metadata/rtf.py index d2049b2..d09277a 100644 --- a/ebook_converter/ebooks/metadata/rtf.py +++ b/ebook_converter/ebooks/metadata/rtf.py @@ -6,7 +6,7 @@ import re from ebook_converter import force_unicode 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(.*?)(? max_nesting_depth: 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'), nsmap={None: XHTML_NS}, attrib=attrib) for elem in data.iterdescendants(): - if isinstance(elem.tag, string_or_bytes) and \ + if isinstance(elem.tag, (str, bytes)) and \ namespace(elem.tag) == ns: elem.tag = XHTML(barename(elem.tag)) for elem in data: diff --git a/ebook_converter/ebooks/oeb/polish/split.py b/ebook_converter/ebooks/oeb/polish/split.py index 6fd8d46..814525d 100644 --- a/ebook_converter/ebooks/oeb/polish/split.py +++ b/ebook_converter/ebooks/oeb/polish/split.py @@ -1,5 +1,4 @@ import copy, os, re -from ebook_converter.polyglot.builtins import string_or_bytes import urllib.parse 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 = '' for first_child in children: - if not isinstance(first_child, string_or_bytes): + if not isinstance(first_child, (str, bytes)): break - if isinstance(first_child, string_or_bytes): + if isinstance(first_child, (str, bytes)): # body contained only text, no tags first_child = body.makeelement(XHTML('p')) 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]) for child in children: - if isinstance(child, string_or_bytes): + if isinstance(child, (str, bytes)): add_text(master_body, child) else: master_body.append(copy.deepcopy(child)) diff --git a/ebook_converter/ebooks/oeb/transforms/flatcss.py b/ebook_converter/ebooks/oeb/transforms/flatcss.py index dfa5379..1110aef 100644 --- a/ebook_converter/ebooks/oeb/transforms/flatcss.py +++ b/ebook_converter/ebooks/oeb/transforms/flatcss.py @@ -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.utils.filenames import ascii_filename, ascii_text 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' @@ -351,7 +351,7 @@ class CSSFlattener(object): cssdict[property] = "%0.5fem" % (value / fsize) 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: return tag = barename(node.tag) diff --git a/ebook_converter/ebooks/oeb/transforms/manglecase.py b/ebook_converter/ebooks/oeb/transforms/manglecase.py index dcadbb9..4e55696 100644 --- a/ebook_converter/ebooks/oeb/transforms/manglecase.py +++ b/ebook_converter/ebooks/oeb/transforms/manglecase.py @@ -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 namespace from ebook_converter.ebooks.oeb.stylizer import Stylizer -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL v3' @@ -95,7 +94,7 @@ class CaseMangler(object): last = child 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: return children = list(elem) diff --git a/ebook_converter/ebooks/pdb/header.py b/ebook_converter/ebooks/pdb/header.py index fbcee72..0f176d7 100644 --- a/ebook_converter/ebooks/pdb/header.py +++ b/ebook_converter/ebooks/pdb/header.py @@ -5,7 +5,6 @@ Read the header data from a pdb file. import re import struct import time -from ebook_converter.polyglot.builtins import long_type __license__ = 'GPL v3' @@ -83,6 +82,6 @@ class PdbHeaderBuilder(object): offset = 78 + (8 * nrecords) + 2 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 out_stream.write(b'\x00\x00') diff --git a/ebook_converter/ebooks/txt/markdownml.py b/ebook_converter/ebooks/txt/markdownml.py index 192be2d..1954179 100644 --- a/ebook_converter/ebooks/txt/markdownml.py +++ b/ebook_converter/ebooks/txt/markdownml.py @@ -8,7 +8,6 @@ from functools import partial 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.stylizer import Stylizer -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL 3' @@ -110,10 +109,10 @@ class MarkdownMLizer(OEB2HTML): ''' # 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: 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: return [elem.tail] return [''] diff --git a/ebook_converter/ebooks/txt/processor.py b/ebook_converter/ebooks/txt/processor.py index 7fbe45f..9a733ee 100644 --- a/ebook_converter/ebooks/txt/processor.py +++ b/ebook_converter/ebooks/txt/processor.py @@ -8,7 +8,7 @@ from ebook_converter.ebooks.metadata.opf2 import OPFCreator from ebook_converter.ebooks.conversion.preprocess import DocAnalysis 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' @@ -62,7 +62,7 @@ def split_txt(txt, epub_split_size_kb=0): txt = txt.encode('utf-8') length_byte = len(txt) # 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 parts = txt.split(b'\n\n') lengths = tuple(map(len, parts)) diff --git a/ebook_converter/ebooks/txt/textileml.py b/ebook_converter/ebooks/txt/textileml.py index 4192416..54e90d3 100644 --- a/ebook_converter/ebooks/txt/textileml.py +++ b/ebook_converter/ebooks/txt/textileml.py @@ -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 import unit_convert from ebook_converter.ebooks.textile.unsmarten import unsmarten -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL 3' @@ -225,10 +224,10 @@ class TextileMLizer(OEB2HTML): ''' # 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: 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: return [elem.tail] return [''] diff --git a/ebook_converter/ebooks/txt/txtml.py b/ebook_converter/ebooks/txt/txtml.py index 78e1f65..ae16e9b 100644 --- a/ebook_converter/ebooks/txt/txtml.py +++ b/ebook_converter/ebooks/txt/txtml.py @@ -4,7 +4,6 @@ Transform OEB content into plain text import re from lxml import etree -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL 3' @@ -189,10 +188,10 @@ class TXTMLizer(object): ''' 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: 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: return [elem.tail] return [''] diff --git a/ebook_converter/library/catalogs/bibtex.py b/ebook_converter/library/catalogs/bibtex.py index aed98e9..d96a6cf 100644 --- a/ebook_converter/library/catalogs/bibtex.py +++ b/ebook_converter/library/catalogs/bibtex.py @@ -6,7 +6,6 @@ from ebook_converter.customize import CatalogPlugin from ebook_converter.library.catalogs import FIELDS, TEMPLATE_ALLOWED_FIELDS from ebook_converter.customize.conversion import DummyReporter from ebook_converter.ebooks.metadata import format_isbn -from ebook_converter.polyglot.builtins import string_or_bytes __license__ = 'GPL v3' @@ -208,11 +207,11 @@ class BIBTEX(CatalogPlugin): bibtex_entry.append('year = "%s"' % item.year) 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:], bibtexdict.utf8ToBibtex(item))) - elif isinstance(item, string_or_bytes): + elif isinstance(item, (str, bytes)): # elif field in ['title', 'publisher', 'cover', 'uuid', 'ondevice', # 'author_sort', 'series', 'title_sort'] : bibtex_entry.append('%s = "%s"' % (field, bibtexdict.utf8ToBibtex(item))) @@ -346,7 +345,7 @@ class BIBTEX(CatalogPlugin): bibtexc.ascii_bibtex = True # 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' : citation_bibtex= False elif opts.impcit == 'True' : @@ -358,7 +357,7 @@ class BIBTEX(CatalogPlugin): citation_bibtex= opts.impcit # 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' : addfiles_bibtex = False elif opts.addfiles == 'True' : diff --git a/ebook_converter/polyglot/builtins.py b/ebook_converter/polyglot/builtins.py index d01e1ad..6c40590 100644 --- a/ebook_converter/polyglot/builtins.py +++ b/ebook_converter/polyglot/builtins.py @@ -21,10 +21,7 @@ def as_bytes(x, encoding='utf-8'): return bytes(x) if isinstance(x, memoryview): return x.tobytes() - ans = str(x) - if isinstance(ans, str): - ans = ans.encode(encoding) - return ans + return str(x).encode(encoding) def as_unicode(x, encoding='utf-8', errors='strict'): @@ -45,9 +42,6 @@ def reraise(tp, value, tb=None): tb = None -string_or_bytes = str, bytes -string_or_unicode = str -long_type = int raw_input = input getcwd = os.getcwd getenv = os.getenv diff --git a/ebook_converter/utils/config.py b/ebook_converter/utils/config.py index 3c773fb..6d48a86 100644 --- a/ebook_converter/utils/config.py +++ b/ebook_converter/utils/config.py @@ -14,7 +14,7 @@ from ebook_converter.utils.config_base import ( tweaks, from_json, to_json ) 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' @@ -191,7 +191,7 @@ class OptionParser(optparse.OptionParser): upper.__dict__[dest] = lower.__dict__[dest] def add_option_group(self, *args, **kwargs): - if isinstance(args[0], string_or_bytes): + if isinstance(args[0], (str, bytes)): args = list(args) args[0] = native_string_type(args[0]) return optparse.OptionParser.add_option_group(self, *args, **kwargs) diff --git a/ebook_converter/utils/img.py b/ebook_converter/utils/img.py index 8cedc5c..c2aa997 100644 --- a/ebook_converter/utils/img.py +++ b/ebook_converter/utils/img.py @@ -17,7 +17,6 @@ from ebook_converter.ptempfile import TemporaryDirectory from ebook_converter.utils.config_base import tweaks from ebook_converter.utils.filenames import atomic_rename from ebook_converter.utils.imghdr import what -from ebook_converter.polyglot.builtins import string_or_bytes # Utilities {{{ # imageops, imageops_err = plugins['imageops'] @@ -505,7 +504,7 @@ def quantize_image(img, max_colors=256, dither=True, palette=''): img = image_from_data(img) if img.hasAlphaChannel(): img = blend_image(img) - if palette and isinstance(palette, string_or_bytes): + if palette and isinstance(palette, (str, bytes)): palette = palette.split() return imageops.quantize(img, max_colors, dither, [QColor(x).rgb() for x in palette]) diff --git a/ebook_converter/utils/imghdr.py b/ebook_converter/utils/imghdr.py index c987e2a..886013b 100644 --- a/ebook_converter/utils/imghdr.py +++ b/ebook_converter/utils/imghdr.py @@ -6,7 +6,6 @@ import os from ebook_converter.utils.speedups import ReadOnlyFileBuffer from ebook_converter.constants import ispy3 -from ebook_converter.polyglot.builtins import string_or_bytes HSIZE = 120 @@ -15,7 +14,7 @@ HSIZE = 120 def what(file, h=None): ' Recognize image headers ' if h is None: - if isinstance(file, string_or_bytes): + if isinstance(file, (str, bytes)): with lopen(file, 'rb') as f: h = f.read(HSIZE) else: diff --git a/ebook_converter/utils/ipc/launch.py b/ebook_converter/utils/ipc/launch.py index 7c05124..b0bc41b 100644 --- a/ebook_converter/utils/ipc/launch.py +++ b/ebook_converter/utils/ipc/launch.py @@ -5,7 +5,7 @@ from ebook_converter.constants import isosx, isfrozen, filesystem_encoding, ispy from ebook_converter.utils.config import prefs from ebook_converter.ptempfile import PersistentTemporaryFile, base_dir 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 try: import win32process @@ -191,7 +191,7 @@ class Worker(object): _cwd = cwd if priority is None: 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 = { 'env' : env, 'cwd' : _cwd, diff --git a/ebook_converter/utils/ipc/simple_worker.py b/ebook_converter/utils/ipc/simple_worker.py index c1cb18a..744055a 100644 --- a/ebook_converter/utils/ipc/simple_worker.py +++ b/ebook_converter/utils/ipc/simple_worker.py @@ -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.serialize import msgpack_loads, msgpack_dumps 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 @@ -162,7 +162,7 @@ def start_pipe_worker(command, env=None, priority='normal', **process_args): args['close_fds'] = True 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) return p diff --git a/ebook_converter/utils/zipfile.py b/ebook_converter/utils/zipfile.py index 27a83fb..56b09ec 100644 --- a/ebook_converter/utils/zipfile.py +++ b/ebook_converter/utils/zipfile.py @@ -10,7 +10,7 @@ from tempfile import SpooledTemporaryFile from ebook_converter import sanitize_file_name from ebook_converter.constants import filesystem_encoding 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: import zlib # We may need its compression method @@ -749,7 +749,7 @@ class ZipFile: self.comment = b'' # Check if we were passed a file-like object - if isinstance(file, string_or_bytes): + if isinstance(file, (str, bytes)): self._filePassed = 0 self.filename = file modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}