~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/utils/filenames.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
1
'''
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
4
3
meaning as possible.
5
4
'''
6
5
 
7
 
import re, string
8
 
 
9
 
MAP = {
10
 
        u"‘" : "'",
11
 
        u"’" : "'",
12
 
        u"«" : '"',
13
 
        u"»" : '"',
14
 
        u"…" : "...",
15
 
        u"№" : "#",
16
 
        u"Щ" : "Shh",
17
 
        u"Ё" : "Jo",
18
 
        u"Ж" : "Zh",
19
 
        u"Ц" : "C",
20
 
        u"Ч" : "Ch",
21
 
        u"Ш" : "Sh",
22
 
        u"Ы" : "Y",
23
 
        u"Ю" : "Ju",
24
 
        u"Я" : "Ja",
25
 
        u"Б" : "B",
26
 
        u"Г" : "G",
27
 
        u"Д" : "D",
28
 
        u"И" : "I",
29
 
        u"Й" : "J",
30
 
        u"К" : "K",
31
 
        u"Л" : "L",
32
 
        u"П" : "P",
33
 
        u"Ф" : "F",
34
 
        u"Э" : "E",
35
 
        u"Ъ" : "`",
36
 
        u"Ь" : "'",
37
 
        u"щ" : "shh",
38
 
        u"ё" : "jo",
39
 
        u"ж" : "zh",
40
 
        u"ц" : "c",
41
 
        u"ч" : "ch",
42
 
        u"ш" : "sh",
43
 
        u"ы" : "y",
44
 
        u"ю" : "ju",
45
 
        u"я" : "ja",
46
 
        u"б" : "b",
47
 
        u"в" : "v",
48
 
        u"г" : "g",
49
 
        u"д" : "d",
50
 
        u"з" : "z",
51
 
        u"и" : "i",
52
 
        u"й" : "j",
53
 
        u"к" : "k",
54
 
        u"л" : "l",
55
 
        u"м" : "m",
56
 
        u"н" : "n",
57
 
        u"о" : "o",
58
 
        u"п" : "p",
59
 
        u"т" : "t",
60
 
        u"ф" : "f",
61
 
        u"э" : "e",
62
 
        u"ъ" : "`",
63
 
        u"ь" : "'",
64
 
        u"А" : "A",
65
 
        u"В" : "V",
66
 
        u"Е" : "Je",
67
 
        u"З" : "Z",
68
 
        u"М" : "M",
69
 
        u"Н" : "N",
70
 
        u"О" : "O",
71
 
        u"Р" : "R",
72
 
        u"С" : "S",
73
 
        u"Т" : "T",
74
 
        u"У" : "U",
75
 
        u"Х" : "Kh",
76
 
        u"Є" : "Je",
77
 
        u"Ї" : "Ji",
78
 
        u"а" : "a",
79
 
        u"е" : "je",
80
 
        u"р" : "r",
81
 
        u"с" : "s",
82
 
        u"у" : "u",
83
 
        u"х" : "kh",
84
 
        u"є" : "je",
85
 
}  #: Translation table
86
 
 
87
 
for c in string.whitespace:
88
 
    MAP[c] = ' '
89
 
PAT = re.compile('['+u''.join(MAP.keys())+']')
 
6
import os
 
7
from math import ceil
 
8
 
 
9
from calibre.ebooks.unidecode.unidecoder import Unidecoder
 
10
from calibre import sanitize_file_name
 
11
from calibre.constants import preferred_encoding
 
12
udc = Unidecoder()
 
13
 
 
14
def ascii_text(orig):
 
15
    try:
 
16
        ascii = udc.decode(orig)
 
17
    except:
 
18
        if isinstance(orig, unicode):
 
19
            ascii = orig.encode('ascii', 'replace')
 
20
        ascii = orig.decode(preferred_encoding,
 
21
                'replace').encode('ascii', 'replace')
 
22
    return ascii
 
23
 
90
24
 
91
25
def ascii_filename(orig):
92
 
    orig =  PAT.sub(lambda m:MAP[m.group()], orig)
93
 
    buf = []
94
 
    for i in range(len(orig)):
95
 
        val = ord(orig[i])
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('?', '_'))
 
27
 
 
28
 
 
29
def supports_long_names(path):
 
30
    t = ('a'*300)+'.txt'
 
31
    try:
 
32
        p = os.path.join(path, t)
 
33
        open(p, 'wb').close()
 
34
        os.remove(p)
 
35
    except:
 
36
        return False
 
37
    else:
 
38
        return True
 
39
 
 
40
def shorten_components_to(length, components):
 
41
    filepath = os.sep.join(components)
 
42
    extra = len(filepath) - length
 
43
    if extra < 1:
 
44
        return components
 
45
    delta = int(ceil(extra/float(len(components))))
 
46
    ans = []
 
47
    for x in components:
 
48
        if delta > len(x):
 
49
            r = x[0] if x is components[-1] else ''
 
50
        else:
 
51
            if x is components[-1]:
 
52
                b, e = os.path.splitext(x)
 
53
                r = b[:-delta]+e
 
54
                if r.startswith('.'): r = x[0]+r
 
55
            else:
 
56
                r = x[:-delta]
 
57
        ans.append(r)
 
58
    return ans
 
59