1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-04-20 21:21:35 +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
+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.utils.cleantext import clean_xml_chars
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
@@ -1035,7 +1035,7 @@ class Manifest(object):
mt = self.media_type.lower()
except Exception:
mt = 'application/octet-stream'
if not isinstance(data, string_or_bytes):
if not isinstance(data, (str, bytes)):
pass # already parsed
elif mt in OEB_DOCS:
data = self._parse_xhtml(data)
@@ -1296,7 +1296,7 @@ class Spine(object):
self.page_progression_direction = None
def _linear(self, linear):
if isinstance(linear, string_or_bytes):
if isinstance(linear, (str, bytes)):
linear = linear.lower()
if linear is None or linear in ('yes', '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 ebook_converter.tinycss.fonts3 import parse_font, serialize_font_family
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'
@@ -125,10 +125,10 @@ def normalize_font(cssvalue, font_family_as_list=False):
ans = {k:DEFAULTS[k] for k in composition}
ans.update(parse_font(val))
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(',')]
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'])
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.constants import filesystem_encoding
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'
@@ -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
# insane nesting depths
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)
if depth > 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:
+3 -4
View File
@@ -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))
@@ -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)
@@ -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)