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