1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-03-16 23:03:32 +01:00

Cleanup init module

This commit is contained in:
2020-07-13 20:38:55 +02:00
parent 16c568eab9
commit 025878dfe5

View File

@@ -1,4 +1,5 @@
import html import html
import logging
import math import math
import mimetypes import mimetypes
import os import os
@@ -8,16 +9,14 @@ import sys
from functools import partial from functools import partial
try:
os.getcwd()
except EnvironmentError:
os.chdir(os.path.expanduser('~'))
from ebook_converter import constants_old from ebook_converter import constants_old
from ebook_converter.constants_old import islinux, isfrozen, \ from ebook_converter.constants_old import islinux, isfrozen, \
isbsd, __appname__, __version__, __author__, \ isbsd, __appname__, __version__, __author__, \
config_dir config_dir
from ebook_converter.ebooks.html_entities import html5_entities
from ebook_converter.libunzip import extract as zipextract
from ebook_converter.startup import winutil, winutilerror from ebook_converter.startup import winutil, winutilerror
from ebook_converter.utils.unrar import extract as rarextract
if False: if False:
@@ -39,11 +38,6 @@ def guess_extension(*args, **kwargs):
return ext return ext
_filename_sanitize_unicode = frozenset(('\\', '|', '?', '*', '<',
'"', ':', '>', '+', '/') +
tuple(map(chr, range(32))))
def sanitize_file_name(name, substitute='_'): def sanitize_file_name(name, substitute='_'):
""" """
Sanitize the filename `name`. All invalid characters are replaced by Sanitize the filename `name`. All invalid characters are replaced by
@@ -61,7 +55,8 @@ def sanitize_file_name(name, substitute='_'):
substitute = substitute.decode(constants_old.filesystem_encoding, substitute = substitute.decode(constants_old.filesystem_encoding,
'replace') 'replace')
chars = (substitute chars = (substitute
if c in _filename_sanitize_unicode else c for c in name) if c in set(('\\', '|', '?', '*', '<', '"', ':', '>', '+', '/') +
tuple(map(chr, range(32)))) else c for c in name)
one = ''.join(chars) one = ''.join(chars)
one = re.sub(r'\s', ' ', one).strip() one = re.sub(r'\s', ' ', one).strip()
bname, ext = os.path.splitext(one) bname, ext = os.path.splitext(one)
@@ -117,7 +112,6 @@ def prints(*args, **kwargs):
def setup_cli_handlers(logger, level): def setup_cli_handlers(logger, level):
import logging
if os.getenv('CALIBRE_WORKER') and logger.handlers: if os.getenv('CALIBRE_WORKER') and logger.handlers:
return return
logger.setLevel(level) logger.setLevel(level)
@@ -144,19 +138,15 @@ def extract(path, dir):
with open(path, 'rb') as f: with open(path, 'rb') as f:
id_ = f.read(3) id_ = f.read(3)
if id_ == b'Rar': if id_ == b'Rar':
from ebook_converter.utils.unrar import extract as rarextract
extractor = rarextract extractor = rarextract
elif id_.startswith(b'PK'): elif id_.startswith(b'PK'):
from ebook_converter.libunzip import extract as zipextract
extractor = zipextract extractor = zipextract
if extractor is None: if extractor is None:
# Fallback to file extension # Fallback to file extension
ext = os.path.splitext(path)[1][1:].lower() ext = os.path.splitext(path)[1][1:].lower()
if ext in ['zip', 'cbz', 'epub', 'oebzip']: if ext in ['zip', 'cbz', 'epub', 'oebzip']:
from ebook_converter.libunzip import extract as zipextract
extractor = zipextract extractor = zipextract
elif ext in ['cbr', 'rar']: elif ext in ['cbr', 'rar']:
from ebook_converter.utils.unrar import extract as rarextract
extractor = rarextract extractor = rarextract
if extractor is None: if extractor is None:
raise Exception('Unknown archive type') raise Exception('Unknown archive type')
@@ -164,7 +154,7 @@ def extract(path, dir):
def fit_image(width, height, pwidth, pheight): def fit_image(width, height, pwidth, pheight):
''' """
Fit image in box of width pwidth and height pheight. Fit image in box of width pwidth and height pheight.
@param width: Width of image @param width: Width of image
@param height: Height of image @param height: Height of image
@@ -172,7 +162,7 @@ def fit_image(width, height, pwidth, pheight):
@param pheight: Height of box @param pheight: Height of box
@return: scaled, new_width, new_height. scaled is True iff new_width @return: scaled, new_width, new_height. scaled is True iff new_width
and/or new_height is different from width or height. and/or new_height is different from width or height.
''' """
scaled = height > pheight or width > pwidth scaled = height > pheight or width > pwidth
if height > pheight: if height > pheight:
corrf = pheight / float(height) corrf = pheight / float(height)
@@ -207,7 +197,7 @@ class CurrentDir(object):
def walk(dir): def walk(dir):
''' A nice interface to os.walk ''' """A nice interface to os.walk"""
for record in os.walk(dir): for record in os.walk(dir):
for f in record[-1]: for f in record[-1]:
yield os.path.join(record[0], f) yield os.path.join(record[0], f)
@@ -265,7 +255,6 @@ def entity_to_unicode(match, exceptions=[], encoding='cp1252',
return check(bytes(bytearray((num,))).decode(encoding)) return check(bytes(bytearray((num,))).decode(encoding))
except UnicodeDecodeError: except UnicodeDecodeError:
return check(my_unichr(num)) return check(my_unichr(num))
from ebook_converter.ebooks.html_entities import html5_entities
try: try:
return check(html5_entities[ent]) return check(html5_entities[ent])
except KeyError: except KeyError: