1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-02-28 21:35:49 +01:00

Removed isbytestring function

This commit is contained in:
2020-06-14 12:10:40 +02:00
parent a69884d724
commit be671ef2d8
10 changed files with 29 additions and 40 deletions

View File

@@ -117,9 +117,9 @@ def sanitize_file_name(name, substitute='_'):
**WARNING:** This function also replaces path separators, so only pass file names **WARNING:** This function also replaces path separators, so only pass file names
and not full paths to it. and not full paths to it.
''' '''
if isbytestring(name): if isinstance(name, bytes):
name = name.decode(filesystem_encoding, 'replace') name = name.decode(filesystem_encoding, 'replace')
if isbytestring(substitute): if isinstance(substitute, bytes):
substitute = substitute.decode(filesystem_encoding, 'replace') substitute = substitute.decode(filesystem_encoding, 'replace')
chars = (substitute if c in _filename_sanitize_unicode else c for c in name) chars = (substitute if c in _filename_sanitize_unicode else c for c in name)
one = ''.join(chars) one = ''.join(chars)
@@ -569,12 +569,8 @@ def prepare_string_for_xml(raw, attribute=False):
return raw return raw
def isbytestring(obj):
return isinstance(obj, bytes)
def force_unicode(obj, enc=preferred_encoding): def force_unicode(obj, enc=preferred_encoding):
if isbytestring(obj): if isinstance(obj, bytes):
try: try:
obj = obj.decode(enc) obj = obj.decode(enc)
except Exception: except Exception:
@@ -586,13 +582,13 @@ def force_unicode(obj, enc=preferred_encoding):
obj = obj.decode('utf-8') obj = obj.decode('utf-8')
except Exception: except Exception:
obj = repr(obj) obj = repr(obj)
if isbytestring(obj): if isinstance(obj, bytes):
obj = obj.decode('utf-8') obj = obj.decode('utf-8')
return obj return obj
def as_unicode(obj, enc=preferred_encoding): def as_unicode(obj, enc=preferred_encoding):
if not isbytestring(obj): if not isinstance(obj, bytes):
try: try:
obj = str(obj) obj = str(obj)
except Exception: except Exception:

View File

@@ -14,8 +14,7 @@ from ebook_converter.ebooks.conversion.preprocess import HTMLPreProcessor
from ebook_converter.ptempfile import PersistentTemporaryDirectory from ebook_converter.ptempfile import PersistentTemporaryDirectory
from ebook_converter.utils.date import parse_date from ebook_converter.utils.date import parse_date
from ebook_converter.utils.zipfile import ZipFile from ebook_converter.utils.zipfile import ZipFile
from ebook_converter import (extract, walk, isbytestring, filesystem_encoding, from ebook_converter import extract, walk, filesystem_encoding, get_types_map
get_types_map)
from ebook_converter.constants_old import __version__ from ebook_converter.constants_old import __version__
@@ -88,9 +87,9 @@ class Plumber(object):
:param input: Path to input file. :param input: Path to input file.
:param output: Path to output file/directory :param output: Path to output file/directory
''' '''
if isbytestring(input): if isinstance(input, bytes):
input = input.decode(filesystem_encoding) input = input.decode(filesystem_encoding)
if isbytestring(output): if isinstance(output, bytes):
output = output.decode(filesystem_encoding) output = output.decode(filesystem_encoding)
self.original_input_arg = input self.original_input_arg = input
self.for_regex_wizard = for_regex_wizard self.for_regex_wizard = for_regex_wizard

View File

