Convert given text to UTF-8 encoding (as far as possible).
(text)
| 66 | |
| 67 | # https://github.com/jhermann/waif/blob/master/python/to_uft8.py |
| 68 | def to_utf8(text): |
| 69 | """Convert given text to UTF-8 encoding (as far as possible).""" |
| 70 | if not text or is_PY3: |
| 71 | return text |
| 72 | |
| 73 | try: # unicode or pure ascii |
| 74 | return text.encode("utf8") |
| 75 | except UnicodeDecodeError: |
| 76 | try: # successful UTF-8 decode means it's pretty sure UTF-8 |
| 77 | text.decode("utf8") |
| 78 | return text |
| 79 | except UnicodeDecodeError: |
| 80 | try: # get desperate; and yes, this has a western hemisphere bias |
| 81 | return text.decode("cp1252").encode("utf8") |
| 82 | except UnicodeDecodeError: |
| 83 | pass |
| 84 | |
| 85 | return text # return unchanged, hope for the best |
| 86 | |
| 87 | |
| 88 | class Config(object): |
no outgoing calls
no test coverage detected