Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/pip/_internal/utils/encoding.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 import codecs | |
2 import locale | |
3 import re | |
4 import sys | |
5 | |
6 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
7 | |
8 if MYPY_CHECK_RUNNING: | |
9 from typing import List, Tuple, Text | |
10 | |
11 BOMS = [ | |
12 (codecs.BOM_UTF8, 'utf-8'), | |
13 (codecs.BOM_UTF16, 'utf-16'), | |
14 (codecs.BOM_UTF16_BE, 'utf-16-be'), | |
15 (codecs.BOM_UTF16_LE, 'utf-16-le'), | |
16 (codecs.BOM_UTF32, 'utf-32'), | |
17 (codecs.BOM_UTF32_BE, 'utf-32-be'), | |
18 (codecs.BOM_UTF32_LE, 'utf-32-le'), | |
19 ] # type: List[Tuple[bytes, Text]] | |
20 | |
21 ENCODING_RE = re.compile(br'coding[:=]\s*([-\w.]+)') | |
22 | |
23 | |
24 def auto_decode(data): | |
25 # type: (bytes) -> Text | |
26 """Check a bytes string for a BOM to correctly detect the encoding | |
27 | |
28 Fallback to locale.getpreferredencoding(False) like open() on Python3""" | |
29 for bom, encoding in BOMS: | |
30 if data.startswith(bom): | |
31 return data[len(bom):].decode(encoding) | |
32 # Lets check the first two lines as in PEP263 | |
33 for line in data.split(b'\n')[:2]: | |
34 if line[0:1] == b'#' and ENCODING_RE.search(line): | |
35 encoding = ENCODING_RE.search(line).groups()[0].decode('ascii') | |
36 return data.decode(encoding) | |
37 return data.decode( | |
38 locale.getpreferredencoding(False) or sys.getdefaultencoding(), | |
39 ) |