@@ -9,7 +9,6 @@ from datetime import datetime, time
from ebook_converter.ebooks.metadata.book import SERIALIZABLE_FIELDS from ebook_converter.ebooks.metadata.book import SERIALIZABLE_FIELDS
from ebook_converter.constants_old import filesystem_encoding, preferred_encoding from ebook_converter.constants_old import filesystem_encoding, preferred_encoding
from ebook_converter.library.field_metadata import FieldMetadata from ebook_converter.library.field_metadata import FieldMetadata
from ebook_converter import isbytestring
from ebook_converter.polyglot.builtins import as_bytes from ebook_converter.polyglot.builtins import as_bytes
from ebook_converter.polyglot.binary import as_base64_unicode, from_base64_bytes from ebook_converter.polyglot.binary import as_base64_unicode, from_base64_bytes
@@ -73,10 +72,10 @@ def object_to_unicode(obj, enc=preferred_encoding):
def dec(x): def dec(x):
return x.decode(enc, 'replace') return x.decode(enc, 'replace')
if isbytestring(obj): if isinstance(obj, bytes):
return dec(obj) return dec(obj)
if isinstance(obj, (list, tuple)): if isinstance(obj, (list, tuple)):
return [dec(x) if isbytestring(x) else object_to_unicode(x) for x in obj] return [dec(x) if isinstance(x, bytes) else object_to_unicode(x) for x in obj]
if isinstance(obj, dict): if isinstance(obj, dict):
ans = {} ans = {}
for k, v in obj.items(): for k, v in obj.items():
@@ -163,7 +162,7 @@ class JsonCodec(object):
value = book.get(key) value = book.get(key)
if key == 'thumbnail': if key == 'thumbnail':
return encode_thumbnail(value) return encode_thumbnail(value)
elif isbytestring(value): # str includes bytes elif isinstance(value, bytes): # str includes bytes
enc = filesystem_encoding if key == 'lpath' else preferred_encoding enc = filesystem_encoding if key == 'lpath' else preferred_encoding
return object_to_unicode(value, enc=enc) return object_to_unicode(value, enc=enc)
elif datatype == 'datetime': elif datatype == 'datetime':

View File

@@ -11,7 +11,7 @@ from lxml.etree import Comment
from ebook_converter.ebooks.metadata import string_to_authors, authors_to_string from ebook_converter.ebooks.metadata import string_to_authors, authors_to_string
from ebook_converter.ebooks.metadata.book.base import Metadata from ebook_converter.ebooks.metadata.book.base import Metadata
from ebook_converter.ebooks.chardet import xml_to_unicode from ebook_converter.ebooks.chardet import xml_to_unicode
from ebook_converter import replace_entities, isbytestring from ebook_converter import replace_entities
from ebook_converter.utils.date import parse_date, is_date_undefined from ebook_converter.utils.date import parse_date, is_date_undefined
@@ -128,7 +128,7 @@ def get_metadata_(src, encoding=None):
# Meta data definitions as in # Meta data definitions as in
# https://www.mobileread.com/forums/showpost.php?p=712544&postcount=9 # https://www.mobileread.com/forums/showpost.php?p=712544&postcount=9
if isbytestring(src): if isinstance(src, bytes):
if not encoding: if not encoding:
src = xml_to_unicode(src)[0] src = xml_to_unicode(src)[0]
else: else:

View File

@@ -3,7 +3,6 @@ import os, re, collections
from ebook_converter.utils.config import prefs from ebook_converter.utils.config import prefs
from ebook_converter.constants_old import filesystem_encoding from ebook_converter.constants_old import filesystem_encoding
from ebook_converter.ebooks.metadata.opf2 import OPF from ebook_converter.ebooks.metadata.opf2 import OPF
from ebook_converter import isbytestring
from ebook_converter.customize.ui import get_file_type_metadata, set_file_type_metadata from ebook_converter.customize.ui import get_file_type_metadata, set_file_type_metadata
from ebook_converter.ebooks.metadata import MetaInformation, string_to_authors from ebook_converter.ebooks.metadata import MetaInformation, string_to_authors
@@ -126,7 +125,7 @@ def set_metadata(stream, mi, stream_type='lrf', report_error=None):
def metadata_from_filename(name, pat=None, fallback_pat=None): def metadata_from_filename(name, pat=None, fallback_pat=None):
if isbytestring(name): if isinstance(name, bytes):
name = name.decode(filesystem_encoding, 'replace') name = name.decode(filesystem_encoding, 'replace')
name = name.rpartition('.')[0] name = name.rpartition('.')[0]
mi = MetaInformation(None, None) mi = MetaInformation(None, None)

