mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-03-30 00:33:32 +02:00
Move specific sanitize_file_name out of init module.
sanitize_file_name function has been moved to utility module filenames, where it logically belongs.
This commit is contained in:
@@ -9,40 +9,6 @@ from ebook_converter import constants_old
|
|||||||
from ebook_converter.ebooks.html_entities import html5_entities
|
from ebook_converter.ebooks.html_entities import html5_entities
|
||||||
|
|
||||||
|
|
||||||
def sanitize_file_name(name, substitute='_'):
|
|
||||||
"""
|
|
||||||
Sanitize the filename `name`. All invalid characters are replaced by
|
|
||||||
`substitute`. The set of invalid characters is the union of the invalid
|
|
||||||
characters in Windows, macOS and Linux. Also removes leading and trailing
|
|
||||||
whitespace.
|
|
||||||
|
|
||||||
**WARNING:** This function also replaces path separators, so only pass
|
|
||||||
file names and not full paths to it.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if isinstance(name, bytes):
|
|
||||||
name = name.decode(constants_old.filesystem_encoding, 'replace')
|
|
||||||
if isinstance(substitute, bytes):
|
|
||||||
substitute = substitute.decode(constants_old.filesystem_encoding,
|
|
||||||
'replace')
|
|
||||||
chars = (substitute
|
|
||||||
if c in set(('\\', '|', '?', '*', '<', '"', ':', '>', '+', '/') +
|
|
||||||
tuple(map(chr, range(32)))) else c for c in name)
|
|
||||||
one = ''.join(chars)
|
|
||||||
one = re.sub(r'\s', ' ', one).strip()
|
|
||||||
bname, ext = os.path.splitext(one)
|
|
||||||
one = re.sub(r'^\.+$', '_', bname)
|
|
||||||
one = one.replace('..', substitute)
|
|
||||||
one += ext
|
|
||||||
# Windows doesn't like path components that end with a period or space
|
|
||||||
if one and one[-1] in ('.', ' '):
|
|
||||||
one = one[:-1]+'_'
|
|
||||||
# Names starting with a period are hidden on Unix
|
|
||||||
if one.startswith('.'):
|
|
||||||
one = '_' + one[1:]
|
|
||||||
return one
|
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from functools import partial
|
|||||||
from collections import Counter, defaultdict
|
from collections import Counter, defaultdict
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
from ebook_converter import sanitize_file_name
|
from ebook_converter.utils import filenames as fms
|
||||||
from ebook_converter.ebooks.chardet import strip_encoding_declarations
|
from ebook_converter.ebooks.chardet import strip_encoding_declarations
|
||||||
from ebook_converter.ebooks.oeb.base import css_text
|
from ebook_converter.ebooks.oeb.base import css_text
|
||||||
from ebook_converter.ebooks.oeb.polish.css import iter_declarations, remove_property_value
|
from ebook_converter.ebooks.oeb.polish.css import iter_declarations, remove_property_value
|
||||||
@@ -203,7 +203,7 @@ def rename_files(container, file_map):
|
|||||||
|
|
||||||
def replace_file(container, name, path, basename, force_mt=None):
|
def replace_file(container, name, path, basename, force_mt=None):
|
||||||
dirname, base = name.rpartition('/')[0::2]
|
dirname, base = name.rpartition('/')[0::2]
|
||||||
nname = sanitize_file_name(basename)
|
nname = fms.sanitize_file_name(basename)
|
||||||
if dirname:
|
if dirname:
|
||||||
nname = dirname + '/' + nname
|
nname = dirname + '/' + nname
|
||||||
with open(path, 'rb') as src:
|
with open(path, 'rb') as src:
|
||||||
|
|||||||
@@ -4,15 +4,51 @@ meaning as possible.
|
|||||||
"""
|
"""
|
||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
from ebook_converter import force_unicode, sanitize_file_name
|
from ebook_converter import constants_old
|
||||||
|
from ebook_converter import force_unicode
|
||||||
from ebook_converter.constants_old import (filesystem_encoding,
|
from ebook_converter.constants_old import (filesystem_encoding,
|
||||||
preferred_encoding)
|
preferred_encoding)
|
||||||
from ebook_converter.utils.localization import get_udc
|
from ebook_converter.utils.localization import get_udc
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_file_name(name, substitute='_'):
|
||||||
|
"""
|
||||||
|
Sanitize the filename `name`. All invalid characters are replaced by
|
||||||
|
`substitute`. The set of invalid characters is the union of the invalid
|
||||||
|
characters in Windows, macOS and Linux. Also removes leading and trailing
|
||||||
|
whitespace.
|
||||||
|
|
||||||
|
**WARNING:** This function also replaces path separators, so only pass
|
||||||
|
file names and not full paths to it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if isinstance(name, bytes):
|
||||||
|
name = name.decode(constants_old.filesystem_encoding, 'replace')
|
||||||
|
if isinstance(substitute, bytes):
|
||||||
|
substitute = substitute.decode(constants_old.filesystem_encoding,
|
||||||
|
'replace')
|
||||||
|
chars = (substitute
|
||||||
|
if c in set(('\\', '|', '?', '*', '<', '"', ':', '>', '+', '/') +
|
||||||
|
tuple(map(chr, range(32)))) else c for c in name)
|
||||||
|
one = ''.join(chars)
|
||||||
|
one = re.sub(r'\s', ' ', one).strip()
|
||||||
|
bname, ext = os.path.splitext(one)
|
||||||
|
one = re.sub(r'^\.+$', '_', bname)
|
||||||
|
one = one.replace('..', substitute)
|
||||||
|
one += ext
|
||||||
|
# Windows doesn't like path components that end with a period or space
|
||||||
|
if one and one[-1] in ('.', ' '):
|
||||||
|
one = one[:-1]+'_'
|
||||||
|
# Names starting with a period are hidden on Unix
|
||||||
|
if one.startswith('.'):
|
||||||
|
one = '_' + one[1:]
|
||||||
|
return one
|
||||||
|
|
||||||
|
|
||||||
def ascii_text(orig):
|
def ascii_text(orig):
|
||||||
udc = get_udc()
|
udc = get_udc()
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import binascii
|
|||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
from tempfile import SpooledTemporaryFile
|
from tempfile import SpooledTemporaryFile
|
||||||
|
|
||||||
from ebook_converter import sanitize_file_name
|
from ebook_converter.utils import filenames as fms
|
||||||
from ebook_converter.constants_old import filesystem_encoding
|
from ebook_converter.constants_old import filesystem_encoding
|
||||||
from ebook_converter.ebooks.chardet import detect
|
from ebook_converter.ebooks.chardet import detect
|
||||||
from ebook_converter.polyglot.builtins import as_bytes
|
from ebook_converter.polyglot.builtins import as_bytes
|
||||||
@@ -1135,7 +1135,7 @@ class ZipFile:
|
|||||||
os.makedirs(upperdirs)
|
os.makedirs(upperdirs)
|
||||||
except: # Added by Kovid
|
except: # Added by Kovid
|
||||||
targetpath = os.path.join(base_target,
|
targetpath = os.path.join(base_target,
|
||||||
sanitize_file_name(fname))
|
fms.sanitize_file_name(fname))
|
||||||
upperdirs = os.path.dirname(targetpath)
|
upperdirs = os.path.dirname(targetpath)
|
||||||
if upperdirs and not os.path.exists(upperdirs):
|
if upperdirs and not os.path.exists(upperdirs):
|
||||||
os.makedirs(upperdirs)
|
os.makedirs(upperdirs)
|
||||||
@@ -1156,7 +1156,7 @@ class ZipFile:
|
|||||||
except:
|
except:
|
||||||
# Try sanitizing the file name to remove invalid characters
|
# Try sanitizing the file name to remove invalid characters
|
||||||
components = list(os.path.split(targetpath))
|
components = list(os.path.split(targetpath))
|
||||||
components[-1] = sanitize_file_name(components[-1])
|
components[-1] = fms.sanitize_file_name(components[-1])
|
||||||
targetpath = os.sep.join(components)
|
targetpath = os.sep.join(components)
|
||||||
with open(targetpath, 'wb') as target:
|
with open(targetpath, 'wb') as target:
|
||||||
shutil.copyfileobj(source, target)
|
shutil.copyfileobj(source, target)
|
||||||
|
|||||||
Reference in New Issue
Block a user