~anitanayak/charms/trusty/ibm-mq/ibm-mq-trusty

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/pip/utils/encoding.py

  • Committer: Anita Nayak
  • Date: 2016-10-24 07:10:08 UTC
  • Revision ID: anitanayak@in.ibm.com-20161024071008-tqk3cefak6nc1c69
checking in after fixing lint errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import codecs
 
2
import locale
 
3
import re
 
4
 
 
5
 
 
6
BOMS = [
 
7
    (codecs.BOM_UTF8, 'utf8'),
 
8
    (codecs.BOM_UTF16, 'utf16'),
 
9
    (codecs.BOM_UTF16_BE, 'utf16-be'),
 
10
    (codecs.BOM_UTF16_LE, 'utf16-le'),
 
11
    (codecs.BOM_UTF32, 'utf32'),
 
12
    (codecs.BOM_UTF32_BE, 'utf32-be'),
 
13
    (codecs.BOM_UTF32_LE, 'utf32-le'),
 
14
]
 
15
 
 
16
ENCODING_RE = re.compile(b'coding[:=]\s*([-\w.]+)')
 
17
 
 
18
 
 
19
def auto_decode(data):
 
20
    """Check a bytes string for a BOM to correctly detect the encoding
 
21
 
 
22
    Fallback to locale.getpreferredencoding(False) like open() on Python3"""
 
23
    for bom, encoding in BOMS:
 
24
        if data.startswith(bom):
 
25
            return data[len(bom):].decode(encoding)
 
26
    # Lets check the first two lines as in PEP263
 
27
    for line in data.split(b'\n')[:2]:
 
28
        if line[0:1] == b'#' and ENCODING_RE.search(line):
 
29
            encoding = ENCODING_RE.search(line).groups()[0].decode('ascii')
 
30
            return data.decode(encoding)
 
31
    return data.decode(locale.getpreferredencoding(False))