View File

@@ -20,7 +20,7 @@ from ebook_converter import force_unicode
from ebook_converter.constants_old import filesystem_encoding, __version__ from ebook_converter.constants_old import filesystem_encoding, __version__
from ebook_converter.ebooks.chardet import xml_to_unicode from ebook_converter.ebooks.chardet import xml_to_unicode
from ebook_converter.ebooks.conversion.preprocess import CSSPreProcessor from ebook_converter.ebooks.conversion.preprocess import CSSPreProcessor
from ebook_converter import (isbytestring, as_unicode, get_types_map) from ebook_converter import as_unicode, get_types_map
from ebook_converter.ebooks.oeb import parse_utils from ebook_converter.ebooks.oeb import parse_utils
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
@@ -501,7 +501,7 @@ class DirContainer(object):
def __init__(self, path, log, ignore_opf=False): def __init__(self, path, log, ignore_opf=False):
self.log = log self.log = log
if isbytestring(path): if isinstance(path, bytes):
path = path.decode(filesystem_encoding) path = path.decode(filesystem_encoding)
self.opfname = None self.opfname = None
ext = os.path.splitext(path)[1].lower() ext = os.path.splitext(path)[1].lower()

View File

@@ -12,7 +12,7 @@ from css_parser import (profile as cssprofiles, parseString, parseStyle, log as
css_parser_log, CSSParser, profiles, replaceUrls) css_parser_log, CSSParser, profiles, replaceUrls)
from ebook_converter import constants as const from ebook_converter import constants as const
from ebook_converter import force_unicode, as_unicode from ebook_converter import force_unicode
from ebook_converter.ebooks import unit_convert from ebook_converter.ebooks import unit_convert
from ebook_converter.ebooks.oeb import base from ebook_converter.ebooks.oeb import base
from ebook_converter.ebooks.oeb.normalize_css import DEFAULTS, normalizers from ebook_converter.ebooks.oeb.normalize_css import DEFAULTS, normalizers
@@ -303,7 +303,7 @@ class Stylizer(object):
try: try:
matches = tuple(select(text)) matches = tuple(select(text))
except SelectorError as err: except SelectorError as err:
self.logger.error('Ignoring CSS rule with invalid selector: %r (%s)' % (text, as_unicode(err))) self.logger.error('Ignoring CSS rule with invalid selector: %r (%s)' % (text, err))
continue continue
if fl is not None: if fl is not None:

View File

@@ -3,17 +3,13 @@ Read content from txt file.
""" """
import os, re import os, re
from ebook_converter import prepare_string_for_xml, isbytestring from ebook_converter import prepare_string_for_xml
from ebook_converter.ebooks.metadata.opf2 import OPFCreator 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
__license__ = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
HTML_TEMPLATE = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s </title></head><body>\n%s\n</body></html>' HTML_TEMPLATE = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s </title></head><body>\n%s\n</body></html>'
@@ -22,7 +18,7 @@ def clean_txt(txt):
Run transformations on the text to put it into Run transformations on the text to put it into
consistent state. consistent state.
''' '''
if isbytestring(txt): if isinstance(txt, bytes):
txt = txt.decode('utf-8', 'replace') txt = txt.decode('utf-8', 'replace')
# Strip whitespace from the end of the line. Also replace # Strip whitespace from the end of the line. Also replace
# all line breaks with \n. # all line breaks with \n.
@@ -69,7 +65,7 @@ def split_txt(txt, epub_split_size_kb=0):
txt = b'\n\n'.join([ txt = b'\n\n'.join([
split_string_separator(line, chunk_size) for line in parts split_string_separator(line, chunk_size) for line in parts
]) ])
if isbytestring(txt): if isinstance(txt, bytes):
txt = txt.decode('utf-8') txt = txt.decode('utf-8')
return txt return txt

View File

