]>
Commit | Line | Data |
---|---|---|
1 | import unicodedata | |
2 | import sys | |
3 | ||
4 | ||
5 | # HFS Plus uses decomposed UTF-8 | |
6 | def decompose(path): | |
7 | if isinstance(path, str): | |
8 | return unicodedata.normalize('NFD', path) | |
9 | try: | |
10 | path = path.decode('utf-8') | |
11 | path = unicodedata.normalize('NFD', path) | |
12 | path = path.encode('utf-8') | |
13 | except UnicodeError: | |
14 | pass # Not UTF-8 | |
15 | return path | |
16 | ||
17 | ||
18 | def filesys_decode(path): | |
19 | """ | |
20 | Ensure that the given path is decoded, | |
21 | NONE when no expected encoding works | |
22 | """ | |
23 | ||
24 | if isinstance(path, str): | |
25 | return path | |
26 | ||
27 | fs_enc = sys.getfilesystemencoding() or 'utf-8' | |
28 | candidates = fs_enc, 'utf-8' | |
29 | ||
30 | for enc in candidates: | |
31 | try: | |
32 | return path.decode(enc) | |
33 | except UnicodeDecodeError: | |
34 | continue | |
35 | ||
36 | ||
37 | def try_encode(string, enc): | |
38 | "turn unicode encoding into a functional routine" | |
39 | try: | |
40 | return string.encode(enc) | |
41 | except UnicodeEncodeError: | |
42 | return None |