1
# -*- coding: utf-8 -*-
3
Make strings safe for use as ASCII filenames, while trying to preserve as much
2
Make strings safe for use as ASCII filenames, while trying to preserve as much
85
} #: Translation table
87
for c in string.whitespace:
89
PAT = re.compile('['+u''.join(MAP.keys())+']')
9
from calibre.ebooks.unidecode.unidecoder import Unidecoder
10
from calibre import sanitize_file_name
11
from calibre.constants import preferred_encoding
16
ascii = udc.decode(orig)
18
if isinstance(orig, unicode):
19
ascii = orig.encode('ascii', 'replace')
20
ascii = orig.decode(preferred_encoding,
21
'replace').encode('ascii', 'replace')
91
25
def ascii_filename(orig):
92
orig = PAT.sub(lambda m:MAP[m.group()], orig)
94
for i in range(len(orig)):
96
buf.append('_' if val < 33 or val > 126 else orig[i])
97
return (''.join(buf)).encode('ascii')
26
return sanitize_file_name(ascii_text(orig).replace('?', '_'))
29
def supports_long_names(path):
32
p = os.path.join(path, t)
40
def shorten_components_to(length, components):
41
filepath = os.sep.join(components)
42
extra = len(filepath) - length
45
delta = int(ceil(extra/float(len(components))))
49
r = x[0] if x is components[-1] else ''
51
if x is components[-1]:
52
b, e = os.path.splitext(x)
54
if r.startswith('.'): r = x[0]+r