@@ -8,7 +8,7 @@ import shutil
import time import time
from math import ceil from math import ceil
from ebook_converter import force_unicode, isbytestring, prints, sanitize_file_name from ebook_converter import force_unicode, prints, sanitize_file_name
from ebook_converter.constants_old import ( from ebook_converter.constants_old import (
filesystem_encoding, iswindows, plugins, preferred_encoding, isosx filesystem_encoding, iswindows, plugins, preferred_encoding, isosx
) )
@@ -145,7 +145,7 @@ def case_preserving_open_file(path, mode='wb', mkdir_mode=0o777):
mkdir_mode specifies the mode with which any missing directories in path mkdir_mode specifies the mode with which any missing directories in path
are created. are created.
''' '''
if isbytestring(path): if isinstance(path, bytes):
path = path.decode(filesystem_encoding) path = path.decode(filesystem_encoding)
path = os.path.abspath(path) path = os.path.abspath(path)
@@ -220,7 +220,7 @@ def windows_get_fileid(path):
all hardlinks to a file). Similar to inode number on linux. ''' all hardlinks to a file). Similar to inode number on linux. '''
import win32file import win32file
from pywintypes import error from pywintypes import error
if isbytestring(path): if isinstance(path, bytes):
path = path.decode(filesystem_encoding) path = path.decode(filesystem_encoding)
try: try:
h = win32file.CreateFileW(path, 0, 0, None, win32file.OPEN_EXISTING, h = win32file.CreateFileW(path, 0, 0, None, win32file.OPEN_EXISTING,
@@ -278,7 +278,7 @@ def windows_get_size(path):
not in the directory entry (which could be out of date). So we open the not in the directory entry (which could be out of date). So we open the
file, and get the actual size. ''' file, and get the actual size. '''
import win32file import win32file
if isbytestring(path): if isinstance(path, bytes):
path = path.decode(filesystem_encoding) path = path.decode(filesystem_encoding)
h = win32file.CreateFileW( h = win32file.CreateFileW(
path, 0, win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE | win32file.FILE_SHARE_DELETE, path, 0, win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE | win32file.FILE_SHARE_DELETE,
@@ -331,7 +331,7 @@ def windows_fast_hardlink(src, dest):
def windows_nlinks(path): def windows_nlinks(path):
import win32file import win32file
dwFlagsAndAttributes = win32file.FILE_FLAG_BACKUP_SEMANTICS if os.path.isdir(path) else 0 dwFlagsAndAttributes = win32file.FILE_FLAG_BACKUP_SEMANTICS if os.path.isdir(path) else 0
if isbytestring(path): if isinstance(path, bytes):
path = path.decode(filesystem_encoding) path = path.decode(filesystem_encoding)
handle = win32file.CreateFileW(path, win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, dwFlagsAndAttributes, None) handle = win32file.CreateFileW(path, win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, dwFlagsAndAttributes, None)
try: try:
@@ -357,7 +357,7 @@ class WindowsAtomicFolderMove(object):
from pywintypes import error from pywintypes import error
from collections import defaultdict from collections import defaultdict
if isbytestring(path): if isinstance(path, bytes):
path = path.decode(filesystem_encoding) path = path.decode(filesystem_encoding)
if not os.path.exists(path): if not os.path.exists(path):

View File

@@ -5,7 +5,7 @@ import sys, traceback, io
from functools import partial from functools import partial
from threading import Lock from threading import Lock
from ebook_converter import isbytestring, force_unicode, as_unicode, prints from ebook_converter import force_unicode, as_unicode, prints
__license__ = 'GPL 3' __license__ = 'GPL 3'
@@ -108,7 +108,7 @@ class UnicodeHTMLStream(HTMLStream):
end = kwargs.get(u'end', u'\n') end = kwargs.get(u'end', u'\n')
for arg in args: for arg in args:
if isbytestring(arg): if isinstance(arg, bytes):
arg = force_unicode(arg) arg = force_unicode(arg)
elif not isinstance(arg, str): elif not isinstance(arg, str):
arg = as_unicode(arg) arg = as_unicode(